From addd19f4afdb50cbc1ac7916bb311a812a721679 Mon Sep 17 00:00:00 2001 From: LinJinYi Date: Tue, 1 Mar 2022 17:10:49 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E4=BD=BF=E7=94=A8core-starter=E5=BA=93=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E5=8D=95=E4=B8=AA=E5=BC=95=E5=85=A5apollo=E6=88=96nac?= =?UTF-8?q?os=E5=BA=93=EF=BC=8C=E5=8F=A6=E5=A4=96=E5=BA=93=E4=B8=8D?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E5=AF=BC=E8=87=B4=E7=B1=BB=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ynamicThreadPoolCoreAutoConfiguration.java | 13 +++----- .../refresher/ApolloRefresherHandler.java | 21 ++++++------ .../refresher/NacosCloudRefresherHandler.java | 32 ++++++++++--------- .../refresher/NacosRefresherHandler.java | 30 +++++++++-------- 4 files changed, 48 insertions(+), 48 deletions(-) diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/config/DynamicThreadPoolCoreAutoConfiguration.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/config/DynamicThreadPoolCoreAutoConfiguration.java index 8bca08ff..1d43563f 100644 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/config/DynamicThreadPoolCoreAutoConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/config/DynamicThreadPoolCoreAutoConfiguration.java @@ -48,7 +48,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) @@ -100,22 +100,19 @@ public class DynamicThreadPoolCoreAutoConfiguration { @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) { diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/ApolloRefresherHandler.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/ApolloRefresherHandler.java index 2f19c927..2be05d80 100644 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/ApolloRefresherHandler.java +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/ApolloRefresherHandler.java @@ -7,7 +7,6 @@ import com.ctrip.framework.apollo.ConfigChangeListener; 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; @@ -18,7 +17,7 @@ import org.springframework.beans.factory.annotation.Value; * @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}"; @@ -30,20 +29,20 @@ public class ApolloRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh 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); } diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/NacosCloudRefresherHandler.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/NacosCloudRefresherHandler.java index 48a5badf..8e2e6c9c 100644 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/NacosCloudRefresherHandler.java +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/NacosCloudRefresherHandler.java @@ -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; @@ -17,31 +18,32 @@ import java.util.concurrent.Executor; * @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 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); + } + }); } - } diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/NacosRefresherHandler.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/NacosRefresherHandler.java index 323fd7de..d685e022 100644 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/NacosRefresherHandler.java +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/NacosRefresherHandler.java @@ -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; @@ -17,31 +18,32 @@ import java.util.concurrent.Executor; * @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 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); + } + }); } } From 0ba99d3ab78bf53dc0cec6535cb96ab715cdb394 Mon Sep 17 00:00:00 2001 From: LinJinYi Date: Tue, 1 Mar 2022 17:16:00 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B8=85=E6=A5=9A=E6=97=A0=E6=95=88?= =?UTF-8?q?=E5=BC=95=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/DynamicThreadPoolCoreAutoConfiguration.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/config/DynamicThreadPoolCoreAutoConfiguration.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/config/DynamicThreadPoolCoreAutoConfiguration.java index 1d43563f..448adbb6 100644 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/config/DynamicThreadPoolCoreAutoConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/config/DynamicThreadPoolCoreAutoConfiguration.java @@ -12,17 +12,13 @@ import cn.hippo4j.common.notify.platform.WeChatSendMessageHandler; 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;