亲宝软件园·资讯

展开

SpringBoot使用RocketMQ SpringBoot中使用RocketMQ的代码实例

陈家宝 人气:1
想了解SpringBoot中使用RocketMQ的代码实例的相关内容吗,陈家宝在本文为您仔细讲解SpringBoot使用RocketMQ的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:SpringBoot使用RocketMQ,SpringBoot,RocketMQ使用,下面大家一起来学习吧。

接下来我们模拟一种场景:商品下单成功之后,向下单用户发送短信。以此来示例SpringBoot中RocketMQ的使用方式。

1 订单微服务发送消息

1.1 订单微服务添加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>

1.2 添加配置

rocketmq:
  name-server: 192.168.232.129:9876 # rocketmq服务的地址
  producer:
    group: jack-producer-group  # 生产者组名

1.3 编写测试代码

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;
    }
}

1.4 测试

访问下单接口,确认RocketMQ是否发送了消息。从RocketMQ控制台得到确认,消息发送成功。

2 用户微服务订阅消息

2.1 用户微服务增加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>

2.2 修改主类,启动nacos客户端

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);
    }
}

2.3 修改配置文件

rocketmq:
  name-server: 192.168.232.129:9876 # rocketmq服务的地址

2.4 编写消息接收服务

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);
    }
}

2.5 测试

启动用户微服务,收到了来自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), 接下来进行短信发送

消息成功消费:

加载全部内容

相关教程
猜你喜欢
用户评论