Commit add90365df8d863bbbfe5fa0323032f5b0e4aa4d
1 parent
75bca650
【功能新增】IoT: 数据桥梁增加 RocketMQConfig 配置,实现executeRocketMQ 发送消息
Showing
5 changed files
with
116 additions
and
2 deletions
yudao-dependencies/pom.xml
| ... | ... | @@ -34,7 +34,7 @@ |
| 34 | 34 | <opengauss.jdbc.version>5.1.0</opengauss.jdbc.version> |
| 35 | 35 | <taos.version>3.3.3</taos.version> |
| 36 | 36 | <!-- 消息队列 --> |
| 37 | - <rocketmq-spring.version>2.3.1</rocketmq-spring.version> | |
| 37 | + <rocketmq-spring.version>2.3.2</rocketmq-spring.version> | |
| 38 | 38 | <!-- 服务保障相关 --> |
| 39 | 39 | <lock4j.version>2.2.7</lock4j.version> |
| 40 | 40 | <!-- 监控相关 --> | ... | ... |
yudao-framework/yudao-spring-boot-starter-mq/pom.xml
yudao-module-iot/yudao-module-iot-biz/pom.xml
| ... | ... | @@ -75,6 +75,11 @@ |
| 75 | 75 | <!-- <groupId>org.eclipse.paho</groupId> <!– MQTT –>--> |
| 76 | 76 | <!-- <artifactId>org.eclipse.paho.client.mqttv3</artifactId>--> |
| 77 | 77 | <!-- </dependency>--> |
| 78 | + <!-- 工具类相关 --> | |
| 79 | + <dependency> | |
| 80 | + <groupId>cn.iocoder.boot</groupId> | |
| 81 | + <artifactId>yudao-spring-boot-starter-mq</artifactId> | |
| 82 | + </dependency> | |
| 78 | 83 | |
| 79 | 84 | <dependency> |
| 80 | 85 | <groupId>org.pf4j</groupId> <!-- PF4J:内置插件机制 --> | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/dal/dataobject/rule/IotDataBridgeDO.java
| ... | ... | @@ -136,5 +136,42 @@ public class IotDataBridgeDO extends BaseDO { |
| 136 | 136 | |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | + /** | |
| 140 | + * RocketMQ 配置 | |
| 141 | + */ | |
| 142 | + @Data | |
| 143 | + public static class RocketMQConfig implements Config { | |
| 144 | + | |
| 145 | + /** | |
| 146 | + * RocketMQ 名称服务器地址 | |
| 147 | + */ | |
| 148 | + private String nameServer; | |
| 149 | + | |
| 150 | + /** | |
| 151 | + * 生产者/消费者组 | |
| 152 | + */ | |
| 153 | + private String group; | |
| 154 | + | |
| 155 | + /** | |
| 156 | + * 主题 | |
| 157 | + */ | |
| 158 | + private String topic; | |
| 159 | + | |
| 160 | + /** | |
| 161 | + * 标签 | |
| 162 | + */ | |
| 163 | + private String tags; | |
| 164 | + | |
| 165 | + /** | |
| 166 | + * 访问密钥 | |
| 167 | + */ | |
| 168 | + private String accessKey; | |
| 169 | + | |
| 170 | + /** | |
| 171 | + * 秘密钥匙 | |
| 172 | + */ | |
| 173 | + private String secretKey; | |
| 174 | + | |
| 175 | + } | |
| 139 | 176 | |
| 140 | 177 | } | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/IotRuleSceneDataBridgeAction.java
| ... | ... | @@ -13,11 +13,17 @@ import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage; |
| 13 | 13 | import cn.iocoder.yudao.module.iot.service.rule.IotDataBridgeService; |
| 14 | 14 | import jakarta.annotation.Resource; |
| 15 | 15 | import lombok.extern.slf4j.Slf4j; |
| 16 | +import org.apache.rocketmq.client.producer.DefaultMQProducer; | |
| 17 | +import org.apache.rocketmq.client.producer.SendResult; | |
| 18 | +import org.apache.rocketmq.client.producer.SendStatus; | |
| 19 | +import org.apache.rocketmq.common.message.Message; | |
| 20 | +import org.apache.rocketmq.remoting.common.RemotingHelper; | |
| 16 | 21 | import org.springframework.http.*; |
| 17 | 22 | import org.springframework.stereotype.Component; |
| 18 | 23 | import org.springframework.web.client.RestTemplate; |
| 19 | 24 | import org.springframework.web.util.UriComponentsBuilder; |
| 20 | 25 | |
| 26 | +import java.time.LocalDateTime; | |
| 21 | 27 | import java.util.HashMap; |
| 22 | 28 | import java.util.Map; |
| 23 | 29 | |
| ... | ... | @@ -64,6 +70,11 @@ public class IotRuleSceneDataBridgeAction implements IotRuleSceneAction { |
| 64 | 70 | executeHttp(message, (IotDataBridgeDO.HttpConfig) dataBridge.getConfig()); |
| 65 | 71 | return; |
| 66 | 72 | } |
| 73 | + // 2.2 执行 RocketMQ 发送消息 | |
| 74 | + if (IotDataBridgTypeEnum.ROCKETMQ.getType().equals(dataBridge.getType())) { | |
| 75 | + executeRocketMQ(message, (IotDataBridgeDO.RocketMQConfig) dataBridge.getConfig()); | |
| 76 | + return; | |
| 77 | + } | |
| 67 | 78 | |
| 68 | 79 | // TODO @芋艿:因为下面的,都是有状态的,所以通过 guava 缓存连接,然后通过 RemovalNotification 实现关闭。例如说,一次新建有效期是 10 分钟; |
| 69 | 80 | // TODO @芋艿:mq-redis |
| ... | ... | @@ -131,4 +142,66 @@ public class IotRuleSceneDataBridgeAction implements IotRuleSceneAction { |
| 131 | 142 | } |
| 132 | 143 | } |
| 133 | 144 | |
| 145 | + private void executeRocketMQ(IotDeviceMessage message, IotDataBridgeDO.RocketMQConfig config) { | |
| 146 | + // 1. 创建生产者实例,指定生产者组名 | |
| 147 | + DefaultMQProducer producer = new DefaultMQProducer(config.getGroup()); | |
| 148 | + try { | |
| 149 | + // 2. 设置 NameServer 地址 | |
| 150 | + producer.setNamesrvAddr(config.getNameServer()); | |
| 151 | + // 3. 启动生产者 | |
| 152 | + producer.start(); | |
| 153 | + // 4. 创建消息对象,指定Topic、Tag和消息体 | |
| 154 | + Message msg = new Message( | |
| 155 | + config.getTopic(), | |
| 156 | + config.getTags(), | |
| 157 | + message.toString().getBytes(RemotingHelper.DEFAULT_CHARSET) | |
| 158 | + ); | |
| 159 | + | |
| 160 | + // 5. 发送同步消息并处理结果 | |
| 161 | + SendResult sendResult = producer.send(msg); | |
| 162 | + // 6. 处理发送结果 | |
| 163 | + if (SendStatus.SEND_OK.equals(sendResult.getSendStatus())) { | |
| 164 | + log.info("[executeRocketMQ][message({}) config({}) 发送成功,结果({})]", | |
| 165 | + message, config, sendResult); | |
| 166 | + } else { | |
| 167 | + log.error("[executeRocketMQ][message({}) config({}) 发送失败,结果({})]", | |
| 168 | + message, config, sendResult); | |
| 169 | + } | |
| 170 | + } catch (Exception e) { | |
| 171 | + log.error("[executeRocketMQ][message({}) config({}) 发送异常]", | |
| 172 | + message, config, e); | |
| 173 | + } finally { | |
| 174 | + // 7. 关闭生产者 | |
| 175 | + producer.shutdown(); | |
| 176 | + } | |
| 177 | + } | |
| 178 | + | |
| 179 | + public static void main(String[] args) { | |
| 180 | + // 1. 创建 IotRuleSceneDataBridgeAction 实例 | |
| 181 | + IotRuleSceneDataBridgeAction action = new IotRuleSceneDataBridgeAction(); | |
| 182 | + | |
| 183 | + // 2. 创建测试消息 | |
| 184 | + IotDeviceMessage message = IotDeviceMessage.builder() | |
| 185 | + .requestId("TEST-001") | |
| 186 | + .productKey("testProduct") | |
| 187 | + .deviceName("testDevice") | |
| 188 | + .deviceKey("testDeviceKey") | |
| 189 | + .type("property") | |
| 190 | + .identifier("temperature") | |
| 191 | + .data("{\"value\": 60}") | |
| 192 | + .reportTime(LocalDateTime.now()) | |
| 193 | + .tenantId(1L) | |
| 194 | + .build(); | |
| 195 | + | |
| 196 | + // 3. 创建 RocketMQ 配置 | |
| 197 | + IotDataBridgeDO.RocketMQConfig config = new IotDataBridgeDO.RocketMQConfig(); | |
| 198 | + config.setNameServer("127.0.0.1:9876"); | |
| 199 | + config.setGroup("test-group"); | |
| 200 | + config.setTopic("test-topic"); | |
| 201 | + config.setTags("test-tag"); | |
| 202 | + | |
| 203 | + // 4. 执行测试 | |
| 204 | + action.executeRocketMQ(message, config); | |
| 205 | + } | |
| 206 | + | |
| 134 | 207 | } | ... | ... |