diff --git a/CHANGELOG.md b/CHANGELOG.md index 301a9921..de052b92 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,3 +21,4 @@ - [fix:set error handler named EnhancedRestTemplateReporter for RestTemplate](https://github.com/Tencent/spring-cloud-tencent/pull/543) - [Fix issue: report the labels in request when report the result of invocation by Feign](https://github.com/Tencent/spring-cloud-tencent/pull/546) - [optimize:add switch for report call result and default false](https://github.com/Tencent/spring-cloud-tencent/pull/547) +- [Optimize: refresh @Value by reflect and only refresh affected ConfigurationProperties beans](https://github.com/Tencent/spring-cloud-tencent/pull/548) diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigAutoConfiguration.java index 197db3e0..05c50b57 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigAutoConfiguration.java @@ -18,14 +18,14 @@ package com.tencent.cloud.polaris.config; +import com.tencent.cloud.polaris.config.adapter.AffectedConfigurationPropertiesRebinder; import com.tencent.cloud.polaris.config.adapter.PolarisConfigPropertyRefresher; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; -import com.tencent.cloud.polaris.config.adapter.PolarisReflectConfigPropertyAutoRefresher; -import com.tencent.cloud.polaris.config.adapter.PolarisRefreshContextConfigPropertyAutoRefresher; -import com.tencent.cloud.polaris.config.adapter.SmartConfigurationPropertiesRebinder; +import com.tencent.cloud.polaris.config.adapter.PolarisRefreshAffectedContextRefresher; +import com.tencent.cloud.polaris.config.adapter.PolarisRefreshEntireContextRefresher; import com.tencent.cloud.polaris.config.annotation.PolarisConfigAnnotationProcessor; import com.tencent.cloud.polaris.config.condition.ConditionalOnConfigReflectEnabled; -import com.tencent.cloud.polaris.config.condition.ConditionalOnNonDefaultBehavior; +import com.tencent.cloud.polaris.config.condition.ConditionalOnReflectRefreshType; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.listener.PolarisConfigChangeEventListener; import com.tencent.cloud.polaris.config.spring.annotation.SpringValueProcessor; @@ -65,19 +65,17 @@ public class PolarisConfigAutoConfiguration { @Bean @ConditionalOnMissingBean(search = SearchStrategy.CURRENT) - @ConditionalOnNonDefaultBehavior - public ConfigurationPropertiesRebinder smartConfigurationPropertiesRebinder( + @ConditionalOnReflectRefreshType + public ConfigurationPropertiesRebinder affectedConfigurationPropertiesRebinder( ConfigurationPropertiesBeans beans) { - // If using default behavior, not use SmartConfigurationPropertiesRebinder. - // Minimize te possibility of making mistakes. - return new SmartConfigurationPropertiesRebinder(beans); + return new AffectedConfigurationPropertiesRebinder(beans); } @Bean @ConditionalOnMissingBean(search = SearchStrategy.CURRENT) public PolarisConfigPropertyRefresher polarisRefreshContextPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties, - PolarisPropertySourceManager polarisPropertySourceManager, ContextRefresher contextRefresher) { - return new PolarisRefreshContextConfigPropertyAutoRefresher(polarisConfigProperties, polarisPropertySourceManager, contextRefresher); + PolarisPropertySourceManager polarisPropertySourceManager, ContextRefresher contextRefresher) { + return new PolarisRefreshEntireContextRefresher(polarisConfigProperties, polarisPropertySourceManager, contextRefresher); } @Configuration(proxyBeanMethods = false) @@ -104,7 +102,8 @@ public class PolarisConfigAutoConfiguration { public PolarisConfigPropertyRefresher polarisReflectPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties, PolarisPropertySourceManager polarisPropertySourceManager, SpringValueRegistry springValueRegistry, PlaceholderHelper placeholderHelper) { - return new PolarisReflectConfigPropertyAutoRefresher(polarisConfigProperties, polarisPropertySourceManager, springValueRegistry, placeholderHelper); + return new PolarisRefreshAffectedContextRefresher(polarisConfigProperties, polarisPropertySourceManager, + springValueRegistry, placeholderHelper); } } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigBootstrapAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigBootstrapAutoConfiguration.java index 9c87f69a..235afb77 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigBootstrapAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigBootstrapAutoConfiguration.java @@ -18,10 +18,10 @@ package com.tencent.cloud.polaris.config; +import com.tencent.cloud.polaris.config.adapter.AffectedConfigurationPropertiesRebinder; import com.tencent.cloud.polaris.config.adapter.PolarisConfigFileLocator; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; -import com.tencent.cloud.polaris.config.adapter.SmartConfigurationPropertiesRebinder; -import com.tencent.cloud.polaris.config.condition.ConditionalOnNonDefaultBehavior; +import com.tencent.cloud.polaris.config.condition.ConditionalOnReflectRefreshType; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration; @@ -89,11 +89,9 @@ public class PolarisConfigBootstrapAutoConfiguration { @Bean @ConditionalOnMissingBean(search = SearchStrategy.CURRENT) - @ConditionalOnNonDefaultBehavior - public ConfigurationPropertiesRebinder smartConfigurationPropertiesRebinder( + @ConditionalOnReflectRefreshType + public ConfigurationPropertiesRebinder affectedConfigurationPropertiesRebinder( ConfigurationPropertiesBeans beans) { - // If using default behavior, not use SmartConfigurationPropertiesRebinder. - // Minimize te possibility of making mistakes. - return new SmartConfigurationPropertiesRebinder(beans); + return new AffectedConfigurationPropertiesRebinder(beans); } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/AffectedConfigurationPropertiesRebinder.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/AffectedConfigurationPropertiesRebinder.java new file mode 100644 index 00000000..8e1bd90c --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/AffectedConfigurationPropertiesRebinder.java @@ -0,0 +1,81 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * 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 com.tencent.cloud.polaris.config.adapter; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + +import org.springframework.beans.BeansException; +import org.springframework.boot.context.properties.ConfigurationPropertiesBean; +import org.springframework.cloud.context.environment.EnvironmentChangeEvent; +import org.springframework.cloud.context.properties.ConfigurationPropertiesBeans; +import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder; +import org.springframework.context.ApplicationContext; +import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.util.CollectionUtils; + +/** + * Optimize {@link ConfigurationPropertiesRebinder}, only rebuild affected beans. + * @author weihubeats 2022-7-10 + */ +public class AffectedConfigurationPropertiesRebinder extends ConfigurationPropertiesRebinder { + + private ApplicationContext applicationContext; + private Map propertiesBeans = new HashMap<>(); + + public AffectedConfigurationPropertiesRebinder(ConfigurationPropertiesBeans beans) { + super(beans); + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + super.setApplicationContext(applicationContext); + + this.applicationContext = applicationContext; + + propertiesBeans = ConfigurationPropertiesBean.getAll(applicationContext); + } + + @Override + public void onApplicationEvent(EnvironmentChangeEvent event) { + if (this.applicationContext.equals(event.getSource())) { + rebindAffectedBeans(event); + } + } + + private void rebindAffectedBeans(EnvironmentChangeEvent event) { + Set changedKeys = event.getKeys(); + + if (CollectionUtils.isEmpty(changedKeys)) { + return; + } + + propertiesBeans.forEach((name, bean) -> { + changedKeys.forEach(key -> { + String propertiesPrefix = Objects.requireNonNull(AnnotationUtils.getValue(bean.getAnnotation())) + .toString(); + if (key.startsWith(propertiesPrefix)) { + rebind(name); + } + }); + }); + } +} diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigPropertyAutoRefresher.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigPropertyAutoRefresher.java index 2b1ccbb7..7dead177 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigPropertyAutoRefresher.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigPropertyAutoRefresher.java @@ -82,7 +82,7 @@ public abstract class PolarisConfigPropertyAutoRefresher .addChangeListener((ConfigKVFileChangeListener) configKVFileChangeEvent -> { LOGGER.info( - "[SCT Config] received polaris config change event and will refresh spring context." + "[SCT Config] received polaris config change event and will refresh spring context." + " namespace = {}, group = {}, fileName = {}", polarisPropertySource.getNamespace(), polarisPropertySource.getGroup(), diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisReflectConfigPropertyAutoRefresher.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshAffectedContextRefresher.java similarity index 78% rename from spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisReflectConfigPropertyAutoRefresher.java rename to spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshAffectedContextRefresher.java index 5a22e46d..dffb23ad 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisReflectConfigPropertyAutoRefresher.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshAffectedContextRefresher.java @@ -21,7 +21,6 @@ package com.tencent.cloud.polaris.config.adapter; import java.util.Collection; import java.util.Set; -import com.tencent.cloud.common.util.JacksonUtils; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper; import com.tencent.cloud.polaris.config.spring.property.SpringValue; @@ -39,15 +38,15 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.lang.NonNull; /** - * PolarisReflectConfigPropertyAutoRefresher to refresh config in reflect type - * we can use it by setting spring.cloud.polaris.config.refresh-type=reflect. + * 1. The refresh of @Value properties is implemented through reflection. + * 2. Implement @ConfigurationProperties bean property refresh via EnvironmentChangeEvent, + * while rebuilding only beans with property changes. * * @author lingxiao.wlx */ -public class PolarisReflectConfigPropertyAutoRefresher extends PolarisConfigPropertyAutoRefresher - implements ApplicationContextAware { +public class PolarisRefreshAffectedContextRefresher extends PolarisConfigPropertyAutoRefresher implements ApplicationContextAware { - private static final Logger LOGGER = LoggerFactory.getLogger(PolarisReflectConfigPropertyAutoRefresher.class); + private static final Logger LOGGER = LoggerFactory.getLogger(PolarisRefreshAffectedContextRefresher.class); private final SpringValueRegistry springValueRegistry; @@ -59,9 +58,9 @@ public class PolarisReflectConfigPropertyAutoRefresher extends PolarisConfigProp private TypeConverter typeConverter; - public PolarisReflectConfigPropertyAutoRefresher(PolarisConfigProperties polarisConfigProperties, - PolarisPropertySourceManager polarisPropertySourceManager, SpringValueRegistry springValueRegistry, - PlaceholderHelper placeholderHelper) { + public PolarisRefreshAffectedContextRefresher(PolarisConfigProperties polarisConfigProperties, + PolarisPropertySourceManager polarisPropertySourceManager, SpringValueRegistry springValueRegistry, + PlaceholderHelper placeholderHelper) { super(polarisConfigProperties, polarisPropertySourceManager); this.springValueRegistry = springValueRegistry; this.placeholderHelper = placeholderHelper; @@ -89,11 +88,11 @@ public class PolarisReflectConfigPropertyAutoRefresher extends PolarisConfigProp Object value = resolvePropertyValue(springValue); springValue.update(value); - LOGGER.info("Auto update polaris changed value successfully, new value: {}, {}", value, + LOGGER.info("[SCT Config] Auto update polaris changed value successfully, new value: {}, {}", value, springValue); } catch (Throwable ex) { - LOGGER.error("Auto update polaris changed value failed, {}", springValue.toString(), ex); + LOGGER.error("[SCT Config] Auto update polaris changed value failed, {}", springValue.toString(), ex); } } @@ -115,16 +114,6 @@ public class PolarisReflectConfigPropertyAutoRefresher extends PolarisConfigProp return value; } - private Object parseJsonValue(String json, Class targetType) { - try { - return JacksonUtils.json2JavaBean(json, targetType); - } - catch (Throwable ex) { - LOGGER.error("Parsing json '{}' to type {} failed!", json, targetType, ex); - throw ex; - } - } - @Override public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException { this.context = (ConfigurableApplicationContext) applicationContext; diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshContextConfigPropertyAutoRefresher.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshEntireContextRefresher.java similarity index 72% rename from spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshContextConfigPropertyAutoRefresher.java rename to spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshEntireContextRefresher.java index 7a52e8dd..a13a68be 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshContextConfigPropertyAutoRefresher.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshEntireContextRefresher.java @@ -25,17 +25,18 @@ import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import org.springframework.cloud.context.refresh.ContextRefresher; /** - * PolarisRefreshContextConfigPropertyAutoRefresher refresh config by refreshContext. + * The default implement of Spring Cloud refreshes the entire Spring Context. + * The disadvantage is that the entire context is rebuilt, which has a large impact and low performance. * * @author lingxiao.wlx */ -public class PolarisRefreshContextConfigPropertyAutoRefresher extends PolarisConfigPropertyAutoRefresher { +public class PolarisRefreshEntireContextRefresher extends PolarisConfigPropertyAutoRefresher { private final ContextRefresher contextRefresher; - public PolarisRefreshContextConfigPropertyAutoRefresher(PolarisConfigProperties polarisConfigProperties, - PolarisPropertySourceManager polarisPropertySourceManager, - ContextRefresher contextRefresher) { + public PolarisRefreshEntireContextRefresher(PolarisConfigProperties polarisConfigProperties, + PolarisPropertySourceManager polarisPropertySourceManager, + ContextRefresher contextRefresher) { super(polarisConfigProperties, polarisPropertySourceManager); this.contextRefresher = contextRefresher; } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/SmartConfigurationPropertiesRebinder.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/SmartConfigurationPropertiesRebinder.java deleted file mode 100644 index c035d88e..00000000 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/SmartConfigurationPropertiesRebinder.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * 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 com.tencent.cloud.polaris.config.adapter; - -import java.lang.reflect.Field; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.Set; - -import com.tencent.cloud.polaris.config.enums.RefreshBehavior; - -import org.springframework.beans.BeansException; -import org.springframework.boot.context.properties.ConfigurationPropertiesBean; -import org.springframework.cloud.context.environment.EnvironmentChangeEvent; -import org.springframework.cloud.context.properties.ConfigurationPropertiesBeans; -import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder; -import org.springframework.context.ApplicationContext; -import org.springframework.core.annotation.AnnotationUtils; -import org.springframework.util.ReflectionUtils; - -import static com.tencent.cloud.polaris.config.condition.NonDefaultBehaviorCondition.POLARIS_CONFIG_REFRESH_BEHAVIOR; -import static com.tencent.cloud.polaris.config.enums.RefreshBehavior.ALL_BEANS; - -/** - * Extend {@link ConfigurationPropertiesRebinder}. - *

- * Spring team doesn't seem to support single {@link ConfigurationPropertiesBean} refresh. - *

- * SmartConfigurationPropertiesRebinder can refresh specific - * {@link ConfigurationPropertiesBean} base on the change keys. - *

- * NOTE: We still use Spring's default behavior (full refresh) as default - * behavior, This feature can be considered an advanced feature, it may not be as stable - * as the default behavior. - * - * SmartConfigurationPropertiesRebinder - * - * @author weihubeats 2022-7-10 - */ -public class SmartConfigurationPropertiesRebinder extends ConfigurationPropertiesRebinder { - - private static final String BEANS = "beans"; - - private Map beanMap; - - private ApplicationContext applicationContext; - - private RefreshBehavior refreshBehavior; - - public SmartConfigurationPropertiesRebinder(ConfigurationPropertiesBeans beans) { - super(beans); - fillBeanMap(beans); - } - - @SuppressWarnings("unchecked") - private void fillBeanMap(ConfigurationPropertiesBeans beans) { - this.beanMap = new HashMap<>(); - Field field = ReflectionUtils.findField(beans.getClass(), BEANS); - if (field != null) { - field.setAccessible(true); - this.beanMap.putAll((Map) Optional - .ofNullable(ReflectionUtils.getField(field, beans)) - .orElse(Collections.emptyMap())); - } - } - - @Override - public void setApplicationContext(ApplicationContext applicationContext) - throws BeansException { - super.setApplicationContext(applicationContext); - this.applicationContext = applicationContext; - this.refreshBehavior = this.applicationContext.getEnvironment().getProperty( - POLARIS_CONFIG_REFRESH_BEHAVIOR, RefreshBehavior.class, - ALL_BEANS); - } - - @Override - public void onApplicationEvent(EnvironmentChangeEvent event) { - if (this.applicationContext.equals(event.getSource()) - // Backwards compatible - || event.getKeys().equals(event.getSource())) { - switch (refreshBehavior) { - case SPECIFIC_BEAN: - rebindSpecificBean(event); - break; - case ALL_BEANS: - default: - rebind(); - break; - } - } - } - - private void rebindSpecificBean(EnvironmentChangeEvent event) { - Set refreshedSet = new HashSet<>(); - beanMap.forEach((name, bean) -> event.getKeys().forEach(changeKey -> { - String prefix = Objects.requireNonNull(AnnotationUtils.getValue(bean.getAnnotation())).toString(); - // prevent multiple refresh one ConfigurationPropertiesBean. - if (changeKey.startsWith(prefix) && refreshedSet.add(name)) { - rebind(name); - } - })); - } - -} diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ConditionalOnNonDefaultBehavior.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ConditionalOnReflectRefreshType.java similarity index 92% rename from spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ConditionalOnNonDefaultBehavior.java rename to spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ConditionalOnReflectRefreshType.java index d799ec28..a8568746 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ConditionalOnNonDefaultBehavior.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ConditionalOnReflectRefreshType.java @@ -34,7 +34,7 @@ import org.springframework.context.annotation.Conditional; @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE, ElementType.METHOD }) @Documented -@Conditional(NonDefaultBehaviorCondition.class) -public @interface ConditionalOnNonDefaultBehavior { +@Conditional(ReflectRefreshTypeCondition.class) +public @interface ConditionalOnReflectRefreshType { } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/NonDefaultBehaviorCondition.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ReflectRefreshTypeCondition.java similarity index 65% rename from spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/NonDefaultBehaviorCondition.java rename to spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ReflectRefreshTypeCondition.java index 48484133..9db7d5b4 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/NonDefaultBehaviorCondition.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/condition/ReflectRefreshTypeCondition.java @@ -18,7 +18,7 @@ package com.tencent.cloud.polaris.config.condition; -import com.tencent.cloud.polaris.config.enums.RefreshBehavior; +import com.tencent.cloud.polaris.config.enums.RefreshType; import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.boot.autoconfigure.condition.SpringBootCondition; @@ -26,30 +26,28 @@ import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; /** - * Extend SpringBootCondition. + * The refresh type of reflect condition. * * @author weihubeats 2022-7-13 */ -public class NonDefaultBehaviorCondition extends SpringBootCondition { +public class ReflectRefreshTypeCondition extends SpringBootCondition { /** - * refresh behavior config. + * the property key of refresh type. */ - public static final String POLARIS_CONFIG_REFRESH_BEHAVIOR = "spring.cloud.polaris.config.refresh-behavior"; + public static final String POLARIS_CONFIG_REFRESH_TYPE = "spring.cloud.polaris.config.refresh-type"; - /** - * refresh behavior config default value. - */ - private static final RefreshBehavior DEFAULT_REFRESH_BEHAVIOR = RefreshBehavior.ALL_BEANS; + private static final RefreshType DEFAULT_REFRESH_TYPE = RefreshType.REFRESH_CONTEXT; @Override - public ConditionOutcome getMatchOutcome(ConditionContext context, - AnnotatedTypeMetadata metadata) { - RefreshBehavior behavior = context.getEnvironment().getProperty( - POLARIS_CONFIG_REFRESH_BEHAVIOR, RefreshBehavior.class, DEFAULT_REFRESH_BEHAVIOR); - if (DEFAULT_REFRESH_BEHAVIOR == behavior) { + public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { + RefreshType refreshType = context.getEnvironment() + .getProperty(POLARIS_CONFIG_REFRESH_TYPE, RefreshType.class, DEFAULT_REFRESH_TYPE); + + if (refreshType == DEFAULT_REFRESH_TYPE) { return ConditionOutcome.noMatch("no matched"); } + return ConditionOutcome.match("matched"); } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 666e392a..e82e3eda 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -42,12 +42,6 @@ "description": "Whether to connect to a remote server, suitable for local development mode.", "sourceType": "com.tencent.cloud.polaris.config.config.PolarisConfigProperties" }, - { - "name": "spring.cloud.polaris.config.refresh-behavior", - "type": "com.tencent.cloud.polaris.config.enums.RefreshBehavior", - "defaultValue": "all_beans", - "description": "ConfigurationPropertiesBean refresh behavior." - }, { "name": "spring.cloud.polaris.config.refresh-type", "type": "com.tencent.cloud.polaris.config.enums.RefreshType", diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertiesSourceAutoRefresherTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertiesSourceAutoRefresherTest.java index 6f738afa..912a12ac 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertiesSourceAutoRefresherTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertiesSourceAutoRefresherTest.java @@ -47,7 +47,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; /** - * test for {@link PolarisReflectConfigPropertyAutoRefresher}. + * test for {@link PolarisRefreshAffectedContextRefresher}. * * @author lepdou 2022-06-11 */ @@ -70,7 +70,7 @@ public class PolarisPropertiesSourceAutoRefresherTest { @Test public void testConfigFileChanged() throws Exception { - PolarisReflectConfigPropertyAutoRefresher refresher = new PolarisReflectConfigPropertyAutoRefresher(polarisConfigProperties, + PolarisRefreshAffectedContextRefresher refresher = new PolarisRefreshAffectedContextRefresher(polarisConfigProperties, polarisPropertySourceManager, springValueRegistry, placeholderHelper); ConfigurableApplicationContext applicationContext = mock(ConfigurableApplicationContext.class); ConfigurableListableBeanFactory beanFactory = mock(ConfigurableListableBeanFactory.class); diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnConfigReflectEnabledTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnConfigReflectEnabledTest.java index c8a14d07..279f3933 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnConfigReflectEnabledTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnConfigReflectEnabledTest.java @@ -21,8 +21,8 @@ package com.tencent.cloud.polaris.config.condition; import com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration; import com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; -import com.tencent.cloud.polaris.config.adapter.PolarisReflectConfigPropertyAutoRefresher; -import com.tencent.cloud.polaris.config.adapter.PolarisRefreshContextConfigPropertyAutoRefresher; +import com.tencent.cloud.polaris.config.adapter.PolarisRefreshAffectedContextRefresher; +import com.tencent.cloud.polaris.config.adapter.PolarisRefreshEntireContextRefresher; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.enums.RefreshType; import com.tencent.cloud.polaris.config.spring.annotation.SpringValueProcessor; @@ -32,6 +32,7 @@ import org.junit.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration; import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; import org.springframework.cloud.context.refresh.ContextRefresher; @@ -50,6 +51,7 @@ public class ConditionalOnConfigReflectEnabledTest { .withConfiguration(AutoConfigurations.of(PolarisConfigBootstrapAutoConfiguration.class)) .withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class)) .withConfiguration(AutoConfigurations.of(RefreshAutoConfiguration.class)) + .withConfiguration(AutoConfigurations.of(ConfigurationPropertiesRebinderAutoConfiguration.class)) .withPropertyValues("spring.application.name=" + "conditionalOnConfigReflectEnabledTest") .withPropertyValues("server.port=" + 8080) .withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081") @@ -59,7 +61,7 @@ public class ConditionalOnConfigReflectEnabledTest { assertThat(context).hasSingleBean(PlaceholderHelper.class); assertThat(context).hasSingleBean(SpringValueRegistry.class); assertThat(context).hasSingleBean(SpringValueProcessor.class); - assertThat(context).hasSingleBean(PolarisReflectConfigPropertyAutoRefresher.class); + assertThat(context).hasSingleBean(PolarisRefreshAffectedContextRefresher.class); }); } @@ -69,6 +71,7 @@ public class ConditionalOnConfigReflectEnabledTest { .withConfiguration(AutoConfigurations.of(PolarisConfigBootstrapAutoConfiguration.class)) .withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class)) .withConfiguration(AutoConfigurations.of(RefreshAutoConfiguration.class)) + .withConfiguration(AutoConfigurations.of(ConfigurationPropertiesRebinderAutoConfiguration.class)) .withPropertyValues("spring.application.name=" + "conditionalOnConfigReflectEnabledTest") .withPropertyValues("server.port=" + 8080) .withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081") @@ -77,7 +80,7 @@ public class ConditionalOnConfigReflectEnabledTest { assertThat(context).hasSingleBean(PolarisConfigProperties.class); assertThat(context).hasSingleBean(PolarisPropertySourceManager.class); assertThat(context).hasSingleBean(ContextRefresher.class); - assertThat(context).hasSingleBean(PolarisRefreshContextConfigPropertyAutoRefresher.class); + assertThat(context).hasSingleBean(PolarisRefreshEntireContextRefresher.class); }); } } diff --git a/spring-cloud-tencent-examples/polaris-config-example/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-config-example/src/main/resources/bootstrap.yml index 6a13aee3..a312188d 100644 --- a/spring-cloud-tencent-examples/polaris-config-example/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-config-example/src/main/resources/bootstrap.yml @@ -12,7 +12,7 @@ spring: groups: - name: ${spring.application.name} # group name files: [ "config/application.properties", "config/bootstrap.yml" ] # config/application.properties takes precedence over config/bootstrap.yml - refresh-behavior: specific_bean + refresh-type: reflect management: endpoints: web: