SpringCloudStream+RocketMQ多topic

之前写过两篇关于SpringCloudStream文章

spring-cloud-stream版本升级,告别旧注解@EnableBinding,拥抱函数式编程_spring-cloud-stream output注解没有了-CSDN博客

SpringCloudStream+RocketMQ事务消息配置_spring-cloud-starter-stream-rocketmq-CSDN博客

提到了一个多topic问题,在不配置spring.cloud.stream.function.definition的情况下只能配置一个in和一个out,这篇文章来解决这个问题。

根据上面的第一篇文章续写代码

一、续写生产者

1.写配置

在bindings后面添加即可,这里给出完整配置

复制代码
spring:
  application:
    name: provider
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
        namespace: local
        username: nacos
        password: nacos
    stream:
      rocketmq:
        binder:
          name-server: 127.0.0.1:9876
      bindings:
        ## 新版本固定格式  channel名字-{out/in}-{index}
        addBounsChannel-out-0:
          destination: add-bouns
          group: bouns-producer-group
        secondChannel-out-0:
          destination: second
          group: second-producer-group
        source-out-0:
          destination: third
          group: third-producer-group
server:
  port: 8081

2.写代码

同样的使用streamBridge.send发消息

复制代码
public void sendMqSecondChannel() {
        streamBridge.send("secondChannel-out-0", "sendMqSecondChannel from Provider!");
    }

二、续写消费者

1.写配置

多个消费者时,需要在spring.cloud.stream.function.definition 定义bean名称,多个时用分号分隔

给出完整yaml代码

复制代码
spring:
  application:
    name: consumer
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
        namespace: local
        username: nacos
        password: nacos
    stream:
      function:
        definition: addBounsChannel;secondChannel;source
      rocketmq:
        binder:
          name-server: 127.0.0.1:9876
      bindings:
        ## 新版本固定格式  channel名字-{out/in}-{index}
        addBounsChannel-in-0:
          destination: add-bouns
          group: bouns-consumer-group
        secondChannel-in-0:
          destination: second
          group: second-consumer-group
        source-in-0:
          destination: third
          group: third-consumer-group
server:
  port: 8082

2.写代码

跟之前一样,定义bean的Consumer

复制代码
   @Bean
    public Consumer<String> secondChannel() {
        return message -> {
            System.out.println("secondChannel消费消息:" + message);
        };
    }

三、常见问题

1.有Consumer必定会有Supplier,为什么不用Supplier发消息

Supplier也需要配置成@Bean,Supplier也跟Consumer一样用的监听模式,启动后不做处理会一直发,不如streamBridge.send发送灵活,这篇文章也有Supplier发送的例子可供参考Kafka Streams with Spring Cloud Stream - Spring Cloud

2.本文完整项目示例代码

【免费】springcloud-stream-rocketmq多topic示例代码资源-CSDN文库

相关推荐
佳腾_1 天前
【消息队列RocketMQ】一、RocketMQ入门核心概念与架构解析
中间件·架构·消息队列·云计算·rocketmq
Pasregret2 天前
09-RocketMQ 深度解析:从原理到实战,构建可靠消息驱动微服务
微服务·wpf·rocketmq
可乐仙人6 天前
常见MQ及类MQ对比:Redis Stream、Redis Pub/Sub、RocketMQ、Kafka 和 RabbitMQ
redis·kafka·rocketmq
小山曲奇11 天前
在docker里装rocketmq-console
rocketmq
明天过后ww12 天前
RocketMQ和kafka 的区别
分布式·kafka·rocketmq
唐人街都是苦瓜脸14 天前
Kafka和RocketMQ相比有什么区别?那个更好用?
分布式·中间件·kafka·rocketmq
小哀216 天前
RocketMQ 5.2.0 集群Dledger的扩展调研/实践
后端·rocketmq
dengjiayue16 天前
消息队列(kafka 与 rocketMQ)
分布式·kafka·rocketmq
Balloon16 天前
使用Sentinel给RocketMQ消费端限流
rocketmq
小园子的小菜17 天前
深入探究 RocketMQ 中的 Broker2Client 组件
java·rocketmq·java-rocketmq