Commit 6d3d0973837fc8602c3211f61f39857d9265b913
1 parent
93dd97af
feat: 增加 IoT Kafka 消息总线实现
- 新增 IotKafkaMessageBus,支持 IoT gateway 与 yudao-server 通过 Kafka 通信 - 增加 type=kafka 自动配置,并补充消息总线类型说明 - 为 iot-gateway 补充 spring-kafka 依赖和 Kafka 连接配置 - 将 iot-gateway 与 yudao-server 的 IoT message-bus 默认切换为 kafka
Showing
4 changed files
with
28 additions
and
3 deletions
yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/messagebus/config/IotMessageBusAutoConfiguration.java
| ... | ... | @@ -6,6 +6,7 @@ import cn.iocoder.yudao.framework.mq.redis.core.job.RedisStreamMessageCleanupJob |
| 6 | 6 | import cn.iocoder.yudao.framework.mq.redis.core.stream.AbstractRedisStreamMessage; |
| 7 | 7 | import cn.iocoder.yudao.framework.mq.redis.core.stream.AbstractRedisStreamMessageListener; |
| 8 | 8 | import cn.iocoder.yudao.module.iot.core.messagebus.core.IotMessageBus; |
| 9 | +import cn.iocoder.yudao.module.iot.core.messagebus.core.kafka.IotKafkaMessageBus; | |
| 9 | 10 | import cn.iocoder.yudao.module.iot.core.messagebus.core.local.IotLocalMessageBus; |
| 10 | 11 | import cn.iocoder.yudao.module.iot.core.messagebus.core.rabbitmq.IotRabbitMQMessageBus; |
| 11 | 12 | import cn.iocoder.yudao.module.iot.core.messagebus.core.redis.IotRedisMessageBus; |
| ... | ... | @@ -18,6 +19,7 @@ import org.redisson.api.RedissonClient; |
| 18 | 19 | import org.springframework.amqp.rabbit.core.RabbitAdmin; |
| 19 | 20 | import org.springframework.amqp.rabbit.core.RabbitTemplate; |
| 20 | 21 | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 22 | +import org.springframework.boot.autoconfigure.kafka.KafkaProperties; | |
| 21 | 23 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 22 | 24 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 23 | 25 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| ... | ... | @@ -27,6 +29,7 @@ import org.springframework.context.annotation.Bean; |
| 27 | 29 | import org.springframework.context.annotation.Configuration; |
| 28 | 30 | import org.springframework.data.redis.core.RedisTemplate; |
| 29 | 31 | import org.springframework.data.redis.core.StringRedisTemplate; |
| 32 | +import org.springframework.kafka.core.KafkaTemplate; | |
| 30 | 33 | |
| 31 | 34 | import java.util.List; |
| 32 | 35 | |
| ... | ... | @@ -77,6 +80,21 @@ public class IotMessageBusAutoConfiguration { |
| 77 | 80 | |
| 78 | 81 | } |
| 79 | 82 | |
| 83 | + // ==================== Kafka 实现 ==================== | |
| 84 | + | |
| 85 | + @Configuration | |
| 86 | + @ConditionalOnProperty(prefix = "yudao.iot.message-bus", name = "type", havingValue = "kafka") | |
| 87 | + @ConditionalOnClass(KafkaTemplate.class) | |
| 88 | + public static class IotKafkaMessageBusConfiguration { | |
| 89 | + | |
| 90 | + @Bean | |
| 91 | + public IotKafkaMessageBus iotKafkaMessageBus(KafkaProperties kafkaProperties) { | |
| 92 | + log.info("[iotKafkaMessageBus][创建 IoT Kafka 消息总线]"); | |
| 93 | + return new IotKafkaMessageBus(kafkaProperties); | |
| 94 | + } | |
| 95 | + | |
| 96 | + } | |
| 97 | + | |
| 80 | 98 | // ==================== Redis 实现 ==================== |
| 81 | 99 | |
| 82 | 100 | /** | ... | ... |
yudao-module-iot/yudao-module-iot-core/src/main/java/cn/iocoder/yudao/module/iot/core/messagebus/config/IotMessageBusProperties.java
| ... | ... | @@ -20,9 +20,9 @@ public class IotMessageBusProperties { |
| 20 | 20 | /** |
| 21 | 21 | * 消息总线类型 |
| 22 | 22 | * |
| 23 | - * 可选值:local、redis、rocketmq、rabbitmq | |
| 23 | + * 可选值:local、redis、rocketmq、kafka、rabbitmq | |
| 24 | 24 | */ |
| 25 | 25 | @NotNull(message = "IoT 消息总线类型不能为空") |
| 26 | 26 | private String type = "local"; |
| 27 | 27 | |
| 28 | -} | |
| 29 | 28 | \ No newline at end of file |
| 29 | +} | ... | ... |
yudao-module-iot/yudao-module-iot-gateway/pom.xml
| ... | ... | @@ -30,9 +30,13 @@ |
| 30 | 30 | |
| 31 | 31 | <!-- 消息队列相关 --> |
| 32 | 32 | <dependency> |
| 33 | + <groupId>org.springframework.kafka</groupId> | |
| 34 | + <artifactId>spring-kafka</artifactId> | |
| 35 | + <optional>true</optional> | |
| 36 | + </dependency> | |
| 37 | + <dependency> | |
| 33 | 38 | <groupId>org.apache.rocketmq</groupId> |
| 34 | 39 | <artifactId>rocketmq-spring-boot-starter</artifactId> |
| 35 | - <!-- TODO @芋艿:消息队列,后续可能去掉,默认不使用 rocketmq --> | |
| 36 | 40 | <optional>true</optional> |
| 37 | 41 | </dependency> |
| 38 | 42 | ... | ... |
yudao-module-iot/yudao-module-iot-gateway/src/main/resources/application.yaml
| ... | ... | @@ -12,6 +12,9 @@ spring: |
| 12 | 12 | database: 0 # Redis 数据库索引 |
| 13 | 13 | # password: # Redis 密码,如果有的话 |
| 14 | 14 | timeout: 30000ms # 连接超时时间 |
| 15 | + # Kafka 配置项,对应 KafkaProperties 配置类 | |
| 16 | + kafka: | |
| 17 | + bootstrap-servers: 127.0.0.1:9092 # 指定 Kafka Broker 地址,可以设置多个,以逗号分隔 | |
| 15 | 18 | |
| 16 | 19 | --- #################### 消息队列相关 #################### |
| 17 | 20 | ... | ... |