feat:refactor loadbalancer module as a basic module for router and circuit breaker.

pull/155/head
SkyeBeFreeman 3 years ago
parent 4fdb85ae7c
commit cc4f81507a

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

@ -16,7 +16,7 @@
<!-- Spring Cloud Tencent dependencies start --> <!-- Spring Cloud Tencent dependencies start -->
<dependency> <dependency>
<groupId>com.tencent.cloud</groupId> <groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-polaris-context</artifactId> <artifactId>spring-cloud-tencent-polaris-loadbalancer</artifactId>
</dependency> </dependency>
<!-- Spring Cloud Tencent dependencies end --> <!-- Spring Cloud Tencent dependencies end -->
@ -84,12 +84,6 @@
</dependency> </dependency>
<!-- Polaris dependencies end --> <!-- Polaris dependencies end -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId> <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.core.ProviderAPI;
import com.tencent.polaris.api.pojo.ServiceInfo; import com.tencent.polaris.api.pojo.ServiceInfo;
import com.tencent.polaris.api.rpc.GetAllInstancesRequest; 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.GetInstancesRequest;
import com.tencent.polaris.api.rpc.GetServicesRequest; import com.tencent.polaris.api.rpc.GetServicesRequest;
import com.tencent.polaris.api.rpc.InstancesResponse; import com.tencent.polaris.api.rpc.InstancesResponse;
@ -59,6 +60,7 @@ public class PolarisDiscoveryHandler {
* @param service service name * @param service service name
* @return list of instances * @return list of instances
*/ */
@Deprecated
public InstancesResponse getFilteredInstances(String service) { public InstancesResponse getFilteredInstances(String service) {
String namespace = polarisDiscoveryProperties.getNamespace(); String namespace = polarisDiscoveryProperties.getNamespace();
GetInstancesRequest getInstancesRequest = new GetInstancesRequest(); GetInstancesRequest getInstancesRequest = new GetInstancesRequest();
@ -80,6 +82,19 @@ public class PolarisDiscoveryHandler {
return polarisConsumer.getInstances(getInstancesRequest); 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. * Return all instances for the given service.
* @param service serviceName * @param service serviceName

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

@ -17,79 +17,16 @@
<!-- Spring Cloud Tencent dependencies start --> <!-- Spring Cloud Tencent dependencies start -->
<dependency> <dependency>
<groupId>com.tencent.cloud</groupId> <groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-polaris-context</artifactId> <artifactId>spring-cloud-tencent-polaris-loadbalancer</artifactId>
</dependency> </dependency>
<!-- Spring Cloud Tencent dependencies end --> <!-- Spring Cloud Tencent dependencies end -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
<!-- Polaris dependencies start --> <!-- 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> <dependency>
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>router-rule</artifactId> <artifactId>router-rule</artifactId>
</dependency> </dependency>
<!-- Polaris dependencies end -->
<dependency> </dependencies>
<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> </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 { public final class ContextConstant {
/**
* Name of Polaris.
*/
public static final String POLARIS = "POLARIS";
private ContextConstant() {
}
/** /**
* Order of configuration modifier. * Order of configuration modifier.
*/ */

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

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

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

@ -35,32 +35,32 @@
<dependency> <dependency>
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>polaris-plugin-api</artifactId> <artifactId>polaris-plugin-api</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>connector-polaris-grpc</artifactId> <artifactId>connector-polaris-grpc</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>connector-consul</artifactId> <artifactId>connector-consul</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>connector-composite</artifactId> <artifactId>connector-composite</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>registry-memory</artifactId> <artifactId>registry-memory</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>flow-cache-expired</artifactId> <artifactId>flow-cache-expired</artifactId>
</dependency> </dependency>
<!-- Discovery depended on base routers --> <!-- Discovery depended on base routers -->
<dependency> <dependency>
@ -72,23 +72,23 @@
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>router-healthy</artifactId> <artifactId>router-healthy</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>loadbalancer-random</artifactId> <artifactId>loadbalancer-random</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>loadbalancer-ringhash</artifactId> <artifactId>loadbalancer-ringhash</artifactId>
</dependency> </dependency>
<!-- Polaris dependencies end --> <!-- Polaris dependencies end -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

@ -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. * 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.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.pojo.PolarisServiceInstance; 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.DefaultServiceInstances;
import com.tencent.polaris.api.pojo.Instance; import com.tencent.polaris.api.pojo.Instance;
import com.tencent.polaris.api.pojo.ServiceInstances; 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> * @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; private final String serviceId;
@ -63,8 +63,7 @@ public class PolarisRoutingLoadbalancer extends RoundRobinLoadBalancer implement
private ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider; private ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider;
public PolarisRoutingLoadbalancer(String serviceId, public PolarisLoadbalancer(String serviceId, ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider,
ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider,
PolarisLoadBalancerProperties loadBalancerProperties, RouterAPI routerAPI) { PolarisLoadBalancerProperties loadBalancerProperties, RouterAPI routerAPI) {
super(supplierObjectProvider, serviceId); super(supplierObjectProvider, serviceId);
this.serviceId = serviceId; this.serviceId = serviceId;
@ -83,7 +82,7 @@ public class PolarisRoutingLoadbalancer extends RoundRobinLoadBalancer implement
@Override @Override
public Mono<Response<ServiceInstance>> choose(Request request) { public Mono<Response<ServiceInstance>> choose(Request request) {
if (!loadBalancerProperties.getLoadbalancerEnabled()) { if (!loadBalancerProperties.getEnabled()) {
return super.choose(request); return super.choose(request);
} }
ServiceInstanceListSupplier supplier = supplierObjectProvider ServiceInstanceListSupplier supplier = supplierObjectProvider

@ -15,7 +15,7 @@
* specific language governing permissions and limitations under the License. * 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.ArrayList;
import java.util.List; import java.util.List;
@ -48,11 +48,11 @@ import org.springframework.util.CollectionUtils;
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
public class PolarisRouterServiceInstanceListSupplier extends DelegatingServiceInstanceListSupplier { public class PolarisServiceInstanceListSupplier extends DelegatingServiceInstanceListSupplier {
private final RouterAPI routerAPI; private final RouterAPI routerAPI;
public PolarisRouterServiceInstanceListSupplier(ServiceInstanceListSupplier delegate, RouterAPI routerAPI) { public PolarisServiceInstanceListSupplier(ServiceInstanceListSupplier delegate, RouterAPI routerAPI) {
super(delegate); super(delegate);
this.routerAPI = routerAPI; this.routerAPI = routerAPI;
} }

@ -15,7 +15,7 @@
* specific language governing permissions and limitations under the License. * 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.cloud.polaris.context.ConditionalOnPolarisEnabled;
import com.tencent.polaris.api.exception.PolarisException; import com.tencent.polaris.api.exception.PolarisException;

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

@ -15,30 +15,34 @@
* specific language governing permissions and limitations under the License. * 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; import org.springframework.boot.context.properties.ConfigurationProperties;
/** /**
* Properties of loadbalancer. * Properties of Polaris loadbalancer.
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
@ConfigurationProperties("spring.cloud.polaris.ribbon") @ConfigurationProperties("spring.cloud.polaris.loadbalancer")
public class PolarisLoadBalancerProperties { public class PolarisLoadBalancerProperties {
/** /**
* If load-balance enabled. * If load-balance enabled.
*/ */
@Value("${spring.cloud.polaris.discovery.loadbalancer.enabled:#{true}}") private Boolean enabled = true;
private Boolean loadbalancerEnabled;
/** /**
* Load balance strategy. * Load balance strategy.
*/ */
@Value("${spring.cloud.polaris.loadbalancer.strategy:#{'weightedRandom'}}") private String strategy = "weightedRandom";
private String strategy;
/**
* Type of discovery server.
*/
private String discoveryType = ContextConstant.POLARIS;
public String getStrategy() { public String getStrategy() {
return strategy; return strategy;
@ -48,18 +52,26 @@ public class PolarisLoadBalancerProperties {
this.strategy = strategy; this.strategy = strategy;
} }
public Boolean getLoadbalancerEnabled() { public Boolean getEnabled() {
return loadbalancerEnabled; return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public String getDiscoveryType() {
return discoveryType;
} }
public void setLoadbalancerEnabled(Boolean loadbalancerEnabled) { public void setDiscoveryType(String discoveryType) {
this.loadbalancerEnabled = loadbalancerEnabled; this.discoveryType = discoveryType;
} }
@Override @Override
public String toString() { 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. * 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.context.PolarisContextAutoConfiguration;
import com.tencent.polaris.router.api.core.RouterAPI; import com.tencent.polaris.router.api.core.RouterAPI;

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