Commit 3266bf0f9829effe1eb0c7774ef2b0ff4265c7a2
1 parent
cd656fad
【代码优化】IoT: Redis Stream 数据桥梁执行器
Showing
5 changed files
with
19 additions
and
35 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/rule/vo/databridge/config/IotDataBridgeAbstractConfig.java
| ... | ... | @@ -18,7 +18,7 @@ import lombok.Data; |
| 18 | 18 | @JsonSubTypes({ |
| 19 | 19 | @JsonSubTypes.Type(value = IotDataBridgeHttpConfig.class, name = "1"), |
| 20 | 20 | @JsonSubTypes.Type(value = IotDataBridgeMqttConfig.class, name = "10"), |
| 21 | - @JsonSubTypes.Type(value = IotDataBridgeRedisStreamMQConfig.class, name = "21"), | |
| 21 | + @JsonSubTypes.Type(value = IotDataBridgeRedisStreamConfig.class, name = "21"), | |
| 22 | 22 | @JsonSubTypes.Type(value = IotDataBridgeRocketMQConfig.class, name = "30"), |
| 23 | 23 | @JsonSubTypes.Type(value = IotDataBridgeRabbitMQConfig.class, name = "31"), |
| 24 | 24 | @JsonSubTypes.Type(value = IotDataBridgeKafkaMQConfig.class, name = "32"), | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/rule/vo/databridge/config/IotDataBridgeRedisStreamMQConfig.java renamed to yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/rule/vo/databridge/config/IotDataBridgeRedisStreamConfig.java
| ... | ... | @@ -2,14 +2,13 @@ package cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config; |
| 2 | 2 | |
| 3 | 3 | import lombok.Data; |
| 4 | 4 | |
| 5 | -// TODO @puhui999:MQ 可以去掉哈。stream 更精准 | |
| 6 | 5 | /** |
| 7 | 6 | * IoT Redis Stream 配置 {@link IotDataBridgeAbstractConfig} 实现类 |
| 8 | 7 | * |
| 9 | 8 | * @author HUIHUI |
| 10 | 9 | */ |
| 11 | 10 | @Data |
| 12 | -public class IotDataBridgeRedisStreamMQConfig extends IotDataBridgeAbstractConfig { | |
| 11 | +public class IotDataBridgeRedisStreamConfig extends IotDataBridgeAbstractConfig { | |
| 13 | 12 | |
| 14 | 13 | /** |
| 15 | 14 | * Redis 服务器地址 | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/AbstractCacheableDataBridgeExecute.java
| ... | ... | @@ -101,7 +101,7 @@ public abstract class AbstractCacheableDataBridgeExecute<Config, Producer> imple |
| 101 | 101 | @Override |
| 102 | 102 | @SuppressWarnings({"unchecked"}) |
| 103 | 103 | public void execute(IotDeviceMessage message, IotDataBridgeDO dataBridge) { |
| 104 | - if (ObjUtil.notEqual(message.getType(), getType())) { | |
| 104 | + if (ObjUtil.notEqual(dataBridge.getType(), getType())) { | |
| 105 | 105 | return; |
| 106 | 106 | } |
| 107 | 107 | try { | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotRedisStreamMQDataBridgeExecute.java renamed to yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotRedisStreamDataBridgeExecute.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.service.rule.action.databridge; |
| 2 | 2 | |
| 3 | -import cn.hutool.core.util.ReflectUtil; | |
| 4 | 3 | import cn.hutool.core.util.StrUtil; |
| 5 | -import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeRedisStreamMQConfig; | |
| 4 | +import cn.iocoder.yudao.module.iot.controller.admin.rule.vo.databridge.config.IotDataBridgeRedisStreamConfig; | |
| 6 | 5 | import cn.iocoder.yudao.module.iot.enums.rule.IotDataBridgeTypeEnum; |
| 7 | 6 | import cn.iocoder.yudao.module.iot.mq.message.IotDeviceMessage; |
| 8 | -import com.fasterxml.jackson.databind.ObjectMapper; | |
| 9 | -import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | |
| 10 | 7 | import lombok.extern.slf4j.Slf4j; |
| 11 | 8 | import org.redisson.Redisson; |
| 12 | 9 | import org.redisson.api.RedissonClient; |
| ... | ... | @@ -21,14 +18,14 @@ import org.springframework.data.redis.serializer.RedisSerializer; |
| 21 | 18 | import org.springframework.stereotype.Component; |
| 22 | 19 | |
| 23 | 20 | /** |
| 24 | - * Redis Stream MQ 的 {@link IotDataBridgeExecute} 实现类 | |
| 21 | + * Redis Stream 的 {@link IotDataBridgeExecute} 实现类 | |
| 25 | 22 | * |
| 26 | 23 | * @author HUIHUI |
| 27 | 24 | */ |
| 28 | 25 | @Component |
| 29 | 26 | @Slf4j |
| 30 | -public class IotRedisStreamMQDataBridgeExecute extends | |
| 31 | - AbstractCacheableDataBridgeExecute<IotDataBridgeRedisStreamMQConfig, RedisTemplate<String, Object>> { | |
| 27 | +public class IotRedisStreamDataBridgeExecute extends | |
| 28 | + AbstractCacheableDataBridgeExecute<IotDataBridgeRedisStreamConfig, RedisTemplate<String, Object>> { | |
| 32 | 29 | |
| 33 | 30 | @Override |
| 34 | 31 | public Integer getType() { |
| ... | ... | @@ -36,7 +33,7 @@ public class IotRedisStreamMQDataBridgeExecute extends |
| 36 | 33 | } |
| 37 | 34 | |
| 38 | 35 | @Override |
| 39 | - public void execute0(IotDeviceMessage message, IotDataBridgeRedisStreamMQConfig config) throws Exception { | |
| 36 | + public void execute0(IotDeviceMessage message, IotDataBridgeRedisStreamConfig config) throws Exception { | |
| 40 | 37 | // 1. 获取 RedisTemplate |
| 41 | 38 | RedisTemplate<String, Object> redisTemplate = getProducer(config); |
| 42 | 39 | |
| ... | ... | @@ -48,7 +45,7 @@ public class IotRedisStreamMQDataBridgeExecute extends |
| 48 | 45 | } |
| 49 | 46 | |
| 50 | 47 | @Override |
| 51 | - protected RedisTemplate<String, Object> initProducer(IotDataBridgeRedisStreamMQConfig config) { | |
| 48 | + protected RedisTemplate<String, Object> initProducer(IotDataBridgeRedisStreamConfig config) { | |
| 52 | 49 | // 1.1 创建 Redisson 配置 |
| 53 | 50 | Config redissonConfig = new Config(); |
| 54 | 51 | SingleServerConfig serverConfig = redissonConfig.useSingleServer() |
| ... | ... | @@ -59,20 +56,17 @@ public class IotRedisStreamMQDataBridgeExecute extends |
| 59 | 56 | serverConfig.setPassword(config.getPassword()); |
| 60 | 57 | } |
| 61 | 58 | |
| 62 | - // TODO @huihui:看看能不能简化一些。按道理说,不用这么多的哈。 | |
| 63 | - // 2.1 创建 RedissonClient | |
| 59 | + // TODO @芋艿:看看怎么优化 | |
| 60 | + // 创建 RedisTemplate 并配置 | |
| 64 | 61 | RedissonClient redisson = Redisson.create(redissonConfig); |
| 65 | - // 2.2 创建并配置 RedisTemplate | |
| 66 | 62 | RedisTemplate<String, Object> template = new RedisTemplate<>(); |
| 67 | - // 设置 RedisConnection 工厂。😈 它就是实现多种 Java Redis 客户端接入的秘密工厂。感兴趣的胖友,可以自己去撸下。 | |
| 68 | 63 | template.setConnectionFactory(new RedissonConnectionFactory(redisson)); |
| 69 | - // 使用 String 序列化方式,序列化 KEY 。 | |
| 64 | + // 设置序列化器 | |
| 70 | 65 | template.setKeySerializer(RedisSerializer.string()); |
| 71 | 66 | template.setHashKeySerializer(RedisSerializer.string()); |
| 72 | - // 使用 JSON 序列化方式(库是 Jackson ),序列化 VALUE 。 | |
| 73 | - template.setValueSerializer(buildRedisSerializer()); | |
| 74 | - template.setHashValueSerializer(buildRedisSerializer()); | |
| 75 | - template.afterPropertiesSet();// 初始化 | |
| 67 | + template.setValueSerializer(RedisSerializer.json()); | |
| 68 | + template.setHashValueSerializer(RedisSerializer.json()); | |
| 69 | + template.afterPropertiesSet(); | |
| 76 | 70 | return template; |
| 77 | 71 | } |
| 78 | 72 | |
| ... | ... | @@ -84,13 +78,4 @@ public class IotRedisStreamMQDataBridgeExecute extends |
| 84 | 78 | } |
| 85 | 79 | } |
| 86 | 80 | |
| 87 | - // TODO @huihui:看看能不能简化一些。按道理说,不用这么多的哈。 | |
| 88 | - public static RedisSerializer<?> buildRedisSerializer() { | |
| 89 | - RedisSerializer<Object> json = RedisSerializer.json(); | |
| 90 | - // 解决 LocalDateTime 的序列化 | |
| 91 | - ObjectMapper objectMapper = (ObjectMapper) ReflectUtil.getFieldValue(json, "mapper"); | |
| 92 | - objectMapper.registerModules(new JavaTimeModule()); | |
| 93 | - return json; | |
| 94 | - } | |
| 95 | - | |
| 96 | 81 | } | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/test/java/cn/iocoder/yudao/module/iot/service/rule/action/databridge/IotDataBridgeExecuteTest.java
| ... | ... | @@ -92,12 +92,12 @@ public class IotDataBridgeExecuteTest extends BaseMockitoUnitTest { |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | @Test |
| 95 | - public void testRedisStreamMQDataBridge() throws Exception { | |
| 95 | + public void testRedisStreamDataBridge() throws Exception { | |
| 96 | 96 | // 1. 创建执行器实例 |
| 97 | - IotRedisStreamMQDataBridgeExecute action = new IotRedisStreamMQDataBridgeExecute(); | |
| 97 | + IotRedisStreamDataBridgeExecute action = new IotRedisStreamDataBridgeExecute(); | |
| 98 | 98 | |
| 99 | 99 | // 2. 创建配置 |
| 100 | - IotDataBridgeRedisMQConfig config = new IotDataBridgeRedisMQConfig() | |
| 100 | + IotDataBridgeRedisStreamConfig config = new IotDataBridgeRedisStreamConfig() | |
| 101 | 101 | .setHost("127.0.0.1") |
| 102 | 102 | .setPort(6379) |
| 103 | 103 | .setDatabase(0) |
| ... | ... | @@ -105,7 +105,7 @@ public class IotDataBridgeExecuteTest extends BaseMockitoUnitTest { |
| 105 | 105 | .setTopic("test-stream"); |
| 106 | 106 | |
| 107 | 107 | // 3. 执行测试并验证缓存 |
| 108 | - executeAndVerifyCache(action, config, "RedisStreamMQ"); | |
| 108 | + executeAndVerifyCache(action, config, "RedisStream"); | |
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | @Test | ... | ... |