您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。