In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to use RocketMQ in SpringBoot. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Next, let's simulate a scenario in which an order is successfully placed and a text message is sent to the user who placed the order. This is an example of how RocketMQ is used in SpringBoot.
1 order microservice send message 1.1 order microservice add rocketmq dependency org.apache.rocketmq rocketmq-spring-boot-starter 2.1.1 org.apache.rocketmq rocketmq-client 4.7.1 1.2 add configuration rocketmq: name-server: 192.168.232. 129group 9876 # address of rocketmq service producer: group: jack-producer-group # producer group name 1.3 write test code package cn.jack.controller Import cn.jack.domain.Order;import cn.jack.domain.Product;import cn.jack.service.OrderService;import cn.jack.service.ProductService;import com.alibaba.fastjson.JSON;import lombok.extern.slf4j.Slf4j;import org.apache.rocketmq.spring.core.RocketMQTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cloud.client.discovery.DiscoveryClient;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController Import org.springframework.web.client.RestTemplate; @ RestController@Slf4jpublic class OrderController {@ Autowired private RestTemplate restTemplate; @ Autowired private OrderService orderService; @ Autowired private DiscoveryClient discoveryClient; @ Autowired private ProductService productService; @ Autowired private RocketMQTemplate rocketMQTemplate / * place an order-call * @ param pid goods id * @ return * / @ RequestMapping ("/ order/prod/ {pid}") public Order order (@ PathVariable ("pid") Long pid) {log.info ("receive an order request, ready to query product information based on the Feign implementation service. Pid= {} ", pid); / / call the commodity micro-service through Feign to query the commodity information Product product = this.productService.findByPid (pid); log.info (" the commodity information query succeeded. The contents are as follows: {} ", JSON.toJSONString (product)); / / Fault tolerant judgment if (product.getPid () =-100) {Order order = new Order (); order.setOid (- 100L); order.setPname (" failed to place order "); return order } / / generate commodity information and save Order order = new Order (); order.setNumber (1); order.setPid (pid); order.setPname (product.getPname ()); order.setPprice (product.getPprice ()); order.setUid (1); order.setUsername ("Chen Jiabao"); this.orderService.createOrder (order) Log.info ("order information saved successfully. The content is: {} ", JSON.toJSONString (order)); / / after TODO successfully places the order, mq sends the order content message rocketMQTemplate.convertAndSend (" jack-topic ", order); return order;}} 1.4 test
Access the order issuing interface to confirm that RocketMQ sent a message. It is confirmed from the RocketMQ console that the message was sent successfully.
2 user micro service subscription message 2.1 user micro service increases rocketmq dependency com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery org.apache.rocketmq rocketmq-spring-boot-starter 2.1.1 org.apache.rocketmq Rocketmq-client 4.7.1 2.2 modify the main class Start nacos client package cn.jack Import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @ SpringBootApplication@EnableDiscoveryClientpublic class UserApplication {public static void main (String [] args) {SpringApplication.run (UserApplication.class);}} 2.3 modify the configuration file rocketmq: name-server: 192.168.232.129purl 9876 # rocketmq service address 2.4.Writing message receiving service package cn.jack.service; import cn.jack.domain.Order Import lombok.extern.slf4j.Slf4j;import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;import org.apache.rocketmq.spring.core.RocketMQListener;import org.springframework.stereotype.Service; @ Slf4j@Service@RocketMQMessageListener (consumerGroup = "jack-consumer-group", topic = "jack-topic") public class SmsService implements RocketMQListener {@ Override public void onMessage (Order order) {log.info ("receipt of order information, the content is as follows: {}, then send SMS", order) 2.5 Test
Start the user microservice and receive a message from RocketMQ.
2020-09-16 21 cn.jack.service.SmsService 46 INFO 30.465 [service-user,] 660-[MessageThread_2] cn.jack.service.SmsService: received the order message as follows: Order (oid=66, uid=1, username= Chen Jiabao, pid=2, pname= Sydney, pprice=2000.0, number=1), followed by SMS.
2020-09-16 21 cn.jack.service.SmsService 46 INFO [service-user,] 660-[MessageThread_1] cn.jack.service.SmsService: received the order message as follows: Order (oid=65, uid=1, username= Chen Jiabao, pid=3, pname= Huawei, pprice=8000.0, number=1), and then send a text message.
Successful consumption of messages:
Thank you for reading! This is the end of this article on "how to use RocketMQ in SpringBoot". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.