Commit 3647fd36860035eda823fa8207b5bc6a312037b3
1 parent
a85890d9
【代码优化】IoT:移除 ServiceRegistry,使用 SpringUtils 替代
Showing
9 changed files
with
19 additions
and
159 deletions
plugins/yudao-module-iot-http-plugin-2.2.0-snapshot.jar
No preview for this file type
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/api/ServiceRegistry.java deleted
| 1 | -package cn.iocoder.yudao.module.iot.api; | ||
| 2 | - | ||
| 3 | -import java.util.HashMap; | ||
| 4 | -import java.util.Map; | ||
| 5 | - | ||
| 6 | -// TODO 芋艿:纠结下 | ||
| 7 | -/** | ||
| 8 | - * 服务注册表 - 插架模块使用,无法使用 Spring 注入 | ||
| 9 | - */ | ||
| 10 | -public class ServiceRegistry { | ||
| 11 | - | ||
| 12 | - private static final Map<Class<?>, Object> services = new HashMap<>(); | ||
| 13 | - | ||
| 14 | - /** | ||
| 15 | - * 注册服务 | ||
| 16 | - * | ||
| 17 | - * @param serviceClass 服务类 | ||
| 18 | - * @param serviceImpl 服务实现 | ||
| 19 | - * @param <T> 服务类 | ||
| 20 | - */ | ||
| 21 | - public static <T> void registerService(Class<T> serviceClass, T serviceImpl) { | ||
| 22 | - services.put(serviceClass, serviceImpl); | ||
| 23 | - } | ||
| 24 | - | ||
| 25 | - /** | ||
| 26 | - * 获得服务 | ||
| 27 | - * | ||
| 28 | - * @param serviceClass 服务类 | ||
| 29 | - * @param <T> 服务类 | ||
| 30 | - * @return 服务实现 | ||
| 31 | - */ | ||
| 32 | - @SuppressWarnings("unchecked") | ||
| 33 | - public static <T> T getService(Class<T> serviceClass) { | ||
| 34 | - return (T) services.get(serviceClass); | ||
| 35 | - } | ||
| 36 | - | ||
| 37 | -} | ||
| 38 | \ No newline at end of file | 0 | \ No newline at end of file |
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/mqttrpc/common/RpcRequest.java deleted
| 1 | -package cn.iocoder.yudao.module.iot.mqttrpc.common; | ||
| 2 | - | ||
| 3 | -import lombok.AllArgsConstructor; | ||
| 4 | -import lombok.Builder; | ||
| 5 | -import lombok.Data; | ||
| 6 | -import lombok.NoArgsConstructor; | ||
| 7 | - | ||
| 8 | -// TODO @芋艿:要不要加个 mqtt 值了的前缀 | ||
| 9 | -/** | ||
| 10 | - * MQTT RPC 请求 | ||
| 11 | - */ | ||
| 12 | -@Data | ||
| 13 | -@Builder | ||
| 14 | -@NoArgsConstructor | ||
| 15 | -@AllArgsConstructor | ||
| 16 | -public class RpcRequest { | ||
| 17 | - | ||
| 18 | - /** | ||
| 19 | - * 方法名 | ||
| 20 | - */ | ||
| 21 | - private String method; | ||
| 22 | - | ||
| 23 | - /** | ||
| 24 | - * 参数 | ||
| 25 | - */ | ||
| 26 | - // TODO @haohao:object 对象会不会不好序列化? | ||
| 27 | - private Object[] params; | ||
| 28 | - | ||
| 29 | - /** | ||
| 30 | - * 关联 ID | ||
| 31 | - */ | ||
| 32 | - private String correlationId; | ||
| 33 | - | ||
| 34 | - /** | ||
| 35 | - * 回复地址 | ||
| 36 | - */ | ||
| 37 | - private String replyTo; | ||
| 38 | - | ||
| 39 | -} |
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/mqttrpc/common/RpcResponse.java deleted
| 1 | -package cn.iocoder.yudao.module.iot.mqttrpc.common; | ||
| 2 | - | ||
| 3 | -import lombok.AllArgsConstructor; | ||
| 4 | -import lombok.Builder; | ||
| 5 | -import lombok.Data; | ||
| 6 | -import lombok.NoArgsConstructor; | ||
| 7 | - | ||
| 8 | -/** | ||
| 9 | - * MQTT RPC 响应 | ||
| 10 | - */ | ||
| 11 | -@Data | ||
| 12 | -@Builder | ||
| 13 | -@NoArgsConstructor | ||
| 14 | -@AllArgsConstructor | ||
| 15 | -public class RpcResponse { | ||
| 16 | - | ||
| 17 | - /** | ||
| 18 | - * 关联 ID | ||
| 19 | - */ | ||
| 20 | - private String correlationId; | ||
| 21 | - | ||
| 22 | - /** | ||
| 23 | - * 结果 | ||
| 24 | - */ | ||
| 25 | - // TODO @haohao:object 对象会不会不好反序列化? | ||
| 26 | - private Object result; | ||
| 27 | - | ||
| 28 | - /** | ||
| 29 | - * 错误 | ||
| 30 | - */ | ||
| 31 | - private String error; | ||
| 32 | - | ||
| 33 | -} |
yudao-module-iot/yudao-module-iot-api/src/main/java/cn/iocoder/yudao/module/iot/mqttrpc/common/SerializationUtils.java deleted
| 1 | -package cn.iocoder.yudao.module.iot.mqttrpc.common; | ||
| 2 | - | ||
| 3 | -import cn.hutool.json.JSONUtil; | ||
| 4 | - | ||
| 5 | -/** | ||
| 6 | - * 序列化工具类 | ||
| 7 | - * | ||
| 8 | - */ | ||
| 9 | -public class SerializationUtils { | ||
| 10 | - | ||
| 11 | - public static String serialize(Object obj) { | ||
| 12 | - return JSONUtil.toJsonStr(obj); | ||
| 13 | - } | ||
| 14 | - | ||
| 15 | - public static <T> T deserialize(String json, Class<T> clazz) { | ||
| 16 | - return JSONUtil.toBean(json, clazz); | ||
| 17 | - } | ||
| 18 | - | ||
| 19 | -} | ||
| 20 | \ No newline at end of file | 0 | \ No newline at end of file |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/PluginStart.java
| 1 | package cn.iocoder.yudao.module.iot.framework.plugin; | 1 | package cn.iocoder.yudao.module.iot.framework.plugin; |
| 2 | 2 | ||
| 3 | -import java.util.List; | ||
| 4 | - | ||
| 5 | -import javax.annotation.Resource; | ||
| 6 | - | 3 | +import cn.hutool.core.collection.CollUtil; |
| 4 | +import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; | ||
| 5 | +import cn.iocoder.yudao.module.iot.dal.dataobject.plugininfo.PluginInfoDO; | ||
| 6 | +import cn.iocoder.yudao.module.iot.enums.plugin.IotPluginStatusEnum; | ||
| 7 | +import cn.iocoder.yudao.module.iot.service.plugin.PluginInfoService; | ||
| 8 | +import lombok.extern.slf4j.Slf4j; | ||
| 7 | import org.pf4j.spring.SpringPluginManager; | 9 | import org.pf4j.spring.SpringPluginManager; |
| 8 | import org.springframework.boot.ApplicationArguments; | 10 | import org.springframework.boot.ApplicationArguments; |
| 9 | import org.springframework.boot.ApplicationRunner; | 11 | import org.springframework.boot.ApplicationRunner; |
| 10 | import org.springframework.stereotype.Component; | 12 | import org.springframework.stereotype.Component; |
| 11 | 13 | ||
| 12 | -import lombok.extern.slf4j.Slf4j; | ||
| 13 | - | ||
| 14 | -import cn.iocoder.yudao.module.iot.service.plugin.PluginInfoService; | ||
| 15 | -import cn.hutool.core.collection.CollUtil; | ||
| 16 | -import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; | ||
| 17 | -import cn.iocoder.yudao.module.iot.dal.dataobject.plugininfo.PluginInfoDO; | ||
| 18 | -import cn.iocoder.yudao.module.iot.enums.plugin.IotPluginStatusEnum; | 14 | +import javax.annotation.Resource; |
| 15 | +import java.util.List; | ||
| 19 | 16 | ||
| 17 | +// TODO @芋艿:需要 review 下 | ||
| 20 | @Component | 18 | @Component |
| 21 | @Slf4j | 19 | @Slf4j |
| 22 | public class PluginStart implements ApplicationRunner { | 20 | public class PluginStart implements ApplicationRunner { |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/UnifiedConfiguration.java
| 1 | package cn.iocoder.yudao.module.iot.framework.plugin; | 1 | package cn.iocoder.yudao.module.iot.framework.plugin; |
| 2 | 2 | ||
| 3 | -import cn.iocoder.yudao.module.iot.api.ServiceRegistry; | ||
| 4 | -import cn.iocoder.yudao.module.iot.api.device.DeviceDataApi; | ||
| 5 | import cn.iocoder.yudao.module.iot.framework.plugin.listener.CustomPluginStateListener; | 3 | import cn.iocoder.yudao.module.iot.framework.plugin.listener.CustomPluginStateListener; |
| 6 | import lombok.extern.slf4j.Slf4j; | 4 | import lombok.extern.slf4j.Slf4j; |
| 7 | import org.pf4j.spring.SpringPluginManager; | 5 | import org.pf4j.spring.SpringPluginManager; |
| 8 | import org.springframework.beans.factory.annotation.Value; | 6 | import org.springframework.beans.factory.annotation.Value; |
| 9 | import org.springframework.context.annotation.Bean; | 7 | import org.springframework.context.annotation.Bean; |
| 10 | import org.springframework.context.annotation.Configuration; | 8 | import org.springframework.context.annotation.Configuration; |
| 11 | -import org.springframework.context.annotation.DependsOn; | ||
| 12 | 9 | ||
| 13 | -import javax.annotation.Resource; | ||
| 14 | import java.nio.file.Paths; | 10 | import java.nio.file.Paths; |
| 15 | 11 | ||
| 12 | +// TODO @芋艿:需要 review 下 | ||
| 16 | @Slf4j | 13 | @Slf4j |
| 17 | @Configuration | 14 | @Configuration |
| 18 | public class UnifiedConfiguration { | 15 | public class UnifiedConfiguration { |
| 19 | 16 | ||
| 20 | - private static final String SERVICE_REGISTRY_INITIALIZED_MARKER = "serviceRegistryInitializedMarker"; | ||
| 21 | - | ||
| 22 | - @Resource | ||
| 23 | - private DeviceDataApi deviceDataApi; | ||
| 24 | @Value("${pf4j.pluginsDir:pluginsDir}") | 17 | @Value("${pf4j.pluginsDir:pluginsDir}") |
| 25 | private String pluginsDir; | 18 | private String pluginsDir; |
| 26 | 19 | ||
| 27 | - @Bean(SERVICE_REGISTRY_INITIALIZED_MARKER) | ||
| 28 | - public Object serviceRegistryInitializedMarker() { | ||
| 29 | - ServiceRegistry.registerService(DeviceDataApi.class, deviceDataApi); | ||
| 30 | - log.info("[init][将 DeviceDataApi 实例注册到 ServiceRegistry 中]"); | ||
| 31 | - return new Object(); | ||
| 32 | - } | ||
| 33 | - | ||
| 34 | @Bean | 20 | @Bean |
| 35 | - @DependsOn(SERVICE_REGISTRY_INITIALIZED_MARKER) | 21 | +// @DependsOn("deviceDataApiImpl") |
| 36 | public SpringPluginManager pluginManager() { | 22 | public SpringPluginManager pluginManager() { |
| 37 | log.info("[init][实例化 SpringPluginManager]"); | 23 | log.info("[init][实例化 SpringPluginManager]"); |
| 38 | SpringPluginManager springPluginManager = new SpringPluginManager(Paths.get(pluginsDir)) { | 24 | SpringPluginManager springPluginManager = new SpringPluginManager(Paths.get(pluginsDir)) { |
| 25 | +// SpringPluginManager springPluginManager = new SpringPluginManager() { | ||
| 26 | + | ||
| 39 | @Override | 27 | @Override |
| 40 | public void startPlugins() { | 28 | public void startPlugins() { |
| 41 | // 禁用插件启动,避免插件启动时,启动所有插件 | 29 | // 禁用插件启动,避免插件启动时,启动所有插件 |
| 42 | log.info("[init][禁用默认启动所有插件]"); | 30 | log.info("[init][禁用默认启动所有插件]"); |
| 43 | } | 31 | } |
| 32 | + | ||
| 44 | }; | 33 | }; |
| 45 | springPluginManager.addPluginStateListener(new CustomPluginStateListener()); | 34 | springPluginManager.addPluginStateListener(new CustomPluginStateListener()); |
| 46 | return springPluginManager; | 35 | return springPluginManager; |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/listener/CustomPluginStateListener.java
| @@ -5,6 +5,7 @@ import org.pf4j.PluginStateEvent; | @@ -5,6 +5,7 @@ import org.pf4j.PluginStateEvent; | ||
| 5 | import org.pf4j.PluginStateListener; | 5 | import org.pf4j.PluginStateListener; |
| 6 | import org.springframework.stereotype.Component; | 6 | import org.springframework.stereotype.Component; |
| 7 | 7 | ||
| 8 | +// TODO @芋艿:需要 review 下 | ||
| 8 | @Component | 9 | @Component |
| 9 | @Slf4j | 10 | @Slf4j |
| 10 | public class CustomPluginStateListener implements PluginStateListener { | 11 | public class CustomPluginStateListener implements PluginStateListener { |
yudao-module-iot/yudao-module-iot-plugin/yudao-module-iot-http-plugin/src/main/java/cn/iocoder/yudao/module/iot/plugin/HttpVertxPlugin.java
| 1 | package cn.iocoder.yudao.module.iot.plugin; | 1 | package cn.iocoder.yudao.module.iot.plugin; |
| 2 | 2 | ||
| 3 | -import cn.iocoder.yudao.module.iot.api.ServiceRegistry; | 3 | +import cn.hutool.extra.spring.SpringUtil; |
| 4 | import cn.iocoder.yudao.module.iot.api.device.DeviceDataApi; | 4 | import cn.iocoder.yudao.module.iot.api.device.DeviceDataApi; |
| 5 | import io.vertx.core.Vertx; | 5 | import io.vertx.core.Vertx; |
| 6 | import io.vertx.ext.web.Router; | 6 | import io.vertx.ext.web.Router; |
| 7 | import io.vertx.ext.web.handler.BodyHandler; | 7 | import io.vertx.ext.web.handler.BodyHandler; |
| 8 | +import lombok.extern.slf4j.Slf4j; | ||
| 8 | import org.pf4j.PluginWrapper; | 9 | import org.pf4j.PluginWrapper; |
| 9 | import org.pf4j.spring.SpringPlugin; | 10 | import org.pf4j.spring.SpringPlugin; |
| 10 | import org.springframework.context.ApplicationContext; | 11 | import org.springframework.context.ApplicationContext; |
| 11 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; | 12 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 12 | 13 | ||
| 13 | -import lombok.extern.slf4j.Slf4j; | ||
| 14 | - | ||
| 15 | @Slf4j | 14 | @Slf4j |
| 16 | public class HttpVertxPlugin extends SpringPlugin { | 15 | public class HttpVertxPlugin extends SpringPlugin { |
| 17 | 16 | ||
| 18 | private static final int PORT = 8092; | 17 | private static final int PORT = 8092; |
| 19 | private Vertx vertx; | 18 | private Vertx vertx; |
| 19 | + | ||
| 20 | private DeviceDataApi deviceDataApi; | 20 | private DeviceDataApi deviceDataApi; |
| 21 | 21 | ||
| 22 | public HttpVertxPlugin(PluginWrapper wrapper) { | 22 | public HttpVertxPlugin(PluginWrapper wrapper) { |
| @@ -28,7 +28,7 @@ public class HttpVertxPlugin extends SpringPlugin { | @@ -28,7 +28,7 @@ public class HttpVertxPlugin extends SpringPlugin { | ||
| 28 | log.info("HttpVertxPlugin.start()"); | 28 | log.info("HttpVertxPlugin.start()"); |
| 29 | 29 | ||
| 30 | // 获取 DeviceDataApi 实例 | 30 | // 获取 DeviceDataApi 实例 |
| 31 | - deviceDataApi = ServiceRegistry.getService(DeviceDataApi.class); | 31 | + deviceDataApi = SpringUtil.getBean(DeviceDataApi.class); |
| 32 | if (deviceDataApi == null) { | 32 | if (deviceDataApi == null) { |
| 33 | log.error("未能从 ServiceRegistry 获取 DeviceDataApi 实例,请确保主程序已正确注册!"); | 33 | log.error("未能从 ServiceRegistry 获取 DeviceDataApi 实例,请确保主程序已正确注册!"); |
| 34 | return; | 34 | return; |