应用场景

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

  @StreamListener(value = TestTopic.INPUT)

  public void receiveV1(String payload, @Header("version") String version) {

  if("1.0".equals(version)) {

  // Version 1.0

  }

  if("2.0".equals(version)) {

  // Version 2.0

  }

  }

  那么当消息处理逻辑复杂的时候,这段逻辑就会变得特别复杂。针对这个问题,在@StreamListener注解中提供了一个不错的属性condition,可以用来优化这样的处理结构。

  动手试试

  下面通过编写一个简单的例子来具体体会一下这个属性的用法:

  @EnableBinding(TestApplication.TestTopic.class)

  @SpringBootApplication

  public class TestApplication {

  public static void main(String[] args) {

  SpringApplication.run(TestApplication.class, args);

  }

  @RestController

  static class TestController {

  @Autowired

  private TestTopic testTopic;

  /**

  * 消息生产接口

  *

  * @param message

  * @return

  */

  @GetMapping("/sendMessage")

  public String messageWithMQ(@RequestParam String message) {

  testTopic.output().send(MessageBuilder.withPayload(message).setHeader("version", "1.0").build());

  testTopic.output().send(MessageBuilder.withPayload(message).setHeader("version", "2.0").build());

  return "ok";

  }

  }

  /**

  * 消息消费逻辑

  */

  @Slf4j无锡妇科医院哪家好 http://wapyyk.39.net/wx/zonghe/fc96e.html/

  @Component

  static class TestListener {

  @StreamListener(value = TestTopic.INPUT, condition = "headers['version']=='1.0'")

  public void receiveV1(String payload, @Header("version") String version) {

  log.info("Received v1 : " + payload + ", " + version);

  }

  @StreamListener(value = TestTopic.INPUT, condition = "headers['version']=='2.0'")

  public void receiveV2(String payload, @Header("version") String version) {

  log.info("Received v2 : " + payload + ", " + version);

  }

  }

  interface TestTopic {

  String OUTPUT = "example-topic-output";

  String INPUT = "example-topic-input";

  @Output(OUTPUT)

  MessageChannel output();

  @Input(INPUT)

  SubscribableChannel input();

  }

  }

  内容很简单,既包含了消息的生产,也包含了消息消费。在/sendMessage接口的定义中,发送了两条消息,一条消息的头信息中包含version=1.0,另外一条消息的头信息中包含version=2.0。在消息监听类TestListener中,对TestTopic.INPUT通道定义了两个@StreamListener,这两个监听逻辑有不同的condition,这里的表达式表示会根据消息头信息中的version值来做不同的处理逻辑分发。

  在启动应用之前,还要记得配置一下输入输出通道对应的物理目标(exchange或topic名),比如:

  spring.cloud.stream.bindings.example-topic-input.destination=test-topic

  spring.cloud.stream.bindings.example-topic-input.group=stream-content-route

  spring.cloud.stream.bindings.example-topic-output.destination=test-topic

  完成了上面配置之后,就可以启动应用,并尝试访问localhost:8080/sendMessage?message=hello接口来发送一个消息到MQ中了。此时可以看到类似下面的日志:

  2018-12-24 15:50:33.361 INFO 17683 --- [content-route-1] c.d.stream.TestApplication$TestListener : Received v1 : hello, 1.0

  2018-12-24 15:50:33.363 INFO 17683 --- [content-route-1] c.d.stream.TestApplication$TestListener : Received v2 : hello, 2.0

  从日志中可以看到,两条带有不同头信息的消息,分别通过不同的监听处理逻辑输出了对应的日志打印。

Spring Cloud Stream在同一通道根据消息内容分发不同的消费逻辑的更多相关文章

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

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

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

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

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

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

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

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

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

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

  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和kafka的那点事

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

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

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

随机推荐

  1. python模块之struct

    # #********struct模块********# # 1.按照指定格式将Python数据转换为字符串,该字符串为字节流,如网络传输时, # 不能传输int,此时先将int转化为字节流,然后再发 ...

  2. urllib库的基本使用

    urllib库的使用 官方文档地址:https://docs.python.org/3/library/urllib.html 什么是urllib Urllib是python内置的HTTP请求库包括以 ...

  3. 使用PHP操作SQL 完成简单的CURD操作

    1.从数据库出发,先建立测试数据,这里使用的MYSQL,通过脚本模式创建测试数据. SET NAMES UTF8; DROP DATABASE IF EXISTS disk; CREATE DATAB ...

  4. Qt 2D绘图之三:绘制文字、路径、图像、复合模式

    一.绘制文字 除了绘制图形以外,还可以使用QPainter::darwText()函数来绘制文字,也可以使用QPainter::setFont()设置文字所使用的字体,使用QPainter::font ...

  5. BZOJ1102(搜索)

    随便写一下的搜索,别的OJ深搜就过了,强大的BZOJ成功栈溢出RE了我并使我屈服地用广搜过掉,第一行手动开栈惨遭无视. 广搜: #pragma comment(linker, "/STACK ...

  6. 502的错误其实不是nginx的问题,要从后端找原因。php-cgi进程数不够用、php执行时间长、或者是php-cgi进程死掉,都会出现502错误。

    502的错误其实不是nginx的问题,要从后端找原因.php-cgi进程数不够用.php执行时间长.或者是php-cgi进程死掉,都会出现502错误.

  7. SpringMVC中,前台jsp封装参数,绑定参数,传递参数到后台controller的过程详解

    前台到后台的流程:前台jsp->后台:controller控制器层->service业务层->DAO数据访问层->数据库model模型层. 从上面流程可知,前台jsp的数据,想 ...

  8. Linux-软件安装(一) —— jdk/tomact 安装(普通安装)

    Linux-软件安装(一) -- jdk/tomact 安装(普通安装) 1. 可使用 FinalShell 上传至 Linux 服务器 2. 解压 cd /usr/local #解压命令 tar - ...

  9. powershell 根据错误GUID查寻错误详情

    使用azurepowershell 部署模板时,碰到了下面类似的问题: The template deployment 'ExampleDeployment-' is not valid accord ...

  10. jsp中<c:forEach varStatus="status">的属性值问题

    c:forEach varStatus属性 current当前这次迭代的(集合中的)项 index当前这次迭代从 0 开始的迭代索引 count当前这次迭代从 1 开始的迭代计数 first用来表明当 ...