【功能完善】IoT: 新增 MQTT RPC 支持,包含请求和响应模型、序列化工具、MQTT 配置及客户端/服务器实现,提供示例服务和控制器接口,优化插件结构以支持 HTTP 插件的集成。
This commit is contained in:
@@ -150,5 +150,10 @@
|
||||
<artifactId>netty-all</artifactId>
|
||||
<version>4.1.63.Final</version> <!-- 版本可根据需要调整 -->
|
||||
</dependency>
|
||||
<!-- MQTT -->
|
||||
<dependency>
|
||||
<groupId>org.eclipse.paho</groupId>
|
||||
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,11 @@
|
||||
package cn.iocoder.yudao.module.iot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class HttpPluginSpringbootApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(HttpPluginSpringbootApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
|
||||
package cn.iocoder.yudao.module.iot.controller;
|
||||
|
||||
import cn.iocoder.yudao.module.iot.mqttrpc.client.RpcClient;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/rpc")
|
||||
@RequiredArgsConstructor
|
||||
public class RpcController {
|
||||
|
||||
@Resource
|
||||
private RpcClient rpcClient;
|
||||
|
||||
@PostMapping("/add")
|
||||
public CompletableFuture<Object> add(@RequestParam int a, @RequestParam int b) throws Exception {
|
||||
return rpcClient.call("add", new Object[]{a, b}, 10);
|
||||
}
|
||||
|
||||
@PostMapping("/concat")
|
||||
public CompletableFuture<Object> concat(@RequestParam String str1, @RequestParam String str2) throws Exception {
|
||||
return rpcClient.call("concat", new Object[]{str1, str2}, 10);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package cn.iocoder.yudao.module.iot.mqttrpc.client;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import cn.iocoder.yudao.module.iot.mqttrpc.common.RpcRequest;
|
||||
import cn.iocoder.yudao.module.iot.mqttrpc.common.RpcResponse;
|
||||
import cn.iocoder.yudao.module.iot.mqttrpc.common.SerializationUtils;
|
||||
import cn.iocoder.yudao.module.iot.mqttrpc.config.MqttConfig;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.eclipse.paho.client.mqttv3.*;
|
||||
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class RpcClient {
|
||||
|
||||
private final MqttConfig mqttConfig;
|
||||
private final MqttClient mqttClient;
|
||||
private final ConcurrentMap<String, CompletableFuture<RpcResponse>> pendingRequests = new ConcurrentHashMap<>();
|
||||
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
|
||||
|
||||
public RpcClient(MqttConfig mqttConfig) throws MqttException {
|
||||
this.mqttConfig = mqttConfig;
|
||||
this.mqttClient = new MqttClient(mqttConfig.getBroker(), mqttConfig.getClientId(), new MemoryPersistence());
|
||||
MqttConnectOptions options = new MqttConnectOptions();
|
||||
options.setAutomaticReconnect(true);
|
||||
options.setCleanSession(true);
|
||||
options.setUserName(mqttConfig.getUsername());
|
||||
options.setPassword(mqttConfig.getPassword().toCharArray());
|
||||
this.mqttClient.connect(options);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() throws MqttException {
|
||||
mqttClient.subscribe(mqttConfig.getResponseTopicPrefix() + "#", this::handleResponse);
|
||||
log.info("RPC Client subscribed to topics: {}", mqttConfig.getResponseTopicPrefix() + "#");
|
||||
}
|
||||
|
||||
private void handleResponse(String topic, MqttMessage message) {
|
||||
String correlationId = topic.substring(mqttConfig.getResponseTopicPrefix().length());
|
||||
RpcResponse response = SerializationUtils.deserialize(new String(message.getPayload()), RpcResponse.class);
|
||||
CompletableFuture<RpcResponse> future = pendingRequests.remove(correlationId);
|
||||
if (future != null) {
|
||||
if (response.getError() != null) {
|
||||
future.completeExceptionally(new RuntimeException(response.getError()));
|
||||
} else {
|
||||
future.complete(response);
|
||||
}
|
||||
} else {
|
||||
log.warn("Received response for unknown correlationId: {}", correlationId);
|
||||
}
|
||||
}
|
||||
|
||||
public CompletableFuture<Object> call(String method, Object[] params, int timeoutSeconds) throws MqttException {
|
||||
String correlationId = UUID.randomUUID().toString();
|
||||
String replyTo = mqttConfig.getResponseTopicPrefix() + correlationId;
|
||||
|
||||
RpcRequest request = new RpcRequest(method, params, correlationId, replyTo);
|
||||
String payload = SerializationUtils.serialize(request);
|
||||
MqttMessage message = new MqttMessage(payload.getBytes());
|
||||
message.setQos(1);
|
||||
mqttClient.publish(mqttConfig.getRequestTopic(), message);
|
||||
|
||||
CompletableFuture<RpcResponse> futureResponse = new CompletableFuture<>();
|
||||
pendingRequests.put(correlationId, futureResponse);
|
||||
|
||||
// 设置超时
|
||||
scheduler.schedule(() -> {
|
||||
CompletableFuture<RpcResponse> removed = pendingRequests.remove(correlationId);
|
||||
if (removed != null) {
|
||||
removed.completeExceptionally(new TimeoutException("RPC call timed out"));
|
||||
}
|
||||
}, timeoutSeconds, TimeUnit.SECONDS);
|
||||
|
||||
// 返回最终的结果
|
||||
return futureResponse.thenApply(RpcResponse::getResult);
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void cleanup() throws MqttException {
|
||||
mqttClient.disconnect();
|
||||
scheduler.shutdown();
|
||||
log.info("RPC Client disconnected");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package cn.iocoder.yudao.module.iot.mqttrpc.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "mqtt")
|
||||
public class MqttConfig {
|
||||
|
||||
/**
|
||||
* MQTT 代理地址
|
||||
*/
|
||||
private String broker;
|
||||
|
||||
/**
|
||||
* MQTT 用户名
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* MQTT 密码
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* MQTT 客户端 ID
|
||||
*/
|
||||
private String clientId;
|
||||
|
||||
/**
|
||||
* MQTT 请求主题
|
||||
*/
|
||||
private String requestTopic;
|
||||
|
||||
/**
|
||||
* MQTT 响应主题前缀
|
||||
*/
|
||||
private String responseTopicPrefix;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
server:
|
||||
port: 8092
|
||||
|
||||
spring:
|
||||
application:
|
||||
name: yudao-module-iot-http-plugin
|
||||
|
||||
# MQTT-RPC 配置
|
||||
mqtt:
|
||||
broker: tcp://chaojiniu.top:1883
|
||||
username: haohao
|
||||
password: ahh@123456
|
||||
clientId: mqtt-rpc-client-${random.int}
|
||||
requestTopic: rpc/request
|
||||
responseTopicPrefix: rpc/response/
|
||||
@@ -145,10 +145,10 @@
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- MQTT -->
|
||||
<dependency>
|
||||
<groupId>io.netty</groupId>
|
||||
<artifactId>netty-all</artifactId>
|
||||
<version>4.1.63.Final</version> <!-- 版本可根据需要调整 -->
|
||||
<groupId>org.eclipse.paho</groupId>
|
||||
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -1,11 +1,12 @@
|
||||
package cn.iocoder.yudao.module.iot.plugin;
|
||||
|
||||
import cn.iocoder.yudao.module.iot.api.device.DeviceDataApi;
|
||||
import cn.iocoder.yudao.module.iot.api.ServiceRegistry;
|
||||
import cn.iocoder.yudao.module.iot.api.device.DeviceDataApi;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.pf4j.PluginWrapper;
|
||||
import org.pf4j.Plugin;
|
||||
import org.pf4j.PluginWrapper;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
@@ -13,11 +14,11 @@ import java.util.concurrent.Executors;
|
||||
public class MqttPlugin extends Plugin {
|
||||
|
||||
private ExecutorService executorService;
|
||||
@Resource
|
||||
private DeviceDataApi deviceDataApi;
|
||||
|
||||
public MqttPlugin(PluginWrapper wrapper) {
|
||||
super(wrapper);
|
||||
// 初始化线程池
|
||||
this.executorService = Executors.newSingleThreadExecutor();
|
||||
}
|
||||
|
||||
@@ -25,24 +26,20 @@ public class MqttPlugin extends Plugin {
|
||||
public void start() {
|
||||
log.info("MqttPlugin.start()");
|
||||
|
||||
// 重新初始化线程池,确保它是活跃的
|
||||
if (executorService.isShutdown() || executorService.isTerminated()) {
|
||||
executorService = Executors.newSingleThreadExecutor();
|
||||
}
|
||||
|
||||
// 从 ServiceRegistry 中获取主程序暴露的 DeviceDataApi 接口实例
|
||||
deviceDataApi = ServiceRegistry.getService(DeviceDataApi.class);
|
||||
if (deviceDataApi == null) {
|
||||
log.error("未能从 ServiceRegistry 获取 DeviceDataApi 实例,请确保主程序已正确注册!");
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
log.info("MqttPlugin.stop()");
|
||||
// 停止线程池
|
||||
executorService.shutdownNow();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user