feat:refactor loadbalancer module as a basic module for router and circuit breaker. (#155)

pull/159/head
Haotian Zhang 3 years ago committed by GitHub
parent 5aad3dfb2c
commit 26b1a116c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -49,6 +49,7 @@
<module>spring-cloud-tencent-examples</module>
<module>spring-cloud-tencent-coverage</module>
<module>spring-cloud-starter-tencent-polaris-config</module>
<module>spring-cloud-tencent-polaris-loadbalancer</module>
</modules>
<developers>
@ -77,7 +78,7 @@
<properties>
<!-- Project revision -->
<revision>1.3.0-2020.0.5</revision>
<revision>1.4.1-2020.0.5-SNAPSHOT</revision>
<!-- Spring Cloud -->
<spring.cloud.version>2020.0.5</spring.cloud.version>

@ -16,7 +16,7 @@
<!-- Spring Cloud Tencent dependencies start -->
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-polaris-context</artifactId>
<artifactId>spring-cloud-tencent-polaris-loadbalancer</artifactId>
</dependency>
<!-- Spring Cloud Tencent dependencies end -->
@ -84,12 +84,6 @@
</dependency>
<!-- Polaris dependencies end -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>

@ -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();
@ -80,6 +82,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

@ -49,7 +49,7 @@ public class PolarisServiceDiscovery {
*/
public List<ServiceInstance> getInstances(String serviceId) throws PolarisException {
List<ServiceInstance> instances = new ArrayList<>();
InstancesResponse filteredInstances = polarisDiscoveryHandler.getFilteredInstances(serviceId);
InstancesResponse filteredInstances = polarisDiscoveryHandler.getHealthyInstances(serviceId);
ServiceInstances serviceInstances = filteredInstances.toServiceInstances();
for (Instance instance : serviceInstances.getInstances()) {
instances.add(new PolarisServiceInstance(instance));

@ -17,79 +17,16 @@
<!-- Spring Cloud Tencent dependencies start -->
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-polaris-context</artifactId>
<artifactId>spring-cloud-tencent-polaris-loadbalancer</artifactId>
</dependency>
<!-- Spring Cloud Tencent dependencies end -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
<!-- Polaris dependencies start -->
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-router-factory</artifactId>
<exclusions>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-rule</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-nearby</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-metadata</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-canary</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-set</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-isolated</artifactId>
</exclusion>
<exclusion>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-healthy</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Router Plugins -->
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-rule</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-nearby</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>router-metadata</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-test-common</artifactId>
<scope>test</scope>
</dependency>
<!-- Polaris dependencies end -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

@ -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;

@ -1,2 +0,0 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tencent.cloud.polaris.router.config.PolarisLoadBalancerAutoConfiguration

@ -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.
*/

@ -70,8 +70,8 @@
</developers>
<properties>
<revision>1.3.0-2020.0.5</revision>
<polaris.version>1.4.0</polaris.version>
<revision>1.4.1-2020.0.5-SNAPSHOT</revision>
<polaris.version>1.5.1</polaris.version>
<powermock.version>2.0.0</powermock.version>
<!-- Maven Plugin Versions -->
@ -102,6 +102,12 @@
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-polaris-loadbalancer</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-metadata-transfer</artifactId>

@ -33,6 +33,11 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-circuitbreaker-spring-retry</artifactId>
</dependency>
</dependencies>
<build>

@ -10,6 +10,8 @@ spring:
enabled: true
circuitbreaker:
enabled: true
loadbalancer:
configurations: polaris
feign:
circuitbreaker:
enabled: true

@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-tencent</artifactId>
<groupId>com.tencent.cloud</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-tencent-polaris-loadbalancer</artifactId>
<name>Spring Cloud Tencent Polaris LoadBalancer</name>
<dependencies>
<!-- Spring Cloud Tencent dependencies start -->
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-polaris-context</artifactId>
</dependency>
<!-- Spring Cloud Tencent dependencies end -->
<!-- Polaris dependencies start -->
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-router-factory</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-discovery-api</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-test-common</artifactId>
<scope>test</scope>
</dependency>
<!-- Polaris dependencies end -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

@ -15,14 +15,14 @@
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.router;
package com.tencent.cloud.polaris.loadbalancer;
import java.util.List;
import java.util.stream.Collectors;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.pojo.PolarisServiceInstance;
import com.tencent.cloud.polaris.router.config.PolarisLoadBalancerProperties;
import com.tencent.cloud.polaris.loadbalancer.config.PolarisLoadBalancerProperties;
import com.tencent.polaris.api.pojo.DefaultServiceInstances;
import com.tencent.polaris.api.pojo.Instance;
import com.tencent.polaris.api.pojo.ServiceInstances;
@ -51,9 +51,9 @@ import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
*
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
public class PolarisRoutingLoadbalancer extends RoundRobinLoadBalancer implements ReactorServiceInstanceLoadBalancer {
public class PolarisLoadbalancer extends RoundRobinLoadBalancer implements ReactorServiceInstanceLoadBalancer {
private static final Logger log = LoggerFactory.getLogger(PolarisRoutingLoadbalancer.class);
private static final Logger log = LoggerFactory.getLogger(PolarisLoadbalancer.class);
private final String serviceId;
@ -63,8 +63,7 @@ public class PolarisRoutingLoadbalancer extends RoundRobinLoadBalancer implement
private ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider;
public PolarisRoutingLoadbalancer(String serviceId,
ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider,
public PolarisLoadbalancer(String serviceId, ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider,
PolarisLoadBalancerProperties loadBalancerProperties, RouterAPI routerAPI) {
super(supplierObjectProvider, serviceId);
this.serviceId = serviceId;
@ -83,7 +82,7 @@ public class PolarisRoutingLoadbalancer extends RoundRobinLoadBalancer implement
@Override
public Mono<Response<ServiceInstance>> choose(Request request) {
if (!loadBalancerProperties.getLoadbalancerEnabled()) {
if (!loadBalancerProperties.getEnabled()) {
return super.choose(request);
}
ServiceInstanceListSupplier supplier = supplierObjectProvider

@ -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;
@ -48,11 +48,11 @@ import org.springframework.util.CollectionUtils;
*
* @author Haotian Zhang
*/
public class PolarisRouterServiceInstanceListSupplier extends DelegatingServiceInstanceListSupplier {
public class PolarisServiceInstanceListSupplier extends DelegatingServiceInstanceListSupplier {
private final RouterAPI routerAPI;
public PolarisRouterServiceInstanceListSupplier(ServiceInstanceListSupplier delegate, RouterAPI routerAPI) {
public PolarisServiceInstanceListSupplier(ServiceInstanceListSupplier delegate, RouterAPI routerAPI) {
super(delegate);
this.routerAPI = routerAPI;
}

@ -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;

@ -15,10 +15,10 @@
* 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.router.PolarisRouterServiceInstanceListSupplier;
import com.tencent.cloud.polaris.router.PolarisRoutingLoadbalancer;
import com.tencent.cloud.polaris.loadbalancer.PolarisLoadbalancer;
import com.tencent.cloud.polaris.loadbalancer.PolarisServiceInstanceListSupplier;
import com.tencent.polaris.router.api.core.RouterAPI;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@ -59,7 +59,7 @@ public class PolarisLoadBalancerClientConfiguration {
LoadBalancerClientFactory loadBalancerClientFactory, PolarisLoadBalancerProperties loadBalancerProperties,
RouterAPI routerAPI) {
String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
return new PolarisRoutingLoadbalancer(name,
return new PolarisLoadbalancer(name,
loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class),
loadBalancerProperties, routerAPI);
}
@ -74,7 +74,7 @@ public class PolarisLoadBalancerClientConfiguration {
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "polaris")
public ServiceInstanceListSupplier polarisRouterDiscoveryClientServiceInstanceListSupplier(RouterAPI routerAPI,
ConfigurableApplicationContext context) {
return new PolarisRouterServiceInstanceListSupplier(
return new PolarisServiceInstanceListSupplier(
ServiceInstanceListSupplier.builder().withDiscoveryClient().build(context), routerAPI);
}
@ -90,7 +90,7 @@ public class PolarisLoadBalancerClientConfiguration {
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "polaris")
public ServiceInstanceListSupplier polarisRouterDiscoveryClientServiceInstanceListSupplier(RouterAPI routerAPI,
ConfigurableApplicationContext context) {
return new PolarisRouterServiceInstanceListSupplier(
return new PolarisServiceInstanceListSupplier(
ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient().build(context), routerAPI);
}

@ -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 loadbalancer.
* Properties of Polaris loadbalancer.
*
* @author Haotian Zhang
*/
@ConfigurationProperties("spring.cloud.polaris.ribbon")
@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,18 +52,26 @@ public class PolarisLoadBalancerProperties {
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 + ", strategy='" + strategy
+ '\'' + '}';
return "PolarisLoadBalancerProperties{" + "loadbalancerEnabled=" + enabled + ", strategy='" + strategy + '\''
+ '}';
}
}

@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tencent.cloud.polaris.loadbalancer.config.PolarisLoadBalancerAutoConfiguration

@ -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;

@ -15,10 +15,10 @@
* 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.cloud.polaris.router.PolarisRouterServiceInstanceListSupplier;
import com.tencent.cloud.polaris.loadbalancer.PolarisServiceInstanceListSupplier;
import com.tencent.polaris.router.api.core.RouterAPI;
import org.junit.Test;
@ -59,7 +59,7 @@ public class PolarisRouterAutoConfigurationTest {
.doesNotHaveBean(PolarisLoadBalancerClientConfiguration.PolarisReactiveSupportConfiguration.class);
assertThat(context)
.hasSingleBean(PolarisLoadBalancerClientConfiguration.PolarisBlockingSupportConfiguration.class);
assertThat(context).doesNotHaveBean(PolarisRouterServiceInstanceListSupplier.class);
assertThat(context).doesNotHaveBean(PolarisServiceInstanceListSupplier.class);
});
}
@ -75,7 +75,7 @@ public class PolarisRouterAutoConfigurationTest {
.doesNotHaveBean(PolarisLoadBalancerClientConfiguration.PolarisReactiveSupportConfiguration.class);
assertThat(context)
.hasSingleBean(PolarisLoadBalancerClientConfiguration.PolarisBlockingSupportConfiguration.class);
assertThat(context).doesNotHaveBean(PolarisRouterServiceInstanceListSupplier.class);
assertThat(context).doesNotHaveBean(PolarisServiceInstanceListSupplier.class);
});
}
Loading…
Cancel
Save