文章目录
Spring Cloud Stream 消息驱动组件帮助我们更快速,更⽅便,更友好的去构建消息驱动微服务的。
当时定时任务和消息驱动的⼀个对⽐。(消息驱动:基于消息机制做⼀些事情)
MQ:消息队列/消息中间件/消息代理,产品有很多,ActiveMQ、 RabbitMQ、RocketMQ、 Kafka
Stream解决的痛点问题
MQ消息中间件⼴泛应⽤在应⽤解耦合、异步消息处理、流量削峰等场景中。不同的MQ消息中间件内部机制包括使⽤⽅式都会有所不同,⽐如RabbitMQ中有Exchange(交换机/交换器)这⼀概念,kafka有Topic、Partition分区这些概念,MQ消息中间件的差异性不利于我们上层的开发应⽤,当我们的系统希望从原有的RabbitMQ切换到Kafka时,我们会发现⽐较困难,很多要操作可能重来(因为应用程序和具体的某⼀款MQ消息中间件耦合在⼀起了)。
Spring Cloud Stream进⾏了很好的上层抽象,可以让我们与具体消息中间件解耦合,屏蔽掉了底层具体MQ消息中间件的细节差异,就像Hibernate屏蔽掉了具体数据库(Mysql/Oracle⼀样)。如此⼀来,我们学习、开发、维护MQ都会变得轻松。⽬前Spring Cloud Stream⽀持RabbitMQ和Kafka。
本质:屏蔽掉了底层不同MQ消息中间件之间的差异,统⼀了MQ的编程模型,降低了学习、开发、维护MQ的成本
Stream重要概念
Spring Cloud Stream 是⼀个构建消息驱动微服务的框架。应⽤程序通过inputs(相当于消息消费者consumer)或者outputs(相当于消息⽣产者producer)来与Spring Cloud Stream中的binder对象交互,⽽Binder对象是⽤来屏蔽底层MQ细节的,它负责与具体的消息中间件交互。
说白了:对于我们来说,只需要知道如何使⽤Spring Cloud Stream与Binder对象交互即可
Binder绑定器
Binder绑定器是Spring Cloud Stream 中⾮常核⼼的概念,就是通过它来屏蔽底层不同MQ消息中间件的细节差异,当需要更换为其他消息中间件时,我们需要做的就是更换对应的Binder绑定器⽽不需要修改任何应⽤逻辑(Binder绑定器的实现是框架内置的,Spring Cloud Stream⽬前⽀持Rabbit、Kafka两种消息队列)
传统MQ模型与Stream消息驱动模型
Stream消息通信⽅式及编程模型
Stream消息通信方式
Stream中的消息通信⽅式遵循了发布—订阅模式。
在Spring Cloud Stream中的消息通信⽅式遵循了发布-订阅模式,当⼀条消息被投递到消息中间件之 后,它会通过共享的 Topic 主题进⾏⼴播,消息消费者在订阅的主题中收到它并触发⾃身的业务逻辑处理。这⾥所提到的 Topic 主题是SpringCloud Stream中的⼀个抽象概念,⽤来代表发布共享消息给消 费者的地⽅。在不同的消息中间件中, Topic 可能对应着不同的概念,⽐如:在RabbitMQ中的它对应了Exchange、在Kakfa中则对应了Kafka中的Topic。
Stream编程注解
如下的注解无非在做⼀件事,把我们结构图中那些组成部分上下关联起来,打通通道(这样的话⽣产者的message数据才能进⼊mq,mq中数据才能进⼊消费者工程)。
注解 | 描述 |
---|---|
@Input(在消费者⼯程中使⽤) | 注解标识输⼊通道,通过该输⼊通道接收到的消息进⼊应⽤程序 |
@Output(在⽣产者⼯程中使⽤) | 注解标识输出通道,发布的消息将通过该通道离开应⽤程序 |
@StreamListener(在消费者⼯程中使⽤,监听message的到来) | 监听队列,⽤于消费者的队列的消息的接收(有消息监听…) |
@EnableBinding | 把Channel和Exchange(对于RabbitMQ)绑定在⼀起 |
接下来,我们基于上一篇文章的工程,再创建三个子⼯程(我们基于RabbitMQ,RabbitMQ的安装和使⽤这⾥不再说明)
- cloud-stream-producer-9090, 作为⽣产者端发消息
- cloud-stream-consumer-9091,作为消费者端接收消息
- cloud-stream-consumer-9092,作为消费者端接收消息
Stream消息驱动之开发生产者端
- 创建子module cloud-stream-producer-9090
- pom引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
</dependencies>
- application.yml添加配置
server:
port: 8081
spring:
application:
name: cloud-stream-producer
cloud:
stream:
binders:
cloudRabbitBinder: # 给Binder定义的名称,用于后面的关联
type: rabbit # MQ类型,如果是Kafka的话,此处配置kafka
environment: # MQ环境配置(用户名、密码等)
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
bindings: # 关联整合通道和binder对象
output: # output是我们定义的通道名称,此处不能乱改
destination: cloudExchange # 要使用的Exchange名称(消息队列主题名称)
content-type: text/plain # application/json # 消息类型设置,比如json
binder: cloudRabbitBinder # 关联MQ服务
eureka:
client:
service-url:
defaultZone: http://eureka8762.com:8762/eureka/,http://eureka8761.com:8761/eureka/ # 入驻的服务注册中心地址
fetch-registry: true
register-with-eureka: true
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ipaddress}:${spring.application.name}:${server.port}:@project.version@
- 主启动类
@SpringBootApplication
@EnableDiscoveryClient
public class StreamProducer9090 {
public static void main(String[] args) {
SpringApplication.run(StreamProducer9090.class, args);
}
}
- 业务类开发(发送消息接⼝、接⼝实现类、Controller)
接口
public interface IMessageProducer {
public void sendMessage(String message);
}
实现类
// Source.class里面就是对输出通道的定义(这是Spring Cloud Stream内置的通道封装)
@EnableBinding(Source.class)
public class MessageProducerImpl implements IMessageProducer {
// 将MessageChannel的封装对象Source注入到这里使用
@Autowired
private Source source;
@Override
public void sendMessage(String message) {
// 向mq中发送消息(并不是直接操作mq,应该操作的是spring cloud stream)
// 使用通道向外发出消息(指的是Source里面的output通道)
source.output().send(MessageBuilder.withPayload(message).build());
}
}
- 测试类
@SpringBootTest(classes = {StreamProducer9090.class})
class MessageProducerImplTest {
@Autowired
private IMessageProducer messageProducer;
@Test
void sendMessage() {
messageProducer.sendMessage("hello this is cloud stream test");
}
}
Stream消息驱动之开发消费者端
- 创建子工程 cloud-stream-consumer-9091
- 修改pom文件
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
</dependencies>
- 创建yaml配置文件
server:
port: 9091
spring:
application:
name: cloud-stream-producer
cloud:
stream:
binders:
cloudRabbitBinder: # 给Binder定义的名称,用于后面的关联
type: rabbit # MQ类型,如果是Kafka的话,此处配置kafka
environment: # MQ环境配置(用户名、密码等)
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
bindings: # 关联整合通道和binder对象
input: # input是我们定义的通道名称,此处不能乱改
destination: cloudExchange # 要使用的Exchange名称(消息队列主题名称)
content-type: text/plain # application/json # 消息类型设置,比如json
binder: cloudRabbitBinder # 关联MQ服务
eureka:
client:
service-url:
defaultZone: http://eureka8762.com:8762/eureka/,http://eureka8761.com:8761/eureka/ # 入驻的服务注册中心地址
fetch-registry: true
register-with-eureka: true
instance:
prefer-ip-address: true
instance-id: ${spring.cloud.client.ipaddress}:${spring.application.name}:${server.port}:@project.version@
- 主启动类
@SpringBootApplication
@EnableDiscoveryClient
public class StreamProducer9091 {
public static void main(String[] args) {
SpringApplication.run(StreamProducer9091.class, args);
}
}
- 消息消费者服务
@EnableBinding(Sink.class)
public class MessageConsumerService {
@StreamListener(Sink.INPUT)
public void input(Message<String> message) {
System.out.println("recv msg : " + message);
}
}
- 测试
启动服务消费者之后,运行上一节中编写的测试类,观察日志窗口
Stream高级之自定义消息通道
Stream 内置了两种接⼝Source和Sink分别定义了 binding 为 “input” 的输⼊流和“output” 的输出流,我们也可以⾃定义各种输⼊输出流(通道),但实际我们可以在我们的服务中使⽤多个binder、多个输⼊通道和输出通道,然⽽默认就带了⼀个input的输⼊通道和⼀个output的输出通道,怎么办?
我们是可以⾃定义消息通道的,学着Source和Sink的样⼦,给你的通道定义个⾃⼰的名字,多个输⼊通道和输出通道是可以写在⼀个类中的。
定义接口
interface CustomChannel {
String INPUT_LOG = "inputLog";
String OUTPUT_LOG = "outputLog";
@Input(INPUT_LOG)
SubscribableChannel inputLog();
@Output(OUTPUT_LOG)
MessageChannel outputLog();
}
如何使⽤?
1)在 @EnableBinding 注解中,绑定⾃定义的接⼝
2)使⽤ @StreamListener 做监听的时候,需要指定 CustomChannel.INPUT_LOG
bindings:
inputLog:
destination: cloud-Exchange-1
outputLog:
destination: cloud-Exchange-1
Stream高级之消息分组
如果消费者端有两个(消费同⼀个MQ的同⼀个主题),但是在业务场景中希望这个主题的⼀个Message只能被⼀个消费者端消费处理,此时就可以使⽤消息分组。
解决的问题:
-
能解决消息重复消费问题
-
消息持久化的问题
解决重复消费的问题
仅仅需要在服务消费者端设置 spring.cloud.stream.bindings.input.group 属性,多个消费者实例配置为同⼀个group名称(在同⼀个group中的多个消费者只有⼀个可以获取到消息并消费)。
分组前
两个消费者yaml文件中添加分组
bindings: # 关联整合通道和binder对象
input: # input是我们定义的通道名称,此处不能乱改
destination: cloudExchange # 要使用的Exchange名称(消息队列主题名称)
content-type: text/plain # application/json # 消息类型设置,比如json
binder: cloudRabbitBinder # 关联MQ服务
group: cloudgroup01
执行两次生产者,查看日志情况
解决消息持久化问题
在上面基础上,先运行生产者再运行消费者,查看是否能收到消息