From 90c60f99254ed86ac9ce89ddaaf321cf0dbc7da8 Mon Sep 17 00:00:00 2001 From: lepdou Date: Mon, 24 Oct 2022 16:21:06 +0800 Subject: [PATCH 1/4] release 1.8.0 version (#660) --- CHANGELOG.md | 15 --------------- README-zh.md | 2 +- README.md | 2 +- changes/changes-1.8.0.md | 17 +++++++++++++++++ pom.xml | 2 +- spring-cloud-tencent-dependencies/pom.xml | 2 +- 6 files changed, 21 insertions(+), 19 deletions(-) create mode 100644 changes/changes-1.8.0.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 342cd58c4..671ece8fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,17 +1,2 @@ # Change Log --- - -- [Optimize: remove discovery module useless code](https://github.com/Tencent/spring-cloud-tencent/pull/596) -- [Optimize: optimize configuration conditional & optimize config data tips"](https://github.com/Tencent/spring-cloud-tencent/pull/604) -- [Optimize: Maybe remove Chinese characters](https://github.com/Tencent/spring-cloud-tencent/pull/608) -- [Optimize: InstanceId of PolarisRegistration and PolarisServiceRegistry](https://github.com/Tencent/spring-cloud-tencent/pull/611) -- [Bugfix: fix feign report call result error when using feign direct call](https://github.com/Tencent/spring-cloud-tencent/pull/622) -- [remove useless code for router](https://github.com/Tencent/spring-cloud-tencent/pull/625) -- [Feature: support spring-retry router](https://github.com/Tencent/spring-cloud-tencent/pull/633) -- [Bugfix: fix throw npe when router context is null](https://github.com/Tencent/spring-cloud-tencent/pull/634) -- [feat:Transfer http headers specified by environment variables](https://github.com/Tencent/spring-cloud-tencent/pull/637) -- [feat:support new label expression](https://github.com/Tencent/spring-cloud-tencent/pull/641) -- [feat:support read config file from local file system.](https://github.com/Tencent/spring-cloud-tencent/pull/643) -- [feat:report the labels when using RestTemplate.](https://github.com/Tencent/spring-cloud-tencent/pull/644) -- [Optimize: optimize transfer metadata code](https://github.com/Tencent/spring-cloud-tencent/pull/650) -- [feat: publish spring event named ConfigChangeSpringEvent when the configuration is changed](https://github.com/Tencent/spring-cloud-tencent/pull/652) diff --git a/README-zh.md b/README-zh.md index da3ad2a7b..a83ec4eab 100644 --- a/README-zh.md +++ b/README-zh.md @@ -70,7 +70,7 @@ Spring Cloud Tencent 所有组件都已上传到 Maven 中央仓库,只需要 com.tencent.cloud spring-cloud-tencent-dependencies - 1.7.0-2020.0.5 + 1.8.0-2020.0.5 pom import diff --git a/README.md b/README.md index 73b99e793..69c4a6584 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ For example: com.tencent.cloud spring-cloud-tencent-dependencies - 1.7.0-2020.0.5 + 1.8.0-2020.0.5 pom import diff --git a/changes/changes-1.8.0.md b/changes/changes-1.8.0.md new file mode 100644 index 000000000..342cd58c4 --- /dev/null +++ b/changes/changes-1.8.0.md @@ -0,0 +1,17 @@ +# Change Log +--- + +- [Optimize: remove discovery module useless code](https://github.com/Tencent/spring-cloud-tencent/pull/596) +- [Optimize: optimize configuration conditional & optimize config data tips"](https://github.com/Tencent/spring-cloud-tencent/pull/604) +- [Optimize: Maybe remove Chinese characters](https://github.com/Tencent/spring-cloud-tencent/pull/608) +- [Optimize: InstanceId of PolarisRegistration and PolarisServiceRegistry](https://github.com/Tencent/spring-cloud-tencent/pull/611) +- [Bugfix: fix feign report call result error when using feign direct call](https://github.com/Tencent/spring-cloud-tencent/pull/622) +- [remove useless code for router](https://github.com/Tencent/spring-cloud-tencent/pull/625) +- [Feature: support spring-retry router](https://github.com/Tencent/spring-cloud-tencent/pull/633) +- [Bugfix: fix throw npe when router context is null](https://github.com/Tencent/spring-cloud-tencent/pull/634) +- [feat:Transfer http headers specified by environment variables](https://github.com/Tencent/spring-cloud-tencent/pull/637) +- [feat:support new label expression](https://github.com/Tencent/spring-cloud-tencent/pull/641) +- [feat:support read config file from local file system.](https://github.com/Tencent/spring-cloud-tencent/pull/643) +- [feat:report the labels when using RestTemplate.](https://github.com/Tencent/spring-cloud-tencent/pull/644) +- [Optimize: optimize transfer metadata code](https://github.com/Tencent/spring-cloud-tencent/pull/650) +- [feat: publish spring event named ConfigChangeSpringEvent when the configuration is changed](https://github.com/Tencent/spring-cloud-tencent/pull/652) diff --git a/pom.xml b/pom.xml index 9d6befd8e..5bb4de41c 100644 --- a/pom.xml +++ b/pom.xml @@ -89,7 +89,7 @@ - 1.8.0-2020.0.5-SNAPSHOT + 1.8.0-2020.0.5 2020.0.5 diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 038166f96..5c5b524a5 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -70,7 +70,7 @@ - 1.8.0-2020.0.5-SNAPSHOT + 1.8.0-2020.0.5 1.9.1 From 9d5cf90d3504be793439d81dd181604705245a09 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Wed, 26 Oct 2022 15:10:13 +0800 Subject: [PATCH 2/4] fix:fix transfer http headers not working bug. (#665) --- .github/workflows/codecov.yml | 34 ------------------- CHANGELOG.md | 2 ++ README-zh.md | 15 +++++--- README.md | 6 ++-- pom.xml | 2 +- .../EncodeTransferMedataFeignInterceptor.java | 2 +- ...TransferMedataRestTemplateInterceptor.java | 2 +- spring-cloud-tencent-dependencies/pom.xml | 2 +- .../gateway-scg-service/pom.xml | 5 +++ 9 files changed, 25 insertions(+), 45 deletions(-) delete mode 100644 .github/workflows/codecov.yml diff --git a/.github/workflows/codecov.yml b/.github/workflows/codecov.yml deleted file mode 100644 index 721286f3d..000000000 --- a/.github/workflows/codecov.yml +++ /dev/null @@ -1,34 +0,0 @@ -name: Codecov - -on: - push: - branches: - - hoxton - - 2021.0 - - 2020.0 - - greenwich - pull_request: - branches: - - hoxton - - 2021.0 - - 2020.0 - - greenwich - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - name: Checkout codes - uses: actions/checkout@v3 - - name: Set up JDK 8 - uses: actions/setup-java@v3 - with: - distribution: 'temurin' - java-version: 8 - - name: Test with Maven - run: mvn -B test --file pom.xml - - name: Upload coverage to Codecov - uses: codecov/codecov-action@v3 - with: - file: ${{ github.workspace }}/target/site/jacoco/jacoco.xml diff --git a/CHANGELOG.md b/CHANGELOG.md index 671ece8fb..0304414e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,2 +1,4 @@ # Change Log --- + +- [fix:fix transfer http headers not working bug.](https://github.com/Tencent/spring-cloud-tencent/pull/665) diff --git a/README-zh.md b/README-zh.md index a83ec4eab..c9bde3337 100644 --- a/README-zh.md +++ b/README-zh.md @@ -17,9 +17,11 @@ Spring Cloud Tencent 是腾讯开源的一站式微服务解决方案。 -Spring Cloud Tencent 实现了Spring Cloud 标准微服务 SPI,开发者可以基于 Spring Cloud Tencent 快速开发 Spring Cloud 云原生分布式应用。 +Spring Cloud Tencent 实现了Spring Cloud 标准微服务 SPI,开发者可以基于 Spring Cloud Tencent 快速开发 Spring Cloud +云原生分布式应用。 -Spring Cloud Tencent 的核心依托腾讯开源的一站式服务发现与治理平台 [Polaris](https://github.com/polarismesh/polaris),实现各种分布式微服务场景。 +Spring Cloud Tencent 的核心依托腾讯开源的一站式服务发现与治理平台 [Polaris](https://github.com/polarismesh/polaris) +,实现各种分布式微服务场景。 - [Polaris Github home page](https://github.com/polarismesh/polaris) - [Polaris official website](https://polarismesh.cn/) @@ -45,7 +47,8 @@ Spring Cloud Tencent提供的能力包括但不限于: - 控制面地址: `grpc://183.47.111.80:8091` - -`spring-cloud-tencent-example` 下 example 地址都默认指向了体验服务地址(`grpc://183.47.111.80:8091`),如果您只是体验 Spring Cloud +`spring-cloud-tencent-example` 下 example 地址都默认指向了体验服务地址(`grpc://183.47.111.80:8091`),如果您只是体验 +Spring Cloud Tencent,可直接一键运行任何 example。 ## 管控台 @@ -58,7 +61,9 @@ Spring Cloud Tencent 所有组件都已上传到 Maven 中央仓库,只需要 > 注意: > -> Spring Cloud Tencent 的版本列表可以查看 [Spring Cloud Tencent 版本管理](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-%E7%89%88%E6%9C%AC%E7%AE%A1%E7%90%86) 。 +> Spring Cloud Tencent +> 的版本列表可以查看 [Spring Cloud Tencent 版本管理](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-%E7%89%88%E6%9C%AC%E7%AE%A1%E7%90%86) +> 。 例如: @@ -70,7 +75,7 @@ Spring Cloud Tencent 所有组件都已上传到 Maven 中央仓库,只需要 com.tencent.cloud spring-cloud-tencent-dependencies - 1.8.0-2020.0.5 + 1.8.1-2020.0.5 pom import diff --git a/README.md b/README.md index 69c4a6584..6d7a51d0e 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,9 @@ dependencies. > Notice: > -> The version list of Spring Cloud Tencent can be found in [Spring Cloud Tencent Version Management](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-%E7%89%88%E6%9C%AC%E7%AE%A1%E7%90%86). +> The version list of Spring Cloud Tencent can be found +> in [Spring Cloud Tencent Version Management](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-%E7%89%88%E6%9C%AC%E7%AE%A1%E7%90%86) +> . For example: @@ -72,7 +74,7 @@ For example: com.tencent.cloud spring-cloud-tencent-dependencies - 1.8.0-2020.0.5 + 1.8.1-2020.0.5 pom import diff --git a/pom.xml b/pom.xml index 5bb4de41c..abc2f3ec5 100644 --- a/pom.xml +++ b/pom.xml @@ -89,7 +89,7 @@ - 1.8.0-2020.0.5 + 1.8.1-2020.0.5 2020.0.5 diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataFeignInterceptor.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataFeignInterceptor.java index 68d3ed000..0cfc0f142 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataFeignInterceptor.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataFeignInterceptor.java @@ -60,7 +60,7 @@ public class EncodeTransferMedataFeignInterceptor implements RequestInterceptor, MetadataContext metadataContext = MetadataContextHolder.get(); Map customMetadata = metadataContext.getCustomMetadata(); Map disposableMetadata = metadataContext.getDisposableMetadata(); - Map transHeaders = metadataContext.getTransHeaders(); + Map transHeaders = metadataContext.getTransHeadersKV(); this.buildMetadataHeader(requestTemplate, disposableMetadata, CUSTOM_DISPOSABLE_METADATA); diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataRestTemplateInterceptor.java b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataRestTemplateInterceptor.java index 9420b6283..2e360cdd8 100644 --- a/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataRestTemplateInterceptor.java +++ b/spring-cloud-starter-tencent-metadata-transfer/src/main/java/com/tencent/cloud/metadata/core/EncodeTransferMedataRestTemplateInterceptor.java @@ -60,7 +60,7 @@ public class EncodeTransferMedataRestTemplateInterceptor implements ClientHttpRe MetadataContext metadataContext = MetadataContextHolder.get(); Map customMetadata = metadataContext.getCustomMetadata(); Map disposableMetadata = metadataContext.getDisposableMetadata(); - Map transHeaders = metadataContext.getTransHeaders(); + Map transHeaders = metadataContext.getTransHeadersKV(); // build custom disposable metadata request header this.buildMetadataHeader(httpRequest, disposableMetadata, CUSTOM_DISPOSABLE_METADATA); diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 5c5b524a5..8d52d261b 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -70,7 +70,7 @@ - 1.8.0-2020.0.5 + 1.8.1-2020.0.5 1.9.1 diff --git a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/pom.xml b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/pom.xml index e79635589..69bf28210 100644 --- a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/pom.xml +++ b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/pom.xml @@ -34,6 +34,11 @@ spring-cloud-tencent-gateway-plugin + + com.tencent.cloud + spring-cloud-starter-tencent-metadata-transfer + + com.tencent.cloud spring-cloud-tencent-featureenv-plugin From 625f919cf4aa18d7ccfe40a2806bb273a6e86376 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Wed, 26 Oct 2022 16:45:55 +0800 Subject: [PATCH 3/4] feat:start 1.9.0. (#666) --- CHANGELOG.md | 1 - changes/changes-1.8.1.md | 4 ++++ pom.xml | 2 +- spring-cloud-tencent-dependencies/pom.xml | 2 +- 4 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 changes/changes-1.8.1.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 0304414e4..06c1f8175 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,3 @@ # Change Log --- -- [fix:fix transfer http headers not working bug.](https://github.com/Tencent/spring-cloud-tencent/pull/665) diff --git a/changes/changes-1.8.1.md b/changes/changes-1.8.1.md new file mode 100644 index 000000000..0304414e4 --- /dev/null +++ b/changes/changes-1.8.1.md @@ -0,0 +1,4 @@ +# Change Log +--- + +- [fix:fix transfer http headers not working bug.](https://github.com/Tencent/spring-cloud-tencent/pull/665) diff --git a/pom.xml b/pom.xml index abc2f3ec5..9c0b5ff61 100644 --- a/pom.xml +++ b/pom.xml @@ -89,7 +89,7 @@ - 1.8.1-2020.0.5 + 1.9.0-2020.0.5-SNAPSHOT 2020.0.5 diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 8d52d261b..dd1d3bc23 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -70,7 +70,7 @@ - 1.8.1-2020.0.5 + 1.9.0-2020.0.5-SNAPSHOT 1.9.1 From 61846a3aaf142f6065fb53de74f036758d2f4e42 Mon Sep 17 00:00:00 2001 From: "lingxiao,wu" <51630311+lingxiao-wu@users.noreply.github.com> Date: Fri, 28 Oct 2022 10:21:59 +0800 Subject: [PATCH 4/4] Optimize:optimize SpringValueProcessor (#668) --- CHANGELOG.md | 1 + .../annotation/SpringValueProcessor.java | 76 +++++++++-- .../SpringValueDefinitionProcessor.java | 126 ------------------ .../annotation/SpringValueProcessorTest.java | 54 ++++++++ .../SpringValueDefinitionProcessorTest.java | 75 ----------- .../src/test/resources/bean.xml | 24 ---- 6 files changed, 123 insertions(+), 233 deletions(-) delete mode 100644 spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinitionProcessor.java delete mode 100644 spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinitionProcessorTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 06c1f8175..368093730 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ # Change Log --- +- [Optimize:optimize SpringValueProcessor.](https://github.com/Tencent/spring-cloud-tencent/pull/668) diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessor.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessor.java index 26635ed44..562fc67f2 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessor.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessor.java @@ -22,27 +22,34 @@ import java.lang.reflect.Field; import java.lang.reflect.Member; import java.lang.reflect.Method; import java.util.Collection; +import java.util.List; +import java.util.Map; import java.util.Set; import com.google.common.collect.LinkedListMultimap; +import com.google.common.collect.Maps; import com.google.common.collect.Multimap; +import com.google.common.collect.Sets; 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; import com.tencent.cloud.polaris.config.spring.property.SpringValueDefinition; -import com.tencent.cloud.polaris.config.spring.property.SpringValueDefinitionProcessor; import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; import org.springframework.beans.BeansException; +import org.springframework.beans.MutablePropertyValues; +import org.springframework.beans.PropertyValue; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.annotation.Value; -import org.springframework.beans.factory.config.BeanFactoryPostProcessor; +import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.beans.factory.config.TypedStringValue; import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; import org.springframework.context.annotation.Bean; import org.springframework.lang.NonNull; @@ -55,10 +62,13 @@ import org.springframework.lang.NonNull; * * @author weihubeats 2022-7-10 */ -public class SpringValueProcessor extends PolarisProcessor implements BeanFactoryPostProcessor, BeanFactoryAware { +public class SpringValueProcessor extends PolarisProcessor implements BeanDefinitionRegistryPostProcessor, BeanFactoryAware { private static final Logger LOGGER = LoggerFactory.getLogger(SpringValueProcessor.class); + private static final Set PROPERTY_VALUES_PROCESSED_BEAN_FACTORIES = Sets.newConcurrentHashSet(); + private static final Map> BEAN_DEFINITION_REGISTRY_MULTIMAP_CONCURRENT_MAP = + Maps.newConcurrentMap(); private final PolarisConfigProperties polarisConfigProperties; private final PlaceholderHelper placeholderHelper; private final SpringValueRegistry springValueRegistry; @@ -79,8 +89,7 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanFactor public void postProcessBeanFactory(@NonNull ConfigurableListableBeanFactory beanFactory) throws BeansException { if (polarisConfigProperties.isAutoRefresh() && beanFactory instanceof BeanDefinitionRegistry) { - beanName2SpringValueDefinitions = SpringValueDefinitionProcessor - .getBeanName2SpringValueDefinitions((BeanDefinitionRegistry) beanFactory); + beanName2SpringValueDefinitions = this.getBeanName2SpringValueDefinitions((BeanDefinitionRegistry) beanFactory); } } @@ -126,6 +135,18 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanFactor doRegister(bean, beanName, method, value); } + @Override + public void setBeanFactory(@NonNull BeanFactory beanFactory) throws BeansException { + this.beanFactory = beanFactory; + } + + @Override + public void postProcessBeanDefinitionRegistry(@NonNull BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException { + if (polarisConfigProperties.isAutoRefresh()) { + processPropertyValues(beanDefinitionRegistry); + } + } + private void doRegister(Object bean, String beanName, Member member, Value value) { Set keys = placeholderHelper.extractPlaceholderKeys(value.value()); if (keys.isEmpty()) { @@ -182,8 +203,47 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanFactor beanName2SpringValueDefinitions.removeAll(beanName); } - @Override - public void setBeanFactory(@NonNull BeanFactory beanFactory) throws BeansException { - this.beanFactory = beanFactory; + private Multimap getBeanName2SpringValueDefinitions(BeanDefinitionRegistry registry) { + Multimap springValueDefinitions = BEAN_DEFINITION_REGISTRY_MULTIMAP_CONCURRENT_MAP.remove(registry); + if (springValueDefinitions == null) { + springValueDefinitions = LinkedListMultimap.create(); + } + return springValueDefinitions; + } + + private void processPropertyValues(BeanDefinitionRegistry beanRegistry) { + if (!PROPERTY_VALUES_PROCESSED_BEAN_FACTORIES.add(beanRegistry)) { + // already initialized + return; + } + + if (!BEAN_DEFINITION_REGISTRY_MULTIMAP_CONCURRENT_MAP.containsKey(beanRegistry)) { + BEAN_DEFINITION_REGISTRY_MULTIMAP_CONCURRENT_MAP.put(beanRegistry, LinkedListMultimap.create()); + } + + Multimap springValueDefinitions = BEAN_DEFINITION_REGISTRY_MULTIMAP_CONCURRENT_MAP.get(beanRegistry); + + String[] beanNames = beanRegistry.getBeanDefinitionNames(); + for (String beanName : beanNames) { + BeanDefinition beanDefinition = beanRegistry.getBeanDefinition(beanName); + MutablePropertyValues mutablePropertyValues = beanDefinition.getPropertyValues(); + List propertyValues = mutablePropertyValues.getPropertyValueList(); + for (PropertyValue propertyValue : propertyValues) { + Object value = propertyValue.getValue(); + if (!(value instanceof TypedStringValue)) { + continue; + } + String placeholder = ((TypedStringValue) value).getValue(); + Set keys = placeholderHelper.extractPlaceholderKeys(placeholder); + + if (keys.isEmpty()) { + continue; + } + + for (String key : keys) { + springValueDefinitions.put(beanName, new SpringValueDefinition(key, placeholder, propertyValue.getName())); + } + } + } } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinitionProcessor.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinitionProcessor.java deleted file mode 100644 index 9064667c0..000000000 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinitionProcessor.java +++ /dev/null @@ -1,126 +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.spring.property; - -import java.util.List; -import java.util.Map; -import java.util.Set; - -import com.google.common.collect.LinkedListMultimap; -import com.google.common.collect.Maps; -import com.google.common.collect.Multimap; -import com.google.common.collect.Sets; -import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; - -import org.springframework.beans.BeansException; -import org.springframework.beans.MutablePropertyValues; -import org.springframework.beans.PropertyValue; -import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; -import org.springframework.beans.factory.config.TypedStringValue; -import org.springframework.beans.factory.support.BeanDefinitionRegistry; -import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; -import org.springframework.lang.NonNull; - -/** - * To process xml config placeholders, e.g. - * - *
- *  <bean class="com.demo.bean.XmlBean">
- *    <property name="timeout" value="${timeout:200}"/>
- *    <property name="batch" value="${batch:100}"/>
- *  </bean>
- * 
- * - * This source file was originally from: - * - * SpringValueDefinitionProcessor - * - * @author weihubeats 2022-7-10 - */ -public class SpringValueDefinitionProcessor implements BeanDefinitionRegistryPostProcessor { - private static final Map> beanName2SpringValueDefinitions = - Maps.newConcurrentMap(); - private static final Set PROPERTY_VALUES_PROCESSED_BEAN_FACTORIES = Sets.newConcurrentHashSet(); - - private final PlaceholderHelper placeholderHelper; - - private final PolarisConfigProperties polarisConfigProperties; - - public SpringValueDefinitionProcessor(PlaceholderHelper placeholderHelper, PolarisConfigProperties polarisConfigProperties) { - this.polarisConfigProperties = polarisConfigProperties; - this.placeholderHelper = placeholderHelper; - } - - public static Multimap getBeanName2SpringValueDefinitions(BeanDefinitionRegistry registry) { - Multimap springValueDefinitions = beanName2SpringValueDefinitions.get(registry); - if (springValueDefinitions == null) { - springValueDefinitions = LinkedListMultimap.create(); - } - - return springValueDefinitions; - } - - @Override - public void postProcessBeanDefinitionRegistry(@NonNull BeanDefinitionRegistry registry) throws BeansException { - if (polarisConfigProperties.isAutoRefresh()) { - processPropertyValues(registry); - } - } - - @Override - public void postProcessBeanFactory(@NonNull ConfigurableListableBeanFactory beanFactory) throws BeansException { - - } - - private void processPropertyValues(BeanDefinitionRegistry beanRegistry) { - if (!PROPERTY_VALUES_PROCESSED_BEAN_FACTORIES.add(beanRegistry)) { - // already initialized - return; - } - - if (!beanName2SpringValueDefinitions.containsKey(beanRegistry)) { - beanName2SpringValueDefinitions.put(beanRegistry, LinkedListMultimap.create()); - } - - Multimap springValueDefinitions = beanName2SpringValueDefinitions.get(beanRegistry); - - String[] beanNames = beanRegistry.getBeanDefinitionNames(); - for (String beanName : beanNames) { - BeanDefinition beanDefinition = beanRegistry.getBeanDefinition(beanName); - MutablePropertyValues mutablePropertyValues = beanDefinition.getPropertyValues(); - List propertyValues = mutablePropertyValues.getPropertyValueList(); - for (PropertyValue propertyValue : propertyValues) { - Object value = propertyValue.getValue(); - if (!(value instanceof TypedStringValue)) { - continue; - } - String placeholder = ((TypedStringValue) value).getValue(); - Set keys = placeholderHelper.extractPlaceholderKeys(placeholder); - - if (keys.isEmpty()) { - continue; - } - - for (String key : keys) { - springValueDefinitions.put(beanName, new SpringValueDefinition(key, placeholder, propertyValue.getName())); - } - } - } - } -} diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessorTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessorTest.java index 99f1b9b14..b4bd33afd 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessorTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/annotation/SpringValueProcessorTest.java @@ -27,6 +27,7 @@ import java.util.Optional; import com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration; import com.tencent.cloud.polaris.config.enums.RefreshType; +import com.tencent.cloud.polaris.config.spring.property.Person; import com.tencent.cloud.polaris.config.spring.property.SpringValue; import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry; import com.tencent.polaris.api.utils.CollectionUtils; @@ -43,6 +44,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; import org.springframework.stereotype.Component; /** @@ -138,6 +140,53 @@ public class SpringValueProcessorTest { }); } + @Test + public void xmlBeamDefinitionTest() { + ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(PolarisConfigBootstrapAutoConfiguration.class)) + .withConfiguration(AutoConfigurations.of(RefreshAutoConfiguration.class)) + .withConfiguration(AutoConfigurations.of(XMLBeamDefinitionTest.class)) + .withConfiguration(AutoConfigurations.of(PolarisConfigAutoConfiguration.class)) + .withPropertyValues("spring.application.name=" + "conditionalOnConfigReflectEnabledTest") + .withPropertyValues("spring.cloud.polaris.address=grpc://127.0.0.1:10081") + .withPropertyValues("spring.cloud.polaris.config.refresh-type=" + RefreshType.REFLECT) + .withPropertyValues("spring.cloud.polaris.config.enabled=true") + .withPropertyValues("name=test"); + contextRunner.run(context -> { + Person person = context.getBean(Person.class); + + SpringValueRegistry springValueRegistry = context.getBean(SpringValueRegistry.class); + BeanFactory beanFactory = person.getBeanFactory(); + Collection name = springValueRegistry.get(beanFactory, "name"); + Assert.assertFalse(CollectionUtils.isEmpty(name)); + Optional nameSpringValueOptional = name.stream().findAny(); + Assert.assertTrue(nameSpringValueOptional.isPresent()); + + SpringValue nameSpringValue = nameSpringValueOptional.get(); + Method method = nameSpringValue.getMethodParameter().getMethod(); + Assert.assertTrue(Objects.nonNull(method)); + Assert.assertEquals("setName", method.getName()); + Assert.assertEquals("${name:test}", nameSpringValue.getPlaceholder()); + Assert.assertFalse(nameSpringValue.isField()); + Assert.assertEquals(String.class, nameSpringValue.getTargetType()); + + + Collection age = springValueRegistry.get(beanFactory, "age"); + Assert.assertFalse(CollectionUtils.isEmpty(age)); + Optional ageSpringValueOptional = age.stream().findAny(); + Assert.assertTrue(ageSpringValueOptional.isPresent()); + + SpringValue ageSpringValue = ageSpringValueOptional.get(); + Method method1 = ageSpringValue.getMethodParameter().getMethod(); + Assert.assertTrue(Objects.nonNull(method1)); + Assert.assertEquals("setAge", method1.getName()); + Assert.assertEquals("${age:10}", ageSpringValue.getPlaceholder()); + Assert.assertFalse(ageSpringValue.isField()); + Assert.assertEquals(String.class, ageSpringValue.getTargetType()); + }); + + } + @Configuration @EnableAutoConfiguration static class PolarisConfigAutoConfiguration { @@ -173,4 +222,9 @@ public class SpringValueProcessorTest { ValueTest.name = name; } } + + @Configuration + @ImportResource("classpath:bean.xml") + static class XMLBeamDefinitionTest { + } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinitionProcessorTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinitionProcessorTest.java deleted file mode 100644 index c82980a41..000000000 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/property/SpringValueDefinitionProcessorTest.java +++ /dev/null @@ -1,75 +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.spring.property; - -import java.lang.reflect.Method; -import java.util.Collection; -import java.util.Objects; -import java.util.Optional; - -import com.tencent.polaris.api.utils.CollectionUtils; -import org.junit.Assert; -import org.junit.Test; - -import org.springframework.beans.factory.BeanFactory; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -/** - * Test for {@link SpringValueDefinitionProcessor}. - * - * @author lingxiao.wlx - */ -public class SpringValueDefinitionProcessorTest { - - @Test - public void springValueDefinitionProcessorTest() { - ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); - Person person = context.getBean(Person.class); - - SpringValueRegistry springValueRegistry = context.getBean(SpringValueRegistry.class); - - BeanFactory beanFactory = person.getBeanFactory(); - Collection name = springValueRegistry.get(beanFactory, "name"); - Assert.assertFalse(CollectionUtils.isEmpty(name)); - Optional nameSpringValueOptional = name.stream().findAny(); - Assert.assertTrue(nameSpringValueOptional.isPresent()); - - SpringValue nameSpringValue = nameSpringValueOptional.get(); - Method method = nameSpringValue.getMethodParameter().getMethod(); - Assert.assertTrue(Objects.nonNull(method)); - Assert.assertEquals("setName", method.getName()); - Assert.assertEquals("${name:test}", nameSpringValue.getPlaceholder()); - Assert.assertFalse(nameSpringValue.isField()); - Assert.assertEquals(String.class, nameSpringValue.getTargetType()); - - - Collection age = springValueRegistry.get(beanFactory, "age"); - Assert.assertFalse(CollectionUtils.isEmpty(age)); - Optional ageSpringValueOptional = age.stream().findAny(); - Assert.assertTrue(ageSpringValueOptional.isPresent()); - - SpringValue ageSpringValue = ageSpringValueOptional.get(); - Method method1 = ageSpringValue.getMethodParameter().getMethod(); - Assert.assertTrue(Objects.nonNull(method1)); - Assert.assertEquals("setAge", method1.getName()); - Assert.assertEquals("${age:10}", ageSpringValue.getPlaceholder()); - Assert.assertFalse(ageSpringValue.isField()); - Assert.assertEquals(String.class, ageSpringValue.getTargetType()); - } -} diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/resources/bean.xml b/spring-cloud-starter-tencent-polaris-config/src/test/resources/bean.xml index e60613a5a..e6484c467 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/resources/bean.xml +++ b/spring-cloud-starter-tencent-polaris-config/src/test/resources/bean.xml @@ -11,28 +11,4 @@ - - - - - - - - - - - - - - - - - - - - - - - -