Commit 815f38436c4088361fa69fb6329c9ac6101e7338
1 parent
def9ff11
review:【IoT 物联网】http、emqx 的实现
Showing
4 changed files
with
28 additions
and
2 deletions
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/config/IotGatewayProperties.java
| @@ -98,6 +98,21 @@ public class IotGatewayProperties { | @@ -98,6 +98,21 @@ public class IotGatewayProperties { | ||
| 98 | */ | 98 | */ |
| 99 | private Integer serverPort; | 99 | private Integer serverPort; |
| 100 | 100 | ||
| 101 | + /** | ||
| 102 | + * 是否开启 SSL | ||
| 103 | + */ | ||
| 104 | + @NotNull(message = "是否开启 SSL 不能为空") | ||
| 105 | + private Boolean sslEnabled = false; | ||
| 106 | + | ||
| 107 | + /** | ||
| 108 | + * SSL 证书路径 | ||
| 109 | + */ | ||
| 110 | + private String sslKeyPath; | ||
| 111 | + /** | ||
| 112 | + * SSL 证书路径 | ||
| 113 | + */ | ||
| 114 | + private String sslCertPath; | ||
| 115 | + | ||
| 101 | } | 116 | } |
| 102 | 117 | ||
| 103 | @Data | 118 | @Data |
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/emqx/IotEmqxAuthEventProtocol.java
| @@ -70,6 +70,7 @@ public class IotEmqxAuthEventProtocol { | @@ -70,6 +70,7 @@ public class IotEmqxAuthEventProtocol { | ||
| 70 | IotEmqxAuthEventHandler handler = new IotEmqxAuthEventHandler(serverId); | 70 | IotEmqxAuthEventHandler handler = new IotEmqxAuthEventHandler(serverId); |
| 71 | router.post(IotMqttTopicUtils.MQTT_AUTH_PATH).handler(handler::handleAuth); | 71 | router.post(IotMqttTopicUtils.MQTT_AUTH_PATH).handler(handler::handleAuth); |
| 72 | router.post(IotMqttTopicUtils.MQTT_EVENT_PATH).handler(handler::handleEvent); | 72 | router.post(IotMqttTopicUtils.MQTT_EVENT_PATH).handler(handler::handleEvent); |
| 73 | + // TODO @haohao:/mqtt/acl 需要处理么? | ||
| 73 | 74 | ||
| 74 | // 3. 启动 HTTP 服务器 | 75 | // 3. 启动 HTTP 服务器 |
| 75 | try { | 76 | try { |
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/emqx/IotEmqxUpstreamProtocol.java
| @@ -127,6 +127,7 @@ public class IotEmqxUpstreamProtocol { | @@ -127,6 +127,7 @@ public class IotEmqxUpstreamProtocol { | ||
| 127 | // 1. 连接 MQTT Broker | 127 | // 1. 连接 MQTT Broker |
| 128 | CountDownLatch latch = new CountDownLatch(1); | 128 | CountDownLatch latch = new CountDownLatch(1); |
| 129 | AtomicBoolean success = new AtomicBoolean(false); | 129 | AtomicBoolean success = new AtomicBoolean(false); |
| 130 | + // TODO @haohao:要不要加 MqttClientOptions 参数?1)setCleanSession true;2)setMaxInflightQueue 10000;3)setKeepAliveInterval 60;4)setSsl/setTrustAll | ||
| 130 | mqttClient.connect(port, host, connectResult -> { | 131 | mqttClient.connect(port, host, connectResult -> { |
| 131 | if (connectResult.succeeded()) { | 132 | if (connectResult.succeeded()) { |
| 132 | log.info("[connectMqttSync][MQTT 客户端连接成功, host: {}, port: {}]", host, port); | 133 | log.info("[connectMqttSync][MQTT 客户端连接成功, host: {}, port: {}]", host, port); |
yudao-module-iot/yudao-module-iot-gateway/src/main/java/cn/iocoder/yudao/module/iot/gateway/protocol/http/IotHttpUpstreamProtocol.java
| @@ -7,6 +7,8 @@ import cn.iocoder.yudao.module.iot.gateway.protocol.http.router.IotHttpUpstreamH | @@ -7,6 +7,8 @@ import cn.iocoder.yudao.module.iot.gateway.protocol.http.router.IotHttpUpstreamH | ||
| 7 | import io.vertx.core.AbstractVerticle; | 7 | import io.vertx.core.AbstractVerticle; |
| 8 | import io.vertx.core.Vertx; | 8 | import io.vertx.core.Vertx; |
| 9 | import io.vertx.core.http.HttpServer; | 9 | import io.vertx.core.http.HttpServer; |
| 10 | +import io.vertx.core.http.HttpServerOptions; | ||
| 11 | +import io.vertx.core.net.PemKeyCertOptions; | ||
| 10 | import io.vertx.ext.web.Router; | 12 | import io.vertx.ext.web.Router; |
| 11 | import io.vertx.ext.web.handler.BodyHandler; | 13 | import io.vertx.ext.web.handler.BodyHandler; |
| 12 | import jakarta.annotation.PostConstruct; | 14 | import jakarta.annotation.PostConstruct; |
| @@ -49,10 +51,17 @@ public class IotHttpUpstreamProtocol extends AbstractVerticle { | @@ -49,10 +51,17 @@ public class IotHttpUpstreamProtocol extends AbstractVerticle { | ||
| 49 | router.post(IotHttpUpstreamHandler.PATH).handler(upstreamHandler); | 51 | router.post(IotHttpUpstreamHandler.PATH).handler(upstreamHandler); |
| 50 | 52 | ||
| 51 | // 启动 HTTP 服务器 | 53 | // 启动 HTTP 服务器 |
| 54 | + HttpServerOptions options = new HttpServerOptions() | ||
| 55 | + .setPort(httpProperties.getServerPort()); | ||
| 56 | + if (Boolean.TRUE.equals(httpProperties.getSslEnabled())) { | ||
| 57 | + PemKeyCertOptions pemKeyCertOptions = new PemKeyCertOptions().setKeyPath(httpProperties.getSslKeyPath()) | ||
| 58 | + .setCertPath(httpProperties.getSslCertPath()); | ||
| 59 | + options = options.setSsl(true).setKeyCertOptions(pemKeyCertOptions); | ||
| 60 | + } | ||
| 52 | try { | 61 | try { |
| 53 | - httpServer = vertx.createHttpServer() | 62 | + httpServer = vertx.createHttpServer(options) |
| 54 | .requestHandler(router) | 63 | .requestHandler(router) |
| 55 | - .listen(httpProperties.getServerPort()) | 64 | + .listen() |
| 56 | .result(); | 65 | .result(); |
| 57 | log.info("[start][IoT 网关 HTTP 协议启动成功,端口:{}]", httpProperties.getServerPort()); | 66 | log.info("[start][IoT 网关 HTTP 协议启动成功,端口:{}]", httpProperties.getServerPort()); |
| 58 | } catch (Exception e) { | 67 | } catch (Exception e) { |