点击查看【bilibili】

安装 RocketMQ

  • 下载脚本文件
  1. git clone https://github.com/pig-mesh/rocketmq-docker-compose
  • 修改 IP 为宿主机 IP
  1. vim rocketmq-docker-compose/data/broker/broker.conf

整合 RocketMQ 消息队列 - 图1

  • 执行启动 rocketmq
  1. ./run.sh

代码整合

  • 目标服务增加 rocketmq 依赖 jar
  1. <dependency>
  2. <groupId>org.apache.rocketmq</groupId>
  3. <artifactId>rocketmq-spring-boot-starter</artifactId>
  4. <version>2.2.1</version>
  5. </dependency>
  • nacos 对应的服务配置文件增加链接相关信息
  1. rocketmq:
  2. name-server: 192.168.0.12:9876
  3. producer:
  4. group: test-group
  • 配置队列监听消费者
  1. @Slf4j
  2. @Service
  3. @RocketMQMessageListener(topic = "test-topic-1", consumerGroup = "test-group")
  4. public static class MyConsumer1 implements RocketMQListener<String> {
  5. public void onMessage(String message) {
  6. log.info("received message: {}", message);
  7. }
  8. }
  • 测试消息发送
  1. @SpringBootTest
  2. class DemoApplicationTests {
  3. @Autowired
  4. private RocketMQTemplate rocketMQTemplate;
  5. @Test
  6. void contextLoads() {
  7. rocketMQTemplate.convertAndSend("test-topic-1", "Hello, World!");
  8. }
  9. }

❤ 问题咨询

手势点击蓝字求关注简约风动态引导关注__2022-09-07+23_18_38.gif