Examples of the usage of RocketMq
This article mainly shows you "examples of the usage of RocketMq", which is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn the article "examples of the usage of RocketMq".
1. Set up rocketmq service
Download: https://www.apache.org/dyn/closer.cgi?path=rocketmq/4.5.1/rocketmq-all-4.5.1-bin-release.zip
# extract to: / usr/local/rocketmq-all-4.5.1-bin-release# Startup / shutdown Service: rocketmq # Startup: / 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 usr/local/rocketmq-all-4.5.1-bin-release/conf/namesrv.properties 6500" & # set custom port: 6500-n / usr/local/rocketmq-all-4.5.1-bin-release/conf/namesrv.properties # file content is: listenPort=6600 # close: / usr/local/rocketmq-all-4.5.1-bin-release/bin/mqshutdown namesrv mqbroker: # start: AutoCreateTopicEnable=true (allows micro services to create topic) / usr/local/rocketmq-all-4.5.1-bin-release/bin/mqbroker-n "192.168.1.180 usr/local/rocketmq-all-4.5.1-bin-release/bin/mqbroker 6500" autoCreateTopicEnable=true & # close: / usr/local/rocketmq-all-4.5.1-bin-release/bin/mqshutdown broker
two。 Producer-microservice (send-queue messages)
# message body-object: @ Data@Builder@ToStringpublic class MemberMoneyMessage {int memberId; String event; double money;} # send message: @ AutowiredRocketMQTemplate rocketMQTemplate .. rocketMQTemplate.convertAndSend ("member-money-act", MemberMoneyMessage.builder () .event ("pay-order") .memberId (50) .money (100) .build ();. Configure rocket server: rocketmq:name-server: tsca.cc:6600producer: group: tsca-group
3. Consumer-microservice (processing-queuing messages)
# snooping processing (the sender's member-money-act is the same as the receiver's member-money-act): @ Service@RocketMQMessageListener (consumerGroup = "tsca-group", topic = "member-money-act") @ Slf4jpublic class MemberMoneyMqListener implements RocketMQListener {@ Override public void onMessage (MemberMoneyMessage memberMoneyMessage) {log.info ("received-user balance change"); log.info (JSON.toJSONString (memberMoneyMessage)) }} # configuration: rocketmq: name-server: tsca.cc:6600 producer: group: tsca-group
4. Build: rocketmq- console
# 1. Download the source code git clone https://github.com/apache/rocketmq-externals.git# 2. Open the rocketmq-console project with IDEA and modify the rocketmq version of the pom file (2-1). Change the rocketmq version of the pom file: 4.4.15 to 4.5.1 (2-2). Change the code: org/apache/rocketmq/console/service/impl/MessageServiceImpl.java# 92 rowsDefaultMQPullConsumer consumer = new DefaultMQPullConsumer (MixAll.TOOLS_CONSUMER_GROUP, null); change to: RPCHook rpcHook = null;DefaultMQPullConsumer consumer = new DefaultMQPullConsumer (MixAll.TOOLS_CONSUMER_GROUP, rpcHook); # 3. Change configuration # current webpage console port server.port=6650# rocketmq service port rocketmq.config.namesrvAddr=192.168.1.180:6600# 4. Compile and package mvn clean package-DskipTests# 5. Run java-jar-Dserver.port=6650-Drocketmq.config.namesrvAddr=192.168.1.180:6600 rocketmq-console-ng-1.0.1.jar above is all the content of this article "examples of the usage of RocketMq". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!