您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關SpringBoot中如何使用RocketMQ的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
接下來我們模擬一種場景:商品下單成功之后,向下單用戶發送短信。以此來示例SpringBoot中RocketMQ的使用方式。
<!--rocketmq--> <dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-client</artifactId> <version>4.7.1</version> </dependency>
rocketmq: name-server: 192.168.232.129:9876 # rocketmq服務的地址 producer: group: jack-producer-group # 生產者組名
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 @Slf4j public class OrderController { @Autowired private RestTemplate restTemplate; @Autowired private OrderService orderService; @Autowired private DiscoveryClient discoveryClient; @Autowired private ProductService productService; @Autowired private RocketMQTemplate rocketMQTemplate; /** * 下單 -- 基于Feign實現服務調用 * @param pid 商品id * @return */ @RequestMapping("/order/prod/{pid}") public Order order(@PathVariable("pid") Long pid) { log.info("收到下單請求,準備查詢商品信息。pid={}", pid); // 通過Feign調用商品微服務,查詢商品信息 Product product = this.productService.findByPid(pid); log.info("商品信息查詢成功。內容為:{}", JSON.toJSONString(product)); // 進行容錯判斷 if (product.getPid() == -100) { Order order = new Order(); order.setOid(-100L); order.setPname("下單失敗"); return order; } // 生成商品信息保存 Order order = new Order(); order.setNumber(1); order.setPid(pid); order.setPname(product.getPname()); order.setPprice(product.getPprice()); order.setUid(1); order.setUsername("陳家寶"); this.orderService.createOrder(order); log.info("訂單信息保存成功。內容為:{}", JSON.toJSONString(order)); // TODO 下單成功之后,mq發送訂單內容消息 rocketMQTemplate.convertAndSend("jack-topic", order); return order; } }
訪問下單接口,確認RocketMQ是否發送了消息。從RocketMQ控制臺得到確認,消息發送成功。
<!--nacos客戶端--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--rocketmq--> <dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-client</artifactId> <version>4.7.1</version> </dependency>
package cn.jack; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class); } }
rocketmq: name-server: 192.168.232.129:9876 # rocketmq服務的地址
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<Order> { @Override public void onMessage(Order order) { log.info("收到訂單信息,內容如下:{}, 接下來進行短信發送", order); } }
啟動用戶微服務,收到了來自RocketMQ的消息。
2020-09-16 21:46:30.465 INFO [service-user,,,] 660 --- [MessageThread_2] cn.jack.service.SmsService : 收到訂單信息,內容如下:Order(oid=66, uid=1, username=陳家寶, pid=2, pname=雪梨, pprice=2000.0, number=1), 接下來進行短信發送
2020-09-16 21:46:30.465 INFO [service-user,,,] 660 --- [MessageThread_1] cn.jack.service.SmsService : 收到訂單信息,內容如下:Order(oid=65, uid=1, username=陳家寶, pid=3, pname=華為, pprice=8000.0, number=1), 接下來進行短信發送
消息成功消費:
感謝各位的閱讀!關于“SpringBoot中如何使用RocketMQ”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。