# springBoot集成RocketMq

springBoot集成RocketMq

1.下载安装相关软件
# springBoot集成RocketMq_第1张图片
2.添加相关依赖(要与jar匹配)

org.apache.rocketmq
rocketmq-spring-boot-starter
2.1.0

3.yml相关配置

RocketMQ 的配置, 这是最低配置

rocketmq:
name-server: 127.0.0.1:9876
producer:
# 发送同一类消息的设置为同一个 group, 保证唯一
group: imooc-study-ecommerce
4.相关代码实现(注意消费者任选其一即可,按照自己的需求来)
(一)生产者接受信息
**

  • 通过 RocketMQ 发送消息

  • Spring Messaging 模块

  • */
    @Slf4j
    @Component
    public class RocketMQProducer {

    /** 类似 Kafka 中的 topic, 默认的读写队列都是4个 */
    private static final String TOPIC = “imooc-study-rocketmq”;

    /** RocketMQ 客户端 */
    private final RocketMQTemplate rocketMQTemplate;

    public RocketMQProducer(RocketMQTemplate rocketMQTemplate) {
    this.rocketMQTemplate = rocketMQTemplate;
    }

    /**

    • 使用同步的方式发送消息, 不指定 key 和 tag

    • */
      public void sendMessageWithValue(String value) {

      // 随机选择一个 Topic 的 Message Queue 发送消息
      SendResult sendResult = rocketMQTemplate.syncSend(TOPIC, value);
      log.info(“sendMessageWithValue result: [{}]”, JSON.toJSONString(sendResult));

      //hashKey 区块分区
      SendResult sendResultOrderly = rocketMQTemplate.syncSendOrderly(
      TOPIC, value, “Qinyi”
      );
      log.info(“sendMessageWithValue orderly result: [{}]”,
      JSON.toJSONString(sendResultOrderly));
      }

    /** 高并发使用,不需要等待

    • 使用异步的方式发送消息, 指定 key

    • */
      public void sendMessageWithKey(String key, String value) {
      //指定k
      Message message = MessageBuilder.withPayload(value)
      .setHeader(RocketMQHeaders.KEYS, key).build();

      // 异步发送消息, 并设定回调
      rocketMQTemplate.asyncSend(TOPIC, message, new SendCallback() {

       @Override
       public void onSuccess(SendResult sendResult) {
           log.info("sendMessageWithKey success result: [{}]",
                   JSON.toJSONString(sendResult));
       }
      
       @Override
       public void onException(Throwable e) {
           log.error("sendMessageWithKey failure: [{}]", e.getMessage(), e);
       }
      

      });
      }

    /**

    • tag 保持消息一致,不同消费组,tag可以不一样

    • 使用同步的方式发送消息, 带有 tag, 且发送的是 Java Pojo(java对象)

    • */
      public void sendMessageWithTag(String tag, String value) {

      QinyiMessage qinyiMessage = JSON.parseObject(value, QinyiMessage.class);
      SendResult sendResult = rocketMQTemplate.syncSend(
      String.format(“%s:%s”, TOPIC, tag),
      qinyiMessage
      );
      log.info(“sendMessageWithTag result: [{}]”, JSON.toJSONString(sendResult));
      }

    /**

    • 使用同步的方式发送消息, 带有 key 和 tag

    • */
      public void sendMessageWithAll(String key, String tag, String value) {

      Message message = MessageBuilder.withPayload(value)
      .setHeader(RocketMQHeaders.KEYS, key).build();
      SendResult sendResult = rocketMQTemplate.syncSend(
      String.format(“%s:%s”, TOPIC, tag),
      message
      );
      log.info(“sendMessageWithAll result: [{}]”, JSON.toJSONString(sendResult));
      }
      }
      (二)value 为String类型的消费者
      /**

  • 第一个 RocketMQ 消费者

  • */
    @Slf4j
    @Component
    @RocketMQMessageListener(
    topic = “imooc-study-rocketmq”,
    consumerGroup = “qinyi-springboot-rocketmq-string”
    )
    public class RocketMQConsumerString implements RocketMQListener {

    @Override
    public void onMessage(String message) {

     QinyiMessage rocketMessage = JSON.parseObject(message, QinyiMessage.class);
     log.info("consume message in RocketMQConsumerString: [{}]",
             JSON.toJSONString(rocketMessage));
    

    }
    }
    (三)value 为QinyiMessage对象的消费者
    /**

  • 第四个, RocketMQ 消费者, 指定消费带有 tag 的消息, 且消费的是 Java Pojo

  • */
    @Slf4j
    @Component
    @RocketMQMessageListener(
    topic = “imooc-study-rocketmq”,
    consumerGroup = “qinyi-springboot-rocketmq-tag-object”,
    selectorExpression = “qinyi” // 根据 tag 做过滤
    )
    public class RocketMQConsumerObject implements RocketMQListener {

    @Override
    public void onMessage(QinyiMessage message) {

     log.info("consume message in RocketMQConsumerObject: [{}]",
             JSON.toJSONString(message));
     // so something
    

    }
    }
    (四)指定带有tag(过滤)的消费者
    /**

  • 第二个 RocketMQ 消费者, 指定了消费带有 tag 的消息

  • */
    @Slf4j
    @Component
    @RocketMQMessageListener(
    topic = “imooc-study-rocketmq”,
    consumerGroup = “qinyi-springboot-rocketmq-tag-string”,
    selectorExpression = “qinyi” // 根据 tag 过滤
    )
    public class RocketMQConsumerTagString implements RocketMQListener {

    @Override
    public void onMessage(String message) {

     QinyiMessage rocketMessage = JSON.parseObject(message, QinyiMessage.class);
     log.info("consume message in RocketMQConsumerTagString: [{}]",
             JSON.toJSONString(rocketMessage));
    

    }
    }
    5.访问层调用
    /**

  • SpringBoot 集成 RocketMQ

  • */
    @Slf4j
    @RestController
    @RequestMapping(“/rocket-mq”)
    public class RocketMQController {

    private static final QinyiMessage RocketMQMessage = new QinyiMessage(
    1,
    “Qinyi-Study-RocketMQ-In-SpringBoot”
    );

    private final RocketMQProducer rocketMQProducer;

    public RocketMQController(RocketMQProducer rocketMQProducer) {
    this.rocketMQProducer = rocketMQProducer;
    }

    @GetMapping(“/message-with-value”)
    public void sendMessageWithValue() {
    rocketMQProducer.sendMessageWithValue(JSON.toJSONString(RocketMQMessage));
    }

    @GetMapping(“/message-with-key”)
    public void sendMessageWithKey() {
    rocketMQProducer.sendMessageWithKey(“Qinyi”, JSON.toJSONString(RocketMQMessage));
    }

    @GetMapping(“/message-with-tag”)
    public void sendMessageWithTag() {
    rocketMQProducer.sendMessageWithTag(“qinyi”,
    JSON.toJSONString(RocketMQMessage));
    }

    @GetMapping(“/message-with-all”)
    public void sendMessageWithAll() {
    rocketMQProducer.sendMessageWithAll(“Qinyi”, “qinyi”,
    JSON.toJSONString(RocketMQMessage));
    }
    }
    4.http测试案例

message-with-value

GET http://127.0.0.1:8001/imooc-study-ecommerce-dev/rocket-mq/message-with-value
Content-Type: application/json

message-with-key

GET http://127.0.0.1:8001/imooc-study-ecommerce-dev/rocket-mq/message-with-key
Content-Type: application/json

message-with-tag

GET http://127.0.0.1:8001/imooc-study-ecommerce-dev/rocket-mq/message-with-tag
Content-Type: application/json

message-with-all

GET http://127.0.0.1:8001/imooc-study-ecommerce-dev/rocket-mq/message-with-all
Content-Type: application/json

你可能感兴趣的:(java,spring,boot,中间件)