From 30c752bd163cd96dcdf39d9af84ffbe8666f501f Mon Sep 17 00:00:00 2001 From: "chen.ma" Date: Sun, 24 Jul 2022 23:09:24 +0800 Subject: [PATCH] Hippo4j core configuration center validation judgment reconstruction (#374) --- .../config/ConfigHandlerConfiguration.java | 91 +++++++++++++++++++ ...ynamicThreadPoolCoreAutoConfiguration.java | 50 ++-------- 2 files changed, 99 insertions(+), 42 deletions(-) create mode 100644 hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/config/ConfigHandlerConfiguration.java diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/config/ConfigHandlerConfiguration.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/config/ConfigHandlerConfiguration.java new file mode 100644 index 00000000..ab25a586 --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/config/ConfigHandlerConfiguration.java @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package cn.hippo4j.core.springboot.starter.config; + +import cn.hippo4j.core.springboot.starter.refresher.ApolloRefresherHandler; +import cn.hippo4j.core.springboot.starter.refresher.NacosCloudRefresherHandler; +import cn.hippo4j.core.springboot.starter.refresher.NacosRefresherHandler; +import cn.hippo4j.core.springboot.starter.refresher.ZookeeperRefresherHandler; +import com.alibaba.cloud.nacos.NacosConfigManager; +import com.alibaba.nacos.api.config.ConfigService; +import lombok.RequiredArgsConstructor; +import org.apache.curator.framework.CuratorFramework; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Config handler configuration. + */ +@Configuration(proxyBeanMethods = false) +public class ConfigHandlerConfiguration { + + private static final String NACOS_CONFIG_MANAGER_KEY = "com.alibaba.cloud.nacos.NacosConfigManager"; + + private static final String NACOS_DATA_ID_KEY = "nacos.data-id"; + + private static final String APOLLO_NAMESPACE_KEY = "apollo.namespace"; + + private static final String ZOOKEEPER_CONNECT_STR_KEY = "zookeeper.zk-connect-str"; + + @RequiredArgsConstructor + @ConditionalOnClass(ConfigService.class) + @ConditionalOnMissingClass(NACOS_CONFIG_MANAGER_KEY) + @ConditionalOnProperty(prefix = BootstrapCoreProperties.PREFIX, name = NACOS_DATA_ID_KEY) + static class EmbeddedNacos { + + public final BootstrapCoreProperties bootstrapCoreProperties; + + @Bean + public NacosRefresherHandler nacosRefresherHandler() { + return new NacosRefresherHandler(bootstrapCoreProperties); + } + } + + @ConditionalOnClass(NacosConfigManager.class) + @ConditionalOnProperty(prefix = BootstrapCoreProperties.PREFIX, name = NACOS_DATA_ID_KEY) + static class EmbeddedNacosCloud { + + @Bean + public NacosCloudRefresherHandler nacosCloudRefresherHandler() { + return new NacosCloudRefresherHandler(); + } + } + + @ConditionalOnClass(com.ctrip.framework.apollo.ConfigService.class) + @ConditionalOnProperty(prefix = BootstrapCoreProperties.PREFIX, name = APOLLO_NAMESPACE_KEY) + static class EmbeddedApollo { + + @Bean + public ApolloRefresherHandler apolloRefresher() { + return new ApolloRefresherHandler(); + } + } + + @ConditionalOnClass(CuratorFramework.class) + @ConditionalOnProperty(prefix = BootstrapCoreProperties.PREFIX, name = ZOOKEEPER_CONNECT_STR_KEY) + static class EmbeddedZookeeper { + + @Bean + public ZookeeperRefresherHandler zookeeperRefresher() { + return new ZookeeperRefresherHandler(); + } + } +} diff --git a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/config/DynamicThreadPoolCoreAutoConfiguration.java b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/config/DynamicThreadPoolCoreAutoConfiguration.java index ae1a4a6c..fb089ee6 100644 --- a/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/config/DynamicThreadPoolCoreAutoConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-core-spring-boot-starter/src/main/java/cn/hippo4j/core/springboot/starter/config/DynamicThreadPoolCoreAutoConfiguration.java @@ -27,10 +27,6 @@ import cn.hippo4j.core.springboot.starter.monitor.DynamicThreadPoolMonitorExecut import cn.hippo4j.core.springboot.starter.monitor.LogMonitorHandler; import cn.hippo4j.core.springboot.starter.monitor.MetricMonitorHandler; import cn.hippo4j.core.springboot.starter.notify.CoreNotifyConfigBuilder; -import cn.hippo4j.core.springboot.starter.refresher.ApolloRefresherHandler; -import cn.hippo4j.core.springboot.starter.refresher.NacosCloudRefresherHandler; -import cn.hippo4j.core.springboot.starter.refresher.NacosRefresherHandler; -import cn.hippo4j.core.springboot.starter.refresher.ZookeeperRefresherHandler; import cn.hippo4j.core.springboot.starter.refresher.event.AdapterExecutorsListener; import cn.hippo4j.core.springboot.starter.refresher.event.ExecutorsListener; import cn.hippo4j.core.springboot.starter.refresher.event.PlatformsListener; @@ -43,10 +39,13 @@ import cn.hippo4j.message.service.AlarmControlHandler; import cn.hippo4j.message.service.HippoSendMessageService; import lombok.AllArgsConstructor; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; -import org.springframework.boot.autoconfigure.condition.*; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; @@ -62,18 +61,14 @@ import org.springframework.core.annotation.Order; @EnableConfigurationProperties(BootstrapCoreProperties.class) @ImportAutoConfiguration({UtilAutoConfiguration.class, MessageConfiguration.class}) @ConditionalOnProperty(prefix = BootstrapCoreProperties.PREFIX, value = "enable", matchIfMissing = true, havingValue = "true") +@Import({ + ConfigHandlerConfiguration.EmbeddedNacos.class, ConfigHandlerConfiguration.EmbeddedNacosCloud.class, + ConfigHandlerConfiguration.EmbeddedApollo.class, ConfigHandlerConfiguration.EmbeddedZookeeper.class +}) public class DynamicThreadPoolCoreAutoConfiguration { private final BootstrapCoreProperties bootstrapCoreProperties; - private static final String NACOS_CONFIG_MANAGER_KEY = "com.alibaba.cloud.nacos.NacosConfigManager"; - - private static final String NACOS_CONFIG_KEY = "com.alibaba.nacos.api.config.ConfigService"; - - private static final String APOLLO_CONFIG_KEY = "com.ctrip.framework.apollo.ConfigService"; - - private static final String ZK_CONFIG_KEY = "org.apache.curator.framework.CuratorFramework"; - @Bean @ConditionalOnMissingBean @Order(Ordered.HIGHEST_PRECEDENCE) @@ -96,35 +91,6 @@ public class DynamicThreadPoolCoreAutoConfiguration { return new DynamicThreadPoolPostProcessor(bootstrapCoreProperties); } - @Bean - @ConditionalOnClass(name = NACOS_CONFIG_KEY) - @ConditionalOnMissingClass(NACOS_CONFIG_MANAGER_KEY) - @ConditionalOnProperty(prefix = BootstrapCoreProperties.PREFIX, name = "nacos.data-id") - public NacosRefresherHandler nacosRefresherHandler() { - return new NacosRefresherHandler(bootstrapCoreProperties); - } - - @Bean - @ConditionalOnClass(name = NACOS_CONFIG_MANAGER_KEY) - @ConditionalOnProperty(prefix = BootstrapCoreProperties.PREFIX, name = "nacos.data-id") - public NacosCloudRefresherHandler nacosCloudRefresherHandler() { - return new NacosCloudRefresherHandler(); - } - - @Bean - @ConditionalOnClass(name = APOLLO_CONFIG_KEY) - @ConditionalOnProperty(prefix = BootstrapCoreProperties.PREFIX, name = "apollo.namespace") - public ApolloRefresherHandler apolloRefresher() { - return new ApolloRefresherHandler(); - } - - @Bean - @ConditionalOnClass(name = ZK_CONFIG_KEY) - @ConditionalOnProperty(prefix = BootstrapCoreProperties.PREFIX, name = "zookeeper.zk-connect-str") - public ZookeeperRefresherHandler zookeeperRefresher() { - return new ZookeeperRefresherHandler(); - } - @Bean public DynamicThreadPoolMonitorExecutor hippo4jDynamicThreadPoolMonitorExecutor() { return new DynamicThreadPoolMonitorExecutor(bootstrapCoreProperties);