spring cloud stream3.0 rocketmq替换rabbit全过程

1、依赖

implementation("org.springframework.cloud:spring-cloud-stream-binder-rabbit")

更改为:

implementation("org.springframework.cloud:spring-cloud-stream-binder-rocketmq:0.9.0.RELEASE")

或者maven:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-stream-binder-rocketmq</artifactId>
    <version>0.9.0.RELEASE</version>
</dependency>

可以去选择自己所需要的版本:https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-stream-binder-rocketmq

2、服务配置

spring:
  rabbitmq:
    host: rabbitmqhost.com

更换为:

spring:
  cloud:
    stream:
      rocketmq:
        binder:
          name-server: rocketmq1.com:9876;rocketmq2.com:9876

3、topic配置

spring:
  cloud:
	stream:
      defaultBinder: rabbit
      binders:
        rabbit:
          type: rabbit
      bindings:
        test-in-0:
          destination: test
          group: test.group
      rabbit:
        bindings:
          test-in-0:
            consumer:
              autoBindDlq: true
              deadLetterQueueName: system.global-dlq.system
    function:
      definition: test

更换为:

spring:
  cloud:
	stream:
      defaultBinder: rocketmq
      binders:
        rocketmq:
          type: rocketmq
      bindings:
        test-in-0:
          destination: test
          # rocketmq 不能使用. 只能使用英文和横线、下划线
          group: test_group
          # 可以设置spring.cloud.stream 自带的重试为1,表示禁止重试
          # 我们使用rocketmq的重试策略就好
          consumer:
            max-attempts: 1
    function:
      definition: test

1、rocketmq的topic只支持 名命为:[%|a-zA-Z0-9_-]+$,大小写英文、数字、下划线、英文横线。不支持其他包括 “.”;
2、一个消费者订阅多个Topic不能使用同一个group,因此这里命名为Topic name + group
3、spring.cloud.stream.bindings.(channelName).consumer.max-attempts=1 表示禁用spring-cloud-stream框架重试,因此项目中使用mq本身重试,默认为16次,也可以设置spring.cloud.stream.rocketmq.bindings.(channelName).consumer.delayLevelWhenNextConsume = -1禁用mq端重试
4、其他配置可参考官方:https://github.com/alibaba/spring-cloud-alibaba/wiki/RocketMQ-en

5、单元测试修改

由于一个消费者订阅多个Topic不能使用同一个group,多个单元测试一起运行时会报错:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.rocketmq.spring.starter.internalRocketMQTransAnnotationProcessor' defined in class path resource [org/springframework/cloud/stream/binder/rocketmq/config/RocketMQComponent4BinderAutoConfiguration.class]: Unsatisfied dependency expressed through method 'transactionAnnotationProcessor' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'transactionHandlerRegistry' defined in class path resource [org/springframework/cloud/stream/binder/rocketmq/config/RocketMQComponent4BinderAutoConfiguration.class]: Unsatisfied dependency expressed through method 'transactionHandlerRegistry' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'rocketMQTemplate' defined in class path resource [org/springframework/cloud/stream/binder/rocketmq/config/RocketMQComponent4BinderAutoConfiguration.class]: Invocation of init method failed; nested exception is org.apache.rocketmq.client.exception.MQClientException: The producer group[rocketmq_binder_default_group_name] has been created before, specify another name please.

在这里插入图片描述
需要添加注解@DirtiesContext 来解决:
在这里插入图片描述
@DirtiesContext :标记为污染ApplicationContext环境,运行完毕后将被移除,新的springbootTest会重新构建环境
有关此注解的可以参考:https://docs.spring.io/spring-framework/docs/5.0.8.RELEASE/spring-framework-reference/testing.html#dirtiescontext

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值