修复客户端使用core-starter库时,单个引入apollo或nacos库,另外库不存在导致类加载失败问题

pull/123/head
LinJinYi 3 years ago
parent 018554ee07
commit addd19f4af

@ -48,7 +48,7 @@ public class DynamicThreadPoolCoreAutoConfiguration {
private static final String NACOS_CONFIG_KEY = "com.alibaba.nacos.api.config"; private static final String NACOS_CONFIG_KEY = "com.alibaba.nacos.api.config";
private static final String APOLLO_CONFIG_KEY = "com.ctrip.framework.apollo.ConfigService.class"; private static final String APOLLO_CONFIG_KEY = "com.ctrip.framework.apollo.ConfigService";
@Bean @Bean
@Order(Ordered.HIGHEST_PRECEDENCE) @Order(Ordered.HIGHEST_PRECEDENCE)
@ -100,22 +100,19 @@ public class DynamicThreadPoolCoreAutoConfiguration {
@Bean @Bean
@ConditionalOnClass(name = NACOS_CONFIG_KEY) @ConditionalOnClass(name = NACOS_CONFIG_KEY)
@ConditionalOnMissingClass(NACOS_CONFIG_MANAGER_KEY) @ConditionalOnMissingClass(NACOS_CONFIG_MANAGER_KEY)
public NacosRefresherHandler nacosRefresherHandler(ConfigService configService, public NacosRefresherHandler nacosRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
BootstrapCoreProperties bootstrapCoreProperties) { BootstrapCoreProperties bootstrapCoreProperties) {
return new NacosRefresherHandler(configService, threadPoolNotifyAlarmHandler, bootstrapCoreProperties); return new NacosRefresherHandler(threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
} }
@Bean @Bean
@ConditionalOnClass(name = NACOS_CONFIG_MANAGER_KEY) @ConditionalOnClass(name = NACOS_CONFIG_MANAGER_KEY)
public NacosCloudRefresherHandler nacosCloudRefresherHandler(NacosConfigManager nacosConfigManager, public NacosCloudRefresherHandler nacosCloudRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
BootstrapCoreProperties bootstrapCoreProperties) { BootstrapCoreProperties bootstrapCoreProperties) {
return new NacosCloudRefresherHandler(nacosConfigManager, threadPoolNotifyAlarmHandler, bootstrapCoreProperties); return new NacosCloudRefresherHandler(threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
} }
@Bean @Bean
@ConditionalOnMissingBean
@ConditionalOnClass(name = APOLLO_CONFIG_KEY) @ConditionalOnClass(name = APOLLO_CONFIG_KEY)
public ApolloRefresherHandler apolloRefresher(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler, public ApolloRefresherHandler apolloRefresher(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
BootstrapCoreProperties bootstrapCoreProperties) { BootstrapCoreProperties bootstrapCoreProperties) {

@ -7,7 +7,6 @@ import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.ConfigFile; import com.ctrip.framework.apollo.ConfigFile;
import com.ctrip.framework.apollo.ConfigService; import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat; import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@ -18,7 +17,7 @@ import org.springframework.beans.factory.annotation.Value;
* @description: * @description:
*/ */
@Slf4j @Slf4j
public class ApolloRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh implements ConfigChangeListener, InitializingBean { public class ApolloRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh implements InitializingBean {
private static final String APOLLO_PROPERTY = "${apollo.bootstrap.namespaces:application}"; private static final String APOLLO_PROPERTY = "${apollo.bootstrap.namespaces:application}";
@ -30,20 +29,20 @@ public class ApolloRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh
super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties); super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
} }
@Override
public void onChange(ConfigChangeEvent configChangeEvent) {
ConfigFile configFile = ConfigService.getConfigFile(namespace,
ConfigFileFormat.fromString(bootstrapCoreProperties.getConfigFileType().getValue()));
String configInfo = configFile.getContent();
dynamicRefresh(configInfo);
}
@Override @Override
public void afterPropertiesSet() { public void afterPropertiesSet() {
String[] apolloNamespaces = this.namespace.split(","); String[] apolloNamespaces = this.namespace.split(",");
this.namespace = apolloNamespaces[0]; this.namespace = apolloNamespaces[0];
Config config = ConfigService.getConfig(namespace); Config config = ConfigService.getConfig(namespace);
config.addChangeListener(this);
ConfigChangeListener configChangeListener = configChangeEvent -> {
ConfigFile configFile = ConfigService.getConfigFile(namespace,
ConfigFileFormat.fromString(bootstrapCoreProperties.getConfigFileType().getValue()));
String configInfo = configFile.getContent();
dynamicRefresh(configInfo);
};
config.addChangeListener(configChangeListener);
log.info("dynamic-thread-pool refresher, add apollo listener success, namespace: {}", namespace); log.info("dynamic-thread-pool refresher, add apollo listener success, namespace: {}", namespace);
} }

@ -1,5 +1,6 @@
package cn.hippo4j.core.starter.refresher; package cn.hippo4j.core.starter.refresher;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler; import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler;
import cn.hippo4j.core.starter.config.BootstrapCoreProperties; import cn.hippo4j.core.starter.config.BootstrapCoreProperties;
import com.alibaba.cloud.nacos.NacosConfigManager; import com.alibaba.cloud.nacos.NacosConfigManager;
@ -17,31 +18,32 @@ import java.util.concurrent.Executor;
* @date 2022/2/26 11:21 * @date 2022/2/26 11:21
*/ */
@Slf4j @Slf4j
public class NacosCloudRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh implements InitializingBean, Listener { public class NacosCloudRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh implements InitializingBean {
private final NacosConfigManager nacosConfigManager; private final NacosConfigManager nacosConfigManager;
public NacosCloudRefresherHandler(NacosConfigManager nacosConfigManager, public NacosCloudRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
BootstrapCoreProperties bootstrapCoreProperties) { BootstrapCoreProperties bootstrapCoreProperties) {
super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties); super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
this.nacosConfigManager = nacosConfigManager; nacosConfigManager = ApplicationContextHolder.getBean(
NacosConfigManager.class);
} }
@Override @Override
public void afterPropertiesSet() throws Exception { public void afterPropertiesSet() throws Exception {
Map<String, String> nacosConfig = bootstrapCoreProperties.getNacos(); Map<String, String> nacosConfig = bootstrapCoreProperties.getNacos();
nacosConfigManager.getConfigService().addListener(nacosConfig.get("data-id"), nacosConfig.get("group"), this);
}
@Override
public Executor getExecutor() {
return dynamicRefreshExecutorService;
}
@Override nacosConfigManager.getConfigService().addListener(nacosConfig.get("data-id"),
public void receiveConfigInfo(String configInfo) { nacosConfig.get("group"), new Listener() {
dynamicRefresh(configInfo); @Override
public Executor getExecutor() {
return dynamicRefreshExecutorService;
}
@Override
public void receiveConfigInfo(String configInfo) {
dynamicRefresh(configInfo);
}
});
} }
} }

@ -1,5 +1,6 @@
package cn.hippo4j.core.starter.refresher; package cn.hippo4j.core.starter.refresher;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler; import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler;
import cn.hippo4j.core.starter.config.BootstrapCoreProperties; import cn.hippo4j.core.starter.config.BootstrapCoreProperties;
import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.ConfigService;
@ -17,31 +18,32 @@ import java.util.concurrent.Executor;
* @date 2022/2/26 00:10 * @date 2022/2/26 00:10
*/ */
@Slf4j @Slf4j
public class NacosRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh implements InitializingBean, Listener { public class NacosRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh implements InitializingBean {
private final ConfigService configService; private final ConfigService configService;
public NacosRefresherHandler(ConfigService configService, public NacosRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
BootstrapCoreProperties bootstrapCoreProperties) { BootstrapCoreProperties bootstrapCoreProperties) {
super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties); super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
this.configService = configService; configService = ApplicationContextHolder.getBean(ConfigService.class);
} }
@Override @Override
public void afterPropertiesSet() throws Exception { public void afterPropertiesSet() throws Exception {
Map<String, String> nacosConfig = bootstrapCoreProperties.getNacos(); Map<String, String> nacosConfig = bootstrapCoreProperties.getNacos();
configService.addListener(nacosConfig.get("data-id"), nacosConfig.get("group"), this);
}
@Override configService.addListener(nacosConfig.get("data-id"), nacosConfig.get("group"),
public Executor getExecutor() { new Listener() {
return dynamicRefreshExecutorService; @Override
} public Executor getExecutor() {
return dynamicRefreshExecutorService;
@Override }
public void receiveConfigInfo(String configInfo) {
dynamicRefresh(configInfo); @Override
public void receiveConfigInfo(String configInfo) {
dynamicRefresh(configInfo);
}
});
} }
} }

Loading…
Cancel
Save