Spring Cloud Stream消息驱动@SendTo和消息降级
参考程序员DD大佬的文章,自己新建demo学习学习,由于需要消息回执,看到了@SendTo这个注解能够实现,下面开始学习demo,新建两个项目cloud-stream-consumer消费端 和 cloud-stream-consumer 生产端
public interface StreamReceive {
@Input("MQRece")
SubscribableChannel mqReceive();
}
添加一个StreamReceive接口,定义@input通道
@Component
@Slf4j
public class ReceiveListener {
@StreamListener("MQRece")
public byte[] receive(byte[] bytes){
log.info("接受消息:"+new String(bytes));
return "ok".getBytes();
}
}
添加消息监听,接受消息定义为byte[]
添加application.properties配置文件信息
spring.cloud.stream.rocketmq.binder.namesrv-addr= 192.168.211.11:9876
spring.cloud.stream.bindings.MQRece.destination=message-topic
spring.cloud.stream.bindings.MQRece.group=rece-group
server.port=19999
为MQRece通道添加主题message-topic,组名rece-group
到此Stream 客户端消费就完成了,本节需要把@SendTo注解用起来,需要新建一个MessageChannel进行产生消息
public interface MsgBackPush {
@Output("back-push")
MessageChannel backPush();
}
然后在ReceiveListener添加@SendTo
@Component
@Slf4j
public class ReceiveListener {
@StreamListener("MQRece")
@SendTo("back-push")
public byte[] receive(byte[] bytes){
log.info("接受消息:"+new String(bytes));
return "ok".getBytes();
}
}
新增通道配置application.properties
spring.cloud.stream.bindings.back-push.destination=back-topic
spring.cloud.stream.bindings.back-push.group=back-group
SpringBoot启动类记得添加 @EnableBinding(value = {StreamReceive.class,MsgBackPush.class})
@SpringBootApplication
@EnableBinding(value = {StreamReceive.class,MsgBackPush.class})
public class CloudStreamConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(CloudStreamConsumerApplication.class, args);
}
}
到此,cloud-stream-consumer这个demo就完成了
接下来看看 cloud-stream-producer
public interface StreamPush {
@Output("MQPush")
MessageChannel mqPush();
}
定义一个通道名为MQPush,进行消息生产
public interface ProducerReceive {
@Input("producer-receive")
SubscribableChannel producerReceive();
}
定义一个通道名为producer-receive,进行回执消息的消费
@Component
@Slf4j
public class ProducerListener {
@StreamListener("producer-receive")
public void producerReceive(byte[] bytes){
log.info("come back message:"+new String(bytes));
}
}
具体回执消息处理逻辑,再来看看application.properties
spring.cloud.stream.rocketmq.binder.namesrv-addr= 192.168.214.191:9876
spring.cloud.stream.bindings.MQPush.destination=message-topic
spring.cloud.stream.bindings.MQPush.group=push-group
spring.cloud.stream.bindings.producer-receive.destination=back-topic
spring.cloud.stream.bindings.producer-receive.group=back-group
server.port=20000
为通道设置topic和group,新建一个Http接口测试一下成果
@SpringBootApplication
@EnableBinding(value = {StreamPush.class,ProducerReceive.class})
@RestController
public class CloudStreamProducerApplication {
public static void main(String[] args) {
SpringApplication.run(CloudStreamProducerApplication.class, args);
}
@Autowired
private StreamPush streamPush;
@GetMapping("/sendMessage")
public String sendMessage(){
streamPush.mqPush().send(MessageBuilder.withPayload("message body".getBytes()).build());
return "ok";
}
}
访问http://localhost:20000/sendMessage,结果图如下
cloud-stream-consumer日志输出

cloud-stream-producer日志输出

学习@ServiceActivator这个注解,上面的项目cloud-stream-consumer ReceiveListener类中添加
@Component
@Slf4j
public class ReceiveListener {
@StreamListener("MQRece")
@SendTo("back-push")
public byte[] receive(byte[] bytes){
log.info("接受消息:"+new String(bytes));
// 抛出异常
if(1==1){
throw new RuntimeException("Message consumer failed!");
}
return "ok".getBytes();
}
@Autowired
private MsgBackPush msgBackPush;
@ServiceActivator(inputChannel = "message-topic.rece-group.errors")
public void error(Message<?> message){
log.info("消费者消费消息失败:"+message);
msgBackPush.backPush().send(MessageBuilder.withPayload("消息消费失败".getBytes()).build());
}
}
通过使用@ServiceActivator(inputChannel = "test-topic.stream-exception-handler.errors")指定了某个通道的错误处理映射。其中,inputChannel的配置中对应关系如下:
- message-topic:消息通道对应的目标(destination,即:spring.cloud.stream.bindings.MQRece.destination的配置)
- rece-group:消息通道对应的消费组(group,即:spring.cloud.stream.bindings.MQRece.group的配置)
访问http://localhost:20000/sendMessage,结果图如下
cloud-stream-consumer日志输出

cloud-stream-producer日志输出

个人联系方式QQ:944484545,欢迎大家的加入,分享学习是一件开心事
Spring Cloud Stream消息驱动@SendTo和消息降级的更多相关文章
- Spring Cloud Stream在同一通道根据消息内容分发不同的消费逻辑
应用场景 有的时候,我们对于同一通道中的消息处理,会通过判断头信息或者消息内容来做一些差异化处理,比如:可能在消息头信息中带入消息版本号,然后通过if判断来执行不同的处理逻辑,其代码结构可能是这样的: ...
- 整合Spring Cloud Stream Binder与RabbitMQ进行消息发送与接收
我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 前言 Spring Cloud Stream专门用于事件驱动的微服务系统,使用消息中间件来收发信息.使用Spring ...
- 使用 Spring Cloud Stream 构建消息驱动微服务
相关源码: spring cloud demo 微服务的目的: 松耦合 事件驱动的优势:高度解耦 Spring Cloud Stream 的几个概念 Spring Cloud Stream is a ...
- Spring Cloud Alibaba学习笔记(12) - 使用Spring Cloud Stream 构建消息驱动微服务
什么是Spring Cloud Stream 一个用于构建消息驱动的微服务的框架 应用程序通过 inputs 或者 outputs 来与 Spring Cloud Stream 中binder 交互, ...
- 「 从0到1学习微服务SpringCloud 」08 构建消息驱动微服务的框架 Spring Cloud Stream
系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现 「 从0到1学习微服务S ...
- 整合Spring Cloud Stream Binder与GCP Pubsub进行消息发送与接收
我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 前言 之前的文章<整合Spring Cloud Stream Binder与RabbitMQ进行消息发送与接收& ...
- Spring cloud stream【入门介绍】
案例代码:https://github.com/q279583842q/springcloud-e-book 在实际开发过程中,服务与服务之间通信经常会使用到消息中间件,而以往使用了哪个中间件比如 ...
- Spring Cloud Stream 进行服务之间的通讯
Spring Cloud Stream Srping cloud Bus的底层实现就是Spring Cloud Stream,Spring Cloud Stream的目的是用于构建基于消息驱动(或事件 ...
- 简单聊一聊spring cloud stream和kafka的那点事
Spring Cloud Stream is a framework for building highly scalable event-driven microservices connected ...
随机推荐
- hdu 1156 && poj 2464 Brownie Points II (BIT)
2464 -- Brownie Points II Problem - 1156 hdu分类线段树的题.题意是,给出一堆点的位置,stan和ollie玩游戏,stan通过其中一个点画垂线,ollie通 ...
- H3C 路由协议与可路由协议
- HTML--简单的注册网页
利用表格,渐变色 <body background="未标题-2.jpg"> <table align="center" border=&qu ...
- [转]Springboot和SpringMVC区别
spring boot只是一个配置工具,整合工具,辅助工具. springmvc是框架,项目中实际运行的代码 Spring 框架就像一个家族,有众多衍生产品例如 boot.security.jpa等等 ...
- 【u212】&&【t036】最大和
Time Limit: 1 second Memory Limit: 128 MB [问题描述] N个数围成一圈,要求从中选择若干个连续的数(注意每个数最多只能选一次)加起来,问能形成的最大的和. [ ...
- 微信里首次跳转会到首页问题(window.location失效)
将window.location.href 换为location.href
- tab选项卡平滑滚动vue
<html lang="en"> <head> <meta charset="UTF-8"> <title>Ti ...
- HTML自制计算器
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- keil中使用_at_绝对地址定位问题
最近在做51单片机的时候,看到程序中某头文件有这样一段: 其中,_at_的作用就是将变量限定存放在指定的RAM空间.比如在这个单片机头文件中,就是将变量P00F,P01F分别存到Addr(0x8000 ...
- 解决Win10电脑右下角的“激活windows转到电脑设置”的水印的方法
Win10正式版的用户反馈新系统在使用一段时候后,自己电脑桌面右下角就突然出现了“激活windows10转到设置以激活windows”的水印字样.这是怎么回事呢?下面,我就向大家分享win10电脑右下 ...