springboot整合rocketmq5.0
时间: 2023-12-07 22:38:58 浏览: 190
以下是SpringBoot整合RocketMQ 5.0的步骤:
1.添加RocketMQ依赖
在pom.xml文件中添加以下依赖:
```xml
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.0.3</version>
</dependency>
```
2.配置RocketMQ
在application.properties文件中添加以下配置:
```properties
# NameServer地址
rocketmq.name-server=127.0.0.1:9876
# 生产者组名
rocketmq.producer.group=producer_group
# 消费者组名
rocketmq.consumer.group=consumer_group
# 消费者最小线程数
rocketmq.consumer.consume-thread-min=20
# 消费者最大线程数
rocketmq.consumer.consume-thread-max=64
```
3.创建生产者
```java
@Service
public class RocketMQProducer {
@Autowired
private RocketMQTemplate rocketMQTemplate;
public void sendMessage(String topic, String message) {
rocketMQTemplate.convertAndSend(topic, message);
}
}
```
4.创建消费者
```java
@Service
@RocketMQMessageListener(topic = "test_topic", consumerGroup = "consumer_group")
public class RocketMQConsumer implements RocketMQListener<String> {
@Override
public void onMessage(String message) {
System.out.println("接收到消息:" + message);
}
}
```
5.发送消息
```java
@Autowired
private RocketMQProducer rocketMQProducer;
rocketMQProducer.sendMessage("test_topic", "Hello RocketMQ!");
```
以上就是SpringBoot整合RocketMQ 5.0的基本步骤。
阅读全文