From fe5a2894e997ff5d107c980451aa43cf32be2b96 Mon Sep 17 00:00:00 2001 From: lepdou Date: Wed, 7 Dec 2022 15:58:29 +0800 Subject: [PATCH 1/2] support delete config for ConfigurationProperties bean (#756) --- CHANGELOG.md | 1 + ...fectedConfigurationPropertiesRebinder.java | 67 +++++++++++++++++++ .../cloud/common/util/ReflectionUtils.java | 23 ++++++- .../cloud/polaris/config/example/Person.java | 29 +++++++- 4 files changed, 118 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93e8a918a..b50c70c09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,3 +14,4 @@ - [fix:fix discovery junit.](https://github.com/Tencent/spring-cloud-tencent/pull/729) - [adapt polaris-java 1.10.1 version](https://github.com/Tencent/spring-cloud-tencent/pull/747) - [Optimize: change RouteArgument.buildCustom to RouteArgument.fromLabel](https://github.com/Tencent/spring-cloud-tencent/pull/751) +- [Optimize: support delete config for ConfigurationProperties bean ](https://github.com/Tencent/spring-cloud-tencent/pull/756) 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 index 8e1bd90c1..bbd6f3e55 100644 --- 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 @@ -18,10 +18,18 @@ package com.tencent.cloud.polaris.config.adapter; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +import com.tencent.cloud.common.util.ReflectionUtils; +import com.tencent.polaris.api.utils.MapUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.boot.context.properties.ConfigurationPropertiesBean; @@ -31,15 +39,18 @@ import org.springframework.cloud.context.properties.ConfigurationPropertiesRebin import org.springframework.context.ApplicationContext; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; /** * Optimize {@link ConfigurationPropertiesRebinder}, only rebuild affected beans. * @author weihubeats 2022-7-10 */ public class AffectedConfigurationPropertiesRebinder extends ConfigurationPropertiesRebinder { + private static final Logger LOGGER = LoggerFactory.getLogger(AffectedConfigurationPropertiesRebinder.class); private ApplicationContext applicationContext; private Map propertiesBeans = new HashMap<>(); + private final Map> propertiesBeanDefaultValues = new ConcurrentHashMap<>(); public AffectedConfigurationPropertiesRebinder(ConfigurationPropertiesBeans beans) { super(beans); @@ -52,6 +63,7 @@ public class AffectedConfigurationPropertiesRebinder extends ConfigurationProper this.applicationContext = applicationContext; propertiesBeans = ConfigurationPropertiesBean.getAll(applicationContext); + initPropertiesBeanDefaultValues(propertiesBeans); } @Override @@ -74,8 +86,63 @@ public class AffectedConfigurationPropertiesRebinder extends ConfigurationProper .toString(); if (key.startsWith(propertiesPrefix)) { rebind(name); + rebindDefaultValue(name, key); } }); }); } + + private void rebindDefaultValue(String beanName, String key) { + String changeValue = applicationContext.getEnvironment().getProperty(key); + if (StringUtils.hasLength(changeValue)) { + return; + } + + Map defaultValues = propertiesBeanDefaultValues.get(beanName); + if (MapUtils.isEmpty(defaultValues)) { + return; + } + try { + String fieldName = key.substring(key.lastIndexOf(".") + 1); + + Object bean = applicationContext.getBean(beanName); + Field field = ReflectionUtils.findField(bean.getClass(), fieldName); + if (field != null) { + field.setAccessible(true); + field.set(bean, defaultValues.get(fieldName)); + } + } + catch (Exception e) { + LOGGER.error("[SCT Config] rebind default value error, bean = {}, key = {}", beanName, key); + } + } + + private void initPropertiesBeanDefaultValues(Map propertiesBeans) { + if (MapUtils.isEmpty(propertiesBeans)) { + return; + } + + for (ConfigurationPropertiesBean propertiesBean : propertiesBeans.values()) { + Map defaultValues = new HashMap<>(); + try { + Object instance = propertiesBean.getInstance().getClass().getDeclaredConstructor((Class[]) null) + .newInstance(); + ReflectionUtils.doWithFields(instance.getClass(), field -> { + try { + field.setAccessible(true); + defaultValues.put(field.getName(), field.get(instance)); + } + catch (Exception ignored) { + } + }, field -> { + int modifiers = field.getModifiers(); + return !Modifier.isFinal(modifiers) && !Modifier.isStatic(modifiers) && ReflectionUtils.writableBeanField(field); + }); + } + catch (Exception ignored) { + } + + propertiesBeanDefaultValues.put(propertiesBean.getName(), defaultValues); + } + } } diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ReflectionUtils.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ReflectionUtils.java index ef706673f..b9f75b94a 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ReflectionUtils.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/ReflectionUtils.java @@ -19,16 +19,37 @@ package com.tencent.cloud.common.util; import java.lang.reflect.Field; +import org.springframework.util.ClassUtils; + +import static java.util.Locale.ENGLISH; + /** * Reflection Utils. * * @author Haotian Zhang */ -public final class ReflectionUtils { +public final class ReflectionUtils extends org.springframework.util.ReflectionUtils { + + private final static String SET_PREFIX = "set"; private ReflectionUtils() { } + public static boolean writableBeanField(Field field) { + String fieldName = field.getName(); + + String setMethodName = SET_PREFIX + capitalize(fieldName); + + return ClassUtils.hasMethod(field.getDeclaringClass(), setMethodName, field.getType()); + } + + public static String capitalize(String name) { + if (name == null || name.length() == 0) { + return name; + } + return name.substring(0, 1).toUpperCase(ENGLISH) + name.substring(1); + } + public static Object getFieldValue(Object instance, String fieldName) { Field field = org.springframework.util.ReflectionUtils.findField(instance.getClass(), fieldName); if (field == null) { diff --git a/spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/Person.java b/spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/Person.java index 1a88235de..38bb2cae6 100644 --- a/spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/Person.java +++ b/spring-cloud-tencent-examples/polaris-config-example/src/main/java/com/tencent/cloud/polaris/config/example/Person.java @@ -18,6 +18,8 @@ package com.tencent.cloud.polaris.config.example; +import java.util.List; + import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @@ -34,6 +36,10 @@ public class Person { private int age; + private boolean director; + + private List students; + public String getName() { return name; } @@ -50,8 +56,29 @@ public class Person { this.age = age; } + public boolean isDirector() { + return director; + } + + public void setDirector(boolean director) { + this.director = director; + } + + public List getStudents() { + return students; + } + + public void setStudents(List students) { + this.students = students; + } + @Override public String toString() { - return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; + return "Person{" + + "name='" + name + '\'' + + ", age=" + age + + ", director=" + director + + ", students=" + students + + '}'; } } From 70a23e36cc4419fe5b3943422d30716825bded02 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Wed, 7 Dec 2022 20:36:26 +0800 Subject: [PATCH 2/2] Test:add sct-stater-polaris-router junit. (#768) --- CHANGELOG.md | 1 + .../PolarisMetadataRouterPropertiesTest.java | 54 +++++++++++++++ .../PolarisNearByRouterPropertiesTest.java | 56 ++++++++++++++++ .../PolarisRuleBasedRouterPropertiesTest.java | 55 ++++++++++++++++ ...sRouterEndpointAutoConfigurationTests.java | 65 +++++++++++++++++++ .../RouterLabelFeignInterceptorTest.java | 3 + .../PolarisLoadBalancerClientFilterTest.java | 3 +- 7 files changed, 236 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisMetadataRouterPropertiesTest.java create mode 100644 spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterPropertiesTest.java create mode 100644 spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisRuleBasedRouterPropertiesTest.java create mode 100644 spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/endpoint/PolarisRouterEndpointAutoConfigurationTests.java diff --git a/CHANGELOG.md b/CHANGELOG.md index b50c70c09..f65c7c9c6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,3 +15,4 @@ - [adapt polaris-java 1.10.1 version](https://github.com/Tencent/spring-cloud-tencent/pull/747) - [Optimize: change RouteArgument.buildCustom to RouteArgument.fromLabel](https://github.com/Tencent/spring-cloud-tencent/pull/751) - [Optimize: support delete config for ConfigurationProperties bean ](https://github.com/Tencent/spring-cloud-tencent/pull/756) +- [Test:add sct-stater-polaris-router junit.](https://github.com/Tencent/spring-cloud-tencent/pull/768) diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisMetadataRouterPropertiesTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisMetadataRouterPropertiesTest.java new file mode 100644 index 000000000..d5f2fbec9 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisMetadataRouterPropertiesTest.java @@ -0,0 +1,54 @@ +/* + * 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.router.config.properties; + +import org.junit.Before; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * test for {@link PolarisMetadataRouterProperties}. + */ +public class PolarisMetadataRouterPropertiesTest { + + PolarisMetadataRouterProperties properties; + + @Before + public void setUp() { + properties = new PolarisMetadataRouterProperties(); + } + + @Test + public void isEnabled() { + assertThat(properties.isEnabled()).isEqualTo(true); + } + + @Test + public void setEnabled() { + properties.setEnabled(false); + assertThat(properties.isEnabled()).isEqualTo(false); + } + + @Test + public void testToString() { + assertThat(properties.toString()) + .isEqualTo("PolarisMetadataRouterProperties{enabled=true}"); + } +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterPropertiesTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterPropertiesTest.java new file mode 100644 index 000000000..35c98adff --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisNearByRouterPropertiesTest.java @@ -0,0 +1,56 @@ +/* + * 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.router.config.properties; + + +import org.junit.Before; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + + +/** + * test for {@link PolarisNearByRouterProperties}. + */ +public class PolarisNearByRouterPropertiesTest { + + PolarisNearByRouterProperties properties; + + @Before + public void setUp() { + properties = new PolarisNearByRouterProperties(); + } + + @Test + public void isEnabled() { + assertThat(properties.isEnabled()).isEqualTo(true); + } + + @Test + public void setEnabled() { + properties.setEnabled(false); + assertThat(properties.isEnabled()).isEqualTo(false); + } + + @Test + public void testToString() { + assertThat(properties.toString()) + .isEqualTo("PolarisNearByRouterProperties{enabled=true}"); + } +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisRuleBasedRouterPropertiesTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisRuleBasedRouterPropertiesTest.java new file mode 100644 index 000000000..6312803dd --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/properties/PolarisRuleBasedRouterPropertiesTest.java @@ -0,0 +1,55 @@ +/* + * 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.router.config.properties; + +import org.junit.Before; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * test for {@link PolarisRuleBasedRouterProperties}. + */ +public class PolarisRuleBasedRouterPropertiesTest { + + PolarisRuleBasedRouterProperties properties; + + @Before + public void setUp() { + properties = new PolarisRuleBasedRouterProperties(); + } + + @Test + public void isEnabled() { + assertThat(properties.isEnabled()).isEqualTo(true); + } + + @Test + public void setEnabled() { + properties.setEnabled(false); + assertThat(properties.isEnabled()).isEqualTo(false); + } + + @Test + public void testToString() { + assertThat(properties.toString()) + .isEqualTo("PolarisNearByRouterProperties{enabled=true}"); + } + +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/endpoint/PolarisRouterEndpointAutoConfigurationTests.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/endpoint/PolarisRouterEndpointAutoConfigurationTests.java new file mode 100644 index 000000000..b5f8912f6 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/endpoint/PolarisRouterEndpointAutoConfigurationTests.java @@ -0,0 +1,65 @@ +/* + * 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.router.endpoint; + +import com.tencent.cloud.polaris.context.ServiceRuleManager; +import com.tencent.polaris.client.api.SDKContext; +import org.junit.Test; + +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * test for {@link PolarisRouterEndpointAutoConfiguration}. + * @author dongyinuo + */ +public class PolarisRouterEndpointAutoConfigurationTests { + + private ServiceRuleManager serviceRuleManager; + + private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() + .withConfiguration(AutoConfigurations.of( + TestServiceRuleManagerConfiguration.class, + PolarisRouterEndpointAutoConfiguration.class)) + .withPropertyValues("endpoints.polaris-router.enabled=true"); + + @Test + public void polarisRouterEndpoint() { + contextRunner.run(context -> { + assertThat(context).hasSingleBean(PolarisRouterEndpointAutoConfiguration.class); + }); + } + + @Configuration + static class TestServiceRuleManagerConfiguration { + + @Bean + public ServiceRuleManager serviceRuleManager(SDKContext sdkContext) { + return new ServiceRuleManager(sdkContext); + } + + @Bean + public SDKContext sdkContext() { + return SDKContext.initContext(); + } + } +} diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/feign/RouterLabelFeignInterceptorTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/feign/RouterLabelFeignInterceptorTest.java index ee589d637..cd358d02b 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/feign/RouterLabelFeignInterceptorTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/feign/RouterLabelFeignInterceptorTest.java @@ -46,6 +46,7 @@ import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; @@ -72,6 +73,8 @@ public class RouterLabelFeignInterceptorTest { Collections.singletonList(routerLabelResolver), staticMetadataManager, routerRuleLabelResolver, polarisContextProperties); + assertThat(routerLabelFeignInterceptor.getOrder()).isEqualTo(0); + // mock request template RequestTemplate requestTemplate = new RequestTemplate(); String headerUidKey = "uid"; diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/scg/PolarisLoadBalancerClientFilterTest.java b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/scg/PolarisLoadBalancerClientFilterTest.java index 34920cc3b..efcbb7d1c 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/scg/PolarisLoadBalancerClientFilterTest.java +++ b/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/scg/PolarisLoadBalancerClientFilterTest.java @@ -52,6 +52,7 @@ import org.springframework.mock.web.server.MockServerWebExchange; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -88,7 +89,7 @@ public class PolarisLoadBalancerClientFilterTest { mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString())) .thenReturn(callerService); - MetadataContext metadataContext = Mockito.mock(MetadataContext.class); + MetadataContext metadataContext = mock(MetadataContext.class); // mock transitive metadata Map transitiveLabels = new HashMap<>();