Spring cloud stream 3.1 rocketmq踩坑记录

spring cloud stream 新绑定方式#

新版spring cloud stream文档

新版提倡用函数式进行发送和消费信息

定义返回类型为Supplier, Function or Consumer的bean提供消息发送和消费的bean

看看绑定名称命名规则

  • input - + -in- +
  • output - + -out- +

在配置文件中指定spring.cloud.function.definition的名称后会把这个bean绑定到对应的消费者和提供者上.

如下定义 会把bean绑定在消费者consumerEvent-in-0或者提供者consumerEvent-out-0上

多个bean可以用 ; 进行分割

Copy Highlighter-hljs
spring: cloud: function: definition: consumerEvent

指定这个消费者的topic和group

Copy Highlighter-hljs
spring: cloud: stream: bindings: consumerEvent-in-0: destination: DEMO group: demo-group

注册消费者的bean

Copy Highlighter-hljs
// 第一种方式(官方推荐) @Bean public Function<Flux<Message<String>>, Mono<Void>> consumerEvent() { return flux -> flux.map(message -> { System.out.println(message.getPayload()); return message; }).then(); } // 第二种方式 // 注意使用Flux 要调用 subscribe 不然这个方法不会被消费 @Bean public Consumer<Flux<Message<String>>> consumerEvent() { return flux -> flux.map(message -> { System.out.println(message.getPayload()); return message; }).subscribe(); } // 或 @Bean public Consumer<Message<String>> consumerEvent() { return message -> System.out.println(message.getPayload()); }

示例#

提供者

Copy Highlighter-hljs
@Configuration public class EventSender { @Bean public Demo demo() { return new Demo(); } static class Demo implements CommandLineRunner { @Autowired StreamBridge streamBridge; @Override public void run(String... args) throws Exception { final Message<T> message = MessageBuilder.withPayload("Body") .setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.APPLICATION_JSON) .build(); // 第一个配置的是目的地 // 如果在yaml中有配置会发送到yaml中目的地 streamBridge.send("DEMO", message); } } }

配置rocketmq和stream的配置

Copy Highlighter-hljs
spring: application: name: demo cloud: stream: rocketmq: binder: name-server: 127.0.0.1:9876 group: demo bindings: consumerEvent-in-0: destination: DEMO content-type: application/json group: demo-group function: definition: consumerEvent

注册一个消费者

Copy Highlighter-hljs
@Configuration public class EventReceptor { @Bean public Function<Flux<Message<String>>, Mono<Void>> consumerEvent() { return flux -> flux.map(message -> { System.out.println(message.getPayload()); return message; }).then(); } }

依赖

spring cloud 2020 默认不使用bootstrap启动 要加这个依赖spring-cloud-starter-bootstrap

Copy Highlighter-hljs
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2020.0.2</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.5-RocketMQ-RC1</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> <version>3.0.2</version> </dependency>

Tag过滤#

在新版的时候过滤tag一直失效, 后面看源码发现新版的sql和tag结合到subscription的属性中

Copy Highlighter-hljs
this.pushConsumer.subscribe(this.topic, RocketMQUtils.getMessageSelector(((RocketMQConsumerProperties)this.extendedConsumerProperties.getExtension()).getSubscription())); public static MessageSelector getMessageSelector(String expression) { return StringUtils.hasText(expression) && expression.startsWith("sql:") ? MessageSelector.bySql(expression.replaceFirst("sql:", "")) : MessageSelector.byTag(expression); }

如果消费者要过滤某个tag需要这么写

Copy Highlighter-hljs
// 新版 (现在的写法) rocketmq: bindings: createUserAccountEvent-in-0: consumer: subscription: DEMO-TAG // 旧版 (以前的写法) rocketmq: bindings: createUserAccountEvent-in-0: consumer: tag: DEMO-TAG
posted @   MY1024-  阅读(1583)  评论(2编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)

Wish.
Do.

点击右上角即可分享
微信分享提示
CONTENTS