From 5e00d4593919b6ffe5e9f7751ee416b8830e4165 Mon Sep 17 00:00:00 2001 From: weihu Date: Mon, 28 Feb 2022 22:40:10 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20apollo=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E5=88=B7=E6=96=B0=E7=BA=BF=E7=A8=8B=E6=B1=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hippo4j-core-spring-boot-starter/pom.xml | 9 ++++ .../refresher/ApolloRefresherHandler.java | 49 +++++++++++++++++++ pom.xml | 1 + 3 files changed, 59 insertions(+) create mode 100644 hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/ApolloRefresherHandler.java diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/pom.xml b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/pom.xml index 54ebf34e..58f70c86 100644 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/pom.xml +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/pom.xml @@ -54,6 +54,15 @@ 2.2.5.RELEASE true + + + com.ctrip.framework.apollo + apollo-client + ${apollo.version} + compile + true + + 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 new file mode 100644 index 00000000..19d49e16 --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/ApolloRefresherHandler.java @@ -0,0 +1,49 @@ +package cn.hippo4j.core.starter.refresher; + +import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler; +import cn.hippo4j.core.starter.config.BootstrapCoreProperties; +import com.ctrip.framework.apollo.Config; +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; + +/** + * @author : wh + * @date : 2022/2/28 21:32 + * @description: + */ +@Slf4j +public class ApolloRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh implements ConfigChangeListener, InitializingBean { + + private static final String APOLLO_PROPERTY = "${apollo.bootstrap.namespaces:application}"; + + @Value(APOLLO_PROPERTY) + private String namespace; + + + public ApolloRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler, ConfigParserHandler configParserHandler, BootstrapCoreProperties bootstrapCoreProperties) { + super(threadPoolNotifyAlarmHandler, configParserHandler, bootstrapCoreProperties); + } + + @Override + public void onChange(ConfigChangeEvent configChangeEvent) { + ConfigFile configFile = ConfigService.getConfigFile(namespace, + ConfigFileFormat.fromString(bootstrapCoreProperties.getConfigFileType())); + 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); + log.info("dynamic-thread-pool refresher, add apollo listener success, namespace: {}", namespace); + } +} diff --git a/pom.xml b/pom.xml index 6fed273b..8877229d 100644 --- a/pom.xml +++ b/pom.xml @@ -47,6 +47,7 @@ 1.2.3 3.4.2 2.3.2.RELEASE + 1.9.1 false UTF-8 From cd402653f4f693c8870faaeb3c89c20e2443e6a0 Mon Sep 17 00:00:00 2001 From: weihu Date: Tue, 1 Mar 2022 07:40:23 +0800 Subject: [PATCH 2/3] add autoConfig --- .../DynamicThreadPoolCoreAutoConfiguration.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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 fd2818c9..ba5b051a 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,6 +12,7 @@ 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.refresher.ApolloRefresherHandler; import cn.hippo4j.core.starter.refresher.ConfigParserHandler; import cn.hippo4j.core.starter.refresher.NacosCloudRefresherHandler; import cn.hippo4j.core.starter.refresher.NacosRefresherHandler; @@ -21,6 +22,7 @@ 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; @@ -46,6 +48,8 @@ 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"; + @Bean @Order(Ordered.HIGHEST_PRECEDENCE) public ApplicationContextHolder hippo4JApplicationContextHolder() { @@ -112,6 +116,15 @@ public class DynamicThreadPoolCoreAutoConfiguration { return new NacosCloudRefresherHandler(nacosConfigManager, threadPoolNotifyAlarmHandler, configParserHandler, bootstrapCoreProperties); } + @Bean + @ConditionalOnMissingBean + @ConditionalOnClass(name = APOLLO_CONFIG_KEY) + public ApolloRefresherHandler apolloRefresher(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler, + ConfigParserHandler configParserHandler, + BootstrapCoreProperties bootstrapCoreProperties) { + return new ApolloRefresherHandler(threadPoolNotifyAlarmHandler, configParserHandler, bootstrapCoreProperties); + } + @Bean public ConfigParserHandler configParserHandler() { return new ConfigParserHandler(); From 100202fe07131e731796dc0b99f8f87ebd73dba3 Mon Sep 17 00:00:00 2001 From: weihu Date: Tue, 1 Mar 2022 08:16:59 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E9=87=8D=E6=9E=84configParser?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/BootstrapCoreProperties.java | 3 +- ...ynamicThreadPoolCoreAutoConfiguration.java | 15 ++---- .../starter/parser/AbstractConfigParser.java | 15 ++++++ .../starter/parser/ConfigFileTypeEnum.java | 35 ++++++++++++++ .../core/starter/parser/ConfigParser.java | 20 ++++++++ .../starter/parser/ConfigParserHandler.java | 47 +++++++++++++++++++ .../parser/PropertiesConfigParser.java | 30 ++++++++++++ .../core/starter/parser/YamlConfigParser.java | 33 +++++++++++++ .../AbstractCoreThreadPoolDynamicRefresh.java | 15 ++++-- .../refresher/ApolloRefresherHandler.java | 7 +-- .../refresher/ConfigParserHandler.java | 44 ----------------- .../refresher/NacosCloudRefresherHandler.java | 3 +- .../refresher/NacosRefresherHandler.java | 3 +- 13 files changed, 203 insertions(+), 67 deletions(-) create mode 100644 hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/AbstractConfigParser.java create mode 100644 hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/ConfigFileTypeEnum.java create mode 100644 hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/ConfigParser.java create mode 100644 hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/ConfigParserHandler.java create mode 100644 hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/PropertiesConfigParser.java create mode 100644 hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/YamlConfigParser.java delete mode 100644 hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/ConfigParserHandler.java diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/config/BootstrapCoreProperties.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/config/BootstrapCoreProperties.java index bd7233b5..2076a075 100644 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/config/BootstrapCoreProperties.java +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/config/BootstrapCoreProperties.java @@ -1,5 +1,6 @@ package cn.hippo4j.core.starter.config; +import cn.hippo4j.core.starter.parser.ConfigFileTypeEnum; import lombok.Getter; import lombok.Setter; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -38,7 +39,7 @@ public class BootstrapCoreProperties { /** * Config file type. */ - private String configFileType; + private ConfigFileTypeEnum configFileType; /** * Nacos config. 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 ba5b051a..8bca08ff 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,8 +12,8 @@ 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.ConfigParserHandler; import cn.hippo4j.core.starter.refresher.NacosCloudRefresherHandler; import cn.hippo4j.core.starter.refresher.NacosRefresherHandler; import cn.hippo4j.core.starter.support.DynamicThreadPoolPostProcessor; @@ -102,32 +102,25 @@ public class DynamicThreadPoolCoreAutoConfiguration { @ConditionalOnMissingClass(NACOS_CONFIG_MANAGER_KEY) public NacosRefresherHandler nacosRefresherHandler(ConfigService configService, ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler, - ConfigParserHandler configParserHandler, BootstrapCoreProperties bootstrapCoreProperties) { - return new NacosRefresherHandler(configService, threadPoolNotifyAlarmHandler, configParserHandler, bootstrapCoreProperties); + return new NacosRefresherHandler(configService, threadPoolNotifyAlarmHandler, bootstrapCoreProperties); } @Bean @ConditionalOnClass(name = NACOS_CONFIG_MANAGER_KEY) public NacosCloudRefresherHandler nacosCloudRefresherHandler(NacosConfigManager nacosConfigManager, ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler, - ConfigParserHandler configParserHandler, BootstrapCoreProperties bootstrapCoreProperties) { - return new NacosCloudRefresherHandler(nacosConfigManager, threadPoolNotifyAlarmHandler, configParserHandler, bootstrapCoreProperties); + return new NacosCloudRefresherHandler(nacosConfigManager, threadPoolNotifyAlarmHandler, bootstrapCoreProperties); } @Bean @ConditionalOnMissingBean @ConditionalOnClass(name = APOLLO_CONFIG_KEY) public ApolloRefresherHandler apolloRefresher(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler, - ConfigParserHandler configParserHandler, BootstrapCoreProperties bootstrapCoreProperties) { - return new ApolloRefresherHandler(threadPoolNotifyAlarmHandler, configParserHandler, bootstrapCoreProperties); + return new ApolloRefresherHandler(threadPoolNotifyAlarmHandler, bootstrapCoreProperties); } - @Bean - public ConfigParserHandler configParserHandler() { - return new ConfigParserHandler(); - } } diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/AbstractConfigParser.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/AbstractConfigParser.java new file mode 100644 index 00000000..9664243d --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/AbstractConfigParser.java @@ -0,0 +1,15 @@ +package cn.hippo4j.core.starter.parser; + +/** + * @author : wh + * @date : 2022/3/1 07:50 + * @description: + */ +public abstract class AbstractConfigParser implements ConfigParser{ + + @Override + public boolean supports(ConfigFileTypeEnum type) { + return getConfigFileTypes().contains(type); + } + +} diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/ConfigFileTypeEnum.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/ConfigFileTypeEnum.java new file mode 100644 index 00000000..cba9c46d --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/ConfigFileTypeEnum.java @@ -0,0 +1,35 @@ +package cn.hippo4j.core.starter.parser; + +import lombok.Getter; + +/** + * @author : wh + * @date : 2022/3/1 07:47 + * @description: + */ +@Getter +public enum ConfigFileTypeEnum { + + PROPERTIES("properties"), + XML("xml"), + JSON("json"), + YML("yml"), + YAML("yaml"), + TXT("txt"); + + private final String value; + + ConfigFileTypeEnum(String value) { + this.value = value; + } + + public static ConfigFileTypeEnum of(String value) { + for (ConfigFileTypeEnum typeEnum : ConfigFileTypeEnum.values()) { + if (typeEnum.value.equals(value)) { + return typeEnum; + } + } + return PROPERTIES; + } + +} diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/ConfigParser.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/ConfigParser.java new file mode 100644 index 00000000..b692e6ca --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/ConfigParser.java @@ -0,0 +1,20 @@ +package cn.hippo4j.core.starter.parser; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +/** + * @author : wh + * @date : 2022/3/1 07:47 + * @description: + */ +public interface ConfigParser { + + boolean supports(ConfigFileTypeEnum type); + + Map doParse(String content) throws IOException; + + List getConfigFileTypes(); + +} diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/ConfigParserHandler.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/ConfigParserHandler.java new file mode 100644 index 00000000..62e05226 --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/ConfigParserHandler.java @@ -0,0 +1,47 @@ +package cn.hippo4j.core.starter.parser; + +import com.google.common.collect.Lists; + +import java.io.IOException; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.ServiceLoader; + +/** + * @author : wh + * @date : 2022/3/1 08:02 + * @description: + */ +public class ConfigParserHandler { + + private static final List PARSERS = Lists.newArrayList(); + + private ConfigParserHandler() { + ServiceLoader loader = ServiceLoader.load(ConfigParser.class); + for (ConfigParser configParser : loader) { + PARSERS.add(configParser); + } + + PARSERS.add(new PropertiesConfigParser()); + PARSERS.add(new YamlConfigParser()); + } + + public Map parseConfig(String content, ConfigFileTypeEnum type) throws IOException { + for (ConfigParser parser : PARSERS) { + if (parser.supports(type)) { + return parser.doParse(content); + } + } + return Collections.emptyMap(); + } + + public static ConfigParserHandler getInstance() { + return ConfigParserHandlerHolder.INSTANCE; + } + + private static class ConfigParserHandlerHolder { + private static final ConfigParserHandler INSTANCE = new ConfigParserHandler(); + } + +} diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/PropertiesConfigParser.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/PropertiesConfigParser.java new file mode 100644 index 00000000..28de7000 --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/PropertiesConfigParser.java @@ -0,0 +1,30 @@ +package cn.hippo4j.core.starter.parser; + +import com.google.common.collect.Lists; + +import java.io.IOException; +import java.io.StringReader; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +/** + * @author : wh + * @date : 2022/3/1 07:49 + * @description: + */ +public class PropertiesConfigParser extends AbstractConfigParser{ + + + @Override + public Map doParse(String content) throws IOException { + Properties properties = new Properties(); + properties.load(new StringReader(content)); + return properties; + } + + @Override + public List getConfigFileTypes() { + return Lists.newArrayList(ConfigFileTypeEnum.PROPERTIES); + } +} diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/YamlConfigParser.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/YamlConfigParser.java new file mode 100644 index 00000000..88b1d7b1 --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/parser/YamlConfigParser.java @@ -0,0 +1,33 @@ +package cn.hippo4j.core.starter.parser; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.util.StringUtils; + +import java.util.List; +import java.util.Map; + +/** + * @author : wh + * @date : 2022/3/1 07:57 + * @description: + */ +public class YamlConfigParser extends AbstractConfigParser{ + + @Override + public Map doParse(String content) { + if (StringUtils.isEmpty(content)) { + return Maps.newHashMapWithExpectedSize(0); + } + YamlPropertiesFactoryBean bean = new YamlPropertiesFactoryBean(); + bean.setResources(new ByteArrayResource(content.getBytes())); + return bean.getObject(); + } + + @Override + public List getConfigFileTypes() { + return Lists.newArrayList(ConfigFileTypeEnum.YML, ConfigFileTypeEnum.YAML); + } +} diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/AbstractCoreThreadPoolDynamicRefresh.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/AbstractCoreThreadPoolDynamicRefresh.java index 03465998..4a91797a 100644 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/AbstractCoreThreadPoolDynamicRefresh.java +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/AbstractCoreThreadPoolDynamicRefresh.java @@ -9,6 +9,7 @@ import cn.hippo4j.core.executor.support.*; import cn.hippo4j.core.proxy.RejectedProxyUtil; import cn.hippo4j.core.starter.config.BootstrapCoreProperties; import cn.hippo4j.core.starter.config.ExecutorProperties; +import cn.hippo4j.core.starter.parser.ConfigParserHandler; import cn.hippo4j.core.starter.support.GlobalCoreThreadPoolManage; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -17,6 +18,7 @@ import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.context.properties.source.ConfigurationPropertySource; import org.springframework.boot.context.properties.source.MapConfigurationPropertySource; +import java.io.IOException; import java.util.List; import java.util.Map; import java.util.Objects; @@ -40,8 +42,6 @@ public abstract class AbstractCoreThreadPoolDynamicRefresh implements ThreadPool private final ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler; - private final ConfigParserHandler configParserHandler; - protected final BootstrapCoreProperties bootstrapCoreProperties; protected final ExecutorService dynamicRefreshExecutorService = ThreadPoolBuilder.builder() @@ -50,8 +50,15 @@ public abstract class AbstractCoreThreadPoolDynamicRefresh implements ThreadPool .build(); @Override - public void dynamicRefresh(String content) { - Map configInfo = configParserHandler.parseConfig(content, bootstrapCoreProperties.getConfigFileType()); + public void dynamicRefresh(String content){ + Map configInfo = null; + try { + configInfo = ConfigParserHandler.getInstance().parseConfig(content, bootstrapCoreProperties.getConfigFileType()); + } catch (IOException e) { + log.error("dynamic-thread-pool parse config file error, content: {}, fileType: {}", + content, bootstrapCoreProperties.getConfigFileType(), e); + return; + } ConfigurationPropertySource sources = new MapConfigurationPropertySource(configInfo); Binder binder = new Binder(sources); 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 19d49e16..2f19c927 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 @@ -26,14 +26,14 @@ public class ApolloRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh private String namespace; - public ApolloRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler, ConfigParserHandler configParserHandler, BootstrapCoreProperties bootstrapCoreProperties) { - super(threadPoolNotifyAlarmHandler, configParserHandler, bootstrapCoreProperties); + public ApolloRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler , BootstrapCoreProperties bootstrapCoreProperties) { + super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties); } @Override public void onChange(ConfigChangeEvent configChangeEvent) { ConfigFile configFile = ConfigService.getConfigFile(namespace, - ConfigFileFormat.fromString(bootstrapCoreProperties.getConfigFileType())); + ConfigFileFormat.fromString(bootstrapCoreProperties.getConfigFileType().getValue())); String configInfo = configFile.getContent(); dynamicRefresh(configInfo); } @@ -46,4 +46,5 @@ public class ApolloRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh config.addChangeListener(this); 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/ConfigParserHandler.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/ConfigParserHandler.java deleted file mode 100644 index 320579fe..00000000 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/ConfigParserHandler.java +++ /dev/null @@ -1,44 +0,0 @@ -package cn.hippo4j.core.starter.refresher; - -import cn.hippo4j.common.toolkit.StringUtil; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; -import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; -import org.springframework.core.io.ByteArrayResource; - -import java.util.List; -import java.util.Map; - -/** - * Config parser service. - * - * @author chen.ma - * @date 2022/2/26 17:33 - */ -public class ConfigParserHandler { - - private final List yamlList = Lists.newArrayList("yaml", "yml"); - - /** - * Parse config. - * - * @param content - * @param configFileType - * @return - */ - public Map parseConfig(String content, String configFileType) { - Map resultMap = Maps.newHashMap(); - if (StringUtil.isBlank(content)) { - return resultMap; - } - - if (yamlList.contains(configFileType)) { - YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean(); - yamlPropertiesFactoryBean.setResources(new ByteArrayResource(content.getBytes())); - resultMap = yamlPropertiesFactoryBean.getObject(); - } - - return resultMap; - } - -} 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 d226c632..48a5badf 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 @@ -23,9 +23,8 @@ public class NacosCloudRefresherHandler extends AbstractCoreThreadPoolDynamicRef public NacosCloudRefresherHandler(NacosConfigManager nacosConfigManager, ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler, - ConfigParserHandler configParserHandler, BootstrapCoreProperties bootstrapCoreProperties) { - super(threadPoolNotifyAlarmHandler, configParserHandler, bootstrapCoreProperties); + super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties); this.nacosConfigManager = nacosConfigManager; } 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 db4fe271..323fd7de 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 @@ -23,9 +23,8 @@ public class NacosRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh public NacosRefresherHandler(ConfigService configService, ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler, - ConfigParserHandler configParserHandler, BootstrapCoreProperties bootstrapCoreProperties) { - super(threadPoolNotifyAlarmHandler, configParserHandler, bootstrapCoreProperties); + super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties); this.configService = configService; }