SpringCloud学习之SpringCloudStream&集成kafka
一、关于Spring-Cloud-Stream
Spring Cloud Stream本质上就是整合了Spring Boot和Spring Integration,实现了一套轻量级的消息驱动的微服务框架。通过使用Spring Cloud Stream,可以有效地简化开发人员对消息中间件的使用复杂度,让系统开发人员可以有更多的精力关注于核心业务逻辑的处理。
在这里我先放一张官网的图:

应用程序通过Spring Cloud Stream注入到输入和输出通道与外界进行通信。根据此规则我们很容易的实现消息传递,订阅消息与消息中转。并且当需要切换消息中间件时,几乎不需要修改代码,只需要变更配置就行了。
在用例图中 Inputs代表了应用程序监听消息 、outputs代表发送消息、binder的话大家可以理解为将应用程序与消息中间件隔离的抽象,类似于三层架构下利用dao屏蔽service与数据库的实现的原理。
springcloud默认提供了rabbitmq与kafka的实现。
二、springcloud集成kafka
1、添加gradle依赖:
dependencies{
compile('org.springframework.cloud:spring-cloud-stream')
compile('org.springframework.cloud:spring-cloud-stream-binder-kafka')
compile('org.springframework.kafka:spring-kafka')
}
2、定义一个接口:
spring-cloud-stream已经给我们定义了最基本的输入与输出接口,他们分别是 Source,Sink, Processor
Sink接口:
package org.springframework.cloud.stream.messaging; import org.springframework.cloud.stream.annotation.Input;
import org.springframework.messaging.SubscribableChannel; public interface Sink {
String INPUT = "input"; @Input("input")
SubscribableChannel input();
}
Source接口:
package org.springframework.cloud.stream.messaging; import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel; public interface Source {
String OUTPUT = "output"; @Output("output")
MessageChannel output();
}
Processor接口:
package org.springframework.cloud.stream.messaging;
public interface Processor extends Source, Sink {
}
这里面Processor这个接口既定义输入通道又定义了输出通道。同时我们也可以自己定义通道接口,代码如下:
package com.bdqn.lyrk.shop.channel; import org.springframework.cloud.stream.annotation.Input;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.SubscribableChannel; public interface ShopChannel { /**
* 发消息的通道名称
*/
String SHOP_OUTPUT = "shop_output"; /**
* 消息的订阅通道名称
*/
String SHOP_INPUT = "shop_input"; /**
* 发消息的通道
*
* @return
*/
@Output(SHOP_OUTPUT)
MessageChannel sendShopMessage(); /**
* 收消息的通道
*
* @return
*/
@Input(SHOP_INPUT)
SubscribableChannel recieveShopMessage(); }
3、定义服务类
package com.bdqn.lyrk.shop.server; import com.bdqn.lyrk.shop.channel.ShopChannel;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController
public class ShopService { @Resource(name = ShopChannel.SHOP_OUTPUT)
private MessageChannel sendShopMessageChannel; @GetMapping("/sendMsg")
public String sendShopMessage(String content) {
boolean isSendSuccess = sendShopMessageChannel.
send(MessageBuilder.withPayload(content).build());
return isSendSuccess ? "发送成功" : "发送失败";
} @StreamListener(ShopChannel.SHOP_INPUT)
public void receive(Message<String> message) {
System.out.println(message.getPayload());
}
}
这里面大家注意 @StreamListener。这个注解可以监听输入通道里的消息内容,注解里面的属性指定我们刚才定义的输入通道名称,而MessageChannel则可以通过
输出通道发送消息。使用@Resource注入时需要指定我们刚才定义的输出通道名称
4、定义启动类
package com.bdqn.lyrk.shop; import com.bdqn.lyrk.shop.channel.ShopChannel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding; @SpringBootApplication
@EnableBinding(ShopChannel.class)
public class ShopServerApplication { public static void main(String[] args) {
SpringApplication.run(ShopServerApplication.class, args);
}
}
注意@EnableBinding注解,这个注解指定刚才我们定义消息通道的接口名称,当然这里也可以传多个相关的接口
5、定义application.yml文件
spring:
application:
name: shop-server
cloud:
stream:
bindings:
#配置自己定义的通道与哪个中间件交互
shop_input: #ShopChannel里Input和Output的值
destination: zhibo #目标主题
shop_output:
destination: zhibo
default-binder: kafka #默认的binder是kafka
kafka:
bootstrap-servers: localhost:9092 #kafka服务地址
consumer:
group-id: consumer1
producer:
key-serializer: org.apache.kafka.common.serialization.ByteArraySerializer
value-serializer: org.apache.kafka.common.serialization.ByteArraySerializer
client-id: producer1
server:
port: 8100
这里是重头戏,我们必须指定所有通道对应的消息主题,同时指定默认的binder为kafka,紧接着定义Spring-kafka的外部化配置,在这里指定producer的序列化类为ByteArraySerializer
启动程序成功后,我们访问 http://localhost:8100/sendMsg?content=2 即可得到如下结果
SpringCloud学习之SpringCloudStream&集成kafka的更多相关文章
- springcloud 集成kafka问题记录,发消息报错:ERROR o.s.kafka.support.LoggingProducerListener - Exception thrown when sending a message with key='null' and payload='{-1,
在springcloud集成kafka,发送消息时报错: 2018-08-15 16:01:34.159 [http-nio-8081-exec-1] INFO org.apache.kafka.c ...
- springcloud集成kafka
项目名称:布控预警 水平拆分出来的项目,作为一个单独的可以对外提供服务的项目 项目设计:springcloud,可以集成各个不同平台的一个作为对外提供的微服务项目 项目功能:实现各个平台和本平台之间的 ...
- SpringCloud学习之Ribbon
一.负载均衡与Ribbon 负载均衡,在集群中是很常见的一个“名词”,顾名思义是根据一定的算法将请求分摊至对应的服务节点上,常见的算法有如下几种: 轮询法:所有请求被依次分发到每台应用服务器上,每台服 ...
- springCloud学习总览
写完最后一篇特意去看了看第一篇是什么时候写的---2018/11/19,到现在三个月多一点,总的来说这三个月通过<Spring 微服务实战>这本书,算是对微服务进行了一次扫盲学习. ...
- SpringCloud学习心得—1.3—Eureka与REST API
SpringCloud学习心得—1.3—Eureka与REST API Eureka的REST API接口 API的基本访问 Eureka REST APIEureka 作为注册中心,其本质是存储 ...
- SpringCloudStream(RabbitMQ&Kafka)&Spring-Kafka配置使用
目录 是什么 解决问题 使用方式 创建生产者项目 pom yml 生产消息方法 接口 实现 创建消费者项目 pom yml 接收消息方法 重复消费 消费者yml 持久化 消费者负载个性配置(预拉取) ...
- Storm集成Kafka应用的开发
我们知道storm的作用主要是进行流式计算,对于源源不断的均匀数据流流入处理是非常有效的,而现实生活中大部分场景并不是均匀的数据流,而是时而多时而少的数据流入,这种情况下显然用批量处理是不合适的,如果 ...
- Windows phone 8 学习笔记(9) 集成
原文:Windows phone 8 学习笔记(9) 集成 本节整理了之前并没有提到的Windows phone 8 系统相关集成支持,包括选择器.锁定屏幕的.联系人的访问等.选择器列举了若干内置应用 ...
- SpringCloud学习之feign
一.关于feigin feigin是一种模板化,声明式的http客户端,feign可以通过注解绑定到接口上来简化Http请求访问.当然我们也可以在创建Feign对象时定制自定义解码器(xml或者jso ...
随机推荐
- 关于mule中使用jdbc时报No Suitable Driver found错误的问题
错误大概信息: Exception in thread "main" org.mule.module.launcher.DeploymentStartException: SQLE ...
- "一不小心就火了"团队采访
团队采访 一. 采访团队 团队:一不小心就火了 采访形式:线上问答 二.采访内容 你们是怎么合理地具体分配组员里的工作的?有些团队会出现个别组员代码任务很重,个别组员无所事事的情况,你们有什么有效的方 ...
- 洛谷 U10783 名字被和谐了
https://www.luogu.org/problem/show?pid=U10783 题目背景 众所周知,我们称g是a的约数,当且仅当g是正数且a mod g = 0. 众所周知,若g既是a的约 ...
- Spring-Data-JPA整合MySQL和配置
一.简介 (1).MySQL是一个关系型数据库系统,是如今互联网公司最常用的数据库和最广泛的数据库.为服务端数据库,能承受高并发的访问量. (2).Spring-Data-Jpa是在JPA规范下提供的 ...
- display属性
display 属性规定元素应该生成的框的类型. 值 描述 none 此元素不会被显示. block 此元素将显示为块级元素,此元素前后会带有换行符. inline 默认.此元素会被显示为内联元素,元 ...
- 【bug清除】新Surface Pro使用OneNote出现毛刺现象的解决方案
在写字的时候,左手触摸Surface的金属外壳背面,大概两个手指指肚大小.问题亲测可以得到解决. 推测是设备使用时接地没有做好,导致电磁笔出现偏移.问题初步锁定在新笔的倾斜感应上. 参考资料: htt ...
- 码农、黑客和2B程序员之间的区别
码农: 黑客: 2B程序员: 求2的32次方: 码农: System.out.println(Math.pow(2, 32)); 黑客: System.out.println(1L<<32 ...
- javascript中的数组对象
1.创建数组的三种方式: 1.1 var 数组名=[元素1,元素2,元素3...]; 例如: var arr1=[1,2,3,4]; 1.2 var 数组名=new Array(元素1,元素2,元素3 ...
- php面向对象相关内容
1.什么是面向对象? 面向对象编程(Object Oriented Programming, OOP, 面向对象程序设计)是一种计算机编程架构,OOP的一条基本原则是计算机程序是由单个能够起到子程序作 ...
- PCB名詞解釋:通孔、盲孔、埋孔(转载)
文章转载自:https://www.researchmfg.com/2011/07/pth-blind-hole-buried-hole/ PCB名詞解釋:通孔.盲孔.埋孔 Posted by 工作熊 ...