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"](https://maven.apache.org/xsd/maven-4.0.0.xsd%22)
>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/>
_<!-- lookup parent from repository --> _
</parent>
<groupId>com.powernode</groupId>
<artifactId>01-rocketmq-producer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rocketmq-producer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>** **
**_<!-- rocketmq的依赖 --> _**
**<dependency> **
**<groupId>org.apache.rocketmq</groupId> **
**<artifactId>rocketmq-spring-boot-starter</artifactId> <version>2.0.2</version> **
**</dependency> **
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
22.1.2 修改配置文件application.yml
spring:
application:
name: rocketmq-producer
rocketmq:
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());
}
运行后查看控制台
文章来源:https://www.toymoban.com/news/detail-441686.html
22.1.4 查看rocketMq的控制台
查看消息细节
文章来源地址https://www.toymoban.com/news/detail-441686.html
到了这里,关于动力节点RocketMQ笔记第三章RocketMQ集成SpringBoot的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!