Commit 76a431b8518fd9af37d8b2dc30f6d99b42be9837
1 parent
a7eeacd5
```
feat(mqtt): 重构MQTT服务为MQTT代理并集成Redis和Vert.x - 将spring.application.name从mqtt-service改为mqtt-agent - 升级Vert.x版本从4.5.10到4.5.22 - 重命名VerticleLifeTime为VerticleLifeCycle并移至config包 - 移除Lombok依赖并手动实现getter/setter方法 - 更新全局异常处理器日志实现 ```
Showing
10 changed files
with
85 additions
and
44 deletions
mqtt-boot/src/main/resources/application.properties
| 1 | -spring.application.name=mqtt-service | 1 | +spring.application.name=mqtt-agent |
| 2 | spring.cloud.nacos.discovery.enabled=true | 2 | spring.cloud.nacos.discovery.enabled=true |
| 3 | spring.cloud.nacos.discovery.group=MICROSERVICE | 3 | spring.cloud.nacos.discovery.group=MICROSERVICE |
| 4 | spring.cloud.nacos.discovery.server-addr=nacos.diligrp.com:8848 | 4 | spring.cloud.nacos.discovery.server-addr=nacos.diligrp.com:8848 |
| @@ -9,3 +9,12 @@ spring.cloud.nacos.config.server-addr=nacos.diligrp.com:8848 | @@ -9,3 +9,12 @@ 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 | +spring.data.redis.host=redis.diligrp.com | ||
| 15 | +spring.data.redis.port=6379 | ||
| 16 | +spring.data.redis.database=12 | ||
| 17 | +#spring.data.redis.username= | ||
| 18 | +#spring.data.redis.password= | ||
| 19 | +spring.data.redis.connect-timeout=15000 | ||
| 20 | +spring.data.redis.timeout=30000 |
mqtt-core/pom.xml
| @@ -15,10 +15,6 @@ | @@ -15,10 +15,6 @@ | ||
| 15 | </parent> | 15 | </parent> |
| 16 | <dependencies> | 16 | <dependencies> |
| 17 | <dependency> | 17 | <dependency> |
| 18 | - <groupId>org.projectlombok</groupId> | ||
| 19 | - <artifactId>lombok</artifactId> | ||
| 20 | - </dependency> | ||
| 21 | - <dependency> | ||
| 22 | <groupId>org.springframework.boot</groupId> | 18 | <groupId>org.springframework.boot</groupId> |
| 23 | <artifactId>spring-boot-starter-web</artifactId> | 19 | <artifactId>spring-boot-starter-web</artifactId> |
| 24 | </dependency> | 20 | </dependency> |
mqtt-core/src/main/java/com/diligrp/mqtt/core/config/GlobalExceptionHandler.java
| @@ -6,7 +6,8 @@ import com.diligrp.mqtt.core.message.Message; | @@ -6,7 +6,8 @@ import com.diligrp.mqtt.core.message.Message; | ||
| 6 | import com.fasterxml.jackson.databind.exc.InvalidFormatException; | 6 | import com.fasterxml.jackson.databind.exc.InvalidFormatException; |
| 7 | import jakarta.validation.ConstraintViolation; | 7 | import jakarta.validation.ConstraintViolation; |
| 8 | import jakarta.validation.ConstraintViolationException; | 8 | import jakarta.validation.ConstraintViolationException; |
| 9 | -import lombok.extern.slf4j.Slf4j; | 9 | +import org.slf4j.Logger; |
| 10 | +import org.slf4j.LoggerFactory; | ||
| 10 | import org.springframework.context.support.DefaultMessageSourceResolvable; | 11 | import org.springframework.context.support.DefaultMessageSourceResolvable; |
| 11 | import org.springframework.http.converter.HttpMessageNotReadableException; | 12 | import org.springframework.http.converter.HttpMessageNotReadableException; |
| 12 | import org.springframework.validation.BindException; | 13 | import org.springframework.validation.BindException; |
| @@ -25,8 +26,9 @@ import java.util.stream.Collectors; | @@ -25,8 +26,9 @@ import java.util.stream.Collectors; | ||
| 25 | * 统一异常处理器 | 26 | * 统一异常处理器 |
| 26 | */ | 27 | */ |
| 27 | @RestControllerAdvice | 28 | @RestControllerAdvice |
| 28 | -@Slf4j | ||
| 29 | public class GlobalExceptionHandler { | 29 | public class GlobalExceptionHandler { |
| 30 | + | ||
| 31 | + private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); | ||
| 30 | /** | 32 | /** |
| 31 | * 处理业务异常 | 33 | * 处理业务异常 |
| 32 | */ | 34 | */ |
mqtt-core/src/main/java/com/diligrp/mqtt/core/config/MqttProperties.java deleted
100644 → 0
| 1 | -package com.diligrp.mqtt.core.config; | ||
| 2 | - | ||
| 3 | -import org.springframework.boot.context.properties.ConfigurationProperties; | ||
| 4 | - | ||
| 5 | -/** | ||
| 6 | - * @Author: zhangmeiyang | ||
| 7 | - * @CreateTime: 2025-12-29 17:41 | ||
| 8 | - * @Version: todo | ||
| 9 | - */ | ||
| 10 | -@ConfigurationProperties(prefix = "mqtt") | ||
| 11 | -public class MqttProperties { | ||
| 12 | - private String host; | ||
| 13 | - private Integer port; | ||
| 14 | - private String username; | ||
| 15 | - private String password; | ||
| 16 | -} |
mqtt-core/src/main/java/com/diligrp/mqtt/core/exception/MqttServiceException.java
| @@ -3,12 +3,10 @@ package com.diligrp.mqtt.core.exception; | @@ -3,12 +3,10 @@ package com.diligrp.mqtt.core.exception; | ||
| 3 | 3 | ||
| 4 | 4 | ||
| 5 | import com.diligrp.mqtt.core.config.MessageConfirmType; | 5 | import com.diligrp.mqtt.core.config.MessageConfirmType; |
| 6 | -import lombok.Getter; | ||
| 7 | 6 | ||
| 8 | /** | 7 | /** |
| 9 | * 所有模块异常类的基类 | 8 | * 所有模块异常类的基类 |
| 10 | */ | 9 | */ |
| 11 | -@Getter | ||
| 12 | public class MqttServiceException extends RuntimeException { | 10 | public class MqttServiceException extends RuntimeException { |
| 13 | /** | 11 | /** |
| 14 | * 错误码 | 12 | * 错误码 |
| @@ -71,4 +69,19 @@ public class MqttServiceException extends RuntimeException { | @@ -71,4 +69,19 @@ public class MqttServiceException extends RuntimeException { | ||
| 71 | return stackTrace ? super.fillInStackTrace() : this; | 69 | return stackTrace ? super.fillInStackTrace() : this; |
| 72 | } | 70 | } |
| 73 | 71 | ||
| 72 | + public int getCode() { | ||
| 73 | + return code; | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + public void setCode(int code) { | ||
| 77 | + this.code = code; | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + public boolean isStackTrace() { | ||
| 81 | + return stackTrace; | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + public void setStackTrace(boolean stackTrace) { | ||
| 85 | + this.stackTrace = stackTrace; | ||
| 86 | + } | ||
| 74 | } | 87 | } |
mqtt-core/src/main/java/com/diligrp/mqtt/core/message/Message.java
| 1 | package com.diligrp.mqtt.core.message; | 1 | package com.diligrp.mqtt.core.message; |
| 2 | 2 | ||
| 3 | import com.diligrp.mqtt.core.config.MessageConfirmType; | 3 | import com.diligrp.mqtt.core.config.MessageConfirmType; |
| 4 | -import lombok.Getter; | ||
| 5 | -import lombok.Setter; | ||
| 6 | 4 | ||
| 7 | -@Setter | ||
| 8 | -@Getter | ||
| 9 | public class Message<T> { | 5 | public class Message<T> { |
| 10 | private Integer code; | 6 | private Integer code; |
| 11 | private String message; | 7 | private String message; |
| @@ -35,4 +31,28 @@ public class Message<T> { | @@ -35,4 +31,28 @@ public class Message<T> { | ||
| 35 | result.message = message; | 31 | result.message = message; |
| 36 | return result; | 32 | return result; |
| 37 | } | 33 | } |
| 34 | + | ||
| 35 | + public Integer getCode() { | ||
| 36 | + return code; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + public void setCode(Integer code) { | ||
| 40 | + this.code = code; | ||
| 41 | + } | ||
| 42 | + | ||
| 43 | + public String getMessage() { | ||
| 44 | + return message; | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + public void setMessage(String message) { | ||
| 48 | + this.message = message; | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + public T getData() { | ||
| 52 | + return data; | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + public void setData(T data) { | ||
| 56 | + this.data = data; | ||
| 57 | + } | ||
| 38 | } | 58 | } |
mqtt-core/src/main/java/com/diligrp/mqtt/core/message/PageQuery.java
| 1 | package com.diligrp.mqtt.core.message; | 1 | package com.diligrp.mqtt.core.message; |
| 2 | 2 | ||
| 3 | import jakarta.validation.constraints.NotNull; | 3 | import jakarta.validation.constraints.NotNull; |
| 4 | -import lombok.Getter; | ||
| 5 | -import lombok.Setter; | ||
| 6 | 4 | ||
| 7 | /** | 5 | /** |
| 8 | * @Author: zhangmeiyang | 6 | * @Author: zhangmeiyang |
| 9 | * @CreateTime: 2025-11-05 15:57 | 7 | * @CreateTime: 2025-11-05 15:57 |
| 10 | * @Version: todo | 8 | * @Version: todo |
| 11 | */ | 9 | */ |
| 12 | -@Getter | ||
| 13 | -@Setter | ||
| 14 | public abstract class PageQuery { | 10 | public abstract class PageQuery { |
| 15 | /** | 11 | /** |
| 16 | * 页码 | 12 | * 页码 |
| @@ -22,4 +18,20 @@ public abstract class PageQuery { | @@ -22,4 +18,20 @@ public abstract class PageQuery { | ||
| 22 | */ | 18 | */ |
| 23 | @NotNull(groups = {Valid.Read.class}) | 19 | @NotNull(groups = {Valid.Read.class}) |
| 24 | protected Integer pageSize; | 20 | protected Integer pageSize; |
| 21 | + | ||
| 22 | + public Integer getPageNumber() { | ||
| 23 | + return pageNumber; | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + public void setPageNumber(Integer pageNumber) { | ||
| 27 | + this.pageNumber = pageNumber; | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + public Integer getPageSize() { | ||
| 31 | + return pageSize; | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + public void setPageSize(Integer pageSize) { | ||
| 35 | + this.pageSize = pageSize; | ||
| 36 | + } | ||
| 25 | } | 37 | } |
mqtt-vertx/pom.xml
| @@ -20,6 +20,14 @@ | @@ -20,6 +20,14 @@ | ||
| 20 | <artifactId>mqtt-core</artifactId> | 20 | <artifactId>mqtt-core</artifactId> |
| 21 | <version>${revision}</version> | 21 | <version>${revision}</version> |
| 22 | </dependency> | 22 | </dependency> |
| 23 | + <dependency> | ||
| 24 | + <groupId>io.vertx</groupId> | ||
| 25 | + <artifactId>vertx-core</artifactId> | ||
| 26 | + </dependency> | ||
| 27 | + <dependency> | ||
| 28 | + <groupId>io.vertx</groupId> | ||
| 29 | + <artifactId>vertx-mqtt</artifactId> | ||
| 30 | + </dependency> | ||
| 23 | </dependencies> | 31 | </dependencies> |
| 24 | 32 | ||
| 25 | </project> | 33 | </project> |
mqtt-vertx/src/main/java/com/diligrp/mqtt/vertx/VerticleLifeTime.java renamed to mqtt-vertx/src/main/java/com/diligrp/mqtt/vertx/config/VerticleLifeCycle.java
| 1 | -package com.diligrp.mqtt.vertx; | 1 | +package com.diligrp.mqtt.vertx.config; |
| 2 | 2 | ||
| 3 | import com.diligrp.mqtt.vertx.verticle.MqttVerticle; | 3 | import com.diligrp.mqtt.vertx.verticle.MqttVerticle; |
| 4 | import io.vertx.core.Vertx; | 4 | import io.vertx.core.Vertx; |
| 5 | -import lombok.extern.slf4j.Slf4j; | ||
| 6 | import org.springframework.beans.factory.DisposableBean; | 5 | import org.springframework.beans.factory.DisposableBean; |
| 7 | import org.springframework.boot.CommandLineRunner; | 6 | import org.springframework.boot.CommandLineRunner; |
| 8 | import org.springframework.stereotype.Component; | 7 | import org.springframework.stereotype.Component; |
| 9 | 8 | ||
| 10 | /** | 9 | /** |
| 11 | - * @Author: zhangmeiyang | ||
| 12 | - * @CreateTime: 2025-12-26 17:16 | ||
| 13 | - * @Version: todo | 10 | + * verticle生命周期 |
| 11 | + * | ||
| 12 | + * @author zhangmeiyang | ||
| 13 | + * @date 2025/12/30 | ||
| 14 | */ | 14 | */ |
| 15 | @Component | 15 | @Component |
| 16 | -@Slf4j | ||
| 17 | -public class VerticleLifeTime implements CommandLineRunner, DisposableBean { | 16 | +public class VerticleLifeCycle implements CommandLineRunner, DisposableBean { |
| 18 | 17 | ||
| 19 | private final Vertx vertx; | 18 | private final Vertx vertx; |
| 20 | 19 | ||
| 21 | - public VerticleLifeTime(Vertx vertx) { | 20 | + public VerticleLifeCycle(Vertx vertx) { |
| 22 | this.vertx = vertx; | 21 | this.vertx = vertx; |
| 23 | } | 22 | } |
| 24 | 23 | ||
| 25 | @Override | 24 | @Override |
| 26 | public void run(String... args) throws Exception { | 25 | public void run(String... args) throws Exception { |
| 27 | vertx.deployVerticle(new MqttVerticle()); | 26 | vertx.deployVerticle(new MqttVerticle()); |
| 28 | - log.info("MqttVerticle deployed"); | ||
| 29 | } | 27 | } |
| 30 | 28 | ||
| 31 | @Override | 29 | @Override |
| 32 | public void destroy() throws Exception { | 30 | public void destroy() throws Exception { |
| 33 | vertx.close(); | 31 | vertx.close(); |
| 34 | - log.info("Vertx closed"); | ||
| 35 | } | 32 | } |
| 36 | } | 33 | } |
pom.xml
| @@ -30,7 +30,7 @@ | @@ -30,7 +30,7 @@ | ||
| 30 | <mybatis-plus.version>3.5.14</mybatis-plus.version> | 30 | <mybatis-plus.version>3.5.14</mybatis-plus.version> |
| 31 | <!-- 工具类库 --> | 31 | <!-- 工具类库 --> |
| 32 | <lombok.version>1.18.42</lombok.version> | 32 | <lombok.version>1.18.42</lombok.version> |
| 33 | - <vertx.version>4.5.10</vertx.version> | 33 | + <vertx.version>4.5.22</vertx.version> |
| 34 | </properties> | 34 | </properties> |
| 35 | 35 | ||
| 36 | <dependencyManagement> | 36 | <dependencyManagement> |