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 3976a7d01..980b985d8 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 @@ -35,6 +35,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.SearchStrategy; import org.springframework.cloud.context.properties.ConfigurationPropertiesBeans; import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder; +import org.springframework.cloud.context.refresh.ContextRefresher; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -53,9 +54,10 @@ public class PolarisConfigAutoConfiguration { PolarisConfigProperties polarisConfigProperties, PolarisPropertySourceManager polarisPropertySourceManager, SpringValueRegistry springValueRegistry, - PlaceholderHelper placeholderHelper) { + PlaceholderHelper placeholderHelper, + ContextRefresher contextRefresher) { return new PolarisPropertySourceAutoRefresher(polarisConfigProperties, - polarisPropertySourceManager, springValueRegistry, placeholderHelper); + polarisPropertySourceManager, springValueRegistry, placeholderHelper, contextRefresher); } @Bean diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceAutoRefresher.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceAutoRefresher.java index fd70a8fa9..9aa001194 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceAutoRefresher.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceAutoRefresher.java @@ -25,6 +25,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import com.tencent.cloud.common.util.JacksonUtils; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; +import com.tencent.cloud.polaris.config.enums.RefreshType; import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper; import com.tencent.cloud.polaris.config.spring.property.SpringValue; import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry; @@ -40,6 +41,7 @@ import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.cloud.context.environment.EnvironmentChangeEvent; +import org.springframework.cloud.context.refresh.ContextRefresher; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationListener; @@ -61,6 +63,8 @@ public class PolarisPropertySourceAutoRefresher private final PolarisPropertySourceManager polarisPropertySourceManager; + private final ContextRefresher contextRefresher; + private final AtomicBoolean registered = new AtomicBoolean(false); private ConfigurableApplicationContext context; @@ -70,16 +74,17 @@ public class PolarisPropertySourceAutoRefresher private ConfigurableBeanFactory beanFactory; private final PlaceholderHelper placeholderHelper; - public PolarisPropertySourceAutoRefresher( PolarisConfigProperties polarisConfigProperties, PolarisPropertySourceManager polarisPropertySourceManager, SpringValueRegistry springValueRegistry, - PlaceholderHelper placeholderHelper) { + PlaceholderHelper placeholderHelper, + ContextRefresher contextRefresher) { this.polarisConfigProperties = polarisConfigProperties; this.polarisPropertySourceManager = polarisPropertySourceManager; this.springValueRegistry = springValueRegistry; this.placeholderHelper = placeholderHelper; + this.contextRefresher = contextRefresher; } @Override @@ -138,17 +143,25 @@ public class PolarisPropertySourceAutoRefresher break; } - Collection targetValues = springValueRegistry.get(beanFactory, changedKey); - if (targetValues == null || targetValues.isEmpty()) { - continue; - } - // update the value - for (SpringValue val : targetValues) { - updateSpringValue(val); + if (polarisConfigProperties.getRefreshType() == RefreshType.REFLECT) { + Collection targetValues = springValueRegistry.get(beanFactory, changedKey); + if (targetValues == null || targetValues.isEmpty()) { + continue; + } + // update the attribute with @Value annotation + for (SpringValue val : targetValues) { + updateSpringValue(val); + } } + } + if (polarisConfigProperties.getRefreshType() == RefreshType.REFLECT) { + // update @ConfigurationProperties beans + context.publishEvent(new EnvironmentChangeEvent(context, configKVFileChangeEvent.changedKeys())); + } + else { + contextRefresher.refresh(); } - context.publishEvent(new EnvironmentChangeEvent(context, configKVFileChangeEvent.changedKeys())); }); } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java index f39c5337f..bdd1821e0 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java @@ -19,6 +19,8 @@ package com.tencent.cloud.polaris.config.config; import java.util.List; +import com.tencent.cloud.polaris.config.enums.RefreshType; + import org.springframework.boot.context.properties.ConfigurationProperties; /** @@ -50,6 +52,11 @@ public class PolarisConfigProperties { */ private boolean autoRefresh = true; + /** + * Attribute refresh type. + */ + private RefreshType refreshType = RefreshType.REFRESH_CONTEXT; + /** * List of injected configuration files. */ @@ -87,6 +94,14 @@ public class PolarisConfigProperties { this.autoRefresh = autoRefresh; } + public RefreshType getRefreshType() { + return refreshType; + } + + public void setRefreshType(RefreshType refreshType) { + this.refreshType = refreshType; + } + public List getGroups() { return groups; } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/enums/RefreshType.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/enums/RefreshType.java new file mode 100644 index 000000000..748ef2069 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/enums/RefreshType.java @@ -0,0 +1,34 @@ +/* + * 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.enums; + +/** + * Attribute refresh type when config updated. + * @author lepdou 2022-07-26 + */ +public enum RefreshType { + + /** + * refresh spring context. + */ + REFRESH_CONTEXT, + /** + * Dynamically refresh a bean's properties via reflection calls. + */ + REFLECT +} 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 3de4948d4..666e392aa 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 @@ -47,6 +47,12 @@ "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", + "defaultValue": "refresh_context", + "description": "Attribute refresh type when config updated. refresh_context or reflect." } ] } 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 d64fcf848..03945edc1 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 @@ -26,6 +26,7 @@ import java.util.Map; import com.google.common.collect.Lists; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; +import com.tencent.cloud.polaris.config.enums.RefreshType; import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper; import com.tencent.cloud.polaris.config.spring.property.SpringValue; import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry; @@ -74,8 +75,9 @@ public class PolarisPropertiesSourceAutoRefresherTest { @Test public void testConfigFileChanged() throws Exception { PolarisPropertySourceAutoRefresher refresher = new PolarisPropertySourceAutoRefresher(polarisConfigProperties, - polarisPropertySourceManager, springValueRegistry, placeholderHelper); + polarisPropertySourceManager, springValueRegistry, placeholderHelper, contextRefresher); + when(polarisConfigProperties.getRefreshType()).thenReturn(RefreshType.REFLECT); ConfigurableApplicationContext applicationContext = mock(ConfigurableApplicationContext.class); ConfigurableListableBeanFactory beanFactory = mock(ConfigurableListableBeanFactory.class); TypeConverter typeConverter = mock(TypeConverter.class); diff --git a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/src/main/resources/bootstrap.yml index 7cfe79ded..a0956f9ae 100644 --- a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/src/main/resources/bootstrap.yml @@ -64,5 +64,6 @@ spring: logging: level: + com.tencent.cloud.plugin.gateway: debug org.springframework.cloud.gateway: info com.tencent.cloud.polaris: debug diff --git a/spring-cloud-tencent-examples/polaris-router-featureenv-example/base/base-backend/pom.xml b/spring-cloud-tencent-examples/polaris-router-featureenv-example/base/base-backend/pom.xml index 8c2c1f422..4b1c3d1e4 100644 --- a/spring-cloud-tencent-examples/polaris-router-featureenv-example/base/base-backend/pom.xml +++ b/spring-cloud-tencent-examples/polaris-router-featureenv-example/base/base-backend/pom.xml @@ -3,7 +3,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - polaris-router-featureenv-base + base com.tencent.cloud ${revision} ../pom.xml diff --git a/spring-cloud-tencent-examples/polaris-router-featureenv-example/base/base-front/pom.xml b/spring-cloud-tencent-examples/polaris-router-featureenv-example/base/base-front/pom.xml index 62f304679..38e365cf5 100644 --- a/spring-cloud-tencent-examples/polaris-router-featureenv-example/base/base-front/pom.xml +++ b/spring-cloud-tencent-examples/polaris-router-featureenv-example/base/base-front/pom.xml @@ -3,7 +3,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - polaris-router-featureenv-base + base com.tencent.cloud ${revision} ../pom.xml diff --git a/spring-cloud-tencent-examples/polaris-router-featureenv-example/base/base-middle/pom.xml b/spring-cloud-tencent-examples/polaris-router-featureenv-example/base/base-middle/pom.xml index 17cc3b701..8b0ed0024 100644 --- a/spring-cloud-tencent-examples/polaris-router-featureenv-example/base/base-middle/pom.xml +++ b/spring-cloud-tencent-examples/polaris-router-featureenv-example/base/base-middle/pom.xml @@ -3,7 +3,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - polaris-router-featureenv-base + base com.tencent.cloud ${revision} ../pom.xml diff --git a/spring-cloud-tencent-examples/polaris-router-featureenv-example/base/pom.xml b/spring-cloud-tencent-examples/polaris-router-featureenv-example/base/pom.xml index c0bea706e..0dc0c3d3d 100644 --- a/spring-cloud-tencent-examples/polaris-router-featureenv-example/base/pom.xml +++ b/spring-cloud-tencent-examples/polaris-router-featureenv-example/base/pom.xml @@ -10,7 +10,7 @@ 4.0.0 - polaris-router-featureenv-base + base pom diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-gateway-plugin/src/main/java/com/tencent/cloud/plugin/gateway/staining/TrafficStainingGatewayFilter.java b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-gateway-plugin/src/main/java/com/tencent/cloud/plugin/gateway/staining/TrafficStainingGatewayFilter.java index dda97563a..c6ddfd457 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-gateway-plugin/src/main/java/com/tencent/cloud/plugin/gateway/staining/TrafficStainingGatewayFilter.java +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-gateway-plugin/src/main/java/com/tencent/cloud/plugin/gateway/staining/TrafficStainingGatewayFilter.java @@ -26,6 +26,9 @@ import java.util.Map; import com.tencent.cloud.common.constant.MetadataConstant; import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContextHolder; +import com.tencent.cloud.common.util.JacksonUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import reactor.core.publisher.Mono; import org.springframework.cloud.gateway.filter.GatewayFilterChain; @@ -43,6 +46,8 @@ import static org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter.R */ public class TrafficStainingGatewayFilter implements GlobalFilter, Ordered { + private static final Logger LOGGER = LoggerFactory.getLogger(TrafficStainingGatewayFilter.class); + private final List trafficStainers; public TrafficStainingGatewayFilter(List trafficStainers) { @@ -87,13 +92,23 @@ public class TrafficStainingGatewayFilter implements GlobalFilter, Ordered { Map getStainedLabels(ServerWebExchange exchange) { Map stainedLabels = new HashMap<>(); int size = trafficStainers.size(); + TrafficStainer stainer = null; for (int i = size - 1; i >= 0; i--) { - TrafficStainer stainer = trafficStainers.get(i); - Map labels = stainer.apply(exchange); - if (!CollectionUtils.isEmpty(labels)) { - stainedLabels.putAll(labels); + try { + stainer = trafficStainers.get(i); + Map labels = stainer.apply(exchange); + if (!CollectionUtils.isEmpty(labels)) { + stainedLabels.putAll(labels); + } + } + catch (Exception e) { + if (stainer != null) { + LOGGER.error("[SCT] traffic stained error. stainer = {}", stainer.getClass().getName(), e); + } } } + LOGGER.debug("[SCT] traffic stained labels. {}", JacksonUtils.serialize2Json(stainedLabels)); + return stainedLabels; } diff --git a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-gateway-plugin/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-gateway-plugin/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 927618f86..a54b4b3fb 100644 --- a/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-gateway-plugin/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-tencent-plugin-starters/spring-cloud-tencent-gateway-plugin/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -9,7 +9,7 @@ { "name": "spring.cloud.tencent.plugin.scg.staining.enabled", "type": "java.lang.Boolean", - "defaultValue": true, + "defaultValue": false, "description": "the switch for spring cloud gateway staining plugin." }, {