RocketMq-简单示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RocketMq-简单示例相关的知识,希望对你有一定的参考价值。
操作步骤
1.搭建rocketmq服务
2.生产者-微服务(发送-队列消息)
3.消费者-微服务(处理-队列消息)
1.搭建rocketmq服务
下载: https://www.apache.org/dyn/closer.cgi?path=rocketmq/4.5.1/rocketmq-all-4.5.1-bin-release.zip
# 解压至: /usr/local/rocketmq-all-4.5.1-bin-release
# 启动/关闭服务:
rocketmq
# 启动:
/usr/local/rocketmq-all-4.5.1-bin-release/bin/mqnamesrv -c /usr/local/rocketmq-all-4.5.1-bin-release/conf/namesrv.properties -n "192.168.1.180:6500" &
# 设置自定义端口: 6500
-n /usr/local/rocketmq-all-4.5.1-bin-release/conf/namesrv.properties
# 文件内容是:
listenPort=6600
# 关闭:
/usr/local/rocketmq-all-4.5.1-bin-release/bin/mqshutdown namesrv
mqbroker:
# 启动: autoCreateTopicEnable=true(允许微服务创建topic)
/usr/local/rocketmq-all-4.5.1-bin-release/bin/mqbroker -n "192.168.1.180:6500" autoCreateTopicEnable=true &
# 关闭:
/usr/local/rocketmq-all-4.5.1-bin-release/bin/mqshutdown broker
2.生产者-微服务(发送-队列消息)
# 消息体-对象:
@Data
@Builder
@ToString
public class MemberMoneyMessage {
int memberId;
String event;
double money;
}
# 发送消息:
@Autowired
RocketMQTemplate rocketMQTemplate;
....
rocketMQTemplate.convertAndSend(
"member-money-act",
MemberMoneyMessage.builder()
.event("pay-order")
.memberId(50)
.money(100)
.build()
);
....
配置rocket服务端:
rocketmq:
name-server: tsca.cc:6600
producer:
group: tsca-group
3.消费者-微服务(处理-队列消息)
# 监听处理:
@Service
@RocketMQMessageListener(consumerGroup = "tsca-group",topic = "member-money-act")
@Slf4j
public class MemberMoneyMqListener implements RocketMQListener<MemberMoneyMessage> {
@Override
public void onMessage(MemberMoneyMessage memberMoneyMessage) {
log.info("收到-用户余额变动");
log.info(JSON.toJSONString(memberMoneyMessage));
}
}
# 配置:
rocketmq:
name-server: tsca.cc:6600
producer:
group: tsca-group
以上是关于RocketMq-简单示例的主要内容,如果未能解决你的问题,请参考以下文章