springboot 使用 rocketMQ

CyrusHuang發表於2024-09-05

POM 依賴

<dependency>
    <groupId>org.apache.rocketmq</groupId>
    <artifactId>rocketmq-spring-boot-starter</artifactId>
    <version>2.2.2</version>
</dependency>

配置檔案

rocketmq:
    name-server: 192.168.206.186:9876

更多的配置可以參考官網,官網寫的很詳細 https://rocketmq.apache.org/zh/docs/4.x/parameterConfiguration/01local#clientconfig%E9%85%8D%E7%BD%AE

生產者

不同的方法完成訊息的花式傳送

@Autowired
private RocketMQTemplate rocketMQTemplate;

@Test
void contextLoads() {
    
    // 同步
    rocketMQTemplate.syncSend("myTopic1", "我是boot的一個訊息");
    
    // 非同步
    rocketMQTemplate.asyncSend("myTopic2", "我是boot的一個非同步訊息", new SendCallback() {
        @Override
        public void onSuccess(SendResult sendResult) {
            System.out.println("成功");
        }

        @Override
        public void onException(Throwable throwable) {
            System.out.println("失敗" + throwable.getMessage());
        }
    });

    // 單向
    rocketMQTemplate.sendOneWay("myTopic3", "我是boot的一個單向訊息");

    // 延遲(延遲級別為3,超時時間為 3000 毫秒)
    Message<String> msg = MessageBuilder.withPayload("我是boot的一個延遲訊息").build();
    rocketMQTemplate.syncSend("myTopic4", msg, 3000, 3);

    // 順序訊息(消費者 需要單執行緒消費)
    List<MsgModel> msgModels = Arrays.asList(
            new MsgModel("qwer", 1, "下單"),
            new MsgModel("qwer", 1, "簡訊"),
            new MsgModel("qwer", 1, "物流"),
            new MsgModel("zxcv", 2, "下單"),
            new MsgModel("zxcv", 2, "簡訊"),
            new MsgModel("zxcv", 2, "物流")
    );
    msgModels.forEach(msgModel -> {
        // 傳送,以 msgModel.getOrderSn() 分組,每組訊息放到一個佇列裡(這裡是兩組訊息)
        rocketMQTemplate.syncSendOrderly("myTopic5", JSON.toJSONString(msgModel), msgModel.getOrderSn());
    });
    
    // 帶 tag 
    rocketMQTemplate.syncSend("myTopic6:tagA", "我是一個帶tag的訊息");
    
    // 帶 key
    Message<String> message = MessageBuilder
        	.withPayload("我是一個帶key的訊息")
            .setHeader(RocketMQHeaders.KEYS, "keyA")
            .build();
    rocketMQTemplate.syncSend("myTopic7", message);
}

消費者

@RocketMQMessageListener 註解不同的配置完成訊息的花式接收,舉幾個示例

普通訊息

@Component
@RocketMQMessageListener(topic = "myTopic1", 
                         consumerGroup = "my-consumer-group1"
)
public class MyRocketConsumerListener1 implements RocketMQListener<MessageExt> {
    /**
     * 這個方法就是消費者的方法
     * 如果泛型制定了固定的型別 那麼訊息體就是我們的引數
     * MessageExt 型別是訊息的所有內容
     * ------------------------
     * 沒有報錯 就簽收了,如果報錯了 就是拒收 就會重試
     * 
     */
    @Override
    public void onMessage(MessageExt message) {
        System.out.println(new String(message.getBody()));
    }
}

順序訊息

@Component
@RocketMQMessageListener(topic = "myTopic5",
        consumerGroup = "my-consumer-group2",
        consumeMode = ConsumeMode.ORDERLY, // 順序消費模式 單執行緒
        maxReconsumeTimes = 5 // 消費重試的次數
)
public class MyRocketConsumerListener2 implements RocketMQListener<MessageExt> {
    @Override
    public void onMessage(MessageExt message) {
        MsgModel msgModel = JSON.parseObject(new String(message.getBody()), MsgModel.class);
        System.out.println(msgModel);
    }
}

帶 tag

@Component
@RocketMQMessageListener(topic = "myTopic6",
        consumerGroup = "my-consumer-group3",
        selectorType = SelectorType.TAG,// tag過濾模式
        selectorExpression = "tagA || tagB"
)
public class MyRocketConsumerListener3 implements RocketMQListener<MessageExt> {

    @Override
    public void onMessage(MessageExt message) {
        System.out.println(new String(message.getBody()));
    }
}

廣播模式,兩個消費者同組,訂閱的也是同一個Topic,myTopic4 的訊息兩個消費者都會訊息

@Component
@RocketMQMessageListener(topic = "myTopic4",
        consumerGroup = "my-consumer-group4",
        messageModel = MessageModel.BROADCASTING // 廣播模式
)
public class MyRocketConsumerListener4 implements RocketMQListener<String> {
    @Override
    public void onMessage(String message) {
        System.out.println("my-consumer-group4消費者MyRocketConsumerListener4:" + message);
    }
}

@Component
@RocketMQMessageListener(topic = "myTopic4",
        consumerGroup = "my-consumer-group4",
        messageModel = MessageModel.BROADCASTING // 廣播模式
)
public class MyRocketConsumerListener5 implements RocketMQListener<String> {
    @Override
    public void onMessage(String message) {
        System.out.println("my-consumer-group4消費者MyRocketConsumerListener5:" + message);
    }
}

相關文章