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