springboot集成rocketmq,实现普通消息、顺序消息、延迟消息、事物消息

安装:

RocketMQ详细安装教程(RocketMQ安装、RocketMQ修改默认端口、console管理后台安装)_Xd聊架构的博客-CSDN博客

1.broker.conf配置

brokerClusterName = DefaultCluster
brokerName = broker-a
brokerId = 0
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH
namesrvAddr=111.111.111.111:9876
brokerIP1=111.111.111.111
messageDelayLevel=1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h 1d 2d
autoCreateTopicEnable=true
autoCreateSubscriptionGroup=true

2.启动

nohup sh bin/mqnamesrv &
nohup sh bin/mqbroker -n 111.111.111.111:9876 -c ./conf/broker.conf &

3.引入依赖


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

4.配置

rocketmq:
  name-server: 111.111.111.111:9876
  producer:
    group: producer_group # 必须指定group
    send-message-timeout: 3000 # 消息发送超时时长,默认3s
    retry-times-when-send-failed: 3 # 同步发送消息失败重试次数,默认2
    retry-times-when-send-async-failed: 3 # 异步发送消息失败重试次数,默认2

5普通消息-生产者

异步消息一样的,只是多一个collback函数接收结果

@Slf4j
@Component
public class RocketmqSendProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    public void sendMsg(OrderTimeOutMessage orderTimeOutMessage) {
        Message message = MessageBuilder.withPayload(orderTimeOutMessage).build();
        SendResult sendResult = rocketMQTemplate.syncSend("topic_temp", message);
        log.info("【发送同步消息】sendResult={}", JSON.toJSONString(sendResult));
    }

    public void sendTagMsg(OrderTimeOutMessage orderTimeOutMessage) {
        Message message = MessageBuilder.withPayload(orderTimeOutMessage).build();
        SendResult sendResult =  rocketMQTemplate.syncSend("topic_temp" + ":tag1", message);
        log.info("【发送同步标签消息】sendResult={}", JSON.toJSONString(sendResult));
    }

    public void sendTagMsgs(OrderTimeOutMessage orderTimeOutMessage) {
        List list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Message message = MessageBuilder.withPayload(orderTimeOutMessage).build();
            list.add(message);
        }
        SendResult sendResult =  rocketMQTemplate.syncSend("topic_temp" + ":tag1", list);
        log.info("【发送同步批量标签消息】sendResult={}", JSON.toJSONString(sendResult));
    }


    public void sendDelayMsg(OrderTimeOutMessage orderTimeOutMessage) {
        //1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
        Message message = MessageBuilder.withPayload(orderTimeOutMessage).build();
        SendResult sendResult =  rocketMQTemplate.syncSend("topic_temp", message, 3000 , 3);
        log.info("【发送延迟消息】sendResult={}", JSON.toJSONString(sendResult));
    }


    public void sendOrdelyMessage(OrderTimeOutMessage orderTimeOutMessage){
        String uuid = UUID.randomUUID().toString();
        for (int i = 0; i < 10; i++) {
            Message message = MessageBuilder.withPayload(orderTimeOutMessage).setHeader(RocketMQHeaders.KEYS, uuid).build();
            SendResult sendResult = rocketMQTemplate.syncSendOrderly(&

你可能感兴趣的:(MQ,java,开发语言,rocketmq)