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 | 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 | 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 | 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 | 9 | import org.pf4j.spring.SpringPluginManager; |
| 8 | 10 | import org.springframework.boot.ApplicationArguments; |
| 9 | 11 | import org.springframework.boot.ApplicationRunner; |
| 10 | 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 | 18 | @Component |
| 21 | 19 | @Slf4j |
| 22 | 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 | 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 | 3 | import cn.iocoder.yudao.module.iot.framework.plugin.listener.CustomPluginStateListener; |
| 6 | 4 | import lombok.extern.slf4j.Slf4j; |
| 7 | 5 | import org.pf4j.spring.SpringPluginManager; |
| 8 | 6 | import org.springframework.beans.factory.annotation.Value; |
| 9 | 7 | import org.springframework.context.annotation.Bean; |
| 10 | 8 | import org.springframework.context.annotation.Configuration; |
| 11 | -import org.springframework.context.annotation.DependsOn; | |
| 12 | 9 | |
| 13 | -import javax.annotation.Resource; | |
| 14 | 10 | import java.nio.file.Paths; |
| 15 | 11 | |
| 12 | +// TODO @芋艿:需要 review 下 | |
| 16 | 13 | @Slf4j |
| 17 | 14 | @Configuration |
| 18 | 15 | public class UnifiedConfiguration { |
| 19 | 16 | |
| 20 | - private static final String SERVICE_REGISTRY_INITIALIZED_MARKER = "serviceRegistryInitializedMarker"; | |
| 21 | - | |
| 22 | - @Resource | |
| 23 | - private DeviceDataApi deviceDataApi; | |
| 24 | 17 | @Value("${pf4j.pluginsDir:pluginsDir}") |
| 25 | 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 | 20 | @Bean |
| 35 | - @DependsOn(SERVICE_REGISTRY_INITIALIZED_MARKER) | |
| 21 | +// @DependsOn("deviceDataApiImpl") | |
| 36 | 22 | public SpringPluginManager pluginManager() { |
| 37 | 23 | log.info("[init][实例化 SpringPluginManager]"); |
| 38 | 24 | SpringPluginManager springPluginManager = new SpringPluginManager(Paths.get(pluginsDir)) { |
| 25 | +// SpringPluginManager springPluginManager = new SpringPluginManager() { | |
| 26 | + | |
| 39 | 27 | @Override |
| 40 | 28 | public void startPlugins() { |
| 41 | 29 | // 禁用插件启动,避免插件启动时,启动所有插件 |
| 42 | 30 | log.info("[init][禁用默认启动所有插件]"); |
| 43 | 31 | } |
| 32 | + | |
| 44 | 33 | }; |
| 45 | 34 | springPluginManager.addPluginStateListener(new CustomPluginStateListener()); |
| 46 | 35 | return springPluginManager; | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/framework/plugin/listener/CustomPluginStateListener.java
yudao-module-iot/yudao-module-iot-plugin/yudao-module-iot-http-plugin/src/main/java/cn/iocoder/yudao/module/iot/plugin/HttpVertxPlugin.java
| 1 | 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 | 4 | import cn.iocoder.yudao.module.iot.api.device.DeviceDataApi; |
| 5 | 5 | import io.vertx.core.Vertx; |
| 6 | 6 | import io.vertx.ext.web.Router; |
| 7 | 7 | import io.vertx.ext.web.handler.BodyHandler; |
| 8 | +import lombok.extern.slf4j.Slf4j; | |
| 8 | 9 | import org.pf4j.PluginWrapper; |
| 9 | 10 | import org.pf4j.spring.SpringPlugin; |
| 10 | 11 | import org.springframework.context.ApplicationContext; |
| 11 | 12 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 12 | 13 | |
| 13 | -import lombok.extern.slf4j.Slf4j; | |
| 14 | - | |
| 15 | 14 | @Slf4j |
| 16 | 15 | public class HttpVertxPlugin extends SpringPlugin { |
| 17 | 16 | |
| 18 | 17 | private static final int PORT = 8092; |
| 19 | 18 | private Vertx vertx; |
| 19 | + | |
| 20 | 20 | private DeviceDataApi deviceDataApi; |
| 21 | 21 | |
| 22 | 22 | public HttpVertxPlugin(PluginWrapper wrapper) { |
| ... | ... | @@ -28,7 +28,7 @@ public class HttpVertxPlugin extends SpringPlugin { |
| 28 | 28 | log.info("HttpVertxPlugin.start()"); |
| 29 | 29 | |
| 30 | 30 | // 获取 DeviceDataApi 实例 |
| 31 | - deviceDataApi = ServiceRegistry.getService(DeviceDataApi.class); | |
| 31 | + deviceDataApi = SpringUtil.getBean(DeviceDataApi.class); | |
| 32 | 32 | if (deviceDataApi == null) { |
| 33 | 33 | log.error("未能从 ServiceRegistry 获取 DeviceDataApi 实例,请确保主程序已正确注册!"); |
| 34 | 34 | return; | ... | ... |