From b4143f8e00e77bed4f8f479fe3f6eef5f111ef99 Mon Sep 17 00:00:00 2001 From: "lingxiao,wu" <51630311+lingxiao-wu@users.noreply.github.com> Date: Mon, 17 Oct 2022 11:43:22 +0800 Subject: [PATCH 01/11] =?UTF-8?q?test=EF=BC=9Atrans=20headers=20test=20(#6?= =?UTF-8?q?46)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/TransHeadersTransferTest.java | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/TransHeadersTransferTest.java diff --git a/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/TransHeadersTransferTest.java b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/TransHeadersTransferTest.java new file mode 100644 index 000000000..eb0c780f0 --- /dev/null +++ b/spring-cloud-starter-tencent-metadata-transfer/src/test/java/com/tencent/cloud/metadata/core/TransHeadersTransferTest.java @@ -0,0 +1,86 @@ +/* + * 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.metadata.core; + +import java.util.Map; + +import com.tencent.cloud.common.metadata.MetadataContext; +import com.tencent.cloud.common.metadata.MetadataContextHolder; +import com.tencent.cloud.common.util.JacksonUtils; +import org.junit.AfterClass; +import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.runner.RunWith; + +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.mock.http.server.reactive.MockServerHttpRequest; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +/** + * Test for {@link TransHeadersTransfer}. + * + * @author lingxiao.wlx + */ +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = RANDOM_PORT, + classes = DecodeTransferMetadataServletFilterTest.TestApplication.class, + properties = {"spring.config.location = classpath:application-test.yml"}) +public class TransHeadersTransferTest { + @AfterClass + public static void afterClass() { + MetadataContextHolder.remove(); + } + + @Test + public void transferServletTest() { + MetadataContext metadataContext = MetadataContextHolder.get(); + metadataContext.setTransHeaders("header1,header2,header3", ""); + MockHttpServletRequest request = new MockHttpServletRequest(); + request.addHeader("header1", "1"); + request.addHeader("header2", "2"); + request.addHeader("header3", "3"); + TransHeadersTransfer.transfer(request); + Map transHeadersKV = MetadataContextHolder.get().getTransHeadersKV(); + Assertions.assertEquals(transHeadersKV.get("header1"), "1"); + Assertions.assertEquals(transHeadersKV.get("header2"), "2"); + Assertions.assertEquals(transHeadersKV.get("header3"), "3"); + } + + @Test + public void transferReactiveTest() { + MetadataContext metadataContext = MetadataContextHolder.get(); + metadataContext.setTransHeaders("header1,header2,header3", ""); + MockServerHttpRequest.BaseBuilder builder = MockServerHttpRequest.get(""); + String[] header1 = {"1"}; + String[] header2 = {"2"}; + String[] header3 = {"3"}; + builder.header("header1", header1); + builder.header("header2", header2); + builder.header("header3", header3); + MockServerHttpRequest request = builder.build(); + TransHeadersTransfer.transfer(request); + Map transHeadersKV = MetadataContextHolder.get().getTransHeadersKV(); + Assertions.assertEquals(transHeadersKV.get("header1"), JacksonUtils.serialize2Json(header1)); + Assertions.assertEquals(transHeadersKV.get("header2"), JacksonUtils.serialize2Json(header2)); + Assertions.assertEquals(transHeadersKV.get("header3"), JacksonUtils.serialize2Json(header3)); + } +} From 3f212890426d5d94d5a48a353c23dccaabb5218a Mon Sep 17 00:00:00 2001 From: lepdou Date: Mon, 17 Oct 2022 11:59:48 +0800 Subject: [PATCH 02/11] add transfer metadata unit test change log (#648) --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a2021ef03..3cdd84ad4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,3 +20,4 @@ - [fix:cancel reporting useless metadata.](https://github.com/Tencent/spring-cloud-tencent/pull/639) - [Feature: support read config file from local file system](https://github.com/Tencent/spring-cloud-tencent/pull/640) - [fix:optimize expression parser V1.](https://github.com/Tencent/spring-cloud-tencent/pull/642) +- [Optimize: add transfer metadata unit test.](https://github.com/Tencent/spring-cloud-tencent/pull/646) From e71d845b931247f05aba75119d1bc110123269bb Mon Sep 17 00:00:00 2001 From: DerekYRC <44155264+DerekYRC@users.noreply.github.com> Date: Mon, 17 Oct 2022 13:47:14 +0800 Subject: [PATCH 03/11] Publish spring event named ConfigChangeSpringEvent when the configuration is changed. (#647) * public config change spring event * update changelog * Update ConfigChangeSpringEventTest.java add license for ConfigChangeSpringEventTest Co-authored-by: Haotian Zhang <928016560@qq.com> --- CHANGELOG.md | 1 + .../PolarisConfigChangeEventListener.java | 14 +++- .../spring/event/ConfigChangeSpringEvent.java | 70 ++++++++++++++++ .../event/ConfigChangeSpringEventTest.java | 80 +++++++++++++++++++ 4 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/event/ConfigChangeSpringEvent.java create mode 100644 spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/event/ConfigChangeSpringEventTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cdd84ad4..fa589fc70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,3 +21,4 @@ - [Feature: support read config file from local file system](https://github.com/Tencent/spring-cloud-tencent/pull/640) - [fix:optimize expression parser V1.](https://github.com/Tencent/spring-cloud-tencent/pull/642) - [Optimize: add transfer metadata unit test.](https://github.com/Tencent/spring-cloud-tencent/pull/646) +- [feat: publish spring event named ConfigChangeSpringEvent when the configuration is changed](https://github.com/Tencent/spring-cloud-tencent/pull/647) diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/listener/PolarisConfigChangeEventListener.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/listener/PolarisConfigChangeEventListener.java index 901898994..087f9f6ed 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/listener/PolarisConfigChangeEventListener.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/listener/PolarisConfigChangeEventListener.java @@ -23,11 +23,14 @@ import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; import com.google.common.collect.Maps; +import com.tencent.cloud.polaris.config.spring.event.ConfigChangeSpringEvent; import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo; import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.cloud.context.environment.EnvironmentChangeEvent; import org.springframework.context.ApplicationEvent; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.ApplicationEventPublisherAware; import org.springframework.context.ApplicationListener; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; @@ -44,10 +47,12 @@ import static com.tencent.cloud.polaris.config.listener.PolarisConfigListenerCon * * @author Elve.Xu 2022-06-08 */ -public final class PolarisConfigChangeEventListener implements ApplicationListener { +public final class PolarisConfigChangeEventListener implements ApplicationListener, ApplicationEventPublisherAware { private static final AtomicBoolean started = new AtomicBoolean(); + private ApplicationEventPublisher eventPublisher; + /** * Handle an application event. * @@ -72,6 +77,8 @@ public final class PolarisConfigChangeEventListener implements ApplicationListen ConfigurableEnvironment environment = context.getEnvironment(); Map ret = loadEnvironmentProperties(environment); Map changes = merge(ret); + ConfigChangeSpringEvent configChangeSpringEvent = new ConfigChangeSpringEvent(Maps.newHashMap(changes)); + eventPublisher.publishEvent(configChangeSpringEvent); fireConfigChange(changes.keySet(), Maps.newHashMap(changes)); changes.clear(); } @@ -106,4 +113,9 @@ public final class PolarisConfigChangeEventListener implements ApplicationListen }); return ret; } + + @Override + public void setApplicationEventPublisher(ApplicationEventPublisher eventPublisher) { + this.eventPublisher = eventPublisher; + } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/event/ConfigChangeSpringEvent.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/event/ConfigChangeSpringEvent.java new file mode 100644 index 000000000..cd5eca7ed --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/spring/event/ConfigChangeSpringEvent.java @@ -0,0 +1,70 @@ +/* + * 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.event; + +import java.util.Map; +import java.util.Set; + +import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo; + +import org.springframework.context.ApplicationEvent; + +/** + * A spring change event when config is changed. + * + * @author Derek Yi 2022-10-16 + */ +public class ConfigChangeSpringEvent extends ApplicationEvent { + /** + * @param source all changed keys map. + */ + public ConfigChangeSpringEvent(Map source) { + super(source); + } + + /** + * Get the changed keys. + * @return the list of the keys + */ + public Set changedKeys() { + return changeMap().keySet(); + } + + /** + * Get a specific change instance for the key specified. + * @param key the changed key + * @return the change instance + */ + public ConfigPropertyChangeInfo getChange(String key) { + return changeMap().get(key); + } + + /** + * Check whether the specified key is changed . + * @param key the key + * @return true if the key is changed, false otherwise. + */ + public boolean isChanged(String key) { + return changeMap().containsKey(key); + } + + private Map changeMap() { + return (Map) getSource(); + } +} diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/event/ConfigChangeSpringEventTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/event/ConfigChangeSpringEventTest.java new file mode 100644 index 000000000..2e44c476e --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/spring/event/ConfigChangeSpringEventTest.java @@ -0,0 +1,80 @@ +/* + * 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.event; + +import java.util.HashMap; +import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import com.tencent.polaris.configuration.api.core.ChangeType; +import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo; +import org.junit.Assert; +import org.junit.Test; + +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.context.ApplicationListener; +import org.springframework.context.annotation.Configuration; + + +/** + * Test for {@link ConfigChangeSpringEvent}. + * + * @author derek.yi 2022-10-16 + */ +public class ConfigChangeSpringEventTest { + + private static CountDownLatch countDownLatch = new CountDownLatch(1); + + private static AtomicInteger receiveEventTimes = new AtomicInteger(); + + @Test + public void testPublishConfigChangeSpringEvent() { + ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(ConfigChangeSpringEventListener.class)); + contextRunner.run(context -> { + HashMap changeMap = new HashMap<>(); + changeMap.put("key", new ConfigPropertyChangeInfo("key", null, "value", ChangeType.ADDED)); + context.publishEvent(new ConfigChangeSpringEvent(changeMap)); + countDownLatch.await(5, TimeUnit.SECONDS); + Assert.assertEquals(1, receiveEventTimes.get()); + }); + } + + @Configuration + static class ConfigChangeSpringEventListener implements ApplicationListener { + + @Override + public void onApplicationEvent(ConfigChangeSpringEvent event) { + Set changedKeys = event.changedKeys(); + Assert.assertEquals(1, changedKeys.size()); + Assert.assertTrue(event.isChanged("key")); + ConfigPropertyChangeInfo changeInfo = event.getChange("key"); + Assert.assertNotNull(changeInfo); + Assert.assertEquals("key", changeInfo.getPropertyName()); + Assert.assertEquals("value", changeInfo.getNewValue()); + Assert.assertEquals(ChangeType.ADDED, changeInfo.getChangeType()); + + receiveEventTimes.incrementAndGet(); + countDownLatch.countDown(); + } + } +} From 194379fd322356080003c1908c5a00fced41149d Mon Sep 17 00:00:00 2001 From: lepdou Date: Thu, 20 Oct 2022 11:25:54 +0800 Subject: [PATCH 04/11] change version to 1.8.0-2021.0.3 (#656) --- pom.xml | 2 +- spring-cloud-tencent-dependencies/pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 704fb68f0..1bd869be3 100644 --- a/pom.xml +++ b/pom.xml @@ -89,7 +89,7 @@ - 1.8.0-2021.0.3-SNAPSHOT + 1.8.0-2021.0.3 2021.0.3 diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 7e2425a9a..237919808 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -70,7 +70,7 @@ - 1.8.0-2021.0.3-SNAPSHOT + 1.8.0-2021.0.3 1.9.1 From 71aefddbb4be24ae221a4f1ad63ac3ad40efa062 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Thu, 20 Oct 2022 15:23:05 +0800 Subject: [PATCH 05/11] docs:update README. --- README-zh.md | 15 ++++++++++----- README.md | 6 ++++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/README-zh.md b/README-zh.md index 8eb306782..bf87a9ad6 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.7.0-2021.0.3 + 1.8.0-2021.0.3 pom import diff --git a/README.md b/README.md index d3a350a8d..6301c5aa6 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.7.0-2021.0.3 + 1.8.0-2021.0.3 pom import From a78c64fed66b3a77a2014462a632d389b68f6213 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Thu, 20 Oct 2022 18:21:17 +0800 Subject: [PATCH 06/11] feat:start 1.9.0. (#657) --- CHANGELOG.md | 22 --------------------- changes/changes-1.8.0.md | 24 +++++++++++++++++++++++ pom.xml | 2 +- spring-cloud-tencent-dependencies/pom.xml | 2 +- 4 files changed, 26 insertions(+), 24 deletions(-) create mode 100644 changes/changes-1.8.0.md diff --git a/CHANGELOG.md b/CHANGELOG.md index fa589fc70..671ece8fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,24 +1,2 @@ # Change Log --- - -- [Fix issue 593:When the project depends on spring-retry, executing restTemplate to call microservice and reporting NPE error](https://github.com/Tencent/spring-cloud-tencent/pull/594) -- [Optimize: remove discovery module useless code](https://github.com/Tencent/spring-cloud-tencent/pull/595) -- [Optimize: remove useless code for rest template router](https://github.com/Tencent/spring-cloud-tencent/pull/601) -- [Optimize: optimize configuration conditional & optimize config data tips](https://github.com/Tencent/spring-cloud-tencent/pull/603) -- [Automatically transmit some headers specified by an environment variable that directly defines header keys.](https://github.com/Tencent/spring-cloud-tencent/pull/606) -- [Optimize: Maybe remove Chinese characters](https://github.com/Tencent/spring-cloud-tencent/pull/607) -- [Optimize: InstanceId of PolarisRegistration and PolarisServiceRegistry](https://github.com/Tencent/spring-cloud-tencent/pull/610) -- [ fix pr #606 cause ci fail](https://github.com/Tencent/spring-cloud-tencent/pull/613) -- [fix pr 613: modify a judgment logic](https://github.com/Tencent/spring-cloud-tencent/pull/618) -- [fix pr 606: modify a part of changes requested by review.](https://github.com/Tencent/spring-cloud-tencent/pull/620) -- [Bugfix: fix feign report call result error when using feign direct call](https://github.com/Tencent/spring-cloud-tencent/pull/621) -- [remove useless code for router](https://github.com/Tencent/spring-cloud-tencent/pull/626) -- [Feature: support new label expression](https://github.com/Tencent/spring-cloud-tencent/pull/627) -- [feat:report the labels when using RestTemplate.](https://github.com/Tencent/spring-cloud-tencent/pull/629) -- [Bugfix: fix throw npe when router context is null](https://github.com/Tencent/spring-cloud-tencent/pull/635) -- [Optimize:optimize transfer](https://github.com/Tencent/spring-cloud-tencent/pull/636) -- [fix:cancel reporting useless metadata.](https://github.com/Tencent/spring-cloud-tencent/pull/639) -- [Feature: support read config file from local file system](https://github.com/Tencent/spring-cloud-tencent/pull/640) -- [fix:optimize expression parser V1.](https://github.com/Tencent/spring-cloud-tencent/pull/642) -- [Optimize: add transfer metadata unit test.](https://github.com/Tencent/spring-cloud-tencent/pull/646) -- [feat: publish spring event named ConfigChangeSpringEvent when the configuration is changed](https://github.com/Tencent/spring-cloud-tencent/pull/647) diff --git a/changes/changes-1.8.0.md b/changes/changes-1.8.0.md new file mode 100644 index 000000000..fa589fc70 --- /dev/null +++ b/changes/changes-1.8.0.md @@ -0,0 +1,24 @@ +# Change Log +--- + +- [Fix issue 593:When the project depends on spring-retry, executing restTemplate to call microservice and reporting NPE error](https://github.com/Tencent/spring-cloud-tencent/pull/594) +- [Optimize: remove discovery module useless code](https://github.com/Tencent/spring-cloud-tencent/pull/595) +- [Optimize: remove useless code for rest template router](https://github.com/Tencent/spring-cloud-tencent/pull/601) +- [Optimize: optimize configuration conditional & optimize config data tips](https://github.com/Tencent/spring-cloud-tencent/pull/603) +- [Automatically transmit some headers specified by an environment variable that directly defines header keys.](https://github.com/Tencent/spring-cloud-tencent/pull/606) +- [Optimize: Maybe remove Chinese characters](https://github.com/Tencent/spring-cloud-tencent/pull/607) +- [Optimize: InstanceId of PolarisRegistration and PolarisServiceRegistry](https://github.com/Tencent/spring-cloud-tencent/pull/610) +- [ fix pr #606 cause ci fail](https://github.com/Tencent/spring-cloud-tencent/pull/613) +- [fix pr 613: modify a judgment logic](https://github.com/Tencent/spring-cloud-tencent/pull/618) +- [fix pr 606: modify a part of changes requested by review.](https://github.com/Tencent/spring-cloud-tencent/pull/620) +- [Bugfix: fix feign report call result error when using feign direct call](https://github.com/Tencent/spring-cloud-tencent/pull/621) +- [remove useless code for router](https://github.com/Tencent/spring-cloud-tencent/pull/626) +- [Feature: support new label expression](https://github.com/Tencent/spring-cloud-tencent/pull/627) +- [feat:report the labels when using RestTemplate.](https://github.com/Tencent/spring-cloud-tencent/pull/629) +- [Bugfix: fix throw npe when router context is null](https://github.com/Tencent/spring-cloud-tencent/pull/635) +- [Optimize:optimize transfer](https://github.com/Tencent/spring-cloud-tencent/pull/636) +- [fix:cancel reporting useless metadata.](https://github.com/Tencent/spring-cloud-tencent/pull/639) +- [Feature: support read config file from local file system](https://github.com/Tencent/spring-cloud-tencent/pull/640) +- [fix:optimize expression parser V1.](https://github.com/Tencent/spring-cloud-tencent/pull/642) +- [Optimize: add transfer metadata unit test.](https://github.com/Tencent/spring-cloud-tencent/pull/646) +- [feat: publish spring event named ConfigChangeSpringEvent when the configuration is changed](https://github.com/Tencent/spring-cloud-tencent/pull/647) diff --git a/pom.xml b/pom.xml index 1bd869be3..dc449774d 100644 --- a/pom.xml +++ b/pom.xml @@ -89,7 +89,7 @@ - 1.8.0-2021.0.3 + 1.9.0-2021.0.3-SNAPSHOT 2021.0.3 diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 237919808..4b7214f9f 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -70,7 +70,7 @@ - 1.8.0-2021.0.3 + 1.9.0-2021.0.3-SNAPSHOT 1.9.1 From 3502ff5809435d7e1569d8a56b335749eb7298cb Mon Sep 17 00:00:00 2001 From: lepdou Date: Mon, 24 Oct 2022 20:55:27 +0800 Subject: [PATCH 07/11] update polaris console screenshot --- README-zh.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README-zh.md b/README-zh.md index bf87a9ad6..6345c5337 100644 --- a/README-zh.md +++ b/README-zh.md @@ -53,7 +53,7 @@ Tencent,可直接一键运行任何 example。 ## 管控台 -image +sc ## 使用指南 From 99116624565c9299c5c7331b8edbf15cf967d856 Mon Sep 17 00:00:00 2001 From: lepdou Date: Mon, 24 Oct 2022 20:56:03 +0800 Subject: [PATCH 08/11] update polaris console screenshots --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6301c5aa6..c1689ac6a 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ directly with one click. ## Screenshots -image +sc ## Use Guide From c72288cb88569fbf5c48c532dc69c0fe23af843c Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Wed, 26 Oct 2022 15:09:39 +0800 Subject: [PATCH 09/11] fix:fix transfer http headers not working bug. (#664) --- .github/workflows/codecov.yml | 34 ------------------- CHANGELOG.md | 2 ++ README-zh.md | 5 +-- README.md | 5 +-- 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, 17 insertions(+), 42 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..d102d7210 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/664) diff --git a/README-zh.md b/README-zh.md index 6345c5337..309365a7e 100644 --- a/README-zh.md +++ b/README-zh.md @@ -62,7 +62,8 @@ 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 版本管理](https://github.com/Tencent/spring-cloud-tencent/wiki/Spring-Cloud-Tencent-%E7%89%88%E6%9C%AC%E7%AE%A1%E7%90%86) > 。 例如: @@ -75,7 +76,7 @@ Spring Cloud Tencent 所有组件都已上传到 Maven 中央仓库,只需要 com.tencent.cloud spring-cloud-tencent-dependencies - 1.8.0-2021.0.3 + 1.8.1-2021.0.3 pom import diff --git a/README.md b/README.md index c1689ac6a..beb5abde4 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,8 @@ 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) +> +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: @@ -74,7 +75,7 @@ For example: com.tencent.cloud spring-cloud-tencent-dependencies - 1.8.0-2021.0.3 + 1.8.1-2021.0.3 pom import diff --git a/pom.xml b/pom.xml index dc449774d..9e5af8d98 100644 --- a/pom.xml +++ b/pom.xml @@ -89,7 +89,7 @@ - 1.9.0-2021.0.3-SNAPSHOT + 1.8.1-2021.0.3 2021.0.3 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 4b7214f9f..bb0f62f2f 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -70,7 +70,7 @@ - 1.9.0-2021.0.3-SNAPSHOT + 1.8.1-2021.0.3 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 47b2160d8d2f347fa6f7a59c48094b0d5c1a0372 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Wed, 26 Oct 2022 16:22:20 +0800 Subject: [PATCH 10/11] feat:start 1.9.0. (#667) --- CHANGELOG.md | 3 +-- changes/changes-1.8.1.md | 4 ++++ pom.xml | 2 +- spring-cloud-tencent-dependencies/pom.xml | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) create mode 100644 changes/changes-1.8.1.md diff --git a/CHANGELOG.md b/CHANGELOG.md index d102d7210..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/664) + diff --git a/changes/changes-1.8.1.md b/changes/changes-1.8.1.md new file mode 100644 index 000000000..ae0a57961 --- /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/664) diff --git a/pom.xml b/pom.xml index 9e5af8d98..dc449774d 100644 --- a/pom.xml +++ b/pom.xml @@ -89,7 +89,7 @@ - 1.8.1-2021.0.3 + 1.9.0-2021.0.3-SNAPSHOT 2021.0.3 diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index bb0f62f2f..4b7214f9f 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -70,7 +70,7 @@ - 1.8.1-2021.0.3 + 1.9.0-2021.0.3-SNAPSHOT 1.9.1 From aa841139246e84d547bb04ed97a6a6e119b1c544 Mon Sep 17 00:00:00 2001 From: "lingxiao,wu" <51630311+lingxiao-wu@users.noreply.github.com> Date: Thu, 27 Oct 2022 10:36:58 +0800 Subject: [PATCH 11/11] Optimize:optimize SpringValueProcessor. (#655) --- 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..d722db453 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ # Change Log --- +- [Optimize:optimize SpringValueProcessor.](https://github.com/Tencent/spring-cloud-tencent/pull/655) 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 @@ - - - - - - - - - - - - - - - - - - - - - - - -