Commit 49ba958f8351b5a8857478d67c175430c7ca7a8f
1 parent
67a28bb1
```
refactor(mqtt): 移除application.properties中的硬编码配置 移除mqtt相关配置项,改为从nacos配置中心获取 BREAKING CHANGE: mqtt连接配置已从application.properties中移除, 需要通过nacos配置中心进行配置 ```
Showing
5 changed files
with
8 additions
and
18 deletions
mqtt-boot/src/main/resources/application.properties
| @@ -9,13 +9,3 @@ spring.cloud.nacos.config.server-addr=nacos.diligrp.com:8848 | @@ -9,13 +9,3 @@ spring.cloud.nacos.config.server-addr=nacos.diligrp.com:8848 | ||
| 9 | spring.cloud.nacos.config.namespace=2267e673-b41f-458d-9643-2a03e4fd92fb | 9 | spring.cloud.nacos.config.namespace=2267e673-b41f-458d-9643-2a03e4fd92fb |
| 10 | spring.config.import[0]=nacos:${spring.application.name}.properties | 10 | spring.config.import[0]=nacos:${spring.application.name}.properties |
| 11 | spring.config.import[1]=nacos:${spring.application.name}-${spring.profiles.active}.properties | 11 | spring.config.import[1]=nacos:${spring.application.name}-${spring.profiles.active}.properties |
| 12 | - | ||
| 13 | - | ||
| 14 | -mqtt.url=tcp://10.30.110.178:11883 | ||
| 15 | -mqtt.clientId=mqtt-agent | ||
| 16 | -mqtt.username=test_group | ||
| 17 | -mqtt.password=1qaz2wsx | ||
| 18 | -mqtt.timeOut=10 | ||
| 19 | -mqtt.cleanSession=true | ||
| 20 | -mqtt.automaticReconnect=true | ||
| 21 | -mqtt.qos=1 |
mqtt-core/pom.xml
| @@ -19,6 +19,10 @@ | @@ -19,6 +19,10 @@ | ||
| 19 | <artifactId>spring-boot-starter-web</artifactId> | 19 | <artifactId>spring-boot-starter-web</artifactId> |
| 20 | </dependency> | 20 | </dependency> |
| 21 | <dependency> | 21 | <dependency> |
| 22 | + <groupId>org.springframework.boot</groupId> | ||
| 23 | + <artifactId>spring-boot-starter-integration</artifactId> | ||
| 24 | + </dependency> | ||
| 25 | + <dependency> | ||
| 22 | <groupId>org.projectlombok</groupId> | 26 | <groupId>org.projectlombok</groupId> |
| 23 | <artifactId>lombok</artifactId> | 27 | <artifactId>lombok</artifactId> |
| 24 | </dependency> | 28 | </dependency> |
mqtt-core/src/main/java/com/diligrp/mqtt/core/event/RecFailEvent.java
| @@ -3,6 +3,7 @@ package com.diligrp.mqtt.core.event; | @@ -3,6 +3,7 @@ package com.diligrp.mqtt.core.event; | ||
| 3 | import lombok.Getter; | 3 | import lombok.Getter; |
| 4 | import lombok.Setter; | 4 | import lombok.Setter; |
| 5 | import org.springframework.context.ApplicationEvent; | 5 | import org.springframework.context.ApplicationEvent; |
| 6 | +import org.springframework.messaging.Message; | ||
| 6 | 7 | ||
| 7 | /** | 8 | /** |
| 8 | * @Author: zhangmeiyang | 9 | * @Author: zhangmeiyang |
| @@ -12,9 +13,9 @@ import org.springframework.context.ApplicationEvent; | @@ -12,9 +13,9 @@ import org.springframework.context.ApplicationEvent; | ||
| 12 | @Getter | 13 | @Getter |
| 13 | @Setter | 14 | @Setter |
| 14 | public class RecFailEvent extends ApplicationEvent { | 15 | public class RecFailEvent extends ApplicationEvent { |
| 15 | - private String message; | 16 | + private Message<?> message; |
| 16 | 17 | ||
| 17 | - public RecFailEvent(Object source, String message) { | 18 | + public RecFailEvent(Object source, Message<?> message) { |
| 18 | super(source); | 19 | super(source); |
| 19 | this.message = message; | 20 | this.message = message; |
| 20 | } | 21 | } |
mqtt-integration/pom.xml
| @@ -21,10 +21,6 @@ | @@ -21,10 +21,6 @@ | ||
| 21 | <version>${revision}</version> | 21 | <version>${revision}</version> |
| 22 | </dependency> | 22 | </dependency> |
| 23 | <dependency> | 23 | <dependency> |
| 24 | - <groupId>org.springframework.boot</groupId> | ||
| 25 | - <artifactId>spring-boot-starter-integration</artifactId> | ||
| 26 | - </dependency> | ||
| 27 | - <dependency> | ||
| 28 | <groupId>org.springframework.integration</groupId> | 24 | <groupId>org.springframework.integration</groupId> |
| 29 | <artifactId>spring-integration-mqtt</artifactId> | 25 | <artifactId>spring-integration-mqtt</artifactId> |
| 30 | </dependency> | 26 | </dependency> |
mqtt-integration/src/main/java/com/diligrp/mqtt/integration/handler/MqttMessageReceiver.java
| @@ -3,7 +3,6 @@ package com.diligrp.mqtt.integration.handler; | @@ -3,7 +3,6 @@ package com.diligrp.mqtt.integration.handler; | ||
| 3 | import com.diligrp.mqtt.core.event.RecFailEvent; | 3 | import com.diligrp.mqtt.core.event.RecFailEvent; |
| 4 | import com.diligrp.mqtt.core.event.RecSuccessEvent; | 4 | import com.diligrp.mqtt.core.event.RecSuccessEvent; |
| 5 | import com.diligrp.mqtt.core.model.ReceiveModel; | 5 | import com.diligrp.mqtt.core.model.ReceiveModel; |
| 6 | -import com.diligrp.mqtt.core.util.JsonUtils; | ||
| 7 | import jakarta.annotation.Resource; | 6 | import jakarta.annotation.Resource; |
| 8 | import lombok.extern.slf4j.Slf4j; | 7 | import lombok.extern.slf4j.Slf4j; |
| 9 | import org.springframework.context.ApplicationEventPublisher; | 8 | import org.springframework.context.ApplicationEventPublisher; |
| @@ -64,7 +63,7 @@ public class MqttMessageReceiver { | @@ -64,7 +63,7 @@ public class MqttMessageReceiver { | ||
| 64 | applicationEventPublisher.publishEvent(new RecSuccessEvent(this, receiveModel)); | 63 | applicationEventPublisher.publishEvent(new RecSuccessEvent(this, receiveModel)); |
| 65 | } catch (Exception e) { | 64 | } catch (Exception e) { |
| 66 | log.error("处理MQTT消息失败", e); | 65 | log.error("处理MQTT消息失败", e); |
| 67 | - applicationEventPublisher.publishEvent(new RecFailEvent(this, JsonUtils.toJsonString(message))); | 66 | + applicationEventPublisher.publishEvent(new RecFailEvent(this, message)); |
| 68 | 67 | ||
| 69 | } | 68 | } |
| 70 | } | 69 | } |