阿里云RocketMQ消费MQTT消息

业务背景:

项目中涉及的消息队列既有RocketMQ,又有MQTT,均为阿里云提供(阿里云有专门的"微消息队列 MQTT 版"模块,但博主公司消息队列的实例都在"消息队列 RocketMQ 版"模块下,只是实例不同,猜测是做了适配,有清楚的大佬欢迎指点)。其中MQTT的消息是由硬件设备上报而来,由java服务进行消费,使用一套内部框架连接。现因框架存在适配问题,经讨论决定放弃使用,需改造原消费代码。

技术方案:

查询资料与询问同事得知两种消息队列在底层逻辑上高度相似,可以用RocketMQ方式连接MQTT的topic并消费。在实际改造过程中发现二者均可通过com.aliyun.openservices.ons.api.ONSFactory#createConsumer这一阿里云官方接口进行连接。而项目中已有一套配置用于连接RocketMQ,两类消息队列的地址、密钥等实例信息并不相同。因此改造的重点其实就是实现RocketMQ多实例配置。

代码实现:

java 复制代码
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 自定义注解
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface OnsMqttMessageListener {

    String topic();

    String consumerGroup();

}
java 复制代码
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * 参数类
 */
@Data
@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "aliyun.rocketmq")
public class OnsMqttMessageProperties {

    private String onsAddr;

    private String accessKey;

    private String secretKey;

    private String groupId;

}
java 复制代码
import com.aliyun.openservices.ons.api.Consumer;
import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.geelytech.bms.annotation.OnsMqttMessageListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Properties;

/**
 * 配置类
 */
@Slf4j
@Component
public class OnsMqttMessageConsumerConfig implements ApplicationListener<ApplicationReadyEvent> {

    @Autowired
    private OnsMqttMessageProperties onsMqttMessageProperties;

    /**
     * 注入所有MessageListener实例
     */
    @Autowired
    private Map<String, MessageListener> messageListeners;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        String onsAddr = onsMqttMessageProperties.getOnsAddr();
        String accessKey = onsMqttMessageProperties.getAccessKey();
        String secretKey = onsMqttMessageProperties.getSecretKey();
        log.info("ConsumerSubscriptionConfig onApplicationEvent messageListeners={}", messageListeners);

        // 订阅每个MessageListener
        for (Map.Entry<String, MessageListener> entry : messageListeners.entrySet()) {
            MessageListener listener = entry.getValue();

            OnsMqttMessageListener annotation = AnnotationUtils.findAnnotation(listener.getClass(), OnsMqttMessageListener.class);
            if (annotation != null) {
                String topic = annotation.topic();
                String consumerGroup = annotation.consumerGroup();

                Properties properties = new Properties();
                properties.setProperty(PropertyKeyConst.GROUP_ID, consumerGroup);
                properties.setProperty(PropertyKeyConst.NAMESRV_ADDR, onsAddr);
                properties.setProperty(PropertyKeyConst.AccessKey, accessKey);
                properties.setProperty(PropertyKeyConst.SecretKey, secretKey);

                Consumer consumer = ONSFactory.createConsumer(properties);
                consumer.subscribe(topic, "*", listener);
                consumer.start();
            }
        }
    }

}
java 复制代码
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.geelytech.bms.annotation.OnsMqttMessageListener;
import com.geelytech.satellite.constant.TopicConstant;
import com.geelytech.satellite.dto.eventOriginalData.BizSwapDone;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import static com.aliyun.openservices.ons.api.Action.CommitMessage;

@Slf4j
@Component
@OnsMqttMessageListener(topic = "", consumerGroup = "")
public class BmsBizSwapDoneConsumer implements MessageListener {

    @Override
    public Action consume(Message message, ConsumeContext consumeContext) {
        // 业务逻辑
        return CommitMessage;
    }

}
相关推荐
棒棒的唐10 小时前
yii2集成阿里云短信服务SDK
阿里云·阿里短信
Henry-SAP12 小时前
阿里云大模型份额居首
阿里云·云计算
jonyleek1 天前
物联网‘假离线’实战排查指南:基于JVS-IOT的数字孪生状态同步与MQTT心跳防抖优化
mqtt·数字孪生·状态同步·心跳保活·jvs-iot·物联网运维·设备影子
yyuuuzz1 天前
云服务器安全组规则优先级的机制记录
运维·服务器·网络·安全·阿里云·aws
云-缩影2 天前
我用阿里云 AI 动画方案跑通了剧本→视频流水线:ComfyUI + 百炼模型实测
人工智能·阿里云·云计算
硅基造物主2 天前
STM32 从 0 到 1 做智能家居(十二):USART2 分时复用 —— ESP-12F + MQTT
网络·stm32·mqtt·智能家居·esp-12f
程序员梅雨3 天前
【从零开始学习数据仓库 | 第一篇】:维度建模理论
大数据·数据仓库·学习·阿里云·数据分析