1、项目 pom.xml 文件引入 RocketMQ 依赖:
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
2、resources 下的 application.yml 添加:
rocketmq:
name-server: 127.0.0.1:9876
# 纯消费者不需要以下配置
producer:
group: test-group
3、编写生产者类
@Api(tags = "RocketMQ 生产者测试")
@RestController
@RequestMapping(value = "/test")
public class RocketMQTestController {
@Resource
private RocketMQTemplate rocketMQTemplate;
@ApiOperation(value = "发送消息")
@GetMapping("/send")
public String query(String topic, String s) {
rocketMQTemplate.convertAndSend(topic, s);
return "ok";
}
}
4、 编写消费者类
@Component
@RocketMQMessageListener(topic = "test-topic", consumerGroup = "test-consumer", consumeMode = ConsumeMode.ORDERLY)
public class DemoListener implements RocketMQListener<String> {
@Override
public void onMessage(String s) {
System.out.println("收到消息啦:" + s);
}
}
5、启动项目测试发送消息:
6、查看控制台收到消息: