diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/config/BootstrapPropertiesInterface.java b/hippo4j-core/src/main/java/cn/hippo4j/core/config/BootstrapPropertiesInterface.java index ee1bc235..e468d07d 100644 --- a/hippo4j-core/src/main/java/cn/hippo4j/core/config/BootstrapPropertiesInterface.java +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/config/BootstrapPropertiesInterface.java @@ -13,41 +13,62 @@ public interface BootstrapPropertiesInterface { * * @return */ - Boolean getEnable(); + default Boolean getEnable() { + return null; + } /** * Get username. * * @return */ - String getUsername(); + default String getUsername() { + return null; + } /** * Get password. * * @return */ - String getPassword(); + default String getPassword() { + return null; + } /** * Get namespace. * * @return */ - String getNamespace(); + default String getNamespace() { + return null; + } /** * Get item id. * * @return */ - String getItemId(); + default String getItemId() { + return null; + } /** * Get server addr. * * @return */ - String getServerAddr(); + default String getServerAddr() { + return null; + } + + /** + * Get banner. + * + * @return + */ + default Boolean getBanner() { + return null; + } } diff --git a/hippo4j-core/src/main/java/cn/hippo4j/core/enable/BeforeCheckConfiguration.java b/hippo4j-core/src/main/java/cn/hippo4j/core/enable/BeforeCheckConfiguration.java index f269ffe0..865e4593 100644 --- a/hippo4j-core/src/main/java/cn/hippo4j/core/enable/BeforeCheckConfiguration.java +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/enable/BeforeCheckConfiguration.java @@ -9,6 +9,8 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.ConfigurableEnvironment; +import java.util.Objects; + /** * Before check configuration. * @@ -19,10 +21,13 @@ import org.springframework.core.env.ConfigurableEnvironment; @AllArgsConstructor public class BeforeCheckConfiguration { + private final String bootstrapPropertiesClassName = "cn.hippo4j.starter.config.BootstrapProperties"; + @Bean public BeforeCheckConfiguration.BeforeCheck dynamicThreadPoolBeforeCheckBean(@Autowired(required = false) BootstrapPropertiesInterface properties, ConfigurableEnvironment environment) { - if (properties != null && properties.getEnable()) { + boolean checkFlag = properties != null && Objects.equals(bootstrapPropertiesClassName, properties.getClass().getName()) && properties.getEnable(); + if (checkFlag) { String username = properties.getUsername(); if (StringUtil.isBlank(username)) { throw new ConfigEmptyException( diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/DynamicThreadPoolBannerHandler.java b/hippo4j-core/src/main/java/cn/hippo4j/core/handler/DynamicThreadPoolBannerHandler.java similarity index 91% rename from hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/DynamicThreadPoolBannerHandler.java rename to hippo4j-core/src/main/java/cn/hippo4j/core/handler/DynamicThreadPoolBannerHandler.java index 818ff602..99a90fed 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/DynamicThreadPoolBannerHandler.java +++ b/hippo4j-core/src/main/java/cn/hippo4j/core/handler/DynamicThreadPoolBannerHandler.java @@ -1,6 +1,6 @@ -package cn.hippo4j.starter.handler; +package cn.hippo4j.core.handler; -import cn.hippo4j.starter.config.BootstrapProperties; +import cn.hippo4j.core.config.BootstrapPropertiesInterface; import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -20,7 +20,7 @@ import org.springframework.boot.ansi.AnsiStyle; public class DynamicThreadPoolBannerHandler implements InitializingBean { @NonNull - private final BootstrapProperties properties; + private final BootstrapPropertiesInterface properties; private final String DYNAMIC_THREAD_POOL = " :: Dynamic ThreadPool :: "; @@ -42,7 +42,7 @@ public class DynamicThreadPoolBannerHandler implements InitializingBean { "|___/ \\_, |_||_\\__,_|_|_|_|_\\__| |_| |_| \n" + " |__/ \n"; - if (properties.getEnableBanner()) { + if (properties.getBanner()) { String version = getVersion(); version = (version != null) ? " (v" + version + ")" : "no version."; 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 58f70c86..d5a37689 100644 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/pom.xml +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/pom.xml @@ -63,6 +63,11 @@ true + + org.springframework.boot + spring-boot-configuration-processor + true + 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 2076a075..9cceccb5 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.config.BootstrapPropertiesInterface; import cn.hippo4j.core.starter.parser.ConfigFileTypeEnum; import lombok.Getter; import lombok.Setter; @@ -17,19 +18,19 @@ import java.util.Map; @Getter @Setter @ConfigurationProperties(prefix = BootstrapCoreProperties.PREFIX) -public class BootstrapCoreProperties { +public class BootstrapCoreProperties implements BootstrapPropertiesInterface { public static final String PREFIX = "spring.dynamic.thread-pool"; /** * Enabled banner. */ - private Boolean enableBanner; + private Boolean banner; /*** * Enabled collect. */ - private Boolean enabledCollect; + private Boolean collect; /** * Check state interval. @@ -46,6 +47,11 @@ public class BootstrapCoreProperties { */ private Map nacos; + /** + * Apollo config. + */ + private Map apollo; + /** * Notify platforms. */ 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 index b692e6ca..d71ac82f 100644 --- 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 @@ -15,6 +15,6 @@ public interface ConfigParser { Map doParse(String content) throws IOException; - List getConfigFileTypes(); + List getConfigFileTypes(); } 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 index 88b1d7b1..be7beef1 100644 --- 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 @@ -14,7 +14,7 @@ import java.util.Map; * @date : 2022/3/1 07:57 * @description: */ -public class YamlConfigParser extends AbstractConfigParser{ +public class YamlConfigParser extends AbstractConfigParser { @Override public Map doParse(String content) { 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 4a91797a..14cc67ff 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 @@ -50,8 +50,8 @@ public abstract class AbstractCoreThreadPoolDynamicRefresh implements ThreadPool .build(); @Override - public void dynamicRefresh(String content){ - Map configInfo = null; + public void dynamicRefresh(String content) { + Map configInfo; try { configInfo = ConfigParserHandler.getInstance().parseConfig(content, bootstrapCoreProperties.getConfigFileType()); } catch (IOException e) { 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 2be05d80..fd29cb3b 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 @@ -24,8 +24,7 @@ public class ApolloRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh @Value(APOLLO_PROPERTY) private String namespace; - - public ApolloRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler , BootstrapCoreProperties bootstrapCoreProperties) { + public ApolloRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler, BootstrapCoreProperties bootstrapCoreProperties) { super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties); } @@ -36,8 +35,11 @@ public class ApolloRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh Config config = ConfigService.getConfig(namespace); ConfigChangeListener configChangeListener = configChangeEvent -> { - ConfigFile configFile = ConfigService.getConfigFile(namespace, - ConfigFileFormat.fromString(bootstrapCoreProperties.getConfigFileType().getValue())); + ConfigFile configFile = ConfigService.getConfigFile( + namespace, + ConfigFileFormat.fromString(bootstrapCoreProperties.getConfigFileType().getValue()) + ); + String configInfo = configFile.getContent(); dynamicRefresh(configInfo); }; 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 8e2e6c9c..564032f7 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 @@ -25,8 +25,7 @@ public class NacosCloudRefresherHandler extends AbstractCoreThreadPoolDynamicRef public NacosCloudRefresherHandler(ThreadPoolNotifyAlarmHandler threadPoolNotifyAlarmHandler, BootstrapCoreProperties bootstrapCoreProperties) { super(threadPoolNotifyAlarmHandler, bootstrapCoreProperties); - nacosConfigManager = ApplicationContextHolder.getBean( - NacosConfigManager.class); + nacosConfigManager = ApplicationContextHolder.getBean(NacosConfigManager.class); } @Override @@ -34,16 +33,17 @@ public class NacosCloudRefresherHandler extends AbstractCoreThreadPoolDynamicRef Map nacosConfig = bootstrapCoreProperties.getNacos(); 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); - } - }); + 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 d685e022..b93a7680 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 @@ -33,17 +33,18 @@ public class NacosRefresherHandler extends AbstractCoreThreadPoolDynamicRefresh Map nacosConfig = bootstrapCoreProperties.getNacos(); configService.addListener(nacosConfig.get("data-id"), nacosConfig.get("group"), - new Listener() { - @Override - public Executor getExecutor() { - return dynamicRefreshExecutorService; + new Listener() { + @Override + public Executor getExecutor() { + return dynamicRefreshExecutorService; + } + + @Override + public void receiveConfigInfo(String configInfo) { + dynamicRefresh(configInfo); + } } - - @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/config/ConfigParser.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/config/ConfigParser.java deleted file mode 100644 index 80fb2514..00000000 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/config/ConfigParser.java +++ /dev/null @@ -1,18 +0,0 @@ -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 deleted file mode 100644 index 8f10b759..00000000 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/config/impl/PropConfigParser.java +++ /dev/null @@ -1,37 +0,0 @@ -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 deleted file mode 100644 index af7a7ad4..00000000 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/starter/refresher/config/impl/YmlConfigParser.java +++ /dev/null @@ -1,32 +0,0 @@ -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; - } -} diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/BootstrapProperties.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/BootstrapProperties.java index 76cb7c16..bd5b5c09 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/BootstrapProperties.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/BootstrapProperties.java @@ -53,12 +53,12 @@ public class BootstrapProperties implements BootstrapPropertiesInterface { /** * Print dynamic thread pool banner */ - private Boolean enableBanner = true; + private Boolean banner = true; /** * Enable client data collect */ - private Boolean enableCollect = true; + private Boolean collect = true; /** * Task buffer container capacity diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/DynamicThreadPoolAutoConfiguration.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/DynamicThreadPoolAutoConfiguration.java index 475e5a83..4898adc8 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/DynamicThreadPoolAutoConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/DynamicThreadPoolAutoConfiguration.java @@ -15,7 +15,7 @@ import cn.hippo4j.starter.core.ThreadPoolOperation; import cn.hippo4j.core.enable.MarkerConfiguration; import cn.hippo4j.starter.event.ApplicationContentPostProcessor; import cn.hippo4j.starter.handler.BaseThreadDetailStateHandler; -import cn.hippo4j.starter.handler.DynamicThreadPoolBannerHandler; +import cn.hippo4j.core.handler.DynamicThreadPoolBannerHandler; import cn.hippo4j.starter.handler.ThreadPoolRunStateHandler; import cn.hippo4j.starter.handler.web.*; import cn.hippo4j.starter.monitor.ReportingEventExecutor; diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/monitor/ReportingEventExecutor.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/monitor/ReportingEventExecutor.java index b46895d6..cb2e4564 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/monitor/ReportingEventExecutor.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/monitor/ReportingEventExecutor.java @@ -76,7 +76,7 @@ public class ReportingEventExecutor implements Runnable, CommandLineRunner, Disp @Override public void run(String... args) { - if (properties.getEnableCollect()) { + if (properties.getCollect()) { Integer bufferSize = properties.getTaskBufferSize(); messageCollectVessel = new ArrayBlockingQueue(bufferSize);