1、添加pom.xml依赖
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.2.3</version>
</dependency>
2、发送消息
@Slf4j
@RestController
public class TestController {
@Autowired
private RocketMQTemplate rocketMQTemplate;
/**
* 同步消息发送
*
* @param tag
* @param delayTime 延迟时间
* @param delayLevel 共18个级别:1s,5s,10s,30s,1m,2m,3m,4m,5m,6m,7m,8m,9m,10m,20m,30m,1h,2h
* @return
*/
@GetMapping("send")
public SendResult send(@RequestParam(defaultValue = "all") String tag,
@RequestParam(required = false) Long delayTime, @RequestParam(required = false) Integer delayLevel) {
String destination = RocketMQConstant.TOPIC + ":" + tag;
UserChange change = UserChange.builder().userName("张三").remark("密码变更").build();
Message<UserChange> message = MessageBuilder.withPayload(change).setHeader(RocketMQHeaders.KEYS, "key").build();
log.info("准备发送消息");
if (delayTime != null) { // 按时间延迟消息
// RocketMQ_4.9.x版本不支持延迟时间,指定时间无效
return rocketMQTemplate.syncSendDelayTimeMills(destination, message, 5000L);
} else if (delayLevel != null) { // 按级别延迟消息
return rocketMQTemplate.syncSend(destination, message, 5000L, delayLevel);
} else {
return rocketMQTemplate.syncSend(destination, message);
}
}
}
3、创建消息消费者
@Component
@Slf4j
public class MessageConsumerService {
@Component
@RocketMQMessageListener(topic = RocketMQConstant.TOPIC, consumerGroup = "consumer-group1", selectorExpression = "all||tag1")
public class Consumer1 implements RocketMQListener<MessageExt> {
@Override
public void onMessage(MessageExt message) {
log.info("收到信息:{},Keys={},Topic={}", new String(message.getBody()), message.getKeys(), message.getTopic());
}
}
@Component
@RocketMQMessageListener(topic = RocketMQConstant.TOPIC, consumerGroup = "consumer-group2", selectorExpression = "all||tag2")
public class Consumer2 implements RocketMQListener<String> {
@Override
public void onMessage(String message) {
log.info("收到信息:{}", message);
}
}
@Component
@RocketMQMessageListener(topic = RocketMQConstant.TOPIC, consumerGroup = "consumer-group3", selectorExpression = "all||tag3")
public class Consumer3 implements RocketMQListener<UserChange> {
@Override
public void onMessage(UserChange message) {
log.info("收到信息:{}", JSON.toJSONString(message));
}
}
}
Consumer1能收到tag为all或tag1的消息;Consumer2能收到tag为all或tag2的消息;Consumer3能收到tag为all或tag3的消息;
4、日志输出
2023-05-09 18:52:12.482 [ConsumeMessageThread_consumer-group1_1] INFO com.zhi.demo.consumer.MessageConsumerService$Consumer1.onMessage 30 -- 收到信息:{"userName":"张三","remark":"密码变更"},Keys=key,Topic=my-topic
2023-05-09 18:52:13.107 [ConsumeMessageThread_consumer-group2_1] INFO com.zhi.demo.consumer.MessageConsumerService$Consumer2.onMessage 39 -- 收到信息:{"userName":"张三","remark":"密码变更"}
2023-05-09 18:52:13.789 [ConsumeMessageThread_consumer-group3_1] INFO com.zhi.demo.consumer.MessageConsumerService$Consumer3.onMessage 48 -- 收到信息:{"remark":"密码变更","userName":"张三"}
5、调整日志输出:
RocketMQ消费者注册信息默认输出到~/logs\rocketmqlogs/rocketmq_client.log(Windows在用户目录下)
1)启动类添加参数
System.setProperty(ClientLogger.CLIENT_LOG_USESLF4J, "true");
详细过程可查看InternalLoggerFactory.getLoggerFactory
2)修改日志级别(不加的话日志会刷屏)
logging.level.RocketmqRemoting=warn
logging.level.RocketmqClient=warn
logging.level.RocketmqCommon=warn
logging.level.RocketmqStore=warn
这里我有话要说下,开始的时候不知道RocketMQ的消费者日志在另外一个文件,搞的我配置信息出错了都不知道,最后研究了半天源码才知道问题所在,浪费我2天时间。天坑!
常见错误:
1、No topic route info in name server for the topic: my-topic
my-topic不存在,要么在控制台手工创建,要么broker配置文件添加参数autoCreateTopicEnable=true
2、connect to 172.17.183.41:10911 failed
防火墙需要开启10911端口
firewall-cmd --zone=public --add-port=10911/tcp --permanent
firewall-cmd --reload
3、sendDefaultImpl call timeout
消息发送超时,可调整rocketmq.producer.send-message-timeout参数,默认3秒