From 2ed9b8790377c25ccfefc13bfb04d2cce52fb9e9 Mon Sep 17 00:00:00 2001 From: SkyeBeFreeman <928016560@qq.com> Date: Fri, 13 May 2022 15:00:17 +0800 Subject: [PATCH] feat:refactor loadbalancer module as a basic module for router and circuit breaker. --- CHANGELOG.md | 9 +- changes/changes-1.3.0.md | 11 ++ pom.xml | 3 +- .../pom.xml | 2 +- .../pom.xml | 2 +- .../discovery/PolarisDiscoveryHandler.java | 15 ++ .../discovery/PolarisServiceDiscovery.java | 2 +- .../polaris/ribbon/PolarisServerList.java | 2 +- .../pom.xml | 69 +-------- .../cloud/polaris/router/package-info.java | 23 +++ .../main/resources/META-INF/spring.factories | 2 - .../common/constant/ContextConstant.java | 8 ++ spring-cloud-tencent-dependencies/pom.xml | 10 +- spring-cloud-tencent-polaris-context/pom.xml | 8 +- .../pom.xml | 54 ++++++++ .../loadbalancer/PolarisLoadBalancer.java | 131 ++++++++++++------ .../PolarisLoadBalancerAutoConfiguration.java | 8 +- .../config/PolarisLoadBalancerProperties.java | 40 ++++-- .../PolarisRibbonClientConfiguration.java | 24 ++-- .../rule/PolarisLoadBalanceRule.java | 2 +- .../rule/PolarisWeightedRandomRule.java | 2 +- ...itional-spring-configuration-metadata.json | 10 +- .../main/resources/META-INF/spring.factories | 2 + ...arisLoadBalancerAutoConfigurationTest.java | 10 +- 24 files changed, 281 insertions(+), 168 deletions(-) create mode 100644 changes/changes-1.3.0.md create mode 100644 spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/package-info.java 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 (50%) 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 (59%) 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 (64%) 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 (61%) 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/CHANGELOG.md b/CHANGELOG.md index 42918fbe6..5d2fee3be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,4 @@ # Change Log --- -- [Bugfix: fix router bug and add router example](https://github.com/Tencent/spring-cloud-tencent/pull/108) -- [feat:add custom label resolver spi for rate limit](https://github.com/Tencent/spring-cloud-tencent/pull/106) -- [feat:fix discovery weight param not set to register request bug](https://github.com/Tencent/spring-cloud-tencent/pull/103) -- [Bugfix: fix causing cpu 100% when set ScheduledThreadPoolExecutor corePoolSize=0](https://github.com/Tencent/spring-cloud-tencent/pull/99) -- [Bugfix: fix circuitbreaker http code greater than 400 as fail response bug](https://github.com/Tencent/spring-cloud-tencent/pull/117) -- [Feat: optimize router dependency](https://github.com/Tencent/spring-cloud-tencent/pull/114) -- [Refactor: refactor transfer metadata](https://github.com/Tencent/spring-cloud-tencent/pull/112) -- [feat:add switch of polaris, discovery and register.](https://github.com/Tencent/spring-cloud-tencent/pull/133) +- [feat:refactor loadbalancer module as a basic module for router and circuit breaker.](https://github.com/Tencent/spring-cloud-tencent/pull/166) diff --git a/changes/changes-1.3.0.md b/changes/changes-1.3.0.md new file mode 100644 index 000000000..42918fbe6 --- /dev/null +++ b/changes/changes-1.3.0.md @@ -0,0 +1,11 @@ +# Change Log +--- + +- [Bugfix: fix router bug and add router example](https://github.com/Tencent/spring-cloud-tencent/pull/108) +- [feat:add custom label resolver spi for rate limit](https://github.com/Tencent/spring-cloud-tencent/pull/106) +- [feat:fix discovery weight param not set to register request bug](https://github.com/Tencent/spring-cloud-tencent/pull/103) +- [Bugfix: fix causing cpu 100% when set ScheduledThreadPoolExecutor corePoolSize=0](https://github.com/Tencent/spring-cloud-tencent/pull/99) +- [Bugfix: fix circuitbreaker http code greater than 400 as fail response bug](https://github.com/Tencent/spring-cloud-tencent/pull/117) +- [Feat: optimize router dependency](https://github.com/Tencent/spring-cloud-tencent/pull/114) +- [Refactor: refactor transfer metadata](https://github.com/Tencent/spring-cloud-tencent/pull/112) +- [feat:add switch of polaris, discovery and register.](https://github.com/Tencent/spring-cloud-tencent/pull/133) diff --git a/pom.xml b/pom.xml index 98cae4818..78de20ecd 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 @@ -85,7 +86,7 @@ - 1.3.0-Greenwich.SR6 + 1.4.2-Greenwich.SR6-SNAPSHOT Greenwich.SR6 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/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-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/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; 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-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-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 633e14579..0b9bf2c9c 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -70,8 +70,8 @@ - 1.3.0-Greenwich.SR6 - 1.4.0 + 1.4.2-Greenwich.SR6-SNAPSHOT + 1.5.2 1.2.7 @@ -131,6 +131,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 03bd9b212..6d7fda248 100644 --- a/spring-cloud-tencent-polaris-context/pom.xml +++ b/spring-cloud-tencent-polaris-context/pom.xml @@ -68,10 +68,10 @@ router-healthy - - com.tencent.polaris - loadbalancer-random - + + com.tencent.polaris + loadbalancer-random + com.tencent.polaris 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 50% 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..37ae60526 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,9 +15,10 @@ * 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.Collections; import java.util.List; import java.util.Map; @@ -28,79 +29,59 @@ 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; 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; +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringUtils; -import org.springframework.util.CollectionUtils; - /** * Routing load balancer of polaris. * * @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 PolarisLoadBalancerProperties polarisLoadBalancerProperties; + + 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 = super.getAllServers(); - if (CollectionUtils.isEmpty(allServers)) { - return allServers; - } - ServiceInstances serviceInstances = null; - if (allServers.get(0) instanceof PolarisServer) { - serviceInstances = ((PolarisServer) allServers.get(0)).getServiceInstances(); + ServiceInstances serviceInstances; + if (polarisLoadBalancerProperties.getDiscoveryType().equals(ContextConstant.POLARIS)) { + serviceInstances = getPolarisDiscoveryServiceInstances(); } else { - 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( - "PolarisRoutingLoadBalancer 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); @@ -129,9 +110,73 @@ public class PolarisRoutingLoadBalancer extends DynamicServerListLoadBalancer 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(); } + /** + * Get a list of instances. + * @param namespace namespace + * @param serviceName service name + * @return list of instances + */ + public InstancesResponse getAllInstances(String namespace, String serviceName) { + GetAllInstancesRequest request = new GetAllInstancesRequest(); + request.setNamespace(namespace); + request.setService(serviceName); + return consumerAPI.getAllInstance(request); + } + } diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfiguration.java b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerAutoConfiguration.java similarity index 90% rename from spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfiguration.java rename to spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerAutoConfiguration.java index 64960f7e4..e073a3634 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonAutoConfiguration.java +++ b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerAutoConfiguration.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.ConditionalOnPolarisEnabled; import com.tencent.polaris.api.exception.PolarisException; @@ -43,12 +43,12 @@ import org.springframework.context.annotation.Configuration; @ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.enabled", matchIfMissing = true) @AutoConfigureAfter(RibbonAutoConfiguration.class) @RibbonClients(defaultConfiguration = PolarisRibbonClientConfiguration.class) -public class PolarisRibbonAutoConfiguration { +public class PolarisLoadBalancerAutoConfiguration { @Bean @ConditionalOnMissingBean - public PolarisRibbonProperties polarisRibbonProperties() { - return new PolarisRibbonProperties(); + public PolarisLoadBalancerProperties polarisLoadBalancerProperties() { + return new PolarisLoadBalancerProperties(); } @Bean(name = "polarisRoute") diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonProperties.java b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerProperties.java similarity index 59% rename from spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonProperties.java rename to spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerProperties.java index db5e14fdd..22760e463 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonProperties.java +++ b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisLoadBalancerProperties.java @@ -15,30 +15,34 @@ * 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.common.constant.ContextConstant; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; /** - * Properties of Ribbon. + * Properties of Polaris loadbalancer. * * @author Haotian Zhang */ -@ConfigurationProperties("spring.cloud.polaris.ribbon") -public class PolarisRibbonProperties { +@ConfigurationProperties("spring.cloud.polaris.loadbalancer") +public class PolarisLoadBalancerProperties { /** * If load-balance enabled. */ - @Value("${spring.cloud.polaris.discovery.loadbalancer.enabled:#{true}}") - private Boolean loadbalancerEnabled; + private Boolean enabled = true; /** * Load balance strategy. */ - @Value("${spring.cloud.polaris.loadbalancer.strategy:#{'weightedRandom'}}") - private String strategy; + private String strategy = "weightedRandom"; + + /** + * Type of discovery server. + */ + private String discoveryType = ContextConstant.POLARIS; public String getStrategy() { return strategy; @@ -48,17 +52,25 @@ public class PolarisRibbonProperties { this.strategy = strategy; } - public Boolean getLoadbalancerEnabled() { - return loadbalancerEnabled; + public Boolean getEnabled() { + return enabled; + } + + public void setEnabled(Boolean enabled) { + this.enabled = enabled; + } + + public String getDiscoveryType() { + return discoveryType; } - public void setLoadbalancerEnabled(Boolean loadbalancerEnabled) { - this.loadbalancerEnabled = loadbalancerEnabled; + public void setDiscoveryType(String discoveryType) { + this.discoveryType = discoveryType; } @Override public String toString() { - return "PolarisRibbonProperties{" + "loadbalancerEnabled=" + loadbalancerEnabled + return "PolarisLoadBalancerProperties{" + "loadbalancerEnabled=" + enabled + ", strategy='" + strategy + '\'' + '}'; } diff --git a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonClientConfiguration.java b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisRibbonClientConfiguration.java similarity index 64% rename from spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonClientConfiguration.java rename to spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisRibbonClientConfiguration.java index 6fa1dc51b..3e84ed57f 100644 --- a/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/config/PolarisRibbonClientConfiguration.java +++ b/spring-cloud-tencent-polaris-loadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/config/PolarisRibbonClientConfiguration.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.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.ILoadBalancer; @@ -23,9 +23,10 @@ import com.netflix.loadbalancer.IPing; import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.ServerList; -import com.tencent.cloud.polaris.router.PolarisRoutingLoadBalancer; -import com.tencent.cloud.polaris.router.rule.PolarisLoadBalanceRule; -import com.tencent.cloud.polaris.router.rule.PolarisWeightedRandomRule; +import com.tencent.cloud.polaris.loadbalancer.PolarisLoadBalancer; +import com.tencent.cloud.polaris.loadbalancer.rule.PolarisLoadBalanceRule; +import com.tencent.cloud.polaris.loadbalancer.rule.PolarisWeightedRandomRule; +import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.router.api.core.RouterAPI; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -42,9 +43,10 @@ public class PolarisRibbonClientConfiguration { @Bean @ConditionalOnMissingBean - public IRule polarisRibbonRule(PolarisRibbonProperties polarisRibbonProperties) { + public IRule polarisRibbonRule( + PolarisLoadBalancerProperties polarisLoadBalancerProperties) { switch (PolarisLoadBalanceRule - .fromStrategy(polarisRibbonProperties.getStrategy())) { + .fromStrategy(polarisLoadBalancerProperties.getStrategy())) { case WEIGHTED_RANDOM_RULE: default: return new PolarisWeightedRandomRule(); @@ -53,11 +55,11 @@ public class PolarisRibbonClientConfiguration { @Bean @ConditionalOnMissingBean - public ILoadBalancer polarisRoutingLoadBalancer(IClientConfig iClientConfig, - IRule iRule, IPing iPing, ServerList 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, PolarisLoadBalancerProperties polarisLoadBalancerProperties) { + return new PolarisLoadBalancer(iClientConfig, iRule, iPing, serverList, + polarisRouter, consumerAPI, polarisLoadBalancerProperties); } } 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 61% 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 index c94ac4e0f..5ecb156bc 100644 --- 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 @@ -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." } ] } 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); }); }