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

pull/136/head
SkyeBeFreeman 3 years ago
parent d152da56fb
commit 3e0d2241bb

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

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

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

@ -50,7 +50,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 InstancesResponse filteredInstances = polarisDiscoveryHandler
.getFilteredInstances(serviceId); .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));

@ -57,7 +57,7 @@ public class PolarisServerList extends AbstractServerList<Server> {
private List<Server> getServers() { private List<Server> getServers() {
InstancesResponse allInstances = polarisDiscoveryHandler InstancesResponse allInstances = polarisDiscoveryHandler
.getFilteredInstances(serviceId); .getHealthyInstances(serviceId);
ServiceInstances serviceInstances = allInstances.toServiceInstances(); ServiceInstances serviceInstances = allInstances.toServiceInstances();
List<Server> polarisServers = new ArrayList<>(); List<Server> polarisServers = new ArrayList<>();
for (Instance instance : serviceInstances.getInstances()) { for (Instance instance : serviceInstances.getInstances()) {

@ -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 -->
<!-- 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.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project> </project>

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

@ -71,7 +71,7 @@
<properties> <properties>
<revision>1.4.0-Hoxton.SR9-SNAPSHOT</revision> <revision>1.4.0-Hoxton.SR9-SNAPSHOT</revision>
<polaris.version>1.4.0</polaris.version> <polaris.version>1.5.0-SNAPSHOT</polaris.version>
<powermock.version>2.0.0</powermock.version> <powermock.version>2.0.0</powermock.version>
<!-- Maven Plugin Versions --> <!-- Maven Plugin Versions -->
@ -103,6 +103,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>

@ -18,44 +18,44 @@
<dependency> <dependency>
<groupId>com.tencent.cloud</groupId> <groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-commons</artifactId> <artifactId>spring-cloud-tencent-commons</artifactId>
</dependency> </dependency>
<!-- Spring Cloud Tencent dependencies end --> <!-- Spring Cloud Tencent dependencies end -->
<!-- Polaris dependencies start --> <!-- Polaris dependencies start -->
<dependency> <dependency>
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>polaris-client</artifactId> <artifactId>polaris-client</artifactId>
</dependency> </dependency>
<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>
@ -67,23 +67,23 @@
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>router-healthy</artifactId> <artifactId>router-healthy</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>loadbalancer-random</artifactId>
</dependency>
<dependency> <dependency>
<groupId>com.tencent.polaris</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>loadbalancer-ringhash</artifactId> <artifactId>loadbalancer-random</artifactId>
</dependency> </dependency>
<!-- Polaris dependencies end -->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>com.tencent.polaris</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>loadbalancer-ringhash</artifactId>
<scope>test</scope> </dependency>
</dependency> <!-- Polaris dependencies end -->
</dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</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-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

@ -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;
@ -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.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder; import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.pojo.PolarisServer; 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.DefaultInstance;
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.ServiceInfo; import com.tencent.polaris.api.pojo.ServiceInfo;
import com.tencent.polaris.api.pojo.ServiceInstances; import com.tencent.polaris.api.pojo.ServiceInstances;
import com.tencent.polaris.api.pojo.ServiceKey; 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.core.RouterAPI;
import com.tencent.polaris.router.api.rpc.ProcessRoutersRequest; import com.tencent.polaris.router.api.rpc.ProcessRoutersRequest;
import com.tencent.polaris.router.api.rpc.ProcessRoutersResponse; import com.tencent.polaris.router.api.rpc.ProcessRoutersResponse;
@ -50,28 +53,51 @@ import org.springframework.util.CollectionUtils;
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
public class PolarisRoutingLoadBalancer extends DynamicServerListLoadBalancer<Server> { public class PolarisLoadBalancer extends DynamicServerListLoadBalancer<Server> {
private final RouterAPI routerAPI; private final RouterAPI routerAPI;
public PolarisRoutingLoadBalancer(IClientConfig config, IRule rule, IPing ping, private ConsumerAPI consumerAPI;
ServerList<Server> serverList, RouterAPI routerAPI) {
private boolean isPolarisDiscovery = true;
private boolean isFirstCall = true;
public PolarisLoadBalancer(IClientConfig config, IRule rule, IPing ping,
ServerList<Server> serverList, RouterAPI routerAPI, ConsumerAPI consumerAPI) {
super(config, rule, ping, serverList, null, new PollingServerListUpdater()); super(config, rule, ping, serverList, null, new PollingServerListUpdater());
this.routerAPI = routerAPI; this.routerAPI = routerAPI;
this.consumerAPI = consumerAPI;
} }
@Override @Override
public List<Server> getReachableServers() { public List<Server> getReachableServers() {
List<Server> allServers = super.getAllServers(); List<Server> allServers = null;
if (CollectionUtils.isEmpty(allServers)) { if (isFirstCall) {
return allServers; 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 serviceInstances;
serviceInstances = ((PolarisServer) allServers.get(0)).getServiceInstances(); 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 { else {
String serviceName; if (CollectionUtils.isEmpty(allServers)) {
allServers = super.getAllServers();
}
// notice the difference between different service registries // notice the difference between different service registries
if (StringUtils.isNotBlank( if (StringUtils.isNotBlank(
allServers.get(0).getMetaInfo().getServiceIdForDiscovery())) { allServers.get(0).getMetaInfo().getServiceIdForDiscovery())) {
@ -82,7 +108,7 @@ public class PolarisRoutingLoadBalancer extends DynamicServerListLoadBalancer<Se
} }
if (StringUtils.isBlank(serviceName)) { if (StringUtils.isBlank(serviceName)) {
throw new IllegalStateException( throw new IllegalStateException(
"PolarisRoutingLoadBalancer only Server with AppName or ServiceIdForDiscovery attribute"); "PolarisLoadBalancer only Server with AppName or ServiceIdForDiscovery attribute");
} }
ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE,
serviceName); serviceName);
@ -134,4 +160,17 @@ public class PolarisRoutingLoadBalancer extends DynamicServerListLoadBalancer<Se
return getReachableServers(); 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);
}
} }

@ -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;
@ -44,12 +44,12 @@ import org.springframework.context.annotation.Configuration;
matchIfMissing = true) matchIfMissing = true)
@AutoConfigureAfter(RibbonAutoConfiguration.class) @AutoConfigureAfter(RibbonAutoConfiguration.class)
@RibbonClients(defaultConfiguration = PolarisRibbonClientConfiguration.class) @RibbonClients(defaultConfiguration = PolarisRibbonClientConfiguration.class)
public class PolarisRibbonAutoConfiguration { public class PolarisLoadBalancerAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public PolarisRibbonProperties polarisRibbonProperties() { public PolarisLoadBalancerProperties polarisLoadBalancerProperties() {
return new PolarisRibbonProperties(); return new PolarisLoadBalancerProperties();
} }
@Bean(name = "polarisRoute") @Bean(name = "polarisRoute")

@ -15,30 +15,27 @@
* 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 org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
/** /**
* Properties of Ribbon. * Properties of Polaris loadbalancer.
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
@ConfigurationProperties("spring.cloud.polaris.ribbon") @ConfigurationProperties("spring.cloud.polaris.loadbalancer")
public class PolarisRibbonProperties { 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;
public String getStrategy() { public String getStrategy() {
return strategy; return strategy;
@ -48,17 +45,17 @@ public class PolarisRibbonProperties {
this.strategy = strategy; this.strategy = strategy;
} }
public Boolean getLoadbalancerEnabled() { public Boolean getEnabled() {
return loadbalancerEnabled; return enabled;
} }
public void setLoadbalancerEnabled(Boolean loadbalancerEnabled) { public void setEnabled(Boolean enabled) {
this.loadbalancerEnabled = loadbalancerEnabled; this.enabled = enabled;
} }
@Override @Override
public String toString() { public String toString() {
return "PolarisRibbonProperties{" + "loadbalancerEnabled=" + loadbalancerEnabled return "PolarisLoadBalancerProperties{" + "loadbalancerEnabled=" + enabled
+ ", strategy='" + strategy + '\'' + '}'; + ", strategy='" + strategy + '\'' + '}';
} }

@ -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.netflix.client.config.IClientConfig; import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ILoadBalancer; import com.netflix.loadbalancer.ILoadBalancer;
@ -23,9 +23,10 @@ import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList; import com.netflix.loadbalancer.ServerList;
import com.tencent.cloud.polaris.router.PolarisRoutingLoadBalancer; import com.tencent.cloud.polaris.loadbalancer.PolarisLoadBalancer;
import com.tencent.cloud.polaris.router.rule.PolarisLoadBalanceRule; import com.tencent.cloud.polaris.loadbalancer.rule.PolarisLoadBalanceRule;
import com.tencent.cloud.polaris.router.rule.PolarisWeightedRandomRule; import com.tencent.cloud.polaris.loadbalancer.rule.PolarisWeightedRandomRule;
import com.tencent.polaris.api.core.ConsumerAPI;
import com.tencent.polaris.router.api.core.RouterAPI; import com.tencent.polaris.router.api.core.RouterAPI;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@ -42,9 +43,10 @@ public class PolarisRibbonClientConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public IRule polarisRibbonRule(PolarisRibbonProperties polarisRibbonProperties) { public IRule polarisRibbonRule(
PolarisLoadBalancerProperties polarisLoadBalancerProperties) {
switch (PolarisLoadBalanceRule switch (PolarisLoadBalanceRule
.fromStrategy(polarisRibbonProperties.getStrategy())) { .fromStrategy(polarisLoadBalancerProperties.getStrategy())) {
case WEIGHTED_RANDOM_RULE: case WEIGHTED_RANDOM_RULE:
default: default:
return new PolarisWeightedRandomRule(); return new PolarisWeightedRandomRule();
@ -53,11 +55,11 @@ public class PolarisRibbonClientConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public ILoadBalancer polarisRoutingLoadBalancer(IClientConfig iClientConfig, public ILoadBalancer polarisLoadBalancer(IClientConfig iClientConfig, IRule iRule,
IRule iRule, IPing iPing, ServerList<Server> serverList, IPing iPing, ServerList<Server> serverList, RouterAPI polarisRouter,
RouterAPI polarisRouter) { ConsumerAPI consumerAPI) {
return new PolarisRoutingLoadBalancer(iClientConfig, iRule, iPing, serverList, return new PolarisLoadBalancer(iClientConfig, iRule, iPing, serverList,
polarisRouter); polarisRouter, consumerAPI);
} }
} }

@ -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.rule; package com.tencent.cloud.polaris.loadbalancer.rule;
import java.util.Arrays; import java.util.Arrays;

@ -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.ArrayList;
import java.util.List; import java.util.List;

@ -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;
@ -31,15 +31,15 @@ import static com.tencent.polaris.test.common.Consts.SERVICE_PROVIDER;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
* Test for {@link PolarisRibbonAutoConfiguration} * Test for {@link PolarisLoadBalancerAutoConfiguration}
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
public class PolarisRibbonAutoConfigurationTest { public class PolarisLoadBalancerAutoConfigurationTest {
private ApplicationContextRunner contextRunner = new ApplicationContextRunner() private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PolarisRibbonTest.class, .withConfiguration(AutoConfigurations.of(PolarisRibbonTest.class,
PolarisRibbonAutoConfiguration.class, PolarisLoadBalancerAutoConfiguration.class,
PolarisContextAutoConfiguration.class)) PolarisContextAutoConfiguration.class))
.withPropertyValues("spring.application.name=" + SERVICE_PROVIDER) .withPropertyValues("spring.application.name=" + SERVICE_PROVIDER)
.withPropertyValues("server.port=" + PORT) .withPropertyValues("server.port=" + PORT)
@ -49,7 +49,7 @@ public class PolarisRibbonAutoConfigurationTest {
public void testDefaultInitialization() { public void testDefaultInitialization() {
this.contextRunner.run(context -> { this.contextRunner.run(context -> {
assertThat(context).hasSingleBean(RouterAPI.class); assertThat(context).hasSingleBean(RouterAPI.class);
assertThat(context).hasSingleBean(PolarisRibbonProperties.class); assertThat(context).hasSingleBean(PolarisLoadBalancerProperties.class);
}); });
} }
Loading…
Cancel
Save