Springboot 快速集成RocketMq


Springboot 快速集成RocketMq

1. 增加pom文件

<dependency>
 <groupId>org.hongxi</groupId>
 <artifactId>rocketmq-spring-boot-starter</artifactId>
</dependency>

2. 配置

rocketmq:
name-server: 192.168.0.84:9876
producer:
 group: test-group
sendMsgTimeoutMillis: 3000
reconsumeTimes: 3

3. 登錄rocketmq管理后台添加topic(operation-topic)

4. 生產者使用

@Autowired
RocketMQTemplate rocketMQTemplate;

@Test
public void sendHelloWorld() {
    SendResult result = rocketMQTemplate.syncSend("operation-topic", "hello world");
    log.info("發送結果:{}", JSON.toJSONString(result));
}

5. 消費者使用

@Component
@RocketMQMessageListener(topic = "operation-topic", //topic主題
        consumerGroup = "consumer-group",          //消費組
        messageModel = MessageModel.CLUSTERING,
        consumeMode = ConsumeMode.ORDERLY)
@Slf4j
public class MqTestListener implements RocketMQListener<String> {

    @Override
    public void onMessage(String message) {
        log.info("接受到消息:{}", message);
    }
}

6. 注意事項

如何使用tag

/**
  * Same to {@link #syncSend(String, Message)}.
  *
  * @param destination formats: `topicName:tags`
  * @param payload     the Object to use as payload
  * @return {@link SendResult}
  */
public SendResult syncSend(String destination, Object payload) {
    return syncSend(destination, payload, producer.getSendMsgTimeout());
}

這里可以看到 destination = topicName:tags

消費模式

  • 廣播模式 MessageModel.BROADCASTING 所有消費者都會收到消息
  • 集群模式 MessageModel.CLUSTERING 只有一個消費者會消費,類似於負載均衡

消費者如果存在一個為廣播模式,消費模式都會廣播模式


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM