Spring Boot集成RocketMQ
程序员文章站
2022-06-28 17:42:46
引入RocketMQ org.apache.rocketmq rocketmq-spring-boot-starter 2.0.3
引入 RocketMQ
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.0.3</version>
</dependency>
Producer实现
配置文件
rocketmq:
name-server: xxx
producer:
access-key: xxx
secret-key: xxx
group: xxx
topic:
xzh: xxx
tag:
xzh: xxx
发送实现
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* 消息发送
*
* @author: 向振华
* @date: 2020/11/30 10:28
*/
@Component
public class RocketMQProducer {
@Autowired
private RocketMQTemplate rocketMQTemplate;
@Value("${rocketmq.producer.topic.xzh}")
private String topic;
@Value("${rocketmq.producer.tag.xzh}")
private String tag;
public void send(String msg) {
if (StringUtils.isBlank(msg)) {
return;
}
rocketMQTemplate.syncSend(topic + ":" + tag, msg);
}
}
Consumer实现
配置文件
rocketmq:
name-server: xxx
consumer:
access-key: xxx
secret-key: xxx
group: xxx
topic:
xzh: xxx
tag:
xzh: xxx
接受实现
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;
/**
* 消息接收
*
* @author: 向振华
* @date: 2020/11/30 10:28
*/
@Slf4j
@Component
@RocketMQMessageListener(consumerGroup = "${rocketmq.consumer.group}", topic = "${rocketmq.consumer.topic.xzh}", selectorExpression = "${rocketmq.consumer.tag.xzh}")
public class RocketMQConsumerListener implements RocketMQListener<String> {
@Override
public void onMessage(String msg) {
log.info("接收到消息 -> " + msg);
}
}
本文地址:https://blog.csdn.net/Anenan/article/details/110379786
推荐阅读
-
Spring boot怎么整合Mybatis
-
spring boot application properties配置实例代码详解
-
详解spring boot starter redis配置文件
-
Spring Boot项目实战之拦截器与过滤器
-
spring boot tomcat jdbc pool的属性绑定
-
spring boot整合mybatis+mybatis-plus的示例代码
-
spring boot 使用Aop通知打印控制器请求报文和返回报文问题
-
spring boot 打jar包,获取resource路径下的文件
-
Spring+SpringMVC+MyBatis集成(SSM)
-
Spring Boot入门HelloWorld程序