Skip to content

Commit

Permalink
Merge pull request opengoofy#123 from voilaf/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
magestacks authored Mar 1, 2022
2 parents 4537878 + 0ba99d3 commit a0d95cb
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@
import cn.hippo4j.core.config.UtilAutoConfiguration;
import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler;
import cn.hippo4j.core.starter.notify.CoreNotifyConfigBuilder;
import cn.hippo4j.core.starter.parser.ConfigParserHandler;
import cn.hippo4j.core.starter.refresher.ApolloRefresherHandler;
import cn.hippo4j.core.starter.refresher.NacosCloudRefresherHandler;
import cn.hippo4j.core.starter.refresher.NacosRefresherHandler;
import cn.hippo4j.core.starter.support.DynamicThreadPoolPostProcessor;
import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.ConfigService;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
Expand All @@ -48,7 +44,7 @@ public class DynamicThreadPoolCoreAutoConfiguration {

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
@Order(Ordered.HIGHEST_PRECEDENCE)
Expand Down Expand Up @@ -100,22 +96,19 @@ public DynamicThreadPoolPostProcessor dynamicThreadPoolPostProcessor(Application
@Bean
@ConditionalOnClass(name = NACOS_CONFIG_KEY)
@ConditionalOnMissingClass(NACOS_CONFIG_MANAGER_KEY)
public NacosRefresherHandler nacosRefresherHandler(ConfigService configService,
ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
public NacosRefresherHandler nacosRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
BootstrapCoreProperties bootstrapCoreProperties) {
return new NacosRefresherHandler(configService, threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
return new NacosRefresherHandler(threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
}

@Bean
@ConditionalOnClass(name = NACOS_CONFIG_MANAGER_KEY)
public NacosCloudRefresherHandler nacosCloudRefresherHandler(NacosConfigManager nacosConfigManager,
ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
public NacosCloudRefresherHandler nacosCloudRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
BootstrapCoreProperties bootstrapCoreProperties) {
return new NacosCloudRefresherHandler(nacosConfigManager, threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
return new NacosCloudRefresherHandler(threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
}

@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(name = APOLLO_CONFIG_KEY)
public ApolloRefresherHandler apolloRefresher(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
BootstrapCoreProperties bootstrapCoreProperties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.ctrip.framework.apollo.ConfigFile;
import com.ctrip.framework.apollo.ConfigService;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -18,7 +17,7 @@
* @description:
*/
@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}";

Expand All @@ -30,20 +29,20 @@ public ApolloRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarm
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
public void afterPropertiesSet() {
String[] apolloNamespaces = this.namespace.split(",");
this.namespace = apolloNamespaces[0];
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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.hippo4j.core.starter.refresher;

import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler;
import cn.hippo4j.core.starter.config.BootstrapCoreProperties;
import com.alibaba.cloud.nacos.NacosConfigManager;
Expand All @@ -17,31 +18,32 @@
* @date 2022/2/26 11:21
*/
@Slf4j
public class NacosCloudRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh implements InitializingBean, Listener {
public class NacosCloudRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh implements InitializingBean {

private final NacosConfigManager nacosConfigManager;

public NacosCloudRefresherHandler(NacosConfigManager nacosConfigManager,
ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
public NacosCloudRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
BootstrapCoreProperties bootstrapCoreProperties) {
super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
this.nacosConfigManager = nacosConfigManager;
nacosConfigManager = ApplicationContextHolder.getBean(
NacosConfigManager.class);
}

@Override
public void afterPropertiesSet() throws Exception {
Map<String, String> nacosConfig = bootstrapCoreProperties.getNacos();
nacosConfigManager.getConfigService().addListener(nacosConfig.get("data-id"), nacosConfig.get("group"), this);
}

@Override
public Executor getExecutor() {
return dynamicRefreshExecutorService;
}

@Override
public void receiveConfigInfo(String configInfo) {
dynamicRefresh(configInfo);
nacosConfigManager.getConfigService().addListener(nacosConfig.get("data-id"),
nacosConfig.get("group"), new Listener() {
@Override
public Executor getExecutor() {
return dynamicRefreshExecutorService;
}

@Override
public void receiveConfigInfo(String configInfo) {
dynamicRefresh(configInfo);
}
});
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.hippo4j.core.starter.refresher;

import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler;
import cn.hippo4j.core.starter.config.BootstrapCoreProperties;
import com.alibaba.nacos.api.config.ConfigService;
Expand All @@ -17,31 +18,32 @@
* @date 2022/2/26 00:10
*/
@Slf4j
public class NacosRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh implements InitializingBean, Listener {
public class NacosRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh implements InitializingBean {

private final ConfigService configService;

public NacosRefresherHandler(ConfigService configService,
ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
public NacosRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler,
BootstrapCoreProperties bootstrapCoreProperties) {
super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties);
this.configService = configService;
configService = ApplicationContextHolder.getBean(ConfigService.class);
}

@Override
public void afterPropertiesSet() throws Exception {
Map<String, String> nacosConfig = bootstrapCoreProperties.getNacos();
configService.addListener(nacosConfig.get("data-id"), nacosConfig.get("group"), this);
}

@Override
public Executor getExecutor() {
return dynamicRefreshExecutorService;
}

@Override
public void receiveConfigInfo(String configInfo) {
dynamicRefresh(configInfo);
configService.addListener(nacosConfig.get("data-id"), nacosConfig.get("group"),
new Listener() {
@Override
public Executor getExecutor() {
return dynamicRefreshExecutorService;
}

@Override
public void receiveConfigInfo(String configInfo) {
dynamicRefresh(configInfo);
}
});
}

}

0 comments on commit a0d95cb

Please sign in to comment.