refactor(测试跟踪): 平台插件加载优化

This commit is contained in:
chenjianxing 2022-11-21 16:58:49 +08:00 committed by jianxing
parent 4cd9c0ab5d
commit 576662b71e
6 changed files with 47 additions and 32 deletions

View File

@ -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<?, String> 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<?, String> record) {
String pluginId = record.value();
LogUtil.info("system setting consume platform_plugin delete message, plugin id: " + pluginId);

View File

@ -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<PluginWithBLOBs> 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<SelectOption> getPlatformOptions() {
List<SelectOption> options = pluginManager.getPluginMetaInfoList()
List<SelectOption> options = getPluginManager().getPluginMetaInfoList()
.stream()
.map(pluginMetaInfo -> new SelectOption(pluginMetaInfo.getLabel(), pluginMetaInfo.getKey()))
.collect(Collectors.toList());
@ -238,7 +241,7 @@ public class PlatformPluginService {
}
public List<String> getThirdPartTemplateSupportPlatform() {
List<PluginMetaInfo> pluginMetaInfoList = pluginManager.getPluginMetaInfoList();
List<PluginMetaInfo> 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);
}
}

View File

@ -70,13 +70,15 @@ public class PluginManagerUtil {
/**
* 加载插件
*/
public static void loadPlugins(PluginManager pluginManager, List<PluginWithBLOBs> plugins) {
public static synchronized void loadPlugins(PluginManager pluginManager, List<PluginWithBLOBs> 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);
}

View File

@ -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<?, String> 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<?, String> record) {
String pluginId = record.value();
LogUtil.info("track service consume platform_plugin delete message, plugin id: " + pluginId);

View File

@ -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<PluginWithBLOBs> 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<SelectOption> getPlatformOptions() {
List<SelectOption> options = pluginManager.getPluginMetaInfoList()
List<SelectOption> options = getPluginManager().getPluginMetaInfoList()
.stream()
.map(pluginMetaInfo -> new SelectOption(pluginMetaInfo.getLabel(), pluginMetaInfo.getKey()))
.collect(Collectors.toList());

View File

@ -35,13 +35,15 @@ public class PluginManagerUtil {
/**
* 加载插件
*/
public static void loadPlugins(PluginManager pluginManager, List<PluginWithBLOBs> plugins) {
public static synchronized void loadPlugins(PluginManager pluginManager, List<PluginWithBLOBs> 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);
}