From 576662b71ed77e66fb1584a9be0c817989737030 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Mon, 21 Nov 2022 16:58:49 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=B5=8B=E8=AF=95=E8=B7=9F=E8=B8=AA):?= =?UTF-8?q?=20=E5=B9=B3=E5=8F=B0=E6=8F=92=E4=BB=B6=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../listener/PlatformPluginListener.java | 5 ++- .../service/PlatformPluginService.java | 37 ++++++++++--------- .../metersphere/utils/PluginManagerUtil.java | 6 ++- .../listener/PlatformPluginListener.java | 5 ++- .../service/PlatformPluginService.java | 20 ++++++---- .../metersphere/utils/PluginManagerUtil.java | 6 ++- 6 files changed, 47 insertions(+), 32 deletions(-) diff --git a/system-setting/backend/src/main/java/io/metersphere/listener/PlatformPluginListener.java b/system-setting/backend/src/main/java/io/metersphere/listener/PlatformPluginListener.java index 7fd27ef32b..25b3496413 100644 --- a/system-setting/backend/src/main/java/io/metersphere/listener/PlatformPluginListener.java +++ b/system-setting/backend/src/main/java/io/metersphere/listener/PlatformPluginListener.java @@ -18,14 +18,15 @@ public class PlatformPluginListener { @Resource private PlatformPluginService platformPluginService; - @KafkaListener(id = ADD_CONSUME_ID, topics = KafkaTopicConstants.PLATFORM_PLUGIN_ADD, groupId = "${spring.application.name}") + // groupId 必须是每个实例唯一 + @KafkaListener(id = ADD_CONSUME_ID, topics = KafkaTopicConstants.PLATFORM_PLUGIN_ADD, groupId = "${eureka.instance.instance-id}") public void handlePluginAdd(ConsumerRecord record) { String pluginId = record.value(); LogUtil.info("system setting service consume platform_plugin add message, plugin id: " + pluginId); platformPluginService.loadPlugin(pluginId); } - @KafkaListener(id = DELETE_CONSUME_ID, topics = KafkaTopicConstants.PLATFORM_PLUGIN_DELETED, groupId = "${spring.application.name}") + @KafkaListener(id = DELETE_CONSUME_ID, topics = KafkaTopicConstants.PLATFORM_PLUGIN_DELETED, groupId = "${eureka.instance.instance-id}") public void handlePluginDelete(ConsumerRecord record) { String pluginId = record.value(); LogUtil.info("system setting consume platform_plugin delete message, plugin id: " + pluginId); diff --git a/system-setting/backend/src/main/java/io/metersphere/service/PlatformPluginService.java b/system-setting/backend/src/main/java/io/metersphere/service/PlatformPluginService.java index 28c91de908..5b2fa35180 100644 --- a/system-setting/backend/src/main/java/io/metersphere/service/PlatformPluginService.java +++ b/system-setting/backend/src/main/java/io/metersphere/service/PlatformPluginService.java @@ -48,15 +48,19 @@ public class PlatformPluginService { private PlatformPluginManager pluginManager; - public PluginWithBLOBs addPlatformPlugin(MultipartFile file) { - if (pluginManager != null) { + public synchronized PlatformPluginManager getPluginManager() { + if (pluginManager == null) { pluginManager = new PlatformPluginManager(); } + return pluginManager; + } + + public PluginWithBLOBs addPlatformPlugin(MultipartFile file) { String id = UUID.randomUUID().toString(); PluginManagerUtil.uploadPlugin(id, file); - PluginManagerUtil.loadPlugin(id, pluginManager, file); - PluginMetaInfo pluginMetaInfo = pluginManager.getImplInstance(id, PluginMetaInfo.class); + PluginManagerUtil.loadPlugin(id, getPluginManager(), file); + PluginMetaInfo pluginMetaInfo = getPluginManager().getImplInstance(id, PluginMetaInfo.class); Map map = JSON.parseMap(pluginMetaInfo.getFrontendMetaData()); map.put("id", id); @@ -86,13 +90,12 @@ public class PlatformPluginService { * 查询所有平台插件并加载 */ public void loadPlatFormPlugins() { - pluginManager = new PlatformPluginManager(); List plugins = basePluginService.getPlugins(PluginScenario.platform.name()); - PluginManagerUtil.loadPlugins(pluginManager, plugins); + PluginManagerUtil.loadPlugins(getPluginManager(), plugins); } public void getPluginResource(String pluginId, String name, HttpServletResponse response) { - InputStream inputStream = pluginManager.getClassLoader(pluginId) + InputStream inputStream = getPluginManager().getClassLoader(pluginId) .getResourceAsStream(name); getImage(inputStream, response); } @@ -137,7 +140,7 @@ public class PlatformPluginService { PlatformRequest platformRequest = new PlatformRequest(); platformRequest.setIntegrationConfig(serviceIntegration.getConfiguration()); - Platform platform = pluginManager.getPlatformByKey(request.getPlatform(), platformRequest); + Platform platform = getPluginManager().getPlatformByKey(request.getPlatform(), platformRequest); GetOptionRequest getOptionRequest = new GetOptionRequest(); getOptionRequest.setOptionMethod(request.getOptionMethod()); getOptionRequest.setProjectConfig(request.getProjectConfig()); @@ -177,7 +180,7 @@ public class PlatformPluginService { pluginMapper.deleteByPrimaryKey(id); try { // 删除文件 - pluginManager.getClassLoader(id).getStorageStrategy().delete(); + getPluginManager().getClassLoader(id).getStorageStrategy().delete(); kafkaTemplate.send(KafkaTopicConstants.PLATFORM_PLUGIN_DELETED, id); } catch (IOException e) { LogUtil.error(e); @@ -188,13 +191,13 @@ public class PlatformPluginService { public Platform getPlatFormInstance(String pluginId, Map IntegrationConfig) { PlatformRequest request = new PlatformRequest(); request.setIntegrationConfig(JSON.toJSONString(IntegrationConfig)); - return pluginManager.getPlatform(pluginId, request); + return getPluginManager().getPlatform(pluginId, request); } public Platform getPlatFormInstance(String pluginId, String integrationConfig) { PlatformRequest request = new PlatformRequest(); request.setIntegrationConfig(integrationConfig); - return pluginManager.getPlatform(pluginId, request); + return getPluginManager().getPlatform(pluginId, request); } public void validateIntegration(String pluginId, Map integrationConfig) { @@ -208,7 +211,7 @@ public class PlatformPluginService { } private Platform getPlatformByPluginId(String pluginId) { - PluginMetaInfo pluginMetaInfo = pluginManager.getPluginMetaInfo(pluginId); + PluginMetaInfo pluginMetaInfo = getPluginManager().getPluginMetaInfo(pluginId); IntegrationRequest integrationRequest = new IntegrationRequest(); integrationRequest.setPlatform(pluginMetaInfo.getKey()); integrationRequest.setWorkspaceId(SessionUtils.getCurrentWorkspaceId()); @@ -223,7 +226,7 @@ public class PlatformPluginService { } public List getPlatformOptions() { - List options = pluginManager.getPluginMetaInfoList() + List options = getPluginManager().getPluginMetaInfoList() .stream() .map(pluginMetaInfo -> new SelectOption(pluginMetaInfo.getLabel(), pluginMetaInfo.getKey())) .collect(Collectors.toList()); @@ -238,7 +241,7 @@ public class PlatformPluginService { } public List getThirdPartTemplateSupportPlatform() { - List pluginMetaInfoList = pluginManager.getPluginMetaInfoList(); + List pluginMetaInfoList = getPluginManager().getPluginMetaInfoList(); return pluginMetaInfoList.stream() .filter(PluginMetaInfo::isThirdPartTemplateSupport) .map(PluginMetaInfo::getKey) @@ -246,10 +249,10 @@ public class PlatformPluginService { } public void loadPlugin(String pluginId) { - if (pluginManager.getClassLoader(pluginId) == null) { + if (getPluginManager().getClassLoader(pluginId) == null) { // 如果没有加载才加载 InputStream pluginJar = basePluginService.getPluginJar(pluginId); - PluginManagerUtil.loadPlugin(pluginId, pluginManager, pluginJar); + PluginManagerUtil.loadPlugin(pluginId, getPluginManager(), pluginJar); } } @@ -259,6 +262,6 @@ public class PlatformPluginService { * @param pluginId */ public void unload(String pluginId) { - pluginManager.deletePlugin(pluginId); + getPluginManager().deletePlugin(pluginId); } } diff --git a/system-setting/backend/src/main/java/io/metersphere/utils/PluginManagerUtil.java b/system-setting/backend/src/main/java/io/metersphere/utils/PluginManagerUtil.java index 95e7d2b796..44f63179ad 100644 --- a/system-setting/backend/src/main/java/io/metersphere/utils/PluginManagerUtil.java +++ b/system-setting/backend/src/main/java/io/metersphere/utils/PluginManagerUtil.java @@ -70,13 +70,15 @@ public class PluginManagerUtil { /** * 加载插件 */ - public static void loadPlugins(PluginManager pluginManager, List plugins) { + public static synchronized void loadPlugins(PluginManager pluginManager, List plugins) { plugins.forEach(plugin -> { String id = plugin.getId(); MinioStorageStrategy minioStorageStrategy = new MinioStorageStrategy(id); InputStream inputStream = minioStorageStrategy.get(plugin.getSourceName()); try { - pluginManager.loadJar(id, inputStream, minioStorageStrategy); + if (pluginManager.getClassLoader(id) == null) { + pluginManager.loadJar(id, inputStream, minioStorageStrategy); + } } catch (IOException e) { LogUtil.error("初始化插件失败:", e); } diff --git a/test-track/backend/src/main/java/io/metersphere/listener/PlatformPluginListener.java b/test-track/backend/src/main/java/io/metersphere/listener/PlatformPluginListener.java index b5ec27452a..1a9e258fe0 100644 --- a/test-track/backend/src/main/java/io/metersphere/listener/PlatformPluginListener.java +++ b/test-track/backend/src/main/java/io/metersphere/listener/PlatformPluginListener.java @@ -17,14 +17,15 @@ public class PlatformPluginListener { @Resource private PlatformPluginService platformPluginService; - @KafkaListener(id = ADD_CONSUME_ID, topics = KafkaTopicConstants.PLATFORM_PLUGIN_ADD, groupId = "${spring.application.name}") + // groupId 必须是每个实例唯一 + @KafkaListener(id = ADD_CONSUME_ID, topics = KafkaTopicConstants.PLATFORM_PLUGIN_ADD, groupId = "${eureka.instance.instance-id}") public void handlePluginAdd(ConsumerRecord record) { String pluginId = record.value(); LogUtil.info("track service consume platform_plugin add message, plugin id: " + pluginId); platformPluginService.loadPlugin(pluginId); } - @KafkaListener(id = DELETE_CONSUME_ID, topics = KafkaTopicConstants.PLATFORM_PLUGIN_DELETED, groupId = "${spring.application.name}") + @KafkaListener(id = DELETE_CONSUME_ID, topics = KafkaTopicConstants.PLATFORM_PLUGIN_DELETED, groupId = "${eureka.instance.instance-id}") public void handlePluginDelete(ConsumerRecord record) { String pluginId = record.value(); LogUtil.info("track service consume platform_plugin delete message, plugin id: " + pluginId); diff --git a/test-track/backend/src/main/java/io/metersphere/service/PlatformPluginService.java b/test-track/backend/src/main/java/io/metersphere/service/PlatformPluginService.java index 2ff467a5a8..b1967b43d4 100644 --- a/test-track/backend/src/main/java/io/metersphere/service/PlatformPluginService.java +++ b/test-track/backend/src/main/java/io/metersphere/service/PlatformPluginService.java @@ -35,20 +35,26 @@ public class PlatformPluginService { private PlatformPluginManager pluginManager; + public synchronized PlatformPluginManager getPluginManager() { + if (pluginManager == null) { + pluginManager = new PlatformPluginManager(); + } + return pluginManager; + } + /** * 查询所有平台插件并加载 */ public void loadPlatFormPlugins() { - pluginManager = new PlatformPluginManager(); List plugins = basePluginService.getPlugins(PluginScenario.platform.name()); - PluginManagerUtil.loadPlugins(pluginManager, plugins); + PluginManagerUtil.loadPlugins(getPluginManager(), plugins); } public void loadPlugin(String pluginId) { - if (pluginManager.getClassLoader(pluginId) == null) { + if (getPluginManager().getClassLoader(pluginId) == null) { // 如果没有加载才加载 InputStream pluginJar = basePluginService.getPluginJar(pluginId); - PluginManagerUtil.loadPlugin(pluginId, pluginManager, pluginJar); + PluginManagerUtil.loadPlugin(pluginId, getPluginManager(), pluginJar); } } @@ -57,7 +63,7 @@ public class PlatformPluginService { * @param pluginId */ public void unloadPlugin(String pluginId) { - pluginManager.deletePlugin(pluginId); + getPluginManager().deletePlugin(pluginId); } public boolean isThirdPartTemplateSupport(String platform) { @@ -76,7 +82,7 @@ public class PlatformPluginService { PlatformRequest pluginRequest = new PlatformRequest(); pluginRequest.setIntegrationConfig(serviceIntegration.getConfiguration()); - return pluginManager.getPlatformByKey(platformKey, pluginRequest); + return getPluginManager().getPlatformByKey(platformKey, pluginRequest); } public Platform getPlatform(String platformKey) { @@ -105,7 +111,7 @@ public class PlatformPluginService { } public List getPlatformOptions() { - List options = pluginManager.getPluginMetaInfoList() + List options = getPluginManager().getPluginMetaInfoList() .stream() .map(pluginMetaInfo -> new SelectOption(pluginMetaInfo.getLabel(), pluginMetaInfo.getKey())) .collect(Collectors.toList()); diff --git a/test-track/backend/src/main/java/io/metersphere/utils/PluginManagerUtil.java b/test-track/backend/src/main/java/io/metersphere/utils/PluginManagerUtil.java index 729ff7971e..5006f4f087 100644 --- a/test-track/backend/src/main/java/io/metersphere/utils/PluginManagerUtil.java +++ b/test-track/backend/src/main/java/io/metersphere/utils/PluginManagerUtil.java @@ -35,13 +35,15 @@ public class PluginManagerUtil { /** * 加载插件 */ - public static void loadPlugins(PluginManager pluginManager, List plugins) { + public static synchronized void loadPlugins(PluginManager pluginManager, List plugins) { plugins.forEach(plugin -> { String id = plugin.getId(); MinioStorageStrategy minioStorageStrategy = new MinioStorageStrategy(id); InputStream inputStream = minioStorageStrategy.get(plugin.getSourceName()); try { - pluginManager.loadJar(id, inputStream, minioStorageStrategy); + if (pluginManager.getClassLoader(id) == null) { + pluginManager.loadJar(id, inputStream, minioStorageStrategy); + } } catch (IOException e) { LogUtil.error("初始化插件失败:", e); }