动力节点双哥最新RocketMQ视频
22. Rocketmq集成SpringBoot
22.1 搭建rocketmq-producer(消息生产者)
22.1.1 创建项目,完整的pom.xml
| <?_xml version="1.0" encoding="UTF-8"_?>
<project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.6.3
_
com.powernode
01-rocketmq-producer
0.0.1-SNAPSHOT
rocketmq-producer
Demo project for Spring Boot
<java.version>1.8</java.version>
org.springframework.boot
spring-boot-starter-web
** **
** _**
** **
**org.apache.rocketmq **
**rocketmq-spring-boot-starter 2.0.2 **
** **
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
org.projectlombok
lombok
22.1.2 修改配置文件application.yml
| spring: application:
name: rocketmq-producerrocketmq:
name-server: 127.0.0.1:9876
rocketMq的nameServer地址
producer:
group: powernode-group
生产者组别
send-message-timeout: 3000
消息发送的超时时间
retry-times-when-send-async-failed: 2
异步消息发送失败重试次数
max-message-size: 4194304
# 消息的最大长度 |
---|
22.1.3 我们在测试类里面测试发送消息
往powernode主题里面发送一个简单的字符串消息
| /** * 注入rocketMQTemplate,我们使用它来操作mq /
@Autowiredprivate RocketMQTemplate rocketMQTemplate;
/* * 测试发送简单的消息 * * @throws Exception */
@Testpublic void testSimpleMsg() throws Exception {
// 往powernode的主题里面发送一个简单的字符串消息
SendResult sendResult = rocketMQTemplate.syncSend(“powernode”, “我是一个简单的消息”);
// 拿到消息的发送状态
System.out.println(sendResult.getSendStatus());
// 拿到消息的id
System.out.println(sendResult.getMsgId());
}
|
| — |
运行后查看控制台
22.1.4 查看rocketMq的控制台
查看消息细节
22.2 搭建rocketmq-consumer(消息消费者)
22.2.1 创建项目,完整的pom.xml
| <?_xml version="1.0" encoding="UTF-8"_?>
<project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
4.0.0
org.springframework.boot
spring-boot-starter-parent ** **
2.6.3