参考程序员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和消息降级的更多相关文章

  1. Spring Cloud Stream在同一通道根据消息内容分发不同的消费逻辑

    应用场景 有的时候,我们对于同一通道中的消息处理,会通过判断头信息或者消息内容来做一些差异化处理,比如:可能在消息头信息中带入消息版本号,然后通过if判断来执行不同的处理逻辑,其代码结构可能是这样的: ...

  2. 整合Spring Cloud Stream Binder与RabbitMQ进行消息发送与接收

    我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 前言 Spring Cloud Stream专门用于事件驱动的微服务系统,使用消息中间件来收发信息.使用Spring ...

  3. 使用 Spring Cloud Stream 构建消息驱动微服务

    相关源码: spring cloud demo 微服务的目的: 松耦合 事件驱动的优势:高度解耦 Spring Cloud Stream 的几个概念 Spring Cloud Stream is a ...

  4. Spring Cloud Alibaba学习笔记(12) - 使用Spring Cloud Stream 构建消息驱动微服务

    什么是Spring Cloud Stream 一个用于构建消息驱动的微服务的框架 应用程序通过 inputs 或者 outputs 来与 Spring Cloud Stream 中binder 交互, ...

  5. 「 从0到1学习微服务SpringCloud 」08 构建消息驱动微服务的框架 Spring Cloud Stream

    系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现 「 从0到1学习微服务S ...

  6. 整合Spring Cloud Stream Binder与GCP Pubsub进行消息发送与接收

    我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 前言 之前的文章<整合Spring Cloud Stream Binder与RabbitMQ进行消息发送与接收& ...

  7. Spring cloud stream【入门介绍】

    案例代码:https://github.com/q279583842q/springcloud-e-book   在实际开发过程中,服务与服务之间通信经常会使用到消息中间件,而以往使用了哪个中间件比如 ...

  8. Spring Cloud Stream 进行服务之间的通讯

    Spring Cloud Stream Srping cloud Bus的底层实现就是Spring Cloud Stream,Spring Cloud Stream的目的是用于构建基于消息驱动(或事件 ...

  9. 简单聊一聊spring cloud stream和kafka的那点事

    Spring Cloud Stream is a framework for building highly scalable event-driven microservices connected ...

随机推荐

  1. 2018-5-28-win10-uwp-动态修改ListView元素布局

    title author date CreateTime categories win10 uwp 动态修改ListView元素布局 lindexi 2018-05-28 15:15:54 +0800 ...

  2. Codeforces Round #180 (Div. 1 + Div. 2)

    A. Snow Footprints 如果只有L或者只有R,那么起点和终点都在边界上,否则在两者的边界. B. Sail 每次根据移动后的曼哈顿距离来判断是否移动. C. Parity Game 如果 ...

  3. 深入理解 Embedding层的本质

    继上文https://blog.csdn.net/weixin_42078618/article/details/82999906探讨了embedding层的降维效果,时隔一个月,分享一下嵌入层在NP ...

  4. navicat ssh通道受限问题处理

    navicat 链接数据库 使用navicat 的ssh通道连接数据库回遇到权限问题 错误代码如下: 80070007: SSH Tunnel: Server does not support dif ...

  5. [转]如何让多个不同类型的后端网站用一个nginx进行反向代理实际场景分析

    前段时间公司根据要求需要将聚石塔上服务器从杭州整体迁移到张家口,刚好趁这次机会将这些乱七八糟的服务器做一次梳理和整合,断断续续一个月迁移完 成大概优化掉了1/3的机器,完成之后遇到了一些问题,比如曾今 ...

  6. 4-10 items设计

    1,items相当于dict,但是又比字典好 2,parse.urljoin(response.url,post_url)方法,其中image_url是一个域名的话,其中的当前域名就不用再添加. yi ...

  7. 模板——Treap实现名次树

    Treap 是一种通过赋予结点随机权值的一种满足堆性质的二叉搜索树,它很好的解决了二叉搜索树顺序插入组成链式的局限性. 名次树是指在treap的每个结点中添加附加域size,表示以它为根的子树的总结点 ...

  8. H3C 路由表查找规则(1)

  9. 2019-4-10-win10-uwp-自定义标记扩展

    title author date CreateTime categories win10 uwp 自定义标记扩展 lindexi 2019-04-10 09:46:13 +0800 2019-04- ...

  10. linux 使用 ioctl 参数

    在看 scull 驱动的 ioctl 代码之前, 我们需要涉及的另一点是如何使用这个额外的参数. 如果它是一个整数, 就容易: 它可以直接使用. 如果它是一个指针, 但是, 必须小心些. 当用一个指针 ...