rocketmq-spring-boot-starter使用指南

司马晋
2023-12-01

https://github.com/javahongxi/whatsmars.git

针对官方starter修改点 官方

  • 支持连接多个集群(订阅) (官方一个应用只能连接一个集群)
  • 顺序消息消费失败,可配重试次数 (非顺序消息默认重试16次,每次时间延后)
  • 发送延时消息方法参数优化(魔法参数改为枚举)
  • 优化getMessageType方法,支持 MyConsumer extends AbstractConsumer implements RocketMQListener
    (官方只支持MyConsumer implements RocketMQListener)
  • RocketMQTemplate方法重载(加入keys)
  • 暂未加入事务消息功能 (官方最新版支持)

关于RocketMQTemplate往多集群发送消息的说明

当需要往其他集群发送消息时,需要拷贝一份RocketMQAutoConfigurationRocketMQProperties
并相应修改@Bean方法名和Conditional,最重要的是给MQProducer设置instanceName,且默认的MQProducer也需要手动实例化。
当然,科学的场景应该是往自己的集群发消息,让外部系统来消费。

dependencies

<dependency>
    <groupId>org.hongxi</groupId>
    <artifactId>rocketmq-spring-boot-starter</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</dependency>

application.yml

spring:
  rocketmq:
    nameServer: 127.0.0.1:9876
    producer:
      group: boot_producer_group

Send Message

@SpringBootApplication
public class ProducerApplication implements CommandLineRunner {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    
    public static void main(String[] args){
        SpringApplication.run(ProducerApplication.class, args);
    }
    
    public void run(String... args) throws Exception {
		// 如下两种方式等价
        rocketMQTemplate.convertAndSend("test-topic-1", "Hello, World!");
        rocketMQTemplate.send("test-topic-1", MessageBuilder.withPayload("Hello, World! I'm from spring message").build());
        // 第三个参数为key
        rocketMQTemplate.syncSend("test-topic-1", "Hello, World! I'm from simple message", "18122811143034568830");
		
		// topic: ORDER,tag: paid, cacel
		rocketMQTemplate.convertAndSend("ORDER:paid", "Hello, World!");
        rocketMQTemplate.convertAndSend("ORDER:cancel", "Hello, World!");
		
	    // 消息体为自定义对象
        rocketMQTemplate.convertAndSend("test-topic-2", new OrderPaidEvent("T_001", new BigDecimal("88.00")));


		// 发送延迟消息
        rocketMQTemplate.sendDelayed("test-topic-1", "I'm delayed message", MessageDelayLevel.TIME_1M);
		
		// 发送即发即失消息(不关心发送结果)
		rocketMQTemplate.sendOneWay("test-topic-1", MessageBuilder.withPayload("I'm one way message").build());


		// 发送顺序消息
        rocketMQTemplate.syncSendOrderly("test-topic-4", "I'm order message", "1234");
		
		// 发送异步消息
		rocketMQTemplate.asyncSend("test-topic-1", MessageBuilder.withPayload("I'm one way message").build(), new SendCallback() {
    @Override
    public void onSuccess(SendResult sendResult) {
        
    }

    @Override
    public void onException(Throwable e) {
        
    }
});

        System.out.println("send finished!");
    }

}

Consume Message

@SpringBootApplication
public class ConsumerApplication{
    
    public static void main(String[] args){
        SpringApplication.run(ConsumerApplication.class, args);
    }

}
@Slf4j
@Service
@RocketMQMessageListener(topic = "test-topic-1", consumerGroup = "my-consumer_test-topic-1")
public class MyConsumer implements RocketMQListener<String> {
    public void onMessage(String message) {
        log.info("received message: " + message);
    }
}
@Slf4j
@Service
@RocketMQMessageListener(topic = "test-topic-2", consumerGroup = "my-consumer_test-topic-2")
public class MyConsumer2 implements RocketMQListener<OrderPaidEvent> {
    public void onMessage(OrderPaidEvent orderPaidEvent) {
        log.info("received orderPaidEvent: " + orderPaidEvent);
    }
}
/**
 * 指定连接某个MQ集群
 */

@Slf4j
@Service
@RocketMQMessageListener(nameServer = "127.0.0.1:9877", instanceName = "tradeCluster", topic = "test-topic-3", consumerGroup = "my-consumer_test-topic-3")
public class MyConsumer3 implements RocketMQListener<String> {
    public void onMessage(String message) {
        log.info("received message: " + message);
    }
}
/**
 * 顺序消息消费失败,默认不重试(官方是一直重试)
 */
@Slf4j
@Service
@RocketMQMessageListener(topic = "test-topic-4", consumerGroup = "my-consumer_test-topic-4",
    consumeMode = ConsumeMode.ORDERLY)
public class MyConsumer4 implements RocketMQListener<String> {
    public void onMessage(String message) {
        log.info("received message: " + message);
        int a = 1 / 0;
    }
}
/**
 * 配置重试次数 reconsumeTimes = 3
 */
@Slf4j
@Service
@RocketMQMessageListener(topic = "test-topic-4", consumerGroup = "my-consumer_test-topic-5",
    consumeMode = ConsumeMode.ORDERLY, reconsumeTimes = 3)
public class MyConsumer5 implements RocketMQListener<MessageExt> {
    public void onMessage(MessageExt messageExt) {
        log.info("received message: " + messageExt);
        int a = 1 / 0;
    }
}
/**
 * 配置重试次数 reconsumeTimes = -1 代表一直重试
 */
@Slf4j
@Service
@RocketMQMessageListener(topic = "test-topic-4", consumerGroup = "my-consumer_test-topic-6",
    consumeMode = ConsumeMode.ORDERLY, reconsumeTimes = -1)
public class MyConsumer6 implements RocketMQListener<MessageExt> {
    public void onMessage(MessageExt messageExt) {
        log.info("received message: " + messageExt);
        int a = 1 / 0;
    }
}

注意:Consumer里抛出异常才会重试,所以使用者不要把Consumer里的整个代码try-catch

 类似资料: