这篇文章主要为大家展示了“RocketMq的用法示例”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“RocketMq的用法示例”这篇文章吧。
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.消费者-微服务(处理-队列消息)
# 监听处理(发送方的member-money-act和接收方的member-money-act一样便能接收): @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
4.搭建: rocketmq-控制台
# 1.下载源码 git clone https://github.com/apache/rocketmq-externals.git # 2.用IDEA打开【rocketmq-console】项目,修改pom文件 rocketmq版本 (2-1).更改pom文件的rocketmq版本: <rocketmq.version>4.4.0</rocketmq.version> # 改为: <rocketmq.version>4.5.1</rocketmq.version> (2-2).更改代码: org/apache/rocketmq/console/service/impl/MessageServiceImpl.java # 92,93 rows DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(MixAll.TOOLS_CONSUMER_GROUP, null); 改为: RPCHook rpcHook = null; DefaultMQPullConsumer consumer = new DefaultMQPullConsumer(MixAll.TOOLS_CONSUMER_GROUP, rpcHook); # 3.更改配置 # 当前网页控制台端口 server.port=6650 # rocketmq服务端口 rocketmq.config.namesrvAddr=192.168.1.180:6600 # 4.编译打包 mvn clean package -DskipTests # 5.运行 java -jar -Dserver.port=6650 -Drocketmq.config.namesrvAddr=192.168.1.180:6600 rocketmq-console-ng-1.0.1.jar
以上是“RocketMq的用法示例”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。