refactor:move loadbalancer to discovery module. (#844)
* refactor:move loadbalancer to discovery module. * fix:fix config rebinder bean conflict.pull/855/head
parent
b6f7dbe8c6
commit
fdc9509d3c
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software distributed
|
||||||
|
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||||
|
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||||
|
* specific language governing permissions and limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.tencent.cloud.polaris.loadbalancer;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled;
|
||||||
|
import com.tencent.polaris.router.api.core.RouterAPI;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
|
||||||
|
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
|
||||||
|
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration of loadbalancer client.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
|
||||||
|
*/
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@ConditionalOnPolarisEnabled
|
||||||
|
@ConditionalOnDiscoveryEnabled
|
||||||
|
public class PolarisLoadBalancerClientConfiguration {
|
||||||
|
|
||||||
|
private final static String STRATEGY_WEIGHT = "polarisWeighted";
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
@ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.strategy", havingValue = STRATEGY_WEIGHT)
|
||||||
|
public ReactorLoadBalancer<ServiceInstance> polarisLoadBalancer(Environment environment,
|
||||||
|
LoadBalancerClientFactory loadBalancerClientFactory, RouterAPI routerAPI) {
|
||||||
|
String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
|
||||||
|
return new PolarisLoadBalancer(name,
|
||||||
|
loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), routerAPI);
|
||||||
|
}
|
||||||
|
}
|
@ -1,14 +0,0 @@
|
|||||||
ESAPI.printProperties=true
|
|
||||||
ESAPI.Encoder=org.owasp.esapi.reference.DefaultEncoder
|
|
||||||
ESAPI.Logger=org.owasp.esapi.logging.slf4j.Slf4JLogFactory
|
|
||||||
|
|
||||||
Encoder.AllowMultipleEncoding=false
|
|
||||||
Encoder.AllowMixedEncoding=false
|
|
||||||
Encoder.DefaultCodecList=HTMLEntityCodec,PercentCodec,JavaScriptCodec
|
|
||||||
|
|
||||||
Logger.LogEncodingRequired=false
|
|
||||||
Logger.UserInfo=false
|
|
||||||
Logger.ClientInfo=false
|
|
||||||
Logger.ApplicationName=ExampleApplication
|
|
||||||
Logger.LogApplicationName=false
|
|
||||||
Logger.LogServerIP=false
|
|
@ -1,14 +0,0 @@
|
|||||||
ESAPI.printProperties=true
|
|
||||||
ESAPI.Encoder=org.owasp.esapi.reference.DefaultEncoder
|
|
||||||
ESAPI.Logger=org.owasp.esapi.logging.slf4j.Slf4JLogFactory
|
|
||||||
|
|
||||||
Encoder.AllowMultipleEncoding=false
|
|
||||||
Encoder.AllowMixedEncoding=false
|
|
||||||
Encoder.DefaultCodecList=HTMLEntityCodec,PercentCodec,JavaScriptCodec
|
|
||||||
|
|
||||||
Logger.LogEncodingRequired=false
|
|
||||||
Logger.UserInfo=false
|
|
||||||
Logger.ClientInfo=false
|
|
||||||
Logger.ApplicationName=ExampleApplication
|
|
||||||
Logger.LogApplicationName=false
|
|
||||||
Logger.LogServerIP=false
|
|
@ -1,14 +0,0 @@
|
|||||||
ESAPI.printProperties=true
|
|
||||||
ESAPI.Encoder=org.owasp.esapi.reference.DefaultEncoder
|
|
||||||
ESAPI.Logger=org.owasp.esapi.logging.slf4j.Slf4JLogFactory
|
|
||||||
|
|
||||||
Encoder.AllowMultipleEncoding=false
|
|
||||||
Encoder.AllowMixedEncoding=false
|
|
||||||
Encoder.DefaultCodecList=HTMLEntityCodec,PercentCodec,JavaScriptCodec
|
|
||||||
|
|
||||||
Logger.LogEncodingRequired=false
|
|
||||||
Logger.UserInfo=false
|
|
||||||
Logger.ClientInfo=false
|
|
||||||
Logger.ApplicationName=ExampleApplication
|
|
||||||
Logger.LogApplicationName=false
|
|
||||||
Logger.LogServerIP=false
|
|
@ -1,14 +0,0 @@
|
|||||||
ESAPI.printProperties=true
|
|
||||||
ESAPI.Encoder=org.owasp.esapi.reference.DefaultEncoder
|
|
||||||
ESAPI.Logger=org.owasp.esapi.logging.slf4j.Slf4JLogFactory
|
|
||||||
|
|
||||||
Encoder.AllowMultipleEncoding=false
|
|
||||||
Encoder.AllowMixedEncoding=false
|
|
||||||
Encoder.DefaultCodecList=HTMLEntityCodec,PercentCodec,JavaScriptCodec
|
|
||||||
|
|
||||||
Logger.LogEncodingRequired=false
|
|
||||||
Logger.UserInfo=false
|
|
||||||
Logger.ClientInfo=false
|
|
||||||
Logger.ApplicationName=ExampleApplication
|
|
||||||
Logger.LogApplicationName=false
|
|
||||||
Logger.LogServerIP=false
|
|
@ -1,14 +0,0 @@
|
|||||||
ESAPI.printProperties=true
|
|
||||||
ESAPI.Encoder=org.owasp.esapi.reference.DefaultEncoder
|
|
||||||
ESAPI.Logger=org.owasp.esapi.logging.slf4j.Slf4JLogFactory
|
|
||||||
|
|
||||||
Encoder.AllowMultipleEncoding=false
|
|
||||||
Encoder.AllowMixedEncoding=false
|
|
||||||
Encoder.DefaultCodecList=HTMLEntityCodec,PercentCodec,JavaScriptCodec
|
|
||||||
|
|
||||||
Logger.LogEncodingRequired=false
|
|
||||||
Logger.UserInfo=false
|
|
||||||
Logger.ClientInfo=false
|
|
||||||
Logger.ApplicationName=ExampleApplication
|
|
||||||
Logger.LogApplicationName=false
|
|
||||||
Logger.LogServerIP=false
|
|
@ -1,72 +0,0 @@
|
|||||||
<?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>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.mockito</groupId>
|
|
||||||
<artifactId>mockito-inline</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.mockito</groupId>
|
|
||||||
<artifactId>mockito-core</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>net.bytebuddy</groupId>
|
|
||||||
<artifactId>byte-buddy</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
</project>
|
|
@ -1,81 +0,0 @@
|
|||||||
/*
|
|
||||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
|
||||||
*
|
|
||||||
* Licensed under the BSD 3-Clause License (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://opensource.org/licenses/BSD-3-Clause
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed
|
|
||||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
||||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
||||||
* specific language governing permissions and limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.tencent.cloud.polaris.loadbalancer;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
|
||||||
import com.tencent.cloud.common.pojo.PolarisServiceInstance;
|
|
||||||
import com.tencent.polaris.api.pojo.DefaultInstance;
|
|
||||||
import org.apache.commons.lang.StringUtils;
|
|
||||||
import reactor.core.publisher.Flux;
|
|
||||||
|
|
||||||
import org.springframework.cloud.client.ServiceInstance;
|
|
||||||
import org.springframework.cloud.client.loadbalancer.Request;
|
|
||||||
import org.springframework.cloud.loadbalancer.core.DelegatingServiceInstanceListSupplier;
|
|
||||||
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
|
|
||||||
import org.springframework.util.CollectionUtils;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Service instance list supplier of Polaris.
|
|
||||||
*
|
|
||||||
* @author Haotian Zhang
|
|
||||||
*/
|
|
||||||
public class PolarisServiceInstanceListSupplier extends DelegatingServiceInstanceListSupplier {
|
|
||||||
|
|
||||||
public PolarisServiceInstanceListSupplier(ServiceInstanceListSupplier delegate) {
|
|
||||||
super(delegate);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Flux<List<ServiceInstance>> get() {
|
|
||||||
return getDelegate().get().map(this::chooseInstances);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Flux<List<ServiceInstance>> get(Request request) {
|
|
||||||
return super.get(request);
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<ServiceInstance> chooseInstances(List<ServiceInstance> allServers) {
|
|
||||||
if (CollectionUtils.isEmpty(allServers)) {
|
|
||||||
return allServers;
|
|
||||||
}
|
|
||||||
|
|
||||||
String serviceName = allServers.get(0).getServiceId();
|
|
||||||
if (StringUtils.isBlank(serviceName)) {
|
|
||||||
throw new IllegalStateException(
|
|
||||||
"PolarisRoutingLoadBalancer only Server with AppName or ServiceIdForDiscovery attribute");
|
|
||||||
}
|
|
||||||
List<ServiceInstance> serviceInstances = new ArrayList<>(allServers.size());
|
|
||||||
for (ServiceInstance server : allServers) {
|
|
||||||
DefaultInstance instance = new DefaultInstance();
|
|
||||||
instance.setNamespace(MetadataContext.LOCAL_NAMESPACE);
|
|
||||||
instance.setService(serviceName);
|
|
||||||
instance.setProtocol(server.getScheme());
|
|
||||||
instance.setId(server.getInstanceId());
|
|
||||||
instance.setHost(server.getHost());
|
|
||||||
instance.setPort(server.getPort());
|
|
||||||
instance.setWeight(100);
|
|
||||||
instance.setMetadata(server.getMetadata());
|
|
||||||
serviceInstances.add(new PolarisServiceInstance(instance));
|
|
||||||
}
|
|
||||||
return serviceInstances;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,104 +0,0 @@
|
|||||||
/*
|
|
||||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
|
||||||
*
|
|
||||||
* Licensed under the BSD 3-Clause License (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://opensource.org/licenses/BSD-3-Clause
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed
|
|
||||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
||||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
||||||
* specific language governing permissions and limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.tencent.cloud.polaris.loadbalancer.config;
|
|
||||||
|
|
||||||
import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled;
|
|
||||||
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;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
||||||
import org.springframework.cloud.client.ConditionalOnBlockingDiscoveryEnabled;
|
|
||||||
import org.springframework.cloud.client.ConditionalOnDiscoveryEnabled;
|
|
||||||
import org.springframework.cloud.client.ConditionalOnReactiveDiscoveryEnabled;
|
|
||||||
import org.springframework.cloud.client.ServiceInstance;
|
|
||||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
|
||||||
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
|
|
||||||
import org.springframework.cloud.loadbalancer.core.ReactorLoadBalancer;
|
|
||||||
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
|
|
||||||
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
|
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.core.annotation.Order;
|
|
||||||
import org.springframework.core.env.Environment;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Configuration of loadbalancer client.
|
|
||||||
*
|
|
||||||
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
|
|
||||||
*/
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
|
||||||
@ConditionalOnPolarisEnabled
|
|
||||||
@ConditionalOnDiscoveryEnabled
|
|
||||||
public class PolarisLoadBalancerClientConfiguration {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Order of reactive discovery service instance supplier.
|
|
||||||
*/
|
|
||||||
private static final int REACTIVE_SERVICE_INSTANCE_SUPPLIER_ORDER = 193827465;
|
|
||||||
|
|
||||||
private final static String STRATEGY_WEIGHT = "polarisWeighted";
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnMissingBean
|
|
||||||
@ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.strategy", havingValue = STRATEGY_WEIGHT)
|
|
||||||
public ReactorLoadBalancer<ServiceInstance> polarisLoadBalancer(Environment environment,
|
|
||||||
LoadBalancerClientFactory loadBalancerClientFactory, PolarisLoadBalancerProperties loadBalancerProperties,
|
|
||||||
RouterAPI routerAPI) {
|
|
||||||
String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
|
|
||||||
return new PolarisLoadBalancer(name,
|
|
||||||
loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class),
|
|
||||||
loadBalancerProperties, routerAPI);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
|
||||||
@ConditionalOnReactiveDiscoveryEnabled
|
|
||||||
@Order(REACTIVE_SERVICE_INSTANCE_SUPPLIER_ORDER)
|
|
||||||
protected static class PolarisReactiveSupportConfiguration {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnMissingBean
|
|
||||||
@ConditionalOnBean(ReactiveDiscoveryClient.class)
|
|
||||||
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "polaris")
|
|
||||||
public ServiceInstanceListSupplier polarisRouterDiscoveryClientServiceInstanceListSupplier(
|
|
||||||
ConfigurableApplicationContext context) {
|
|
||||||
return new PolarisServiceInstanceListSupplier(
|
|
||||||
ServiceInstanceListSupplier.builder().withDiscoveryClient().build(context));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
|
||||||
@ConditionalOnBlockingDiscoveryEnabled
|
|
||||||
@Order(REACTIVE_SERVICE_INSTANCE_SUPPLIER_ORDER + 1)
|
|
||||||
protected static class PolarisBlockingSupportConfiguration {
|
|
||||||
|
|
||||||
@Bean
|
|
||||||
@ConditionalOnMissingBean
|
|
||||||
@ConditionalOnBean(DiscoveryClient.class)
|
|
||||||
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "polaris")
|
|
||||||
public ServiceInstanceListSupplier polarisRouterDiscoveryClientServiceInstanceListSupplier(
|
|
||||||
ConfigurableApplicationContext context) {
|
|
||||||
return new PolarisServiceInstanceListSupplier(
|
|
||||||
ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient().build(context));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,76 +0,0 @@
|
|||||||
/*
|
|
||||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
|
||||||
*
|
|
||||||
* Licensed under the BSD 3-Clause License (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://opensource.org/licenses/BSD-3-Clause
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed
|
|
||||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
||||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
||||||
* specific language governing permissions and limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.tencent.cloud.polaris.loadbalancer.config;
|
|
||||||
|
|
||||||
import com.tencent.cloud.common.constant.ContextConstant;
|
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Properties of Polaris loadbalancer.
|
|
||||||
*
|
|
||||||
* @author Haotian Zhang
|
|
||||||
*/
|
|
||||||
@ConfigurationProperties("spring.cloud.polaris.loadbalancer")
|
|
||||||
public class PolarisLoadBalancerProperties {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* If load-balance enabled.
|
|
||||||
*/
|
|
||||||
private Boolean enabled = true;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Load balance strategy.
|
|
||||||
*/
|
|
||||||
private String strategy;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Type of discovery server.
|
|
||||||
*/
|
|
||||||
private String discoveryType = ContextConstant.POLARIS;
|
|
||||||
|
|
||||||
public String getStrategy() {
|
|
||||||
return strategy;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setStrategy(String strategy) {
|
|
||||||
this.strategy = strategy;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Boolean getEnabled() {
|
|
||||||
return enabled;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setEnabled(Boolean enabled) {
|
|
||||||
this.enabled = enabled;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDiscoveryType() {
|
|
||||||
return discoveryType;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDiscoveryType(String discoveryType) {
|
|
||||||
this.discoveryType = discoveryType;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "PolarisLoadBalancerProperties{" + "loadbalancerEnabled=" + enabled + ", strategy='" + strategy + '\''
|
|
||||||
+ '}';
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
{
|
|
||||||
"properties": [
|
|
||||||
{
|
|
||||||
"name": "spring.cloud.polaris.loadbalancer.enabled",
|
|
||||||
"type": "java.lang.Boolean",
|
|
||||||
"defaultValue": "true",
|
|
||||||
"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."
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
@ -1,2 +0,0 @@
|
|||||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
|
||||||
com.tencent.cloud.polaris.loadbalancer.config.PolarisLoadBalancerAutoConfiguration
|
|
@ -1,94 +0,0 @@
|
|||||||
/*
|
|
||||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
|
||||||
*
|
|
||||||
* Licensed under the BSD 3-Clause License (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* https://opensource.org/licenses/BSD-3-Clause
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software distributed
|
|
||||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
||||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
||||||
* specific language governing permissions and limitations under the License.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.tencent.cloud.polaris.loadbalancer;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import com.tencent.cloud.common.pojo.PolarisServiceInstance;
|
|
||||||
import com.tencent.cloud.common.util.ApplicationContextAwareUtils;
|
|
||||||
import org.assertj.core.api.Assertions;
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.mockito.Mock;
|
|
||||||
import org.mockito.MockedStatic;
|
|
||||||
import org.mockito.Mockito;
|
|
||||||
import org.mockito.junit.MockitoJUnitRunner;
|
|
||||||
|
|
||||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
|
||||||
import org.springframework.cloud.client.ServiceInstance;
|
|
||||||
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
|
|
||||||
|
|
||||||
import static com.tencent.cloud.common.metadata.MetadataContext.LOCAL_NAMESPACE;
|
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test for {@link PolarisServiceInstanceListSupplier}.
|
|
||||||
*
|
|
||||||
* @author rod.xu
|
|
||||||
*/
|
|
||||||
@RunWith(MockitoJUnitRunner.class)
|
|
||||||
public class PolarisServiceInstanceListSupplierTest {
|
|
||||||
|
|
||||||
@Mock
|
|
||||||
private ServiceInstanceListSupplier serviceInstanceListSupplier;
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void chooseInstancesTest() {
|
|
||||||
try (MockedStatic<ApplicationContextAwareUtils> mockedApplicationContextAwareUtils = Mockito
|
|
||||||
.mockStatic(ApplicationContextAwareUtils.class)) {
|
|
||||||
mockedApplicationContextAwareUtils.when(() -> ApplicationContextAwareUtils.getProperties(anyString()))
|
|
||||||
.thenReturn("test-unit");
|
|
||||||
|
|
||||||
PolarisServiceInstanceListSupplier instanceListSupplier =
|
|
||||||
new PolarisServiceInstanceListSupplier(serviceInstanceListSupplier);
|
|
||||||
|
|
||||||
List<ServiceInstance> allServers = new ArrayList<>();
|
|
||||||
ServiceInstance instance1 = new DefaultServiceInstance("unit-test-instanceId-01",
|
|
||||||
"unit-test-serviceId", "unit-test-host-01", 8090, false);
|
|
||||||
ServiceInstance instance2 = new DefaultServiceInstance("unit-test-instanceId-02",
|
|
||||||
"unit-test-serviceId", "unit-test-host-02", 8090, false);
|
|
||||||
|
|
||||||
allServers.add(instance1);
|
|
||||||
allServers.add(instance2);
|
|
||||||
|
|
||||||
List<ServiceInstance> polarisInstanceList = instanceListSupplier.chooseInstances(allServers);
|
|
||||||
|
|
||||||
Assertions.assertThat(polarisInstanceList).isNotNull();
|
|
||||||
Assertions.assertThat(polarisInstanceList.size()).isEqualTo(allServers.size());
|
|
||||||
|
|
||||||
for (ServiceInstance serviceInstance : polarisInstanceList) {
|
|
||||||
Assertions.assertThat(serviceInstance instanceof PolarisServiceInstance).isTrue();
|
|
||||||
|
|
||||||
PolarisServiceInstance polarisServiceInstance = (PolarisServiceInstance) serviceInstance;
|
|
||||||
|
|
||||||
Assertions.assertThat(polarisServiceInstance.isSecure()).isFalse();
|
|
||||||
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getService())
|
|
||||||
.isEqualTo("unit-test-serviceId");
|
|
||||||
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getNamespace())
|
|
||||||
.isEqualTo(LOCAL_NAMESPACE);
|
|
||||||
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getPort()).isEqualTo(8090);
|
|
||||||
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getId()
|
|
||||||
.startsWith("unit-test-instanceId")).isTrue();
|
|
||||||
Assertions.assertThat(polarisServiceInstance.getPolarisInstance().getHost()
|
|
||||||
.startsWith("unit-test-host")).isTrue();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue