1、概述简介
- 屏蔽底层消息中间件的差异,降低切换成本,统一消息的编程模式。
- 通过定义绑定器作为中间层,完美的实现应用程序与消息中间件间的隔离
- 通过向应用程序暴露统一的channel通道,使得应用程序不需要再考虑各种不同的消息中间件的实现
2、常用组件注解
- Middleware:中间件,目前只支持RabbitMQ和Kafka
- Binder:Binder是应用与消息中间件之间的封装,目前实行了Kafka和RabbitMQ的Binder,通过Binder可以很方便的连接中间件,可以动态的改变消息类型(对应于Kafka的topic,RabbitMQ的exchange),这些都可以同配置文件实现。
- @Input:输入通道,通过该输入通道接收到的消息进入应用程序
- @Output:输出通道,发布的消息将通过该通道离开应用程序
- @StreamListener:监听队列,用于消费者队列的消息接收
- @EnableBinding:指通道channel和exchange绑定在一起
3、程序
3.1、生产者
启动类
@SpringBootApplication
public class StreamMQMain8801 {
public static void main(String[] args) {
SpringApplication.run(StreamMQMain8801.class,args);
}
}
yml文件
server:
port: 8801
spring:
application:
name: cloud-stream-provider
cloud:
stream:
binders:
defaultRabbit:
type: rabbit
environment:
spring:
rabbitmq:
host: 192.168.75.128
port: 5672
username: guest
password: guest
bindings:
output:
destination: studyExchange
content-type: application/json
binder: defaultRabbit
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
instance:
lease-renewal-interval-in-seconds: 2
lease-expiration-duration-in-seconds: 5
instance-id: send-8801.com
prefer-ip-address: true
service实现
@EnableBinding(Source.class)
@Slf4j
public class MessageProviderImpl implements IMessageProvider {
@Resource
private MessageChannel output;
@Override
public String send() {
String serial = UUID.randomUUID().toString();
output.send(MessageBuilder.withPayload(serial).build());
log.info("******serial:"+serial);
return null;
}
}
controller业务
@RestController
@Slf4j
public class SendMessageController {
@Resource
private IMessageProvider iMessageProvider;
@GetMapping("/sendMessage")
public String sendMessage(){
return iMessageProvider.send();
}
}
3.2、消费者
启动类
@SpringBootApplication
public class StreamMQMain8802 {
public static void main(String[] args) {
SpringApplication.run(StreamMQMain8802.class,args);
}
}
yml文件
server:
port: 8802
spring:
application:
name: cloud-stream-consumer
cloud:
stream:
binders:
defaultRabbit:
type: rabbit
environment:
spring:
rabbitmq:
host: 192.168.75.128
port: 5672
username: guest
password: guest
bindings:
input:
destination: studyExchange
content-type: application/json
binder: defaultRabbit
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
instance:
lease-renewal-interval-in-seconds: 2
lease-expiration-duration-in-seconds: 5
instance-id: receive-8802.com
prefer-ip-address: true
controller业务
@Component
@Slf4j
@EnableBinding(Sink.class)
public class ReceiveMessageController {
@Value("${server.port}")
private String serverPort;
@StreamListener(Sink.INPUT)
public void input(Message<String> message){
log.info("消费者1号:----->接收到的消息:"+message.getPayload()+"\t"+" serverPort:"+serverPort);
}
}
测试
生产者发送消息,http://localhost:8801/sendMessage点击三次… 生产者: 消费者: ribbitmq
4、消息重复消费
创建项目cloud-stream-rabbitmq-consumer8803与cloud-stream-rabbitmq-consumer8802是并列的消费者。 此时生产者发送消息,这两个消费者都会收到,因为默认情况下不同的微服务是不同组,是并列关系,只要改为竞争关系即可。
解决方案:自定义同一个组。
测试启动服务
生产者发送4条消息,http://localhost:8801/sendMessage 生产者: 消费者1: 消费者2:
5、消息持久化
cloud-stream-rabbitmq-consumer8802去掉group分组 cloud-stream-rabbitmq-consumer8802分组group依然为myGroup
只启动生产者
生产者发送4条消息,http://localhost:8801/sendMessage 启动8802消费者: 启动8803消费者: 由此可见group既可以解决重复消费又可以持久化消息
|