From 2948419b19a7ee895c2c796e4228780bdcf13889 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=89=91=E9=91=AB?= <1064730540@qq.com> Date: Wed, 12 Oct 2022 20:14:58 +0800 Subject: [PATCH] feat: support spring boot 1.5.x --- .../adapter/web/WebIpAndPortHolder.java | 14 +- hippo4j-common/pom.xml | 5 +- .../pom.xml | 1 + ...igNacosSpringBoot15ExampleApplication.java | 31 ++++ .../src/main/resources/bootstrap.properties | 1 + hippo4j-example/pom.xml | 1 + .../pom.xml | 1 + .../config/ConfigHandlerConfiguration.java | 36 +++++ ...5BootstrapConfigPropertiesBinderAdapt.java | 109 ++++++++++++++ .../CustomPropertyNamePatternsMatcher.java | 41 ++++++ .../main/resources/META-INF/spring.factories | 1 + .../config/ConfigHandlerConfiguration.java | 14 +- .../DynamicThreadPoolAutoConfiguration.java | 6 +- ...bstractConfigThreadPoolDynamicRefresh.java | 8 +- .../BootstrapConfigPropertiesBinderAdapt.java | 135 +----------------- ...tBootstrapConfigPropertiesBinderAdapt.java | 47 ++++++ .../refresher/NacosRefresherHandler.java | 8 +- hippo4j-spring-boot/pom.xml | 1 + 18 files changed, 307 insertions(+), 153 deletions(-) create mode 100644 hippo4j-example/hippo4j-config-nacos-spring-boot-1.5-starter-example/pom.xml create mode 100644 hippo4j-example/hippo4j-config-nacos-spring-boot-1.5-starter-example/src/main/java/cn/hippo4j/example/config/nacos/ConfigNacosSpringBoot15ExampleApplication.java create mode 100644 hippo4j-example/hippo4j-config-nacos-spring-boot-1.5-starter-example/src/main/resources/bootstrap.properties create mode 100644 hippo4j-spring-boot/hippo4j-config-spring-boot-1.5-starter/pom.xml create mode 100644 hippo4j-spring-boot/hippo4j-config-spring-boot-1.5-starter/src/main/java/cn/hippo4j/config/springboot15x/starter/config/ConfigHandlerConfiguration.java create mode 100644 hippo4j-spring-boot/hippo4j-config-spring-boot-1.5-starter/src/main/java/cn/hippo4j/config/springboot15x/starter/refresher/SpringBoot15BootstrapConfigPropertiesBinderAdapt.java create mode 100644 hippo4j-spring-boot/hippo4j-config-spring-boot-1.5-starter/src/main/java/org/springframework/boot/bind/CustomPropertyNamePatternsMatcher.java create mode 100644 hippo4j-spring-boot/hippo4j-config-spring-boot-1.5-starter/src/main/resources/META-INF/spring.factories create mode 100644 hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/DefaultBootstrapConfigPropertiesBinderAdapt.java diff --git a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebIpAndPortHolder.java b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebIpAndPortHolder.java index c35f90af..aad45db9 100644 --- a/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebIpAndPortHolder.java +++ b/hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/WebIpAndPortHolder.java @@ -33,6 +33,15 @@ import java.util.concurrent.atomic.AtomicReference; */ public class WebIpAndPortHolder { + private static boolean support = false; + static { + try { + Class.forName("org.springframework.boot.web.server.WebServer"); + support = true; + } catch (Exception e) { + + } + } /** * Application ip and application post */ @@ -47,6 +56,9 @@ public class WebIpAndPortHolder { } protected static void initIpAndPort() { + if (!support) { + return; + } webIpAndPort.compareAndSet(null, getWebIpAndPortInfo()); } @@ -83,7 +95,7 @@ public class WebIpAndPortHolder { */ public static boolean check(String nodes) { WebIpAndPortInfo webIpAndPort = WebIpAndPortHolder.getWebIpAndPort(); - if (StringUtil.isEmpty(nodes) || ALL.equals(nodes)) { + if (StringUtil.isEmpty(nodes) || ALL.equals(nodes) || webIpAndPort == null) { return true; } String[] splitNodes = nodes.split(SEPARATOR); diff --git a/hippo4j-common/pom.xml b/hippo4j-common/pom.xml index 5e40c3f4..deaebf44 100644 --- a/hippo4j-common/pom.xml +++ b/hippo4j-common/pom.xml @@ -49,10 +49,7 @@ org.projectlombok lombok - - org.springframework.boot - spring-boot-starter-json - + org.springframework.boot spring-boot-starter-test diff --git a/hippo4j-example/hippo4j-config-nacos-spring-boot-1.5-starter-example/pom.xml b/hippo4j-example/hippo4j-config-nacos-spring-boot-1.5-starter-example/pom.xml new file mode 100644 index 00000000..54b14221 --- /dev/null +++ b/hippo4j-example/hippo4j-config-nacos-spring-boot-1.5-starter-example/pom.xml @@ -0,0 +1 @@ + 4.0.0 cn.hippo4j hippo4j-example ${revision} hippo4j-config-nacos-spring-boot-1.5-starter-example true 1.5.22.RELEASE 1.1.4 com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config 1.5.1.RELEASE org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test cn.hippo4j hippo4j-example-core ${revision} cn.hippo4j hippo4j-config-spring-boot-1.5-starter ${revision} io.micrometer micrometer-registry-prometheus 1.1.3 io.micrometer micrometer-spring-legacy 1.1.3 org.springframework.boot spring-boot-starter-actuator \ No newline at end of file diff --git a/hippo4j-example/hippo4j-config-nacos-spring-boot-1.5-starter-example/src/main/java/cn/hippo4j/example/config/nacos/ConfigNacosSpringBoot15ExampleApplication.java b/hippo4j-example/hippo4j-config-nacos-spring-boot-1.5-starter-example/src/main/java/cn/hippo4j/example/config/nacos/ConfigNacosSpringBoot15ExampleApplication.java new file mode 100644 index 00000000..6db043c1 --- /dev/null +++ b/hippo4j-example/hippo4j-config-nacos-spring-boot-1.5-starter-example/src/main/java/cn/hippo4j/example/config/nacos/ConfigNacosSpringBoot15ExampleApplication.java @@ -0,0 +1,31 @@ +/* + * 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.example.config.nacos; + +import cn.hippo4j.core.enable.EnableDynamicThreadPool; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@EnableDynamicThreadPool +@SpringBootApplication(scanBasePackages = "cn.hippo4j.example.core") +public class ConfigNacosSpringBoot15ExampleApplication { + + public static void main(String[] args) { + SpringApplication.run(ConfigNacosSpringBoot15ExampleApplication.class, args); + } +} diff --git a/hippo4j-example/hippo4j-config-nacos-spring-boot-1.5-starter-example/src/main/resources/bootstrap.properties b/hippo4j-example/hippo4j-config-nacos-spring-boot-1.5-starter-example/src/main/resources/bootstrap.properties new file mode 100644 index 00000000..62d1b702 --- /dev/null +++ b/hippo4j-example/hippo4j-config-nacos-spring-boot-1.5-starter-example/src/main/resources/bootstrap.properties @@ -0,0 +1 @@ +debug=true server.port=8089 server.servlet.context-path=/example management.metrics.export.prometheus.enabled=true management.server.port=29999 management.endpoints.web.exposure.include=* spring.profiles.active=dev spring.application.name=dynamic-threadpool-example spring.cloud.nacos.config.password=nacos spring.cloud.nacos.config.server-addr=127.0.0.1:8848 spring.cloud.nacos.config.username=nacos spring.cloud.nacos.config.extension-configs[0].data-id=hippo4j-nacos.yaml spring.cloud.nacos.config.extension-configs[0].group=DEFAULT_GROUP spring.cloud.nacos.config.extension-configs[0].refresh=true spring.dynamic.thread-pool.enable=true spring.dynamic.thread-pool.banner=true spring.dynamic.thread-pool.collect=true spring.dynamic.thread-pool.collect-type=micrometer spring.dynamic.thread-pool.check-state-interval=5 spring.dynamic.thread-pool.notify-platforms[0].platform=WECHAT spring.dynamic.thread-pool.notify-platforms[0].secret-key=ac0426a5-c712-474c-9bff-72b8b8f5caff spring.dynamic.thread-pool.notify-platforms[1].platform=DING spring.dynamic.thread-pool.notify-platforms[1].secret-key=56417ebba6a27ca352f0de77a2ae9da66d01f39610b5ee8a6033c60ef9071c55 spring.dynamic.thread-pool.notify-platforms[2].platform=LARK spring.dynamic.thread-pool.notify-platforms[2].secret-key=2cbf2808-3839-4c26-a04d-fd201dd51f9e spring.dynamic.thread-pool.nacos.data-id=hippo4j-nacos.yaml spring.dynamic.thread-pool.nacos.group=DEFAULT_GROUP spring.dynamic.thread-pool.config-file-type=yml \ No newline at end of file diff --git a/hippo4j-example/pom.xml b/hippo4j-example/pom.xml index 93dab68b..ee233eb9 100644 --- a/hippo4j-example/pom.xml +++ b/hippo4j-example/pom.xml @@ -23,6 +23,7 @@ hippo4j-spring-boot-starter-adapter-spring-cloud-stream-rocketmq-example hippo4j-spring-boot-starter-adapter-rocketmq-example hippo4j-config-etcd-spring-boot-starter-example + hippo4j-config-nacos-spring-boot-1.5-starter-example diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-1.5-starter/pom.xml b/hippo4j-spring-boot/hippo4j-config-spring-boot-1.5-starter/pom.xml new file mode 100644 index 00000000..7ad3b051 --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-1.5-starter/pom.xml @@ -0,0 +1 @@ + cn.hippo4j hippo4j-spring-boot ${revision} 4.0.0 hippo4j-config-spring-boot-1.5-starter 8 8 1.5.22.RELEASE cn.hippo4j hippo4j-config-spring-boot-starter ${revision} \ No newline at end of file diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-1.5-starter/src/main/java/cn/hippo4j/config/springboot15x/starter/config/ConfigHandlerConfiguration.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-1.5-starter/src/main/java/cn/hippo4j/config/springboot15x/starter/config/ConfigHandlerConfiguration.java new file mode 100644 index 00000000..d8164f0f --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-1.5-starter/src/main/java/cn/hippo4j/config/springboot15x/starter/config/ConfigHandlerConfiguration.java @@ -0,0 +1,36 @@ +/* + * 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.config.springboot15x.starter.config; + +import cn.hippo4j.config.springboot.starter.refresher.BootstrapConfigPropertiesBinderAdapt; +import cn.hippo4j.config.springboot15x.starter.refresher.SpringBoot15BootstrapConfigPropertiesBinderAdapt; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.bind.RelaxedDataBinder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ConfigHandlerConfiguration { + + @Bean + @ConditionalOnClass(RelaxedDataBinder.class) + public BootstrapConfigPropertiesBinderAdapt bootstrapConfigPropertiesBinderAdapt() { + return new SpringBoot15BootstrapConfigPropertiesBinderAdapt(); + } + +} diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-1.5-starter/src/main/java/cn/hippo4j/config/springboot15x/starter/refresher/SpringBoot15BootstrapConfigPropertiesBinderAdapt.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-1.5-starter/src/main/java/cn/hippo4j/config/springboot15x/starter/refresher/SpringBoot15BootstrapConfigPropertiesBinderAdapt.java new file mode 100644 index 00000000..41e771c7 --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-1.5-starter/src/main/java/cn/hippo4j/config/springboot15x/starter/refresher/SpringBoot15BootstrapConfigPropertiesBinderAdapt.java @@ -0,0 +1,109 @@ +/* + * 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.config.springboot15x.starter.refresher; + +import cn.hippo4j.config.springboot.starter.config.BootstrapConfigProperties; +import cn.hippo4j.config.springboot.starter.refresher.BootstrapConfigPropertiesBinderAdapt; +import org.springframework.beans.BeanUtils; +import org.springframework.beans.BeansException; +import org.springframework.beans.PropertyValues; +import org.springframework.beans.support.ResourceEditorRegistrar; +import org.springframework.boot.bind.CustomPropertyNamePatternsMatcher; +import org.springframework.boot.bind.RelaxedDataBinder; +import org.springframework.boot.bind.RelaxedNames; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.MutablePropertySources; + +import java.beans.PropertyDescriptor; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Set; + +/** + * Bootstrap core properties binder adapt. + */ +public class SpringBoot15BootstrapConfigPropertiesBinderAdapt implements ApplicationContextAware, BootstrapConfigPropertiesBinderAdapt { + + private ApplicationContext applicationContext; + + /** + * Bootstrap core properties binder. + * + * @param configInfo + * @param bootstrapConfigProperties + * @return + */ + @Override + public BootstrapConfigProperties bootstrapCorePropertiesBinder(Map configInfo, BootstrapConfigProperties bootstrapConfigProperties) { + BootstrapConfigProperties bindableCoreProperties = new BootstrapConfigProperties(); + RelaxedNames relaxedNames = new RelaxedNames(BootstrapConfigProperties.PREFIX); + Set names = getNames(bindableCoreProperties, relaxedNames); + // 绑定器 + Map stringConfigInfo = new HashMap<>(configInfo.size()); + configInfo.forEach((key, value) -> stringConfigInfo.put(key.toString(), value)); + MapPropertySource test = new MapPropertySource("Hippo4j", stringConfigInfo); + MutablePropertySources propertySources = new MutablePropertySources(); + propertySources.addFirst(test); + PropertyValues propertyValues = CustomPropertyNamePatternsMatcher.getPropertySourcesPropertyValues(names, propertySources); + + RelaxedDataBinder dataBinder = new RelaxedDataBinder(bindableCoreProperties, BootstrapConfigProperties.PREFIX); + dataBinder.setAutoGrowCollectionLimit(Integer.MAX_VALUE); + dataBinder.setIgnoreNestedProperties(false); + dataBinder.setIgnoreInvalidFields(false); + dataBinder.setIgnoreUnknownFields(true); + + ResourceEditorRegistrar resourceEditorRegistrar = new ResourceEditorRegistrar(applicationContext, applicationContext.getEnvironment()); + resourceEditorRegistrar.registerCustomEditors(dataBinder); + dataBinder.bind(propertyValues); + return bindableCoreProperties; + } + + private static Set getNames(Object target, Iterable prefixes) { + Set names = new LinkedHashSet<>(); + if (target != null) { + PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(target.getClass()); + for (PropertyDescriptor descriptor : descriptors) { + String name = descriptor.getName(); + if (!"class".equals(name)) { + RelaxedNames relaxedNames = RelaxedNames.forCamelCase(name); + if (prefixes == null) { + for (String relaxedName : relaxedNames) { + names.add(relaxedName); + } + } else { + for (String prefix : prefixes) { + for (String relaxedName : relaxedNames) { + names.add(prefix + "." + relaxedName); + names.add(prefix + "_" + relaxedName); + } + } + } + } + } + } + return names; + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } +} diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-1.5-starter/src/main/java/org/springframework/boot/bind/CustomPropertyNamePatternsMatcher.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-1.5-starter/src/main/java/org/springframework/boot/bind/CustomPropertyNamePatternsMatcher.java new file mode 100644 index 00000000..39431a6f --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-1.5-starter/src/main/java/org/springframework/boot/bind/CustomPropertyNamePatternsMatcher.java @@ -0,0 +1,41 @@ +/* + * 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 org.springframework.boot.bind; + +import org.springframework.beans.PropertyValues; +import org.springframework.boot.bind.DefaultPropertyNamePatternsMatcher; +import org.springframework.boot.bind.PropertyNamePatternsMatcher; +import org.springframework.boot.bind.PropertySourcesPropertyValues; +import org.springframework.core.env.MutablePropertySources; + +import java.util.Set; + +public class CustomPropertyNamePatternsMatcher { + + private static final char[] EXACT_DELIMITERS = {'_', '.', '['}; + + public static PropertyValues getPropertySourcesPropertyValues(Set names, MutablePropertySources propertySources) { + PropertyNamePatternsMatcher includes = getPropertyNamePatternsMatcher(names); + return new PropertySourcesPropertyValues(propertySources, names, includes, true); + } + + private static PropertyNamePatternsMatcher getPropertyNamePatternsMatcher(Set names) { + + return new DefaultPropertyNamePatternsMatcher(EXACT_DELIMITERS, true, names); + } +} diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-1.5-starter/src/main/resources/META-INF/spring.factories b/hippo4j-spring-boot/hippo4j-config-spring-boot-1.5-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 00000000..20dfeef4 --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-1.5-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.hippo4j.config.springboot15x.starter.config.ConfigHandlerConfiguration \ No newline at end of file diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/ConfigHandlerConfiguration.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/ConfigHandlerConfiguration.java index a374cb4e..4ab2c09a 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/ConfigHandlerConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/ConfigHandlerConfiguration.java @@ -19,6 +19,7 @@ package cn.hippo4j.config.springboot.starter.config; import cn.hippo4j.config.springboot.starter.refresher.*; import com.alibaba.cloud.nacos.NacosConfigManager; +import com.alibaba.cloud.nacos.NacosConfigProperties; import com.alibaba.nacos.api.config.ConfigService; import com.tencent.polaris.configuration.api.core.ConfigFileService; import io.etcd.jetcd.Client; @@ -26,6 +27,7 @@ import lombok.RequiredArgsConstructor; import org.apache.curator.framework.CuratorFramework; 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.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; @@ -49,17 +51,21 @@ public class ConfigHandlerConfiguration { private static final String POLARIS = "config.serverConnector"; + @Bean + @ConditionalOnMissingBean + public BootstrapConfigPropertiesBinderAdapt bootstrapConfigPropertiesBinderAdapt() { + return new DefaultBootstrapConfigPropertiesBinderAdapt(); + } + @RequiredArgsConstructor @ConditionalOnClass(ConfigService.class) @ConditionalOnMissingClass(NACOS_CONFIG_MANAGER_KEY) @ConditionalOnProperty(prefix = BootstrapConfigProperties.PREFIX, name = NACOS_DATA_ID_KEY) static class EmbeddedNacos { - public final BootstrapConfigProperties bootstrapConfigProperties; - @Bean - public NacosRefresherHandler nacosRefresherHandler() { - return new NacosRefresherHandler(bootstrapConfigProperties); + public NacosRefresherHandler nacosRefresherHandler(NacosConfigProperties nacosConfigProperties) { + return new NacosRefresherHandler(nacosConfigProperties); } } diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java index b38df56c..46bad601 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/config/DynamicThreadPoolAutoConfiguration.java @@ -59,11 +59,7 @@ import org.springframework.core.annotation.Order; @ConditionalOnBean(MarkerConfiguration.Marker.class) @EnableConfigurationProperties(BootstrapConfigProperties.class) @ConditionalOnProperty(prefix = BootstrapConfigProperties.PREFIX, value = "enable", matchIfMissing = true, havingValue = "true") -@Import({ - ConfigHandlerConfiguration.EmbeddedNacos.class, ConfigHandlerConfiguration.EmbeddedNacosCloud.class, - ConfigHandlerConfiguration.EmbeddedApollo.class, ConfigHandlerConfiguration.EmbeddedZookeeper.class, - ConfigHandlerConfiguration.EmbeddedEtcd.class -}) +@Import({ConfigHandlerConfiguration.class}) @ImportAutoConfiguration({WebAdapterConfiguration.class, UtilAutoConfiguration.class, MessageConfiguration.class, LocalLogMonitorConfiguration.class, MicrometerMonitorConfiguration.class}) public class DynamicThreadPoolAutoConfiguration { diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/AbstractConfigThreadPoolDynamicRefresh.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/AbstractConfigThreadPoolDynamicRefresh.java index ba5933fc..ea8c2eef 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/AbstractConfigThreadPoolDynamicRefresh.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/AbstractConfigThreadPoolDynamicRefresh.java @@ -37,19 +37,21 @@ import java.util.concurrent.ExecutorService; * Abstract core thread-pool dynamic refresh. */ @Slf4j -@RequiredArgsConstructor public abstract class AbstractConfigThreadPoolDynamicRefresh implements ThreadPoolDynamicRefresh, ThreadPoolInitRefresh, InitializingBean { - protected final BootstrapConfigProperties bootstrapConfigProperties; + private final BootstrapConfigPropertiesBinderAdapt bootstrapConfigPropertiesBinderAdapt; + + protected BootstrapConfigProperties bootstrapConfigProperties; protected final ExecutorService dynamicRefreshExecutorService = ThreadPoolBuilder.builder().singlePool("client.dynamic.refresh").build(); public AbstractConfigThreadPoolDynamicRefresh() { bootstrapConfigProperties = ApplicationContextHolder.getBean(BootstrapConfigProperties.class); + bootstrapConfigPropertiesBinderAdapt = ApplicationContextHolder.getBean(BootstrapConfigPropertiesBinderAdapt.class); } @Override @@ -69,7 +71,7 @@ public abstract class AbstractConfigThreadPoolDynamicRefresh if (CollectionUtil.isNotEmpty(newValueChangeMap)) { Optional.ofNullable(configInfo).ifPresent(each -> each.putAll(newValueChangeMap)); } - BootstrapConfigProperties bindableCoreProperties = BootstrapConfigPropertiesBinderAdapt.bootstrapCorePropertiesBinder(configInfo, bootstrapConfigProperties); + BootstrapConfigProperties bindableCoreProperties = bootstrapConfigPropertiesBinderAdapt.bootstrapCorePropertiesBinder(configInfo, bootstrapConfigProperties); ApplicationContextHolder.getInstance().publishEvent(new Hippo4jConfigDynamicRefreshEvent(this, bindableCoreProperties)); } catch (Exception ex) { log.error("Hippo-4J core dynamic refresh failed.", ex); diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/BootstrapConfigPropertiesBinderAdapt.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/BootstrapConfigPropertiesBinderAdapt.java index 9f4c1e0f..44bbca6b 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/BootstrapConfigPropertiesBinderAdapt.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/BootstrapConfigPropertiesBinderAdapt.java @@ -17,142 +17,11 @@ package cn.hippo4j.config.springboot.starter.refresher; -import cn.hippo4j.common.toolkit.BeanUtil; -import cn.hippo4j.common.toolkit.CollectionUtil; -import cn.hippo4j.common.toolkit.StringUtil; import cn.hippo4j.config.springboot.starter.config.BootstrapConfigProperties; -import cn.hippo4j.config.springboot.starter.config.DynamicThreadPoolNotifyProperties; -import cn.hippo4j.config.springboot.starter.config.ExecutorProperties; -import cn.hippo4j.config.springboot.starter.config.NotifyPlatformProperties; -import org.springframework.boot.context.properties.bind.Bindable; -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.util.ArrayList; -import java.util.HashMap; -import java.util.List; import java.util.Map; -/** - * Bootstrap core properties binder adapt. - */ -public class BootstrapConfigPropertiesBinderAdapt { - - /** - * Bootstrap core properties binder. - * - * @param configInfo - * @param bootstrapConfigProperties - * @return - */ - public static BootstrapConfigProperties bootstrapCorePropertiesBinder(Map configInfo, BootstrapConfigProperties bootstrapConfigProperties) { - BootstrapConfigProperties bindableCoreProperties = null; - try { - ConfigurationPropertySource sources = new MapConfigurationPropertySource(configInfo); - Binder binder = new Binder(sources); - bindableCoreProperties = binder.bind(BootstrapConfigProperties.PREFIX, Bindable.ofInstance(bootstrapConfigProperties)).get(); - } catch (Exception ex) { - try { - Class.forName("org.springframework.boot.context.properties.bind.Binder"); - } catch (ClassNotFoundException notEx) { - bindableCoreProperties = adapt(configInfo); - } - } - return bindableCoreProperties; - } - - /** - * 此处采用硬编码适配低版本 SpringBoot 1.5.x, 如果有更好的方法进行逻辑转换的话, 欢迎 PR. - * - * @param configInfo - * @return - */ - @Deprecated - private static BootstrapConfigProperties adapt(Map configInfo) { - BootstrapConfigProperties bindableConfigProperties; - try { - // filter - Map targetMap = new HashMap<>(); - configInfo.forEach((key, val) -> { - boolean containFlag = key != null - && StringUtil.isNotBlank((String) key) - && (((String) key).indexOf(BootstrapConfigProperties.PREFIX + ".executors") != -1 - || ((String) key).indexOf(BootstrapConfigProperties.PREFIX + ".notify-platforms") != -1 - || ((String) key).indexOf(BootstrapConfigProperties.PREFIX + ".notifyPlatforms") != -1); - if (containFlag) { - String targetKey = key.toString().replace(BootstrapConfigProperties.PREFIX + ".", ""); - targetMap.put(targetKey, val); - } - }); - // convert - List executorPropertiesList = new ArrayList<>(); - List notifyPropertiesList = new ArrayList<>(); - for (int i = 0; i < Integer.MAX_VALUE; i++) { - Map executorSingleMap = new HashMap<>(); - Map platformSingleMap = new HashMap<>(); - Map notifySingleMap = new HashMap<>(); - - for (Map.Entry entry : targetMap.entrySet()) { - String key = entry.getKey().toString(); - if (key.indexOf("executors[" + i + "].") != -1) { - if (key.indexOf("executors[" + i + "].notify.") != -1) { - key = key.replace("executors[" + i + "].notify.", ""); - String[] notifyKeySplit = key.split("-"); - if (notifyKeySplit != null && notifyKeySplit.length > 0) { - key = key.replace("-", "_"); - } - notifySingleMap.put(key, entry.getValue()); - } else { - key = key.replace("executors[" + i + "].", ""); +public interface BootstrapConfigPropertiesBinderAdapt { - String[] keySplit = key.split("-"); - if (keySplit != null && keySplit.length > 0) { - key = key.replace("-", "_"); - } - executorSingleMap.put(key, entry.getValue()); - } - } - if (key.indexOf("notify-platforms[" + i + "].") != -1 || key.indexOf("notifyPlatforms[" + i + "].") != -1) { - if (key.indexOf("notify-platforms[" + i + "].") != -1) { - key = key.replace("notify-platforms[" + i + "].", ""); - } else { - key = key.replace("notifyPlatforms[" + i + "].", ""); - } - String[] keySplit = key.split("-"); - if (keySplit != null && keySplit.length > 0) { - key = key.replace("-", "_"); - } - platformSingleMap.put(key, entry.getValue()); - } - } - if (CollectionUtil.isEmpty(executorSingleMap) && CollectionUtil.isEmpty(platformSingleMap)) { - break; - } - if (CollectionUtil.isNotEmpty(executorSingleMap)) { - ExecutorProperties executorProperties = BeanUtil.mapToBean(executorSingleMap, ExecutorProperties.class, true); - if (executorProperties != null) { - if (CollectionUtil.isNotEmpty(notifySingleMap)) { - DynamicThreadPoolNotifyProperties alarm = BeanUtil.mapToBean(notifySingleMap, DynamicThreadPoolNotifyProperties.class, true); - alarm.setReceives(alarm.getReceives()); - executorProperties.setNotify(alarm); - } - executorPropertiesList.add(executorProperties); - } - } - if (CollectionUtil.isNotEmpty(platformSingleMap)) { - NotifyPlatformProperties notifyPlatformProperties = BeanUtil.mapToBean(platformSingleMap, NotifyPlatformProperties.class, true); - if (notifyPlatformProperties != null) { - notifyPropertiesList.add(notifyPlatformProperties); - } - } - } - bindableConfigProperties = new BootstrapConfigProperties(); - bindableConfigProperties.setExecutors(executorPropertiesList); - bindableConfigProperties.setNotifyPlatforms(notifyPropertiesList); - } catch (Exception ex) { - throw ex; - } - return bindableConfigProperties; - } + BootstrapConfigProperties bootstrapCorePropertiesBinder(Map configInfo, BootstrapConfigProperties bootstrapConfigProperties); } diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/DefaultBootstrapConfigPropertiesBinderAdapt.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/DefaultBootstrapConfigPropertiesBinderAdapt.java new file mode 100644 index 00000000..1d4cd228 --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/DefaultBootstrapConfigPropertiesBinderAdapt.java @@ -0,0 +1,47 @@ +/* + * 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.config.springboot.starter.refresher; + +import cn.hippo4j.config.springboot.starter.config.BootstrapConfigProperties; +import org.springframework.boot.context.properties.bind.Bindable; +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.util.Map; + +/** + * Bootstrap core properties binder adapt. + */ +public class DefaultBootstrapConfigPropertiesBinderAdapt implements BootstrapConfigPropertiesBinderAdapt { + + /** + * Bootstrap core properties binder. + * + * @param configInfo + * @param bootstrapConfigProperties + * @return + */ + @Override + public BootstrapConfigProperties bootstrapCorePropertiesBinder(Map configInfo, BootstrapConfigProperties bootstrapConfigProperties) { + ConfigurationPropertySource sources = new MapConfigurationPropertySource(configInfo); + Binder binder = new Binder(sources); + return binder.bind(BootstrapConfigProperties.PREFIX, Bindable.ofInstance(bootstrapConfigProperties)).get(); + } + +} diff --git a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/NacosRefresherHandler.java b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/NacosRefresherHandler.java index ad052018..0bde29a5 100644 --- a/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/NacosRefresherHandler.java +++ b/hippo4j-spring-boot/hippo4j-config-spring-boot-starter/src/main/java/cn/hippo4j/config/springboot/starter/refresher/NacosRefresherHandler.java @@ -18,6 +18,7 @@ package cn.hippo4j.config.springboot.starter.refresher; import cn.hippo4j.config.springboot.starter.config.BootstrapConfigProperties; +import com.alibaba.cloud.nacos.NacosConfigProperties; import com.alibaba.nacos.api.annotation.NacosInjected; import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.listener.Listener; @@ -38,8 +39,9 @@ public class NacosRefresherHandler extends AbstractConfigThreadPoolDynamicRefres @NacosInjected private ConfigService configService; - public NacosRefresherHandler(BootstrapConfigProperties bootstrapConfigProperties) { - super(bootstrapConfigProperties); + public NacosRefresherHandler(NacosConfigProperties nacosConfigProperties) { + super(); + this.configService = nacosConfigProperties.configServiceInstance(); } @Override @@ -47,7 +49,7 @@ public class NacosRefresherHandler extends AbstractConfigThreadPoolDynamicRefres Map nacosConfig = bootstrapConfigProperties.getNacos(); String dataId = nacosConfig.get(DATA_ID); String group = nacosConfig.get(GROUP); - return configService.getConfig(dataId, group, Long.MAX_VALUE); + return configService.getConfig(dataId, group, 60 * 10000); } @Override diff --git a/hippo4j-spring-boot/pom.xml b/hippo4j-spring-boot/pom.xml index 45ae7e48..9e5d2a3c 100644 --- a/hippo4j-spring-boot/pom.xml +++ b/hippo4j-spring-boot/pom.xml @@ -15,5 +15,6 @@ hippo4j-spring-boot-starter hippo4j-spring-boot-starter-adapter hippo4j-spring-boot-starter-monitor + hippo4j-config-spring-boot-1.5-starter