refactor(测试跟踪): 平台插件加载优化
This commit is contained in:
parent
4cd9c0ab5d
commit
576662b71e
|
@ -18,14 +18,15 @@ public class PlatformPluginListener {
|
||||||
@Resource
|
@Resource
|
||||||
private PlatformPluginService platformPluginService;
|
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) {
|
public void handlePluginAdd(ConsumerRecord<?, String> record) {
|
||||||
String pluginId = record.value();
|
String pluginId = record.value();
|
||||||
LogUtil.info("system setting service consume platform_plugin add message, plugin id: " + pluginId);
|
LogUtil.info("system setting service consume platform_plugin add message, plugin id: " + pluginId);
|
||||||
platformPluginService.loadPlugin(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) {
|
public void handlePluginDelete(ConsumerRecord<?, String> record) {
|
||||||
String pluginId = record.value();
|
String pluginId = record.value();
|
||||||
LogUtil.info("system setting consume platform_plugin delete message, plugin id: " + pluginId);
|
LogUtil.info("system setting consume platform_plugin delete message, plugin id: " + pluginId);
|
||||||
|
|
|
@ -48,15 +48,19 @@ public class PlatformPluginService {
|
||||||
|
|
||||||
private PlatformPluginManager pluginManager;
|
private PlatformPluginManager pluginManager;
|
||||||
|
|
||||||
public PluginWithBLOBs addPlatformPlugin(MultipartFile file) {
|
public synchronized PlatformPluginManager getPluginManager() {
|
||||||
if (pluginManager != null) {
|
if (pluginManager == null) {
|
||||||
pluginManager = new PlatformPluginManager();
|
pluginManager = new PlatformPluginManager();
|
||||||
}
|
}
|
||||||
|
return pluginManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PluginWithBLOBs addPlatformPlugin(MultipartFile file) {
|
||||||
String id = UUID.randomUUID().toString();
|
String id = UUID.randomUUID().toString();
|
||||||
|
|
||||||
PluginManagerUtil.uploadPlugin(id, file);
|
PluginManagerUtil.uploadPlugin(id, file);
|
||||||
PluginManagerUtil.loadPlugin(id, pluginManager, file);
|
PluginManagerUtil.loadPlugin(id, getPluginManager(), file);
|
||||||
PluginMetaInfo pluginMetaInfo = pluginManager.getImplInstance(id, PluginMetaInfo.class);
|
PluginMetaInfo pluginMetaInfo = getPluginManager().getImplInstance(id, PluginMetaInfo.class);
|
||||||
|
|
||||||
Map map = JSON.parseMap(pluginMetaInfo.getFrontendMetaData());
|
Map map = JSON.parseMap(pluginMetaInfo.getFrontendMetaData());
|
||||||
map.put("id", id);
|
map.put("id", id);
|
||||||
|
@ -86,13 +90,12 @@ public class PlatformPluginService {
|
||||||
* 查询所有平台插件并加载
|
* 查询所有平台插件并加载
|
||||||
*/
|
*/
|
||||||
public void loadPlatFormPlugins() {
|
public void loadPlatFormPlugins() {
|
||||||
pluginManager = new PlatformPluginManager();
|
|
||||||
List<PluginWithBLOBs> plugins = basePluginService.getPlugins(PluginScenario.platform.name());
|
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) {
|
public void getPluginResource(String pluginId, String name, HttpServletResponse response) {
|
||||||
InputStream inputStream = pluginManager.getClassLoader(pluginId)
|
InputStream inputStream = getPluginManager().getClassLoader(pluginId)
|
||||||
.getResourceAsStream(name);
|
.getResourceAsStream(name);
|
||||||
getImage(inputStream, response);
|
getImage(inputStream, response);
|
||||||
}
|
}
|
||||||
|
@ -137,7 +140,7 @@ public class PlatformPluginService {
|
||||||
PlatformRequest platformRequest = new PlatformRequest();
|
PlatformRequest platformRequest = new PlatformRequest();
|
||||||
platformRequest.setIntegrationConfig(serviceIntegration.getConfiguration());
|
platformRequest.setIntegrationConfig(serviceIntegration.getConfiguration());
|
||||||
|
|
||||||
Platform platform = pluginManager.getPlatformByKey(request.getPlatform(), platformRequest);
|
Platform platform = getPluginManager().getPlatformByKey(request.getPlatform(), platformRequest);
|
||||||
GetOptionRequest getOptionRequest = new GetOptionRequest();
|
GetOptionRequest getOptionRequest = new GetOptionRequest();
|
||||||
getOptionRequest.setOptionMethod(request.getOptionMethod());
|
getOptionRequest.setOptionMethod(request.getOptionMethod());
|
||||||
getOptionRequest.setProjectConfig(request.getProjectConfig());
|
getOptionRequest.setProjectConfig(request.getProjectConfig());
|
||||||
|
@ -177,7 +180,7 @@ public class PlatformPluginService {
|
||||||
pluginMapper.deleteByPrimaryKey(id);
|
pluginMapper.deleteByPrimaryKey(id);
|
||||||
try {
|
try {
|
||||||
// 删除文件
|
// 删除文件
|
||||||
pluginManager.getClassLoader(id).getStorageStrategy().delete();
|
getPluginManager().getClassLoader(id).getStorageStrategy().delete();
|
||||||
kafkaTemplate.send(KafkaTopicConstants.PLATFORM_PLUGIN_DELETED, id);
|
kafkaTemplate.send(KafkaTopicConstants.PLATFORM_PLUGIN_DELETED, id);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LogUtil.error(e);
|
LogUtil.error(e);
|
||||||
|
@ -188,13 +191,13 @@ public class PlatformPluginService {
|
||||||
public Platform getPlatFormInstance(String pluginId, Map IntegrationConfig) {
|
public Platform getPlatFormInstance(String pluginId, Map IntegrationConfig) {
|
||||||
PlatformRequest request = new PlatformRequest();
|
PlatformRequest request = new PlatformRequest();
|
||||||
request.setIntegrationConfig(JSON.toJSONString(IntegrationConfig));
|
request.setIntegrationConfig(JSON.toJSONString(IntegrationConfig));
|
||||||
return pluginManager.getPlatform(pluginId, request);
|
return getPluginManager().getPlatform(pluginId, request);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Platform getPlatFormInstance(String pluginId, String integrationConfig) {
|
public Platform getPlatFormInstance(String pluginId, String integrationConfig) {
|
||||||
PlatformRequest request = new PlatformRequest();
|
PlatformRequest request = new PlatformRequest();
|
||||||
request.setIntegrationConfig(integrationConfig);
|
request.setIntegrationConfig(integrationConfig);
|
||||||
return pluginManager.getPlatform(pluginId, request);
|
return getPluginManager().getPlatform(pluginId, request);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void validateIntegration(String pluginId, Map integrationConfig) {
|
public void validateIntegration(String pluginId, Map integrationConfig) {
|
||||||
|
@ -208,7 +211,7 @@ public class PlatformPluginService {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Platform getPlatformByPluginId(String pluginId) {
|
private Platform getPlatformByPluginId(String pluginId) {
|
||||||
PluginMetaInfo pluginMetaInfo = pluginManager.getPluginMetaInfo(pluginId);
|
PluginMetaInfo pluginMetaInfo = getPluginManager().getPluginMetaInfo(pluginId);
|
||||||
IntegrationRequest integrationRequest = new IntegrationRequest();
|
IntegrationRequest integrationRequest = new IntegrationRequest();
|
||||||
integrationRequest.setPlatform(pluginMetaInfo.getKey());
|
integrationRequest.setPlatform(pluginMetaInfo.getKey());
|
||||||
integrationRequest.setWorkspaceId(SessionUtils.getCurrentWorkspaceId());
|
integrationRequest.setWorkspaceId(SessionUtils.getCurrentWorkspaceId());
|
||||||
|
@ -223,7 +226,7 @@ public class PlatformPluginService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<SelectOption> getPlatformOptions() {
|
public List<SelectOption> getPlatformOptions() {
|
||||||
List<SelectOption> options = pluginManager.getPluginMetaInfoList()
|
List<SelectOption> options = getPluginManager().getPluginMetaInfoList()
|
||||||
.stream()
|
.stream()
|
||||||
.map(pluginMetaInfo -> new SelectOption(pluginMetaInfo.getLabel(), pluginMetaInfo.getKey()))
|
.map(pluginMetaInfo -> new SelectOption(pluginMetaInfo.getLabel(), pluginMetaInfo.getKey()))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
@ -238,7 +241,7 @@ public class PlatformPluginService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<String> getThirdPartTemplateSupportPlatform() {
|
public List<String> getThirdPartTemplateSupportPlatform() {
|
||||||
List<PluginMetaInfo> pluginMetaInfoList = pluginManager.getPluginMetaInfoList();
|
List<PluginMetaInfo> pluginMetaInfoList = getPluginManager().getPluginMetaInfoList();
|
||||||
return pluginMetaInfoList.stream()
|
return pluginMetaInfoList.stream()
|
||||||
.filter(PluginMetaInfo::isThirdPartTemplateSupport)
|
.filter(PluginMetaInfo::isThirdPartTemplateSupport)
|
||||||
.map(PluginMetaInfo::getKey)
|
.map(PluginMetaInfo::getKey)
|
||||||
|
@ -246,10 +249,10 @@ public class PlatformPluginService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadPlugin(String pluginId) {
|
public void loadPlugin(String pluginId) {
|
||||||
if (pluginManager.getClassLoader(pluginId) == null) {
|
if (getPluginManager().getClassLoader(pluginId) == null) {
|
||||||
// 如果没有加载才加载
|
// 如果没有加载才加载
|
||||||
InputStream pluginJar = basePluginService.getPluginJar(pluginId);
|
InputStream pluginJar = basePluginService.getPluginJar(pluginId);
|
||||||
PluginManagerUtil.loadPlugin(pluginId, pluginManager, pluginJar);
|
PluginManagerUtil.loadPlugin(pluginId, getPluginManager(), pluginJar);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -259,6 +262,6 @@ public class PlatformPluginService {
|
||||||
* @param pluginId
|
* @param pluginId
|
||||||
*/
|
*/
|
||||||
public void unload(String pluginId) {
|
public void unload(String pluginId) {
|
||||||
pluginManager.deletePlugin(pluginId);
|
getPluginManager().deletePlugin(pluginId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 -> {
|
plugins.forEach(plugin -> {
|
||||||
String id = plugin.getId();
|
String id = plugin.getId();
|
||||||
MinioStorageStrategy minioStorageStrategy = new MinioStorageStrategy(id);
|
MinioStorageStrategy minioStorageStrategy = new MinioStorageStrategy(id);
|
||||||
InputStream inputStream = minioStorageStrategy.get(plugin.getSourceName());
|
InputStream inputStream = minioStorageStrategy.get(plugin.getSourceName());
|
||||||
try {
|
try {
|
||||||
|
if (pluginManager.getClassLoader(id) == null) {
|
||||||
pluginManager.loadJar(id, inputStream, minioStorageStrategy);
|
pluginManager.loadJar(id, inputStream, minioStorageStrategy);
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LogUtil.error("初始化插件失败:", e);
|
LogUtil.error("初始化插件失败:", e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,14 +17,15 @@ public class PlatformPluginListener {
|
||||||
@Resource
|
@Resource
|
||||||
private PlatformPluginService platformPluginService;
|
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) {
|
public void handlePluginAdd(ConsumerRecord<?, String> record) {
|
||||||
String pluginId = record.value();
|
String pluginId = record.value();
|
||||||
LogUtil.info("track service consume platform_plugin add message, plugin id: " + pluginId);
|
LogUtil.info("track service consume platform_plugin add message, plugin id: " + pluginId);
|
||||||
platformPluginService.loadPlugin(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) {
|
public void handlePluginDelete(ConsumerRecord<?, String> record) {
|
||||||
String pluginId = record.value();
|
String pluginId = record.value();
|
||||||
LogUtil.info("track service consume platform_plugin delete message, plugin id: " + pluginId);
|
LogUtil.info("track service consume platform_plugin delete message, plugin id: " + pluginId);
|
||||||
|
|
|
@ -35,20 +35,26 @@ public class PlatformPluginService {
|
||||||
|
|
||||||
private PlatformPluginManager pluginManager;
|
private PlatformPluginManager pluginManager;
|
||||||
|
|
||||||
|
public synchronized PlatformPluginManager getPluginManager() {
|
||||||
|
if (pluginManager == null) {
|
||||||
|
pluginManager = new PlatformPluginManager();
|
||||||
|
}
|
||||||
|
return pluginManager;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询所有平台插件并加载
|
* 查询所有平台插件并加载
|
||||||
*/
|
*/
|
||||||
public void loadPlatFormPlugins() {
|
public void loadPlatFormPlugins() {
|
||||||
pluginManager = new PlatformPluginManager();
|
|
||||||
List<PluginWithBLOBs> plugins = basePluginService.getPlugins(PluginScenario.platform.name());
|
List<PluginWithBLOBs> plugins = basePluginService.getPlugins(PluginScenario.platform.name());
|
||||||
PluginManagerUtil.loadPlugins(pluginManager, plugins);
|
PluginManagerUtil.loadPlugins(getPluginManager(), plugins);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void loadPlugin(String pluginId) {
|
public void loadPlugin(String pluginId) {
|
||||||
if (pluginManager.getClassLoader(pluginId) == null) {
|
if (getPluginManager().getClassLoader(pluginId) == null) {
|
||||||
// 如果没有加载才加载
|
// 如果没有加载才加载
|
||||||
InputStream pluginJar = basePluginService.getPluginJar(pluginId);
|
InputStream pluginJar = basePluginService.getPluginJar(pluginId);
|
||||||
PluginManagerUtil.loadPlugin(pluginId, pluginManager, pluginJar);
|
PluginManagerUtil.loadPlugin(pluginId, getPluginManager(), pluginJar);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,7 +63,7 @@ public class PlatformPluginService {
|
||||||
* @param pluginId
|
* @param pluginId
|
||||||
*/
|
*/
|
||||||
public void unloadPlugin(String pluginId) {
|
public void unloadPlugin(String pluginId) {
|
||||||
pluginManager.deletePlugin(pluginId);
|
getPluginManager().deletePlugin(pluginId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isThirdPartTemplateSupport(String platform) {
|
public boolean isThirdPartTemplateSupport(String platform) {
|
||||||
|
@ -76,7 +82,7 @@ public class PlatformPluginService {
|
||||||
|
|
||||||
PlatformRequest pluginRequest = new PlatformRequest();
|
PlatformRequest pluginRequest = new PlatformRequest();
|
||||||
pluginRequest.setIntegrationConfig(serviceIntegration.getConfiguration());
|
pluginRequest.setIntegrationConfig(serviceIntegration.getConfiguration());
|
||||||
return pluginManager.getPlatformByKey(platformKey, pluginRequest);
|
return getPluginManager().getPlatformByKey(platformKey, pluginRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Platform getPlatform(String platformKey) {
|
public Platform getPlatform(String platformKey) {
|
||||||
|
@ -105,7 +111,7 @@ public class PlatformPluginService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<SelectOption> getPlatformOptions() {
|
public List<SelectOption> getPlatformOptions() {
|
||||||
List<SelectOption> options = pluginManager.getPluginMetaInfoList()
|
List<SelectOption> options = getPluginManager().getPluginMetaInfoList()
|
||||||
.stream()
|
.stream()
|
||||||
.map(pluginMetaInfo -> new SelectOption(pluginMetaInfo.getLabel(), pluginMetaInfo.getKey()))
|
.map(pluginMetaInfo -> new SelectOption(pluginMetaInfo.getLabel(), pluginMetaInfo.getKey()))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
|
@ -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 -> {
|
plugins.forEach(plugin -> {
|
||||||
String id = plugin.getId();
|
String id = plugin.getId();
|
||||||
MinioStorageStrategy minioStorageStrategy = new MinioStorageStrategy(id);
|
MinioStorageStrategy minioStorageStrategy = new MinioStorageStrategy(id);
|
||||||
InputStream inputStream = minioStorageStrategy.get(plugin.getSourceName());
|
InputStream inputStream = minioStorageStrategy.get(plugin.getSourceName());
|
||||||
try {
|
try {
|
||||||
|
if (pluginManager.getClassLoader(id) == null) {
|
||||||
pluginManager.loadJar(id, inputStream, minioStorageStrategy);
|
pluginManager.loadJar(id, inputStream, minioStorageStrategy);
|
||||||
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LogUtil.error("初始化插件失败:", e);
|
LogUtil.error("初始化插件失败:", e);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue