From 03ed3a438f73004388b7114bcfd9149b88189b53 Mon Sep 17 00:00:00 2001 From: provenceee <83857838+provenceee@users.noreply.github.com> Date: Wed, 20 Nov 2024 10:43:58 +0800 Subject: [PATCH] *fix spi classloader Signed-off-by: provenceee <83857838+provenceee@users.noreply.github.com> --- .../core/classloader/ClassLoaderManager.java | 19 +++++++++++++++++++ .../ClassLoaderLoadClassInterceptor.java | 1 + .../plugin/classloader/PluginClassLoader.java | 12 ++++++++++-- .../sermant/core/utils/ClassUtils.java | 3 ++- .../sermant/core/utils/ReflectUtils.java | 3 ++- .../inject/DynamicClassInjectDefine.java | 4 +++- .../SpringFactoriesInterceptor.java | 5 +++-- .../common/handler/retry/AbstractRetry.java | 4 +++- .../config/SpringFactoriesInterceptor.java | 5 +++-- ...ontrolSpringConfigurationInjectDefine.java | 4 +++- .../retry/ExtensionLoaderInterceptor.java | 3 ++- .../SpringLbChooseServerInterceptor.java | 3 ++- .../interceptor/UrlInterceptor.java | 5 +++-- .../SpringFactoriesInterceptor.java | 5 +++-- .../RibbonLoadBalancerInterceptor.java | 3 ++- .../dubbo/registry/utils/ReflectUtils.java | 3 ++- .../config/RegistrationProperties.java | 4 +++- .../inject/BaseAutoConfigurationDefine.java | 4 +++- .../inject/RibbonConfigurationDefine.java | 3 ++- .../registry/inject/ScServerInjectDefine.java | 3 ++- .../SpringFactoriesInterceptor.java | 5 +++-- .../health/EurekaHealthInterceptor.java | 3 ++- .../support/InstanceInterceptorSupport.java | 3 ++- 23 files changed, 80 insertions(+), 27 deletions(-) diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/classloader/ClassLoaderManager.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/classloader/ClassLoaderManager.java index da1f7041ea..a117461cd5 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/classloader/ClassLoaderManager.java +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/classloader/ClassLoaderManager.java @@ -45,6 +45,8 @@ public class ClassLoaderManager { private static FrameworkClassLoader frameworkClassLoader; + private static ClassLoader userClassLoader; + private ClassLoaderManager() { } @@ -87,6 +89,23 @@ public PluginClassLoader run() { }); } + public static void setUserClassLoader(ClassLoader userClassLoader) { + ClassLoaderManager.userClassLoader = userClassLoader; + } + + /** + * get ContextClassLoader or UserClassLoader + * + * @return ClassLoader + */ + public static ClassLoader getContextClassLoaderOrUserClassLoader() { + ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); + if (classLoader != null) { + return classLoader; + } + return userClassLoader; + } + public static SermantClassLoader getSermantClassLoader() { return sermantClassLoader; } diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/agent/enhance/ClassLoaderLoadClassInterceptor.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/agent/enhance/ClassLoaderLoadClassInterceptor.java index ac3de05101..2f9b450820 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/agent/enhance/ClassLoaderLoadClassInterceptor.java +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/agent/enhance/ClassLoaderLoadClassInterceptor.java @@ -47,6 +47,7 @@ public ClassLoaderLoadClassInterceptor() { @Override public ExecuteContext before(ExecuteContext context) throws Exception { + ClassLoaderManager.setUserClassLoader((ClassLoader) context.getObject()); return context; } diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/classloader/PluginClassLoader.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/classloader/PluginClassLoader.java index 97f81bace7..78bb5a9abd 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/classloader/PluginClassLoader.java +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/plugin/classloader/PluginClassLoader.java @@ -16,6 +16,7 @@ package com.huaweicloud.sermant.core.plugin.classloader; +import com.huaweicloud.sermant.core.classloader.ClassLoaderManager; import com.huaweicloud.sermant.core.common.LoggerFactory; import com.huaweicloud.sermant.core.config.ConfigManager; import com.huaweicloud.sermant.core.plugin.agent.config.AgentConfig; @@ -112,9 +113,16 @@ public Class loadClass(String name, boolean resolve) throws ClassNotFoundExce // 无法从Sermant搜索路径中找到类,则尝试通过线程绑定的局部类加载器加载 if (clazz == null) { ClassLoader loader = localLoader.get(Thread.currentThread().getId()); - + if (loader == null) { + LOGGER.log(Level.FINE, "localLoader is null, thread name is {0}, classs name is {1}.", + new Object[]{Thread.currentThread().getName(), name}); + } if (loader == null && useContextLoader) { - loader = Thread.currentThread().getContextClassLoader(); + loader = ClassLoaderManager.getContextClassLoaderOrUserClassLoader(); + if (loader == null) { + LOGGER.log(Level.WARNING, "contextClassLoader is null, thread name is {0}, classs name is {1}.", + new Object[]{Thread.currentThread().getName(), name}); + } } // 确保局部类加载器不是当前类加载器,否则会stackoverflow diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/ClassUtils.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/ClassUtils.java index 8d61a451eb..68ce69546b 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/ClassUtils.java +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/ClassUtils.java @@ -17,6 +17,7 @@ package com.huaweicloud.sermant.core.utils; +import com.huaweicloud.sermant.core.classloader.ClassLoaderManager; import com.huaweicloud.sermant.core.common.LoggerFactory; import java.io.IOException; @@ -111,7 +112,7 @@ public static Optional> loadClass(String className, ClassLoader classLo public static Optional createInstance(String className, ClassLoader classLoader, Class[] paramTypes) { ClassLoader curClassLoader = classLoader; if (curClassLoader == null) { - curClassLoader = Thread.currentThread().getContextClassLoader(); + curClassLoader = ClassLoaderManager.getContextClassLoaderOrUserClassLoader(); } try { final Class clazz = curClassLoader.loadClass(className); diff --git a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/ReflectUtils.java b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/ReflectUtils.java index 2a6cb215eb..ee742acefd 100644 --- a/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/ReflectUtils.java +++ b/sermant-agentcore/sermant-agentcore-core/src/main/java/com/huaweicloud/sermant/core/utils/ReflectUtils.java @@ -17,6 +17,7 @@ package com.huaweicloud.sermant.core.utils; +import com.huaweicloud.sermant.core.classloader.ClassLoaderManager; import com.huaweicloud.sermant.core.common.LoggerFactory; import java.lang.reflect.AccessibleObject; @@ -166,7 +167,7 @@ private static Optional> loadClass(String className) { return Optional.empty(); } return CLASS_CACHE.computeIfAbsent(className, value -> { - final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + final ClassLoader contextClassLoader = ClassLoaderManager.getContextClassLoaderOrUserClassLoader(); try { return Optional.ofNullable(contextClassLoader.loadClass(className)); } catch (ClassNotFoundException ignored) { diff --git a/sermant-plugins/sermant-dynamic-config/dynamic-config-plugin/src/main/java/com/huawei/dynamic/config/inject/DynamicClassInjectDefine.java b/sermant-plugins/sermant-dynamic-config/dynamic-config-plugin/src/main/java/com/huawei/dynamic/config/inject/DynamicClassInjectDefine.java index 8dd1079c0a..48e300e776 100644 --- a/sermant-plugins/sermant-dynamic-config/dynamic-config-plugin/src/main/java/com/huawei/dynamic/config/inject/DynamicClassInjectDefine.java +++ b/sermant-plugins/sermant-dynamic-config/dynamic-config-plugin/src/main/java/com/huawei/dynamic/config/inject/DynamicClassInjectDefine.java @@ -19,6 +19,7 @@ import com.huawei.dynamic.config.DynamicConfiguration; +import com.huaweicloud.sermant.core.classloader.ClassLoaderManager; import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; import com.huaweicloud.sermant.core.service.inject.ClassInjectDefine; import com.huaweicloud.sermant.core.utils.ClassUtils; @@ -43,6 +44,7 @@ public Plugin plugin() { @Override public boolean canInject() { return PluginConfigManager.getPluginConfig(DynamicConfiguration.class).isEnableDynamicConfig() - && ClassUtils.loadClass(REFRESH_CLASS, Thread.currentThread().getContextClassLoader()).isPresent(); + && ClassUtils.loadClass(REFRESH_CLASS, ClassLoaderManager.getContextClassLoaderOrUserClassLoader()) + .isPresent(); } } diff --git a/sermant-plugins/sermant-dynamic-config/dynamic-config-plugin/src/main/java/com/huawei/dynamic/config/interceptors/SpringFactoriesInterceptor.java b/sermant-plugins/sermant-dynamic-config/dynamic-config-plugin/src/main/java/com/huawei/dynamic/config/interceptors/SpringFactoriesInterceptor.java index 8ccafc208e..54bcd636c4 100644 --- a/sermant-plugins/sermant-dynamic-config/dynamic-config-plugin/src/main/java/com/huawei/dynamic/config/interceptors/SpringFactoriesInterceptor.java +++ b/sermant-plugins/sermant-dynamic-config/dynamic-config-plugin/src/main/java/com/huawei/dynamic/config/interceptors/SpringFactoriesInterceptor.java @@ -17,6 +17,7 @@ package com.huawei.dynamic.config.interceptors; +import com.huaweicloud.sermant.core.classloader.ClassLoaderManager; import com.huaweicloud.sermant.core.common.LoggerFactory; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.service.ServiceManager; @@ -94,7 +95,7 @@ private boolean isHasMethodLoadSpringFactories() { private void injectConfigurationsWithLowVersion(Object result, String factoryName) { final ClassInjectService service = ServiceManager.getService(ClassInjectService.class); - final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + final ClassLoader contextClassLoader = ClassLoaderManager.getContextClassLoaderOrUserClassLoader(); if (result instanceof List) { final List convertedResult = (List) result; CLASS_DEFINES.forEach(classInjectDefine -> { @@ -108,7 +109,7 @@ private void injectConfigurationsWithLowVersion(Object result, String factoryNam private void injectConfigurations(Object result) { final ClassInjectService service = ServiceManager.getService(ClassInjectService.class); - final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + final ClassLoader contextClassLoader = ClassLoaderManager.getContextClassLoaderOrUserClassLoader(); final boolean isMultiValueMap = result instanceof MultiValueMap; if (result instanceof Map) { // spring 高版本处理, 针对List其为不可变list,需做一层处理 diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/handler/retry/AbstractRetry.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/handler/retry/AbstractRetry.java index 9571f52fc5..69d53b4c7a 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/handler/retry/AbstractRetry.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-common/src/main/java/com/huawei/flowcontrol/common/handler/retry/AbstractRetry.java @@ -20,6 +20,7 @@ import com.huawei.flowcontrol.common.config.FlowControlConfig; import com.huawei.flowcontrol.common.support.ReflectMethodCacheSupport; +import com.huaweicloud.sermant.core.classloader.ClassLoaderManager; import com.huaweicloud.sermant.core.common.LoggerFactory; import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; @@ -54,7 +55,8 @@ protected final Class[] findClass(String[] classNames) { final List> result = new ArrayList<>(classNames.length); for (String className : classNames) { try { - result.add(Class.forName(className, false, Thread.currentThread().getContextClassLoader())); + result.add(Class.forName(className, false, + ClassLoaderManager.getContextClassLoaderOrUserClassLoader())); } catch (ClassNotFoundException exception) { LoggerFactory.getLogger().info(String.format(Locale.ENGLISH, "Can not find retry exception class %s", className)); diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/config/SpringFactoriesInterceptor.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/config/SpringFactoriesInterceptor.java index e466556e1f..bea798a632 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/config/SpringFactoriesInterceptor.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/config/SpringFactoriesInterceptor.java @@ -17,6 +17,7 @@ package com.huawei.flowcontrol.config; +import com.huaweicloud.sermant.core.classloader.ClassLoaderManager; import com.huaweicloud.sermant.core.common.LoggerFactory; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; @@ -100,7 +101,7 @@ private boolean isHasMethodLoadSpringFactories() { private void injectConfigurationsWithLowVersion(Object result, String factoryName) { final ClassInjectService service = ServiceManager.getService(ClassInjectService.class); - final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + final ClassLoader contextClassLoader = ClassLoaderManager.getContextClassLoaderOrUserClassLoader(); if (result instanceof List) { final List convertedResult = (List) result; CLASS_DEFINES.forEach(classInjectDefine -> { @@ -114,7 +115,7 @@ private void injectConfigurationsWithLowVersion(Object result, String factoryNam private void injectConfigurations(Object result) { final ClassInjectService service = ServiceManager.getService(ClassInjectService.class); - final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + final ClassLoader contextClassLoader = ClassLoaderManager.getContextClassLoaderOrUserClassLoader(); final boolean isMultiValueMap = result instanceof MultiValueMap; if (result instanceof Map) { // spring 高版本处理, 针对List其为不可变list,需做一层处理 diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/inject/FlowControlSpringConfigurationInjectDefine.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/inject/FlowControlSpringConfigurationInjectDefine.java index 733b10d785..43be777deb 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/inject/FlowControlSpringConfigurationInjectDefine.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/inject/FlowControlSpringConfigurationInjectDefine.java @@ -17,6 +17,7 @@ package com.huawei.flowcontrol.inject; +import com.huaweicloud.sermant.core.classloader.ClassLoaderManager; import com.huaweicloud.sermant.core.service.inject.ClassInjectDefine; import com.huaweicloud.sermant.core.utils.ClassUtils; @@ -53,6 +54,7 @@ public Plugin plugin() { } private boolean isLoadedClass(String className) { - return ClassUtils.loadClass(className, Thread.currentThread().getContextClassLoader(), true).isPresent(); + return ClassUtils.loadClass(className, ClassLoaderManager.getContextClassLoaderOrUserClassLoader(), true) + .isPresent(); } } diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/retry/ExtensionLoaderInterceptor.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/retry/ExtensionLoaderInterceptor.java index 5f959ad39d..0782a54af0 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/retry/ExtensionLoaderInterceptor.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/retry/ExtensionLoaderInterceptor.java @@ -20,6 +20,7 @@ import com.huawei.flowcontrol.retry.cluster.ClusterInvokerCreator; import com.huawei.flowcontrol.service.InterceptorSupporter; +import com.huaweicloud.sermant.core.classloader.ClassLoaderManager; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.ClassUtils; @@ -67,7 +68,7 @@ protected ExecuteContext doAfter(ExecuteContext context) { return context; } final Optional> retryInvokerClass; - final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + final ClassLoader contextClassLoader = ClassLoaderManager.getContextClassLoaderOrUserClassLoader(); if (APACHE_DUBBO_CLUSTER_CLASS_NAME.equals(type.getName())) { ClassUtils.defineClass( "com.huawei.flowcontrol.retry.cluster.ApacheDubboClusterInvoker", contextClassLoader); diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/retry/SpringLbChooseServerInterceptor.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/retry/SpringLbChooseServerInterceptor.java index 27239e1a01..d2a5f09bba 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/retry/SpringLbChooseServerInterceptor.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/retry/SpringLbChooseServerInterceptor.java @@ -21,6 +21,7 @@ import com.huawei.flowcontrol.common.handler.retry.policy.RetryPolicy; import com.huawei.flowcontrol.service.InterceptorSupporter; +import com.huaweicloud.sermant.core.classloader.ClassLoaderManager; import com.huaweicloud.sermant.core.common.LoggerFactory; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.utils.ReflectUtils; @@ -98,7 +99,7 @@ private Optional buildResult(Object lastServer, String responseClassName if (defaultResponseClazz == null) { return Optional.empty(); } - final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + final ClassLoader contextClassLoader = ClassLoaderManager.getContextClassLoaderOrUserClassLoader(); try { final Class clazz = contextClassLoader.loadClass(defaultResponseClazz); final Constructor declaredConstructor = clazz.getDeclaredConstructor(ServiceInstance.class); diff --git a/sermant-plugins/sermant-loadbalancer/dubbo-loadbalancer-plugin/src/main/java/com/huaweicloud/loadbalancer/interceptor/UrlInterceptor.java b/sermant-plugins/sermant-loadbalancer/dubbo-loadbalancer-plugin/src/main/java/com/huaweicloud/loadbalancer/interceptor/UrlInterceptor.java index 7b05994960..0c7f3331a8 100644 --- a/sermant-plugins/sermant-loadbalancer/dubbo-loadbalancer-plugin/src/main/java/com/huaweicloud/loadbalancer/interceptor/UrlInterceptor.java +++ b/sermant-plugins/sermant-loadbalancer/dubbo-loadbalancer-plugin/src/main/java/com/huaweicloud/loadbalancer/interceptor/UrlInterceptor.java @@ -25,6 +25,7 @@ import com.huaweicloud.loadbalancer.constants.DubboUrlParamsConstants; import com.huaweicloud.loadbalancer.rule.LoadbalancerRule; import com.huaweicloud.loadbalancer.rule.RuleManager; +import com.huaweicloud.sermant.core.classloader.ClassLoaderManager; import com.huaweicloud.sermant.core.common.LoggerFactory; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; @@ -110,7 +111,7 @@ private void fillSupportRules(String extensionLoaderClazz, String lbClassName) { } supportRules = new HashSet<>(); final Optional> lbClazz = ClassUtils - .loadClass(lbClassName, Thread.currentThread().getContextClassLoader(), true); + .loadClass(lbClassName, ClassLoaderManager.getContextClassLoaderOrUserClassLoader(), true); if (!lbClazz.isPresent()) { return; } @@ -134,7 +135,7 @@ private void fillSupportRules(String extensionLoaderClazz, String lbClassName) { } private boolean isAlibaba() { - return ClassUtils.loadClass(ALIBABA_LOADER, Thread.currentThread().getContextClassLoader(), false) + return ClassUtils.loadClass(ALIBABA_LOADER, ClassLoaderManager.getContextClassLoaderOrUserClassLoader(), false) .isPresent(); } diff --git a/sermant-plugins/sermant-loadbalancer/loadbalancer-common/src/main/java/com/huaweicloud/loadbalancer/interceptor/SpringFactoriesInterceptor.java b/sermant-plugins/sermant-loadbalancer/loadbalancer-common/src/main/java/com/huaweicloud/loadbalancer/interceptor/SpringFactoriesInterceptor.java index 127e05c075..3b41b86308 100644 --- a/sermant-plugins/sermant-loadbalancer/loadbalancer-common/src/main/java/com/huaweicloud/loadbalancer/interceptor/SpringFactoriesInterceptor.java +++ b/sermant-plugins/sermant-loadbalancer/loadbalancer-common/src/main/java/com/huaweicloud/loadbalancer/interceptor/SpringFactoriesInterceptor.java @@ -17,6 +17,7 @@ package com.huaweicloud.loadbalancer.interceptor; +import com.huaweicloud.sermant.core.classloader.ClassLoaderManager; import com.huaweicloud.sermant.core.common.LoggerFactory; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; @@ -100,7 +101,7 @@ private boolean isHasMethodLoadSpringFactories() { private void injectConfigurationsWithLowVersion(Object result, String factoryName) { final ClassInjectService service = ServiceManager.getService(ClassInjectService.class); - final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + final ClassLoader contextClassLoader = ClassLoaderManager.getContextClassLoaderOrUserClassLoader(); if (result instanceof List) { final List convertedResult = (List) result; CLASS_DEFINES.forEach(classInjectDefine -> { @@ -114,7 +115,7 @@ private void injectConfigurationsWithLowVersion(Object result, String factoryNam private void injectConfigurations(Object result) { final ClassInjectService service = ServiceManager.getService(ClassInjectService.class); - final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + final ClassLoader contextClassLoader = ClassLoaderManager.getContextClassLoaderOrUserClassLoader(); final boolean isMultiValueMap = result instanceof MultiValueMap; if (result instanceof Map) { // spring 高版本处理, 针对List其为不可变list,需做一层处理 diff --git a/sermant-plugins/sermant-loadbalancer/spring-cloud-loadbalancer-plugin/src/main/java/com/huaweicloud/loadbalancer/interceptor/RibbonLoadBalancerInterceptor.java b/sermant-plugins/sermant-loadbalancer/spring-cloud-loadbalancer-plugin/src/main/java/com/huaweicloud/loadbalancer/interceptor/RibbonLoadBalancerInterceptor.java index 9f0aa9362b..994240057e 100644 --- a/sermant-plugins/sermant-loadbalancer/spring-cloud-loadbalancer-plugin/src/main/java/com/huaweicloud/loadbalancer/interceptor/RibbonLoadBalancerInterceptor.java +++ b/sermant-plugins/sermant-loadbalancer/spring-cloud-loadbalancer-plugin/src/main/java/com/huaweicloud/loadbalancer/interceptor/RibbonLoadBalancerInterceptor.java @@ -22,6 +22,7 @@ import com.huaweicloud.loadbalancer.config.LoadbalancerConfig; import com.huaweicloud.loadbalancer.config.RibbonLoadbalancerType; import com.huaweicloud.loadbalancer.rule.RuleManager; +import com.huaweicloud.sermant.core.classloader.ClassLoaderManager; import com.huaweicloud.sermant.core.common.LoggerFactory; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; @@ -67,7 +68,7 @@ public class RibbonLoadBalancerInterceptor extends AbstractInterceptor { private final Function> ruleCreator = type -> { final String clazzName = type.getClazzName(); - final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + final ClassLoader contextClassLoader = ClassLoaderManager.getContextClassLoaderOrUserClassLoader(); try { final Class ruleClazz = contextClassLoader.loadClass(clazzName); return Optional.of((AbstractLoadBalancerRule) ruleClazz.newInstance()); diff --git a/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/main/java/com/huawei/dubbo/registry/utils/ReflectUtils.java b/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/main/java/com/huawei/dubbo/registry/utils/ReflectUtils.java index 487bac6d3d..79512924fd 100644 --- a/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/main/java/com/huawei/dubbo/registry/utils/ReflectUtils.java +++ b/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/main/java/com/huawei/dubbo/registry/utils/ReflectUtils.java @@ -19,6 +19,7 @@ import com.huawei.dubbo.registry.cache.DubboCache; import com.huawei.dubbo.registry.constants.Constant; +import com.huaweicloud.sermant.core.classloader.ClassLoaderManager; import com.huaweicloud.sermant.core.common.LoggerFactory; import com.huaweicloud.sermant.core.utils.ClassLoaderUtils; @@ -77,7 +78,7 @@ private ReflectUtils() { * @return 宿主类 */ public static Optional> defineClass(String className) { - ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + ClassLoader contextClassLoader = ClassLoaderManager.getContextClassLoaderOrUserClassLoader(); try { return Optional.of(ClassLoaderUtils.defineClass(className, contextClassLoader, ClassLoaderUtils.getClassResource(ReflectUtils.class.getClassLoader(), className))); diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/config/RegistrationProperties.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/config/RegistrationProperties.java index d21a51552b..7d8bbbfefc 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/config/RegistrationProperties.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/config/RegistrationProperties.java @@ -21,6 +21,7 @@ import com.huawei.registry.utils.CommonUtils; import com.huawei.registry.utils.HostUtils; +import com.huaweicloud.sermant.core.classloader.ClassLoaderManager; import com.huaweicloud.sermant.core.common.LoggerFactory; import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager; @@ -120,7 +121,8 @@ private Optional tryGetInetUtils() { private Optional tryGetInetUtilsByClazz() { final Optional> clazz = ClassUtils - .loadClass(INET_UTILS_CLASS, Thread.currentThread().getContextClassLoader(), false); + .loadClass(INET_UTILS_CLASS, ClassLoaderManager.getContextClassLoaderOrUserClassLoader(), + false); if (!clazz.isPresent()) { return Optional.empty(); } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/inject/BaseAutoConfigurationDefine.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/inject/BaseAutoConfigurationDefine.java index 3df5fcfe9b..373c98b80c 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/inject/BaseAutoConfigurationDefine.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/inject/BaseAutoConfigurationDefine.java @@ -19,6 +19,7 @@ import com.huawei.registry.config.RegisterConfig; +import com.huaweicloud.sermant.core.classloader.ClassLoaderManager; import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; import com.huaweicloud.sermant.core.service.inject.ClassInjectDefine; import com.huaweicloud.sermant.core.utils.ClassUtils; @@ -48,6 +49,7 @@ public Plugin plugin() { * @return 是否被加载 */ protected boolean isClassExistedOnCurrentClassLoader(String className) { - return ClassUtils.loadClass(className, Thread.currentThread().getContextClassLoader(), false).isPresent(); + return ClassUtils.loadClass(className, ClassLoaderManager.getContextClassLoaderOrUserClassLoader(), false) + .isPresent(); } } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/inject/RibbonConfigurationDefine.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/inject/RibbonConfigurationDefine.java index caf56ab9c1..743d927766 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/inject/RibbonConfigurationDefine.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/inject/RibbonConfigurationDefine.java @@ -17,6 +17,7 @@ package com.huawei.registry.inject; +import com.huaweicloud.sermant.core.classloader.ClassLoaderManager; import com.huaweicloud.sermant.core.service.inject.ClassInjectDefine; import com.huaweicloud.sermant.core.utils.ClassUtils; @@ -52,6 +53,6 @@ public ClassInjectDefine[] requiredDefines() { public boolean canInject() { return super.canInject() && ClassUtils.loadClass( "org.springframework.cloud.netflix.ribbon.SpringClientFactory", - Thread.currentThread().getContextClassLoader(), false).isPresent(); + ClassLoaderManager.getContextClassLoaderOrUserClassLoader(), false).isPresent(); } } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/inject/ScServerInjectDefine.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/inject/ScServerInjectDefine.java index 807e3f676b..93a8c0dc01 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/inject/ScServerInjectDefine.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/inject/ScServerInjectDefine.java @@ -19,6 +19,7 @@ import com.huawei.registry.config.RegisterConfig; +import com.huaweicloud.sermant.core.classloader.ClassLoaderManager; import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; import com.huaweicloud.sermant.core.service.inject.ClassInjectDefine; import com.huaweicloud.sermant.core.utils.ClassUtils; @@ -42,7 +43,7 @@ public String factoryName() { @Override public boolean canInject() { - final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + final ClassLoader contextClassLoader = ClassLoaderManager.getContextClassLoaderOrUserClassLoader(); final RegisterConfig pluginConfig = PluginConfigManager.getPluginConfig(RegisterConfig.class); return pluginConfig.isOpenMigration() && pluginConfig.isEnableSpringRegister() && ClassUtils.loadClass("com.netflix.loadbalancer.Server", contextClassLoader).isPresent(); diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/SpringFactoriesInterceptor.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/SpringFactoriesInterceptor.java index 3d45b38f97..cfe8fd13d7 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/SpringFactoriesInterceptor.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/SpringFactoriesInterceptor.java @@ -20,6 +20,7 @@ import com.huawei.registry.config.GraceConfig; import com.huawei.registry.support.RegisterSwitchSupport; +import com.huaweicloud.sermant.core.classloader.ClassLoaderManager; import com.huaweicloud.sermant.core.common.LoggerFactory; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; @@ -98,7 +99,7 @@ private boolean isHasMethodLoadSpringFactories() { private void injectConfigurationsWithLowVersion(Object result, String factoryName) { final ClassInjectService service = ServiceManager.getService(ClassInjectService.class); - final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + final ClassLoader contextClassLoader = ClassLoaderManager.getContextClassLoaderOrUserClassLoader(); if (result instanceof List) { final List convertedResult = (List) result; CLASS_DEFINES.forEach(classInjectDefine -> { @@ -112,7 +113,7 @@ private void injectConfigurationsWithLowVersion(Object result, String factoryNam private void injectConfigurations(Object result) { final ClassInjectService service = ServiceManager.getService(ClassInjectService.class); - final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + final ClassLoader contextClassLoader = ClassLoaderManager.getContextClassLoaderOrUserClassLoader(); final boolean isMultiValueMap = result instanceof MultiValueMap; if (result instanceof Map) { // spring 高版本处理, 针对List其为不可变list,需做一层处理 diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/health/EurekaHealthInterceptor.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/health/EurekaHealthInterceptor.java index c95fd8e61f..38f12dddcd 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/health/EurekaHealthInterceptor.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/health/EurekaHealthInterceptor.java @@ -19,6 +19,7 @@ import com.huawei.registry.context.RegisterContext; import com.huawei.registry.handler.SingleStateCloseHandler; +import com.huaweicloud.sermant.core.classloader.ClassLoaderManager; import com.huaweicloud.sermant.core.common.LoggerFactory; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; @@ -41,7 +42,7 @@ protected boolean needCloseRegisterCenter() { @Override protected void close() throws Exception { // 关闭Eureka定时器 - final Class discoveryClientClass = Thread.currentThread().getContextClassLoader() + final Class discoveryClientClass = ClassLoaderManager.getContextClassLoaderOrUserClassLoader() .loadClass("com.netflix.discovery.DiscoveryClient"); discoveryClientClass.getDeclaredMethod("shutdown").invoke(target); LOGGER.warning("Eureka register center has been closed by user."); diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/support/InstanceInterceptorSupport.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/support/InstanceInterceptorSupport.java index 930a7d2aae..08fd188459 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/support/InstanceInterceptorSupport.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/support/InstanceInterceptorSupport.java @@ -20,6 +20,7 @@ import com.huawei.registry.config.RegisterConfig; import com.huawei.registry.entity.MicroServiceInstance; +import com.huaweicloud.sermant.core.classloader.ClassLoaderManager; import com.huaweicloud.sermant.core.common.LoggerFactory; import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; import com.huaweicloud.sermant.core.utils.ClassLoaderUtils; @@ -74,7 +75,7 @@ private RegisterConfig getRegisterConfig() { */ protected final Class getInstanceClass(String className) { return cacheClasses.computeIfAbsent(className, fn -> { - ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); + ClassLoader contextClassLoader = ClassLoaderManager.getContextClassLoaderOrUserClassLoader(); Class result = null; try { result = ClassLoaderUtils.defineClass(className, contextClassLoader,