From dfc7f0eab146c4ac2a43e4012fe1dcb6635430b3 Mon Sep 17 00:00:00 2001 From: Serenity <1360359624@qq.com> Date: Mon, 28 Feb 2022 22:56:20 +0800 Subject: [PATCH] =?UTF-8?q?[code]=E5=AE=8C=E6=88=90nacos=E9=80=82=E9=85=8D?= =?UTF-8?q?=E8=A7=A3=E6=9E=90properties=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ynamicThreadPoolCoreAutoConfiguration.java | 31 ++++++++++--- .../AbstractCoreThreadPoolDynamicRefresh.java | 5 ++- .../refresher/ConfigParserHandler.java | 44 ------------------- .../refresher/NacosCloudRefresherHandler.java | 5 ++- .../refresher/NacosRefresherHandler.java | 5 ++- .../refresher/config/ConfigParser.java | 18 ++++++++ .../config/impl/PropConfigParser.java | 37 ++++++++++++++++ .../config/impl/YmlConfigParser.java | 32 ++++++++++++++ 8 files changed, 120 insertions(+), 57 deletions(-) delete mode 100644 hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/ConfigParserHandler.java create mode 100644 hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/config/ConfigParser.java create mode 100644 hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/config/impl/PropConfigParser.java create mode 100644 hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/config/impl/YmlConfigParser.java 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..dc5aba1d 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,12 +12,15 @@ 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.ConfigParserHandler; import cn.hippo4j.core.starter.refresher.NacosCloudRefresherHandler; import cn.hippo4j.core.starter.refresher.NacosRefresherHandler; +import cn.hippo4j.core.starter.refresher.config.ConfigParser; +import cn.hippo4j.core.starter.refresher.config.impl.PropConfigParser; +import cn.hippo4j.core.starter.refresher.config.impl.YmlConfigParser; import cn.hippo4j.core.starter.support.DynamicThreadPoolPostProcessor; import com.alibaba.cloud.nacos.NacosConfigManager; import com.alibaba.nacos.api.config.ConfigService; +import com.google.common.collect.Lists; import lombok.AllArgsConstructor; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -28,6 +31,8 @@ import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; +import java.util.List; + /** * Dynamic thread pool auto configuration. * @@ -46,6 +51,9 @@ public class DynamicThreadPoolCoreAutoConfiguration { private static final String NACOS_CONFIG_KEY = "com.alibaba.nacos.api.config"; + private final List yamlList = Lists.newArrayList("yaml", "yml"); + private final List propList = Lists.newArrayList("properties"); + @Bean @Order(Ordered.HIGHEST_PRECEDENCE) public ApplicationContextHolder hippo4JApplicationContextHolder() { @@ -98,23 +106,32 @@ public class DynamicThreadPoolCoreAutoConfiguration { @ConditionalOnMissingClass(NACOS_CONFIG_MANAGER_KEY) public NacosRefresherHandler nacosRefresherHandler(ConfigService configService, ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler, - ConfigParserHandler configParserHandler, + ConfigParser configParser, BootstrapCoreProperties bootstrapCoreProperties) { - return new NacosRefresherHandler(configService, threadPoolNotifyAlarmHandler, configParserHandler, bootstrapCoreProperties); + return new NacosRefresherHandler(configService, threadPoolNotifyAlarmHandler, configParser, bootstrapCoreProperties); } @Bean @ConditionalOnClass(name = NACOS_CONFIG_MANAGER_KEY) public NacosCloudRefresherHandler nacosCloudRefresherHandler(NacosConfigManager nacosConfigManager, ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler, - ConfigParserHandler configParserHandler, + ConfigParser configParser, BootstrapCoreProperties bootstrapCoreProperties) { - return new NacosCloudRefresherHandler(nacosConfigManager, threadPoolNotifyAlarmHandler, configParserHandler, bootstrapCoreProperties); + return new NacosCloudRefresherHandler(nacosConfigManager, threadPoolNotifyAlarmHandler, configParser, bootstrapCoreProperties); } @Bean - public ConfigParserHandler configParserHandler() { - return new ConfigParserHandler(); + public ConfigParser configParserHandler() { + // return new ConfigParserHandler(); + String configFileType = bootstrapCoreProperties.getConfigFileType(); + if (yamlList.contains(configFileType)) { + return new YmlConfigParser(); + } + if (propList.contains(configFileType)) { + return new PropConfigParser(); + } + + throw new UnsupportedOperationException("暂不支持的配置文件类型: " + configFileType); } } 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..4267c261 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.refresher.config.ConfigParser; import cn.hippo4j.core.starter.support.GlobalCoreThreadPoolManage; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -40,7 +41,7 @@ public abstract class AbstractCoreThreadPoolDynamicRefresh implements ThreadPool private final ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler; - private final ConfigParserHandler configParserHandler; + private final ConfigParser configParser; protected final BootstrapCoreProperties bootstrapCoreProperties; @@ -51,7 +52,7 @@ public abstract class AbstractCoreThreadPoolDynamicRefresh implements ThreadPool @Override public void dynamicRefresh(String content) { - Map configInfo = configParserHandler.parseConfig(content, bootstrapCoreProperties.getConfigFileType()); + Map configInfo = configParser.parseConfig(content); 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/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..954fce04 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 @@ -2,6 +2,7 @@ package cn.hippo4j.core.starter.refresher; import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler; import cn.hippo4j.core.starter.config.BootstrapCoreProperties; +import cn.hippo4j.core.starter.refresher.config.ConfigParser; import com.alibaba.cloud.nacos.NacosConfigManager; import com.alibaba.nacos.api.config.listener.Listener; import lombok.extern.slf4j.Slf4j; @@ -23,9 +24,9 @@ public class NacosCloudRefresherHandler extends AbstractCoreThreadPoolDynamicRef public NacosCloudRefresherHandler(NacosConfigManager nacosConfigManager, ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler, - ConfigParserHandler configParserHandler, + ConfigParser configParser, BootstrapCoreProperties bootstrapCoreProperties) { - super(threadPoolNotifyAlarmHandler, configParserHandler, bootstrapCoreProperties); + super(threadPoolNotifyAlarmHandler, configParser, 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..37262ebb 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 @@ -2,6 +2,7 @@ package cn.hippo4j.core.starter.refresher; import cn.hippo4j.core.executor.ThreadPoolNotifyAlarmHandler; import cn.hippo4j.core.starter.config.BootstrapCoreProperties; +import cn.hippo4j.core.starter.refresher.config.ConfigParser; import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.listener.Listener; import lombok.extern.slf4j.Slf4j; @@ -23,9 +24,9 @@ public class NacosRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh public NacosRefresherHandler(ConfigService configService, ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler, - ConfigParserHandler configParserHandler, + ConfigParser configParser, BootstrapCoreProperties bootstrapCoreProperties) { - super(threadPoolNotifyAlarmHandler, configParserHandler, bootstrapCoreProperties); + super(threadPoolNotifyAlarmHandler, configParser, bootstrapCoreProperties); this.configService = configService; } diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/config/ConfigParser.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/config/ConfigParser.java new file mode 100644 index 00000000..80fb2514 --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/config/ConfigParser.java @@ -0,0 +1,18 @@ +package cn.hippo4j.core.starter.refresher.config; + +import java.util.Map; + +/** + * 配置序列化接口 + * + * @author serenity SerenitySir@outlook.com + * @since 2022/2/28 + */ +public interface ConfigParser { + /** + * 对配置进行序列化 + * @param content 配置的字符串形式 + * @return 序列化后的k,v + */ + Map parseConfig(String content); +} diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/config/impl/PropConfigParser.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/config/impl/PropConfigParser.java new file mode 100644 index 00000000..8f10b759 --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/config/impl/PropConfigParser.java @@ -0,0 +1,37 @@ +package cn.hippo4j.core.starter.refresher.config.impl; + +import cn.hippo4j.core.starter.refresher.config.ConfigParser; +import com.google.common.collect.Maps; +import lombok.SneakyThrows; +import org.springframework.util.StringUtils; + +import java.io.StringReader; +import java.util.Map; +import java.util.Properties; + +/** + * Properties序列化配置 + * + * @author serenity SerenitySir@outlook.com + * @since 2022/2/28 + */ +public class PropConfigParser implements ConfigParser { + + @SneakyThrows + @Override + public Map parseConfig(String content) { + if (!StringUtils.hasText(content)){ + return Maps.newHashMap(); + } + + Properties properties = new Properties(); + properties.load(new StringReader(content)); + return properties; + } + + public static void main(String[] args) { + ConfigParser propConfigParser = new PropConfigParser(); + Map map = propConfigParser.parseConfig("db.aa=11\ndb.bb=22"); + System.out.println(map.toString()); + } +} diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/config/impl/YmlConfigParser.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/config/impl/YmlConfigParser.java new file mode 100644 index 00000000..af7a7ad4 --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/config/impl/YmlConfigParser.java @@ -0,0 +1,32 @@ +package cn.hippo4j.core.starter.refresher.config.impl; + +import cn.hippo4j.common.toolkit.StringUtil; +import cn.hippo4j.core.starter.refresher.config.ConfigParser; +import com.google.common.collect.Maps; +import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; +import org.springframework.core.io.ByteArrayResource; + +import java.util.Map; + +/** + * Yml序列化配置 + * + * @author serenity SerenitySir@outlook.com + * @since 2022/2/28 + */ +public class YmlConfigParser implements ConfigParser { + + @Override + public Map parseConfig(String content) { + Map resultMap = Maps.newHashMap(); + if (StringUtil.isBlank(content)) { + return resultMap; + } + + YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean(); + yamlPropertiesFactoryBean.setResources(new ByteArrayResource(content.getBytes())); + resultMap = yamlPropertiesFactoryBean.getObject(); + + return resultMap; + } +}