您好,登录后才能下订单哦!
这篇文章给大家分享的是有关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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。