使用apache的rocketmq-spring-boot-starter自动化配置RocketMQ

尚嘉勋
2023-12-01


pom引入

<dependency>
   <groupId>org.apache.rocketmq</groupId>
   <artifactId>rocketmq-spring-boot-starter</artifactId>
   <version>2.2.0</version>
</dependency>

application.yml配置

rocketmq:
  name-server: 127.0.0.1:9876
  producer:
    group: test
  consumer:
    group: test
    pullBatchSize: 10

注:rocketmq不再spring下,应放到做外层

producer

@Component
public class RocketMQProducer {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public void sendMsg(String topic, String msg)
    {
        System.out.println("发送报文:" + msg);
        this.rocketMQTemplate.convertAndSend(topic, msg);
    }
}

consumer

@Component
@RocketMQMessageListener(topic = "test", consumerGroup = "${rocketmq.consumer.group}")
public class RocketMQConsumer implements RocketMQListener<String> {

    @Override
    public void onMessage(String s) {
        System.out.println("接收到rocketmq消息:" + s);
    }
}
 类似资料: