From 6c8cb574d8a2113a732da9daa03e0b80088ef1de Mon Sep 17 00:00:00 2001 From: lepdou Date: Wed, 20 Apr 2022 19:52:17 +0800 Subject: [PATCH 01/16] remove spring-javaformat-maven-plugin --- CHANGELOG.md | 2 ++ pom.xml | 4 ---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 283595815..118650e29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,3 +3,5 @@ - [Feature: Support custom rate limit reject response info](https://github.com/Tencent/spring-cloud-tencent/pull/128) +- [Feature: Remove spring-javaformat-maven-plugin](https://github.com/Tencent/spring-cloud-tencent/pull/131) + diff --git a/pom.xml b/pom.xml index 92b0fb7b4..3e40e76da 100644 --- a/pom.xml +++ b/pom.xml @@ -144,10 +144,6 @@ - - io.spring.javaformat - spring-javaformat-maven-plugin - org.apache.maven.plugins maven-checkstyle-plugin From 3f787f142ecd509a550e5beb91848f12fc38b20c Mon Sep 17 00:00:00 2001 From: lepdou Date: Wed, 20 Apr 2022 19:58:25 +0800 Subject: [PATCH 02/16] Update README.md --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 2a1490990..9d0a6aa8b 100644 --- a/README.md +++ b/README.md @@ -77,3 +77,10 @@ For example: ## License The spring-cloud-tencent is licensed under the BSD 3-Clause License. Copyright and license information can be found in the file [LICENSE](LICENSE) + + +## Stargazers over time + +If you are interested in Spring Cloud Tencent, please follow our project, thank you very much. + +[![Stargazers over time](https://starchart.cc/Tencent/spring-cloud-tencent.svg)](https://starchart.cc/Tencent/spring-cloud-tencent) From 17c4df0dab872031c81164d00e5f7649b2a95c07 Mon Sep 17 00:00:00 2001 From: lepdou Date: Wed, 20 Apr 2022 19:59:06 +0800 Subject: [PATCH 03/16] Update README-zh.md --- README-zh.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README-zh.md b/README-zh.md index 540bb9ef9..4ae211484 100644 --- a/README-zh.md +++ b/README-zh.md @@ -79,3 +79,10 @@ Spring Cloud Tencent 所有组件都已上传到 Maven 中央仓库,只需要 ## License The spring-cloud-tencent is licensed under the BSD 3-Clause License. Copyright and license information can be found in the file [LICENSE](LICENSE) + +## Stargazers over time + +如果您对 Spring Cloud Tencent 有兴趣,请关注我们的项目~ + +[![Stargazers over time](https://starchart.cc/Tencent/spring-cloud-tencent.svg)](https://starchart.cc/Tencent/spring-cloud-tencent) + From f20179a7122a3568c49379f0eee5e66bc735996b Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Thu, 21 Apr 2022 11:37:21 +0800 Subject: [PATCH 04/16] Update pom.xml --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index 92b0fb7b4..f71249237 100644 --- a/pom.xml +++ b/pom.xml @@ -97,6 +97,7 @@ 0.8.3 3.2.0 1.2.7 + 3.0.1 true From 434eae8f9455927e900f30e2781661b5fb5da9cc Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Thu, 21 Apr 2022 11:47:27 +0800 Subject: [PATCH 05/16] Update pom.xml --- spring-cloud-tencent-dependencies/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 5f727c168..d4d012c8d 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -78,7 +78,7 @@ 3.2.0 3.1.1 1.2.7 - 1.6 + 3.0.1 From 6e0aadf1b9caa0ae4d08721aaa1c43265dfc4dda Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Thu, 21 Apr 2022 11:50:44 +0800 Subject: [PATCH 06/16] Update pom.xml --- spring-cloud-tencent-dependencies/pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index d4d012c8d..5a5bd16c1 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -76,7 +76,6 @@ 3.2.0 - 3.1.1 1.2.7 3.0.1 From 92448c72f5e608ba85e123c91911b14a1699351f Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Thu, 21 Apr 2022 19:53:41 +0800 Subject: [PATCH 07/16] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 9d0a6aa8b..e7b735c3a 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,9 @@ [![Build Status](https://github.com/Tencent/spring-cloud-tencent/actions/workflows/junit_test.yml/badge.svg)](https://github.com/Tencent/spring-cloud-tencent/actions/workflows/junit_test.yml) [![Maven Central](https://img.shields.io/maven-central/v/com.tencent.cloud/spring-cloud-tencent?label=Maven%20Central)](https://search.maven.org/search?q=g:com.tencent.cloud%20AND%20a:spring-cloud-tencent) +[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause) +[![Percentage of issues still open](http://isitmaintained.com/badge/open/Tencent/spring-cloud-tencent.svg)](https://github.com/Tencent/spring-cloud-tencent/issues) +[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/Tencent/spring-cloud-tencent/wiki/Contributing) English | [简体中文](./README-zh.md) From 3ef243af36f21d3331c6f3af6f2b9079167df340 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Thu, 21 Apr 2022 19:54:07 +0800 Subject: [PATCH 08/16] Update README-zh.md --- README-zh.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README-zh.md b/README-zh.md index 4ae211484..52b7f871c 100644 --- a/README-zh.md +++ b/README-zh.md @@ -2,6 +2,9 @@ [![Build Status](https://github.com/Tencent/spring-cloud-tencent/actions/workflows/junit_test.yml/badge.svg)](https://github.com/Tencent/spring-cloud-tencent/actions/workflows/junit_test.yml) [![Maven Central](https://img.shields.io/maven-central/v/com.tencent.cloud/spring-cloud-tencent?label=Maven%20Central)](https://search.maven.org/search?q=g:com.tencent.cloud%20AND%20a:spring-cloud-tencent) +[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause) +[![Percentage of issues still open](http://isitmaintained.com/badge/open/Tencent/spring-cloud-tencent.svg)](https://github.com/Tencent/spring-cloud-tencent/issues) +[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/Tencent/spring-cloud-tencent/wiki/Contributing) [English](./README.md) | 简体中文 From 3e0d2241bbf50442e50319b37fde71b0032c5902 Mon Sep 17 00:00:00 2001 From: SkyeBeFreeman <928016560@qq.com> Date: Sun, 24 Apr 2022 18:22:31 +0800 Subject: [PATCH 09/16] feat:refactor loadbalancer module as a basic module for router and circuit breaker. --- pom.xml | 1 + .../pom.xml | 2 +- .../discovery/PolarisDiscoveryHandler.java | 15 +++ .../discovery/PolarisServiceDiscovery.java | 2 +- .../polaris/ribbon/PolarisServerList.java | 2 +- .../pom.xml | 69 +------------- .../main/resources/META-INF/spring.factories | 2 - spring-cloud-tencent-dependencies/pom.xml | 8 +- spring-cloud-tencent-polaris-context/pom.xml | 94 +++++++++---------- .../pom.xml | 54 +++++++++++ .../loadbalancer/PolarisLoadBalancer.java | 63 ++++++++++--- .../PolarisLoadBalancerAutoConfiguration.java | 8 +- .../config/PolarisLoadBalancerProperties.java | 25 +++-- .../PolarisRibbonClientConfiguration.java | 24 ++--- .../rule/PolarisLoadBalanceRule.java | 2 +- .../rule/PolarisWeightedRandomRule.java | 2 +- ...itional-spring-configuration-metadata.json | 0 .../main/resources/META-INF/spring.factories | 2 + ...arisLoadBalancerAutoConfigurationTest.java | 10 +- 19 files changed, 218 insertions(+), 167 deletions(-) delete mode 100644 spring-cloud-starter-tencent-polaris-router/src/main/resources/META-INF/spring.factories create mode 100644 spring-cloud-tencent-polaris-loadbalancer/pom.xml rename spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRoutingLoadBalancer.java => spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancer.java (71%) rename spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfiguration.java => spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerAutoConfiguration.java (90%) rename spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonProperties.java => spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerProperties.java (61%) rename {spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router => spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer}/config/PolarisRibbonClientConfiguration.java (67%) rename {spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router => spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer}/rule/PolarisLoadBalanceRule.java (96%) rename {spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router => spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer}/rule/PolarisWeightedRandomRule.java (98%) rename {spring-cloud-starter-tencent-polaris-router => spring-cloud-tencent-polaris-loadbalancer}/src/main/resources/META-INF/additional-spring-configuration-metadata.json (100%) create mode 100644 spring-cloud-tencent-polaris-loadbalancer/src/main/resources/META-INF/spring.factories rename spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfigurationTest.java => spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerAutoConfigurationTest.java (87%) diff --git a/pom.xml b/pom.xml index 92b0fb7b4..2bb87f4f4 100644 --- a/pom.xml +++ b/pom.xml @@ -49,6 +49,7 @@ spring-cloud-tencent-examples spring-cloud-tencent-coverage spring-cloud-starter-tencent-polaris-config + spring-cloud-tencent-polaris-loadbalancer diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/pom.xml b/spring-cloud-starter-tencent-polaris-circuitbreaker/pom.xml index a76c72d0d..c55bb3d78 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/pom.xml +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/pom.xml @@ -16,7 +16,7 @@ com.tencent.cloud - spring-cloud-tencent-polaris-context + spring-cloud-tencent-polaris-loadbalancer diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryHandler.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryHandler.java index fd8b878e2..951c99f76 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryHandler.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisDiscoveryHandler.java @@ -28,6 +28,7 @@ import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.api.core.ProviderAPI; import com.tencent.polaris.api.pojo.ServiceInfo; import com.tencent.polaris.api.rpc.GetAllInstancesRequest; +import com.tencent.polaris.api.rpc.GetHealthyInstancesRequest; import com.tencent.polaris.api.rpc.GetInstancesRequest; import com.tencent.polaris.api.rpc.GetServicesRequest; import com.tencent.polaris.api.rpc.InstancesResponse; @@ -59,6 +60,7 @@ public class PolarisDiscoveryHandler { * @param service service name * @return list of instances */ + @Deprecated public InstancesResponse getFilteredInstances(String service) { String namespace = polarisDiscoveryProperties.getNamespace(); GetInstancesRequest getInstancesRequest = new GetInstancesRequest(); @@ -82,6 +84,19 @@ public class PolarisDiscoveryHandler { return polarisConsumer.getInstances(getInstancesRequest); } + /** + * Get a list of healthy instances. + * @param service service name + * @return list of healthy instances + */ + public InstancesResponse getHealthyInstances(String service) { + String namespace = polarisDiscoveryProperties.getNamespace(); + GetHealthyInstancesRequest getHealthyInstancesRequest = new GetHealthyInstancesRequest(); + getHealthyInstancesRequest.setNamespace(namespace); + getHealthyInstancesRequest.setService(service); + return polarisConsumer.getHealthyInstancesInstance(getHealthyInstancesRequest); + } + /** * Return all instances for the given service. * @param service serviceName diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisServiceDiscovery.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisServiceDiscovery.java index 6f85be6c3..95387ae5c 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisServiceDiscovery.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/discovery/PolarisServiceDiscovery.java @@ -50,7 +50,7 @@ public class PolarisServiceDiscovery { public List getInstances(String serviceId) throws PolarisException { List instances = new ArrayList<>(); InstancesResponse filteredInstances = polarisDiscoveryHandler - .getFilteredInstances(serviceId); + .getHealthyInstances(serviceId); ServiceInstances serviceInstances = filteredInstances.toServiceInstances(); for (Instance instance : serviceInstances.getInstances()) { instances.add(new PolarisServiceInstance(instance)); diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisServerList.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisServerList.java index 866cdcf1d..71833fde9 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisServerList.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisServerList.java @@ -57,7 +57,7 @@ public class PolarisServerList extends AbstractServerList { private List getServers() { InstancesResponse allInstances = polarisDiscoveryHandler - .getFilteredInstances(serviceId); + .getHealthyInstances(serviceId); ServiceInstances serviceInstances = allInstances.toServiceInstances(); List polarisServers = new ArrayList<>(); for (Instance instance : serviceInstances.getInstances()) { diff --git a/spring-cloud-starter-tencent-polaris-router/pom.xml b/spring-cloud-starter-tencent-polaris-router/pom.xml index 0bf38eec6..d623da7b1 100644 --- a/spring-cloud-starter-tencent-polaris-router/pom.xml +++ b/spring-cloud-starter-tencent-polaris-router/pom.xml @@ -17,79 +17,16 @@ com.tencent.cloud - spring-cloud-tencent-polaris-context + spring-cloud-tencent-polaris-loadbalancer - - com.tencent.polaris - polaris-router-factory - - - com.tencent.polaris - router-rule - - - com.tencent.polaris - router-nearby - - - com.tencent.polaris - router-metadata - - - com.tencent.polaris - router-canary - - - com.tencent.polaris - router-set - - - com.tencent.polaris - router-isolated - - - com.tencent.polaris - router-healthy - - - - - com.tencent.polaris router-rule - - - com.tencent.polaris - router-nearby - - - - com.tencent.polaris - router-metadata - - - - com.tencent.polaris - polaris-test-common - test - - - - - org.springframework.cloud - spring-cloud-starter-netflix-ribbon - - - - org.springframework.boot - spring-boot-starter-test - test - - + + diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/resources/META-INF/spring.factories b/spring-cloud-starter-tencent-polaris-router/src/main/resources/META-INF/spring.factories deleted file mode 100644 index 75c191046..000000000 --- a/spring-cloud-starter-tencent-polaris-router/src/main/resources/META-INF/spring.factories +++ /dev/null @@ -1,2 +0,0 @@ -org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ - com.tencent.cloud.polaris.router.config.PolarisRibbonAutoConfiguration diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 5f727c168..823845b8c 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -71,7 +71,7 @@ 1.4.0-Hoxton.SR9-SNAPSHOT - 1.4.0 + 1.5.0-SNAPSHOT 2.0.0 @@ -103,6 +103,12 @@ ${revision} + + com.tencent.cloud + spring-cloud-tencent-polaris-loadbalancer + ${revision} + + com.tencent.cloud spring-cloud-starter-tencent-metadata-transfer diff --git a/spring-cloud-tencent-polaris-context/pom.xml b/spring-cloud-tencent-polaris-context/pom.xml index c3f9e2adb..6d7fda248 100644 --- a/spring-cloud-tencent-polaris-context/pom.xml +++ b/spring-cloud-tencent-polaris-context/pom.xml @@ -18,44 +18,44 @@ com.tencent.cloud spring-cloud-tencent-commons - - + + - - - com.tencent.polaris - polaris-client - + + + com.tencent.polaris + polaris-client + - - com.tencent.polaris - polaris-plugin-api - + + com.tencent.polaris + polaris-plugin-api + - - com.tencent.polaris - connector-polaris-grpc - + + com.tencent.polaris + connector-polaris-grpc + - - com.tencent.polaris - connector-consul - + + com.tencent.polaris + connector-consul + - - com.tencent.polaris - connector-composite - + + com.tencent.polaris + connector-composite + - - com.tencent.polaris - registry-memory - + + com.tencent.polaris + registry-memory + - - com.tencent.polaris - flow-cache-expired - + + com.tencent.polaris + flow-cache-expired + @@ -67,23 +67,23 @@ com.tencent.polaris router-healthy - - - com.tencent.polaris - loadbalancer-random - - - com.tencent.polaris - loadbalancer-ringhash - - + + com.tencent.polaris + loadbalancer-random + - - org.springframework.boot - spring-boot-starter-test - test - - + + com.tencent.polaris + loadbalancer-ringhash + + + + + org.springframework.boot + spring-boot-starter-test + test + + diff --git a/spring-cloud-tencent-polaris-loadbalancer/pom.xml b/spring-cloud-tencent-polaris-loadbalancer/pom.xml new file mode 100644 index 000000000..dc64bf192 --- /dev/null +++ b/spring-cloud-tencent-polaris-loadbalancer/pom.xml @@ -0,0 +1,54 @@ + + + + spring-cloud-tencent + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + spring-cloud-tencent-polaris-loadbalancer + Spring Cloud Tencent Polaris LoadBalancer + + + + + com.tencent.cloud + spring-cloud-tencent-polaris-context + + + + + + com.tencent.polaris + polaris-router-factory + + + + com.tencent.polaris + polaris-discovery-api + + + + com.tencent.polaris + polaris-test-common + test + + + + + org.springframework.cloud + spring-cloud-starter-netflix-ribbon + + + + org.springframework.boot + spring-boot-starter-test + test + + + + \ No newline at end of file diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRoutingLoadBalancer.java b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancer.java similarity index 71% rename from spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRoutingLoadBalancer.java rename to spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancer.java index 513557c07..b4103c6ce 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRoutingLoadBalancer.java +++ b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancer.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.router; +package com.tencent.cloud.polaris.loadbalancer; import java.util.ArrayList; import java.util.List; @@ -32,12 +32,15 @@ import com.tencent.cloud.common.constant.MetadataConstant.SystemMetadataKey; import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.common.pojo.PolarisServer; +import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.api.pojo.DefaultInstance; import com.tencent.polaris.api.pojo.DefaultServiceInstances; import com.tencent.polaris.api.pojo.Instance; import com.tencent.polaris.api.pojo.ServiceInfo; import com.tencent.polaris.api.pojo.ServiceInstances; import com.tencent.polaris.api.pojo.ServiceKey; +import com.tencent.polaris.api.rpc.GetAllInstancesRequest; +import com.tencent.polaris.api.rpc.InstancesResponse; import com.tencent.polaris.router.api.core.RouterAPI; import com.tencent.polaris.router.api.rpc.ProcessRoutersRequest; import com.tencent.polaris.router.api.rpc.ProcessRoutersResponse; @@ -50,28 +53,51 @@ import org.springframework.util.CollectionUtils; * * @author Haotian Zhang */ -public class PolarisRoutingLoadBalancer extends DynamicServerListLoadBalancer { +public class PolarisLoadBalancer extends DynamicServerListLoadBalancer { private final RouterAPI routerAPI; - public PolarisRoutingLoadBalancer(IClientConfig config, IRule rule, IPing ping, - ServerList serverList, RouterAPI routerAPI) { + private ConsumerAPI consumerAPI; + + private boolean isPolarisDiscovery = true; + + private boolean isFirstCall = true; + + public PolarisLoadBalancer(IClientConfig config, IRule rule, IPing ping, + ServerList serverList, RouterAPI routerAPI, ConsumerAPI consumerAPI) { super(config, rule, ping, serverList, null, new PollingServerListUpdater()); this.routerAPI = routerAPI; + this.consumerAPI = consumerAPI; } @Override public List getReachableServers() { - List allServers = super.getAllServers(); - if (CollectionUtils.isEmpty(allServers)) { - return allServers; + List allServers = null; + if (isFirstCall) { + allServers = super.getAllServers(); + if (CollectionUtils.isEmpty(allServers)) { + return allServers; + } + if (allServers.get(0) instanceof PolarisServer) { + isPolarisDiscovery = true; + } + else { + isPolarisDiscovery = false; + } + isFirstCall = false; } - ServiceInstances serviceInstances = null; - if (allServers.get(0) instanceof PolarisServer) { - serviceInstances = ((PolarisServer) allServers.get(0)).getServiceInstances(); + + ServiceInstances serviceInstances; + String serviceName = null; + if (isPolarisDiscovery) { + // serviceName = ((PolarisServer)allServers.get(0)).getServiceInstances().getService(); + serviceInstances = getAllInstances(MetadataContext.LOCAL_NAMESPACE, + MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.PEER_SERVICE)).toServiceInstances(); } else { - String serviceName; + if (CollectionUtils.isEmpty(allServers)) { + allServers = super.getAllServers(); + } // notice the difference between different service registries if (StringUtils.isNotBlank( allServers.get(0).getMetaInfo().getServiceIdForDiscovery())) { @@ -82,7 +108,7 @@ public class PolarisRoutingLoadBalancer extends DynamicServerListLoadBalancer serverList, - RouterAPI polarisRouter) { - return new PolarisRoutingLoadBalancer(iClientConfig, iRule, iPing, serverList, - polarisRouter); + public ILoadBalancer polarisLoadBalancer(IClientConfig iClientConfig, IRule iRule, + IPing iPing, ServerList serverList, RouterAPI polarisRouter, + ConsumerAPI consumerAPI) { + return new PolarisLoadBalancer(iClientConfig, iRule, iPing, serverList, + polarisRouter, consumerAPI); } } diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/rule/PolarisLoadBalanceRule.java b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/rule/PolarisLoadBalanceRule.java similarity index 96% rename from spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/rule/PolarisLoadBalanceRule.java rename to spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/rule/PolarisLoadBalanceRule.java index b95f35197..4831e76e6 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/rule/PolarisLoadBalanceRule.java +++ b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/rule/PolarisLoadBalanceRule.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.router.rule; +package com.tencent.cloud.polaris.loadbalancer.rule; import java.util.Arrays; diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/rule/PolarisWeightedRandomRule.java b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/rule/PolarisWeightedRandomRule.java similarity index 98% rename from spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/rule/PolarisWeightedRandomRule.java rename to spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/rule/PolarisWeightedRandomRule.java index e65b0a2ea..055f4bab3 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/rule/PolarisWeightedRandomRule.java +++ b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/rule/PolarisWeightedRandomRule.java @@ -16,7 +16,7 @@ * */ -package com.tencent.cloud.polaris.router.rule; +package com.tencent.cloud.polaris.loadbalancer.rule; import java.util.ArrayList; import java.util.List; diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-tencent-polaris-loadbalancer/src/main/resources/META-INF/additional-spring-configuration-metadata.json similarity index 100% rename from spring-cloud-starter-tencent-polaris-router/src/main/resources/META-INF/additional-spring-configuration-metadata.json rename to spring-cloud-tencent-polaris-loadbalancer/src/main/resources/META-INF/additional-spring-configuration-metadata.json diff --git a/spring-cloud-tencent-polaris-loadbalancer/src/main/resources/META-INF/spring.factories b/spring-cloud-tencent-polaris-loadbalancer/src/main/resources/META-INF/spring.factories new file mode 100644 index 000000000..a13924bac --- /dev/null +++ b/spring-cloud-tencent-polaris-loadbalancer/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + com.tencent.cloud.polaris.loadbalancer.config.PolarisLoadBalancerAutoConfiguration diff --git a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfigurationTest.java b/spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerAutoConfigurationTest.java similarity index 87% rename from spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfigurationTest.java rename to spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerAutoConfigurationTest.java index 4a3ab3384..3e87d5fdd 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/test/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfigurationTest.java +++ b/spring-cloud-tencent-polaris-loadbalancer/src/test/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerAutoConfigurationTest.java @@ -15,7 +15,7 @@ * specific language governing permissions and limitations under the License. */ -package com.tencent.cloud.polaris.router.config; +package com.tencent.cloud.polaris.loadbalancer.config; import com.tencent.cloud.polaris.context.PolarisContextAutoConfiguration; import com.tencent.polaris.router.api.core.RouterAPI; @@ -31,15 +31,15 @@ import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER; import static org.assertj.core.api.Assertions.assertThat; /** - * Test for {@link PolarisRibbonAutoConfiguration} + * Test for {@link PolarisLoadBalancerAutoConfiguration} * * @author Haotian Zhang */ -public class PolarisRibbonAutoConfigurationTest { +public class PolarisLoadBalancerAutoConfigurationTest { private ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(PolarisRibbonTest.class, - PolarisRibbonAutoConfiguration.class, + PolarisLoadBalancerAutoConfiguration.class, PolarisContextAutoConfiguration.class)) .withPropertyValues("spring.application.name=" + SERVICE_PROVIDER) .withPropertyValues("server.port=" + PORT) @@ -49,7 +49,7 @@ public class PolarisRibbonAutoConfigurationTest { public void testDefaultInitialization() { this.contextRunner.run(context -> { assertThat(context).hasSingleBean(RouterAPI.class); - assertThat(context).hasSingleBean(PolarisRibbonProperties.class); + assertThat(context).hasSingleBean(PolarisLoadBalancerProperties.class); }); } From d8d0c7445d2542c867d024d0f7349aa686cc4f7f Mon Sep 17 00:00:00 2001 From: lepdou Date: Sun, 24 Apr 2022 21:06:53 +0800 Subject: [PATCH 10/16] add override recover router config --- ...sCircuitBreakerBootstrapConfiguration.java | 12 +++++ .../polaris/DiscoveryConfigModifier.java | 50 +++++++++++++++++++ .../DiscoveryPropertiesAutoConfiguration.java | 5 ++ .../common/constant/ContextConstant.java | 5 ++ spring-cloud-tencent-dependencies/pom.xml | 2 +- spring-cloud-tencent-polaris-context/pom.xml | 2 +- .../context/PolarisContextProperties.java | 6 ++- 7 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryConfigModifier.java diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerBootstrapConfiguration.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerBootstrapConfiguration.java index 2dc681389..42c9dc8b6 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerBootstrapConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerBootstrapConfiguration.java @@ -20,7 +20,9 @@ package com.tencent.cloud.polaris.circuitbreaker; import com.tencent.cloud.common.constant.ContextConstant; import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled; import com.tencent.cloud.polaris.context.PolarisConfigModifier; +import com.tencent.polaris.api.config.consumer.ServiceRouterConfig; import com.tencent.polaris.factory.config.ConfigurationImpl; +import com.tencent.polaris.plugins.router.healthy.RecoverRouterConfig; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; @@ -48,6 +50,16 @@ public class PolarisCircuitBreakerBootstrapConfiguration { public void modify(ConfigurationImpl configuration) { // Turn on circuitbreaker configuration configuration.getConsumer().getCircuitBreaker().setEnable(true); + + // Set excludeCircuitBreakInstances to false + RecoverRouterConfig recoverRouterConfig = configuration.getConsumer().getServiceRouter() + .getPluginConfig(ServiceRouterConfig.DEFAULT_ROUTER_RECOVER, RecoverRouterConfig.class); + + recoverRouterConfig.setExcludeCircuitBreakInstances(true); + + // Update modified config to source properties + configuration.getConsumer().getServiceRouter() + .setPluginConfig(ServiceRouterConfig.DEFAULT_ROUTER_RECOVER, recoverRouterConfig); } @Override diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryConfigModifier.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryConfigModifier.java new file mode 100644 index 000000000..a52a6dd4b --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryConfigModifier.java @@ -0,0 +1,50 @@ +/* + * 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; + +import com.tencent.cloud.common.constant.ContextConstant; +import com.tencent.cloud.polaris.context.PolarisConfigModifier; +import com.tencent.polaris.api.config.consumer.ServiceRouterConfig; +import com.tencent.polaris.factory.config.ConfigurationImpl; +import com.tencent.polaris.plugins.router.healthy.RecoverRouterConfig; + +/** + * Spring Cloud Tencent config Override polaris config. + * + *@author lepdou 2022-04-24 + */ +public class DiscoveryConfigModifier implements PolarisConfigModifier { + + @Override + public void modify(ConfigurationImpl configuration) { + // Set excludeCircuitBreakInstances to false + RecoverRouterConfig recoverRouterConfig = configuration.getConsumer().getServiceRouter() + .getPluginConfig(ServiceRouterConfig.DEFAULT_ROUTER_RECOVER, RecoverRouterConfig.class); + recoverRouterConfig.setExcludeCircuitBreakInstances(false); + + // Update modified config to source properties + configuration.getConsumer().getServiceRouter() + .setPluginConfig(ServiceRouterConfig.DEFAULT_ROUTER_RECOVER, recoverRouterConfig); + } + + @Override + public int getOrder() { + return ContextConstant.ModifierOrder.DISCOVERY_ORDER; + } +} diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfiguration.java index 805a7f47d..21ad954c1 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryPropertiesAutoConfiguration.java @@ -74,6 +74,11 @@ public class DiscoveryPropertiesAutoConfiguration { return new PolarisDiscoveryHandler(); } + @Bean + public DiscoveryConfigModifier discoveryConfigModifier() { + return new DiscoveryConfigModifier(); + } + @PostConstruct public void init() { if (null != polarisDiscoveryProperties) { diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/ContextConstant.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/ContextConstant.java index 54f203878..6aa70064b 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/ContextConstant.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/ContextConstant.java @@ -44,6 +44,11 @@ public final class ContextConstant { */ public static Integer CIRCUIT_BREAKER_ORDER = 1; + /** + * Order of discovery configuration modifier. + */ + public static Integer DISCOVERY_ORDER = 0; + /** * Order of configuration modifier. */ diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 5a5bd16c1..3d4f28ab6 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -71,7 +71,7 @@ 1.4.0-Hoxton.SR9-SNAPSHOT - 1.4.0 + 1.5.0-SNAPSHOT 2.0.0 diff --git a/spring-cloud-tencent-polaris-context/pom.xml b/spring-cloud-tencent-polaris-context/pom.xml index c3f9e2adb..c9ca6e193 100644 --- a/spring-cloud-tencent-polaris-context/pom.xml +++ b/spring-cloud-tencent-polaris-context/pom.xml @@ -67,7 +67,7 @@ com.tencent.polaris router-healthy - + com.tencent.polaris loadbalancer-random diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextProperties.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextProperties.java index 9c69a4129..95c1c2672 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextProperties.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextProperties.java @@ -74,11 +74,14 @@ public class PolarisContextProperties { private List modifierList; protected Configuration configuration() { + // 1. Read user-defined polaris.yml configuration ConfigurationImpl configuration = (ConfigurationImpl) ConfigAPIFactory .defaultConfig(ConfigProvider.DEFAULT_CONFIG); - configuration.setDefault(); + + // 2. Override user-defined polaris.yml configuration with SCT configuration String defaultHost = getHost(); configuration.getGlobal().getAPI().setBindIP(defaultHost); + Collection modifiers = modifierList; modifiers = modifiers.stream() .sorted(Comparator.comparingInt(PolarisConfigModifier::getOrder)) @@ -88,6 +91,7 @@ public class PolarisContextProperties { modifier.modify(configuration); } } + return configuration; } From 8f668870b5830d6800cb5985a7167079f07e3e7d Mon Sep 17 00:00:00 2001 From: SkyeBeFreeman <928016560@qq.com> Date: Mon, 25 Apr 2022 15:04:54 +0800 Subject: [PATCH 11/16] feat:optimize code. --- .../pom.xml | 2 +- .../common/constant/ContextConstant.java | 8 ++ .../loadbalancer/PolarisLoadBalancer.java | 124 +++++++++--------- .../PolarisLoadBalancerAutoConfiguration.java | 3 +- .../config/PolarisLoadBalancerProperties.java | 15 +++ .../PolarisRibbonClientConfiguration.java | 4 +- ...itional-spring-configuration-metadata.json | 10 +- 7 files changed, 100 insertions(+), 66 deletions(-) diff --git a/spring-cloud-starter-tencent-polaris-discovery/pom.xml b/spring-cloud-starter-tencent-polaris-discovery/pom.xml index 71ae069b1..56095e975 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/pom.xml +++ b/spring-cloud-starter-tencent-polaris-discovery/pom.xml @@ -17,7 +17,7 @@ com.tencent.cloud - spring-cloud-tencent-polaris-context + spring-cloud-tencent-polaris-loadbalancer diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/ContextConstant.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/ContextConstant.java index 54f203878..54d8b3224 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/ContextConstant.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/ContextConstant.java @@ -24,6 +24,14 @@ package com.tencent.cloud.common.constant; */ public final class ContextConstant { + /** + * Name of Polaris. + */ + public static final String POLARIS = "POLARIS"; + + private ContextConstant() { + } + /** * Order of configuration modifier. */ diff --git a/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancer.java b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancer.java index b4103c6ce..37ae60526 100644 --- a/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancer.java +++ b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisLoadBalancer.java @@ -18,6 +18,7 @@ package com.tencent.cloud.polaris.loadbalancer; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -28,10 +29,12 @@ import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.PollingServerListUpdater; import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.ServerList; +import com.tencent.cloud.common.constant.ContextConstant; import com.tencent.cloud.common.constant.MetadataConstant.SystemMetadataKey; import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.common.pojo.PolarisServer; +import com.tencent.cloud.polaris.loadbalancer.config.PolarisLoadBalancerProperties; import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.api.pojo.DefaultInstance; import com.tencent.polaris.api.pojo.DefaultServiceInstances; @@ -44,10 +47,9 @@ import com.tencent.polaris.api.rpc.InstancesResponse; import com.tencent.polaris.router.api.core.RouterAPI; import com.tencent.polaris.router.api.rpc.ProcessRoutersRequest; import com.tencent.polaris.router.api.rpc.ProcessRoutersResponse; +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringUtils; -import org.springframework.util.CollectionUtils; - /** * Routing load balancer of polaris. * @@ -59,74 +61,27 @@ public class PolarisLoadBalancer extends DynamicServerListLoadBalancer { private ConsumerAPI consumerAPI; - private boolean isPolarisDiscovery = true; - - private boolean isFirstCall = true; + private PolarisLoadBalancerProperties polarisLoadBalancerProperties; - public PolarisLoadBalancer(IClientConfig config, IRule rule, IPing ping, - ServerList serverList, RouterAPI routerAPI, ConsumerAPI consumerAPI) { + public PolarisLoadBalancer(IClientConfig config, IRule rule, IPing ping, ServerList serverList, + RouterAPI routerAPI, ConsumerAPI consumerAPI, PolarisLoadBalancerProperties properties) { super(config, rule, ping, serverList, null, new PollingServerListUpdater()); this.routerAPI = routerAPI; this.consumerAPI = consumerAPI; + this.polarisLoadBalancerProperties = properties; } @Override public List getReachableServers() { - List allServers = null; - if (isFirstCall) { - allServers = super.getAllServers(); - if (CollectionUtils.isEmpty(allServers)) { - return allServers; - } - if (allServers.get(0) instanceof PolarisServer) { - isPolarisDiscovery = true; - } - else { - isPolarisDiscovery = false; - } - isFirstCall = false; - } - ServiceInstances serviceInstances; - String serviceName = null; - if (isPolarisDiscovery) { - // serviceName = ((PolarisServer)allServers.get(0)).getServiceInstances().getService(); - serviceInstances = getAllInstances(MetadataContext.LOCAL_NAMESPACE, - MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.PEER_SERVICE)).toServiceInstances(); + if (polarisLoadBalancerProperties.getDiscoveryType().equals(ContextConstant.POLARIS)) { + serviceInstances = getPolarisDiscoveryServiceInstances(); } else { - if (CollectionUtils.isEmpty(allServers)) { - allServers = super.getAllServers(); - } - // notice the difference between different service registries - if (StringUtils.isNotBlank( - allServers.get(0).getMetaInfo().getServiceIdForDiscovery())) { - serviceName = allServers.get(0).getMetaInfo().getServiceIdForDiscovery(); - } - else { - serviceName = allServers.get(0).getMetaInfo().getAppName(); - } - if (StringUtils.isBlank(serviceName)) { - throw new IllegalStateException( - "PolarisLoadBalancer only Server with AppName or ServiceIdForDiscovery attribute"); - } - ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, - serviceName); - List instances = new ArrayList<>(8); - for (Server server : allServers) { - DefaultInstance instance = new DefaultInstance(); - instance.setNamespace(MetadataContext.LOCAL_NAMESPACE); - instance.setService(serviceName); - instance.setHealthy(server.isAlive()); - instance.setProtocol(server.getScheme()); - instance.setId(server.getId()); - instance.setHost(server.getHost()); - instance.setPort(server.getPort()); - instance.setZone(server.getZone()); - instance.setWeight(100); - instances.add(instance); - } - serviceInstances = new DefaultServiceInstances(serviceKey, instances); + serviceInstances = getExtendDiscoveryServiceInstances(); + } + if (serviceInstances == null || CollectionUtils.isEmpty(serviceInstances.getInstances())) { + return Collections.emptyList(); } ProcessRoutersRequest processRoutersRequest = new ProcessRoutersRequest(); processRoutersRequest.setDstInstances(serviceInstances); @@ -155,6 +110,57 @@ public class PolarisLoadBalancer extends DynamicServerListLoadBalancer { return filteredInstances; } + private ServiceInstances getPolarisDiscoveryServiceInstances() { + String serviceName = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.PEER_SERVICE); + if (StringUtils.isBlank(serviceName)) { + List allServers = super.getAllServers(); + if (CollectionUtils.isEmpty(allServers)) { + return null; + } + serviceName = ((PolarisServer) super.getAllServers().get(0)).getServiceInstances().getService(); + } + return getAllInstances(MetadataContext.LOCAL_NAMESPACE, serviceName).toServiceInstances(); + } + + private ServiceInstances getExtendDiscoveryServiceInstances() { + List allServers = super.getAllServers(); + if (CollectionUtils.isEmpty(allServers)) { + return null; + } + ServiceInstances serviceInstances; + String serviceName; + // notice the difference between different service registries + if (StringUtils.isNotBlank( + allServers.get(0).getMetaInfo().getServiceIdForDiscovery())) { + serviceName = allServers.get(0).getMetaInfo().getServiceIdForDiscovery(); + } + else { + serviceName = allServers.get(0).getMetaInfo().getAppName(); + } + if (StringUtils.isBlank(serviceName)) { + throw new IllegalStateException( + "PolarisLoadBalancer only Server with AppName or ServiceIdForDiscovery attribute"); + } + ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, + serviceName); + List instances = new ArrayList<>(8); + for (Server server : allServers) { + DefaultInstance instance = new DefaultInstance(); + instance.setNamespace(MetadataContext.LOCAL_NAMESPACE); + instance.setService(serviceName); + instance.setHealthy(server.isAlive()); + instance.setProtocol(server.getScheme()); + instance.setId(server.getId()); + instance.setHost(server.getHost()); + instance.setPort(server.getPort()); + instance.setZone(server.getZone()); + instance.setWeight(100); + instances.add(instance); + } + serviceInstances = new DefaultServiceInstances(serviceKey, instances); + return serviceInstances; + } + @Override public List getAllServers() { return getReachableServers(); diff --git a/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerAutoConfiguration.java b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerAutoConfiguration.java index 119745d97..0b8b0583c 100644 --- a/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerAutoConfiguration.java +++ b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerAutoConfiguration.java @@ -40,8 +40,7 @@ import org.springframework.context.annotation.Configuration; @Configuration(proxyBeanMethods = false) @EnableConfigurationProperties @ConditionalOnPolarisEnabled -@ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.enabled", - matchIfMissing = true) +@ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.enabled", matchIfMissing = true) @AutoConfigureAfter(RibbonAutoConfiguration.class) @RibbonClients(defaultConfiguration = PolarisRibbonClientConfiguration.class) public class PolarisLoadBalancerAutoConfiguration { diff --git a/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerProperties.java b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerProperties.java index fafb1450b..22760e463 100644 --- a/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerProperties.java +++ b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerProperties.java @@ -17,6 +17,8 @@ package com.tencent.cloud.polaris.loadbalancer.config; +import com.tencent.cloud.common.constant.ContextConstant; + import org.springframework.boot.context.properties.ConfigurationProperties; /** @@ -37,6 +39,11 @@ public class PolarisLoadBalancerProperties { */ private String strategy = "weightedRandom"; + /** + * Type of discovery server. + */ + private String discoveryType = ContextConstant.POLARIS; + public String getStrategy() { return strategy; } @@ -53,6 +60,14 @@ public class PolarisLoadBalancerProperties { this.enabled = enabled; } + public String getDiscoveryType() { + return discoveryType; + } + + public void setDiscoveryType(String discoveryType) { + this.discoveryType = discoveryType; + } + @Override public String toString() { return "PolarisLoadBalancerProperties{" + "loadbalancerEnabled=" + enabled diff --git a/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisRibbonClientConfiguration.java b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisRibbonClientConfiguration.java index b64a8d50a..3e84ed57f 100644 --- a/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisRibbonClientConfiguration.java +++ b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisRibbonClientConfiguration.java @@ -57,9 +57,9 @@ public class PolarisRibbonClientConfiguration { @ConditionalOnMissingBean public ILoadBalancer polarisLoadBalancer(IClientConfig iClientConfig, IRule iRule, IPing iPing, ServerList serverList, RouterAPI polarisRouter, - ConsumerAPI consumerAPI) { + ConsumerAPI consumerAPI, PolarisLoadBalancerProperties polarisLoadBalancerProperties) { return new PolarisLoadBalancer(iClientConfig, iRule, iPing, serverList, - polarisRouter, consumerAPI); + polarisRouter, consumerAPI, polarisLoadBalancerProperties); } } diff --git a/spring-cloud-tencent-polaris-loadbalancer/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-tencent-polaris-loadbalancer/src/main/resources/META-INF/additional-spring-configuration-metadata.json index c94ac4e0f..5ecb156bc 100644 --- a/spring-cloud-tencent-polaris-loadbalancer/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-tencent-polaris-loadbalancer/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -4,13 +4,19 @@ "name": "spring.cloud.polaris.loadbalancer.enabled", "type": "java.lang.Boolean", "defaultValue": "true", - "description": "polaris loadbalancer" + "description": "polaris loadbalancer." + }, + { + "name": "spring.cloud.polaris.loadbalancer.discoveryType", + "type": "java.lang.String", + "defaultValue": "POLARIS", + "description": "Type of discovery server." }, { "name": "spring.cloud.polaris.loadbalancer.strategy", "type": "java.lang.String", "defaultValue": "random", - "description": "retry,best_available,availability_filtering,round_robin,weighted_response_time,zone_avoidance,random,consistent_hash,weighted_random" + "description": "retry,best_available,availability_filtering,round_robin,weighted_response_time,zone_avoidance,random,consistent_hash,weighted_random." } ] } From c73d224ab3a74fa52562c33b2ba7e232014a8a33 Mon Sep 17 00:00:00 2001 From: SkyeBeFreeman <928016560@qq.com> Date: Mon, 25 Apr 2022 15:18:41 +0800 Subject: [PATCH 12/16] docs:update CHANGELOG.md. --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 118650e29..7e7ba0edd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,5 @@ --- - [Feature: Support custom rate limit reject response info](https://github.com/Tencent/spring-cloud-tencent/pull/128) - - [Feature: Remove spring-javaformat-maven-plugin](https://github.com/Tencent/spring-cloud-tencent/pull/131) - +- [feat:refactor loadbalancer module as a basic module for router and circuit breaker.](https://github.com/Tencent/spring-cloud-tencent/pull/136) From b3bb71cdc3400921fc1a4719e6d9dbb34e9797d2 Mon Sep 17 00:00:00 2001 From: SkyeBeFreeman <928016560@qq.com> Date: Mon, 25 Apr 2022 15:26:10 +0800 Subject: [PATCH 13/16] fix:fix code style error. --- .../PolarisCircuitBreakerBootstrapConfiguration.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerBootstrapConfiguration.java b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerBootstrapConfiguration.java index 42c9dc8b6..d0b276059 100644 --- a/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerBootstrapConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/PolarisCircuitBreakerBootstrapConfiguration.java @@ -51,7 +51,7 @@ public class PolarisCircuitBreakerBootstrapConfiguration { // Turn on circuitbreaker configuration configuration.getConsumer().getCircuitBreaker().setEnable(true); - // Set excludeCircuitBreakInstances to false + // Set excludeCircuitBreakInstances to false RecoverRouterConfig recoverRouterConfig = configuration.getConsumer().getServiceRouter() .getPluginConfig(ServiceRouterConfig.DEFAULT_ROUTER_RECOVER, RecoverRouterConfig.class); From be1044fb7c69fc5fa93177417160a569be7c6edf Mon Sep 17 00:00:00 2001 From: SkyeBeFreeman <928016560@qq.com> Date: Mon, 25 Apr 2022 16:12:05 +0800 Subject: [PATCH 14/16] fix:fix empty content jar error. --- .../cloud/polaris/router/package-info.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/package-info.java diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/package-info.java b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/package-info.java new file mode 100644 index 000000000..3eb18e376 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/package-info.java @@ -0,0 +1,23 @@ +/* + * 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 info of router. + * + * @author Haotian Zhang + */ +package com.tencent.cloud.polaris.router; From 65c68728867488c686d095a7042fc1d4d2092086 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Mon, 25 Apr 2022 16:34:43 +0800 Subject: [PATCH 15/16] Update README-zh.md --- README-zh.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README-zh.md b/README-zh.md index 52b7f871c..4f7d026b2 100644 --- a/README-zh.md +++ b/README-zh.md @@ -21,7 +21,7 @@ Spring Cloud Tencent 的核心依托腾讯开源的一站式服务发现与治 - [Polaris Github home page](https://github.com/polarismesh/polaris) - [Polaris official website](https://polarismesh.cn/) -Spring Cloud 腾讯提供的能力包括但不限于: +Spring Cloud Tencent提供的能力包括但不限于: - 服务注册和发现 - 动态配置管理 From 87ead067585f1327bdee3b383707c3d7689d9ba9 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Mon, 25 Apr 2022 16:35:12 +0800 Subject: [PATCH 16/16] Update README-zh.md --- README-zh.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README-zh.md b/README-zh.md index 4f7d026b2..ff760c7d5 100644 --- a/README-zh.md +++ b/README-zh.md @@ -26,8 +26,8 @@ Spring Cloud Tencent提供的能力包括但不限于: - 服务注册和发现 - 动态配置管理 - 服务治理 - - 服务电流限制 - - 服务断路器 + - 服务限流 + - 服务熔断 - 服务路由 - ... - 标签透传