Merge branch 'main' into h/config_address

pull/130/head
Haotian Zhang 3 years ago committed by GitHub
commit d559d11673
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,3 +3,5 @@
- [Feature: Support custom rate limit reject response info](https://github.com/Tencent/spring-cloud-tencent/pull/128)
- [Feature: Optimize config server address](https://github.com/Tencent/spring-cloud-tencent/pull/130)
- [Feature: Remove spring-javaformat-maven-plugin](https://github.com/Tencent/spring-cloud-tencent/pull/131)
- [feat:refactor loadbalancer module as a basic module for router and circuit breaker.](https://github.com/Tencent/spring-cloud-tencent/pull/136)

@ -2,6 +2,9 @@
[![Build Status](https://github.com/Tencent/spring-cloud-tencent/actions/workflows/junit_test.yml/badge.svg)](https://github.com/Tencent/spring-cloud-tencent/actions/workflows/junit_test.yml)
[![Maven Central](https://img.shields.io/maven-central/v/com.tencent.cloud/spring-cloud-tencent?label=Maven%20Central)](https://search.maven.org/search?q=g:com.tencent.cloud%20AND%20a:spring-cloud-tencent)
[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)
[![Percentage of issues still open](http://isitmaintained.com/badge/open/Tencent/spring-cloud-tencent.svg)](https://github.com/Tencent/spring-cloud-tencent/issues)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/Tencent/spring-cloud-tencent/wiki/Contributing)
[English](./README.md) | 简体中文
@ -18,13 +21,13 @@ Spring Cloud Tencent 的核心依托腾讯开源的一站式服务发现与治
- [Polaris Github home page](https://github.com/polarismesh/polaris)
- [Polaris official website](https://polarismesh.cn/)
Spring Cloud 腾讯提供的能力包括但不限于:
Spring Cloud Tencent提供的能力包括但不限于:
- 服务注册和发现
- 动态配置管理
- 服务治理
- 服务电流限制
- 服务断路器
- 服务限流
- 服务
- 服务路由
- ...
- 标签透传
@ -79,3 +82,10 @@ Spring Cloud Tencent 所有组件都已上传到 Maven 中央仓库,只需要
## License
The spring-cloud-tencent is licensed under the BSD 3-Clause License. Copyright and license information can be found in the file [LICENSE](LICENSE)
## Stargazers over time
如果您对 Spring Cloud Tencent 有兴趣,请关注我们的项目~
[![Stargazers over time](https://starchart.cc/Tencent/spring-cloud-tencent.svg)](https://starchart.cc/Tencent/spring-cloud-tencent)

@ -2,6 +2,9 @@
[![Build Status](https://github.com/Tencent/spring-cloud-tencent/actions/workflows/junit_test.yml/badge.svg)](https://github.com/Tencent/spring-cloud-tencent/actions/workflows/junit_test.yml)
[![Maven Central](https://img.shields.io/maven-central/v/com.tencent.cloud/spring-cloud-tencent?label=Maven%20Central)](https://search.maven.org/search?q=g:com.tencent.cloud%20AND%20a:spring-cloud-tencent)
[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)
[![Percentage of issues still open](http://isitmaintained.com/badge/open/Tencent/spring-cloud-tencent.svg)](https://github.com/Tencent/spring-cloud-tencent/issues)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/Tencent/spring-cloud-tencent/wiki/Contributing)
English | [简体中文](./README-zh.md)
@ -77,3 +80,10 @@ For example:
## License
The spring-cloud-tencent is licensed under the BSD 3-Clause License. Copyright and license information can be found in the file [LICENSE](LICENSE)
## Stargazers over time
If you are interested in Spring Cloud Tencent, please follow our project, thank you very much.
[![Stargazers over time](https://starchart.cc/Tencent/spring-cloud-tencent.svg)](https://starchart.cc/Tencent/spring-cloud-tencent)

@ -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>
@ -97,6 +98,7 @@
<jacoco.version>0.8.3</jacoco.version>
<maven-source-plugin.version>3.2.0</maven-source-plugin.version>
<flatten-maven-plugin.version>1.2.7</flatten-maven-plugin.version>
<maven-gpg-plugin.version>3.0.1</maven-gpg-plugin.version>
<!-- Checkstyle -->
<maven-checkstyle-plugin.failsOnError>true</maven-checkstyle-plugin.failsOnError>
@ -144,10 +146,6 @@
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>io.spring.javaformat</groupId>
<artifactId>spring-javaformat-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>

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

@ -20,7 +20,9 @@ package com.tencent.cloud.polaris.circuitbreaker;
import com.tencent.cloud.common.constant.ContextConstant;
import com.tencent.cloud.polaris.context.ConditionalOnPolarisEnabled;
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.polaris.api.config.consumer.ServiceRouterConfig;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.plugins.router.healthy.RecoverRouterConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
@ -48,6 +50,16 @@ public class PolarisCircuitBreakerBootstrapConfiguration {
public void modify(ConfigurationImpl configuration) {
// Turn on circuitbreaker configuration
configuration.getConsumer().getCircuitBreaker().setEnable(true);
// Set excludeCircuitBreakInstances to false
RecoverRouterConfig recoverRouterConfig = configuration.getConsumer().getServiceRouter()
.getPluginConfig(ServiceRouterConfig.DEFAULT_ROUTER_RECOVER, RecoverRouterConfig.class);
recoverRouterConfig.setExcludeCircuitBreakInstances(true);
// Update modified config to source properties
configuration.getConsumer().getServiceRouter()
.setPluginConfig(ServiceRouterConfig.DEFAULT_ROUTER_RECOVER, recoverRouterConfig);
}
@Override

@ -17,7 +17,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 -->

@ -0,0 +1,50 @@
/*
* 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;
import com.tencent.cloud.common.constant.ContextConstant;
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.polaris.api.config.consumer.ServiceRouterConfig;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.plugins.router.healthy.RecoverRouterConfig;
/**
* Spring Cloud Tencent config Override polaris config.
*
*@author lepdou 2022-04-24
*/
public class DiscoveryConfigModifier implements PolarisConfigModifier {
@Override
public void modify(ConfigurationImpl configuration) {
// Set excludeCircuitBreakInstances to false
RecoverRouterConfig recoverRouterConfig = configuration.getConsumer().getServiceRouter()
.getPluginConfig(ServiceRouterConfig.DEFAULT_ROUTER_RECOVER, RecoverRouterConfig.class);
recoverRouterConfig.setExcludeCircuitBreakInstances(false);
// Update modified config to source properties
configuration.getConsumer().getServiceRouter()
.setPluginConfig(ServiceRouterConfig.DEFAULT_ROUTER_RECOVER, recoverRouterConfig);
}
@Override
public int getOrder() {
return ContextConstant.ModifierOrder.DISCOVERY_ORDER;
}
}

@ -74,6 +74,11 @@ public class DiscoveryPropertiesAutoConfiguration {
return new PolarisDiscoveryHandler();
}
@Bean
public DiscoveryConfigModifier discoveryConfigModifier() {
return new DiscoveryConfigModifier();
}
@PostConstruct
public void init() {
if (null != polarisDiscoveryProperties) {

@ -28,6 +28,7 @@ import com.tencent.polaris.api.core.ConsumerAPI;
import com.tencent.polaris.api.core.ProviderAPI;
import com.tencent.polaris.api.pojo.ServiceInfo;
import com.tencent.polaris.api.rpc.GetAllInstancesRequest;
import com.tencent.polaris.api.rpc.GetHealthyInstancesRequest;
import com.tencent.polaris.api.rpc.GetInstancesRequest;
import com.tencent.polaris.api.rpc.GetServicesRequest;
import com.tencent.polaris.api.rpc.InstancesResponse;
@ -59,6 +60,7 @@ public class PolarisDiscoveryHandler {
* @param service service name
* @return list of instances
*/
@Deprecated
public InstancesResponse getFilteredInstances(String service) {
String namespace = polarisDiscoveryProperties.getNamespace();
GetInstancesRequest getInstancesRequest = new GetInstancesRequest();
@ -82,6 +84,19 @@ public class PolarisDiscoveryHandler {
return polarisConsumer.getInstances(getInstancesRequest);
}
/**
* Get a list of healthy instances.
* @param service service name
* @return list of healthy instances
*/
public InstancesResponse getHealthyInstances(String service) {
String namespace = polarisDiscoveryProperties.getNamespace();
GetHealthyInstancesRequest getHealthyInstancesRequest = new GetHealthyInstancesRequest();
getHealthyInstancesRequest.setNamespace(namespace);
getHealthyInstancesRequest.setService(service);
return polarisConsumer.getHealthyInstancesInstance(getHealthyInstancesRequest);
}
/**
* Return all instances for the given service.
* @param service serviceName

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

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

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

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

@ -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.
*/
@ -44,6 +52,11 @@ public final class ContextConstant {
*/
public static Integer CIRCUIT_BREAKER_ORDER = 1;
/**
* Order of discovery configuration modifier.
*/
public static Integer DISCOVERY_ORDER = 0;
/**
* Order of configuration modifier.
*/

@ -71,14 +71,13 @@
<properties>
<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>
<!-- Maven Plugin Versions -->
<maven-source-plugin.version>3.2.0</maven-source-plugin.version>
<maven-javadoc-plugin.version>3.1.1</maven-javadoc-plugin.version>
<flatten-maven-plugin.version>1.2.7</flatten-maven-plugin.version>
<maven-gpg-plugin.version>1.6</maven-gpg-plugin.version>
<maven-gpg-plugin.version>3.0.1</maven-gpg-plugin.version>
</properties>
<dependencyManagement>
@ -103,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>

@ -74,11 +74,14 @@ public class PolarisContextProperties {
private List<PolarisConfigModifier> modifierList;
protected Configuration configuration() {
// 1. Read user-defined polaris.yml configuration
ConfigurationImpl configuration = (ConfigurationImpl) ConfigAPIFactory
.defaultConfig(ConfigProvider.DEFAULT_CONFIG);
configuration.setDefault();
// 2. Override user-defined polaris.yml configuration with SCT configuration
String defaultHost = getHost();
configuration.getGlobal().getAPI().setBindIP(defaultHost);
Collection<PolarisConfigModifier> modifiers = modifierList;
modifiers = modifiers.stream()
.sorted(Comparator.comparingInt(PolarisConfigModifier::getOrder))
@ -88,6 +91,7 @@ public class PolarisContextProperties {
modifier.modify(configuration);
}
}
return configuration;
}

@ -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,9 +15,10 @@
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.router;
package com.tencent.cloud.polaris.loadbalancer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -28,49 +29,105 @@ import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.PollingServerListUpdater;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import com.tencent.cloud.common.constant.ContextConstant;
import com.tencent.cloud.common.constant.MetadataConstant.SystemMetadataKey;
import com.tencent.cloud.common.metadata.MetadataContext;
import com.tencent.cloud.common.metadata.MetadataContextHolder;
import com.tencent.cloud.common.pojo.PolarisServer;
import com.tencent.cloud.polaris.loadbalancer.config.PolarisLoadBalancerProperties;
import com.tencent.polaris.api.core.ConsumerAPI;
import com.tencent.polaris.api.pojo.DefaultInstance;
import com.tencent.polaris.api.pojo.DefaultServiceInstances;
import com.tencent.polaris.api.pojo.Instance;
import com.tencent.polaris.api.pojo.ServiceInfo;
import com.tencent.polaris.api.pojo.ServiceInstances;
import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.api.rpc.GetAllInstancesRequest;
import com.tencent.polaris.api.rpc.InstancesResponse;
import com.tencent.polaris.router.api.core.RouterAPI;
import com.tencent.polaris.router.api.rpc.ProcessRoutersRequest;
import com.tencent.polaris.router.api.rpc.ProcessRoutersResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;
/**
* Routing load balancer of polaris.
*
* @author Haotian Zhang
*/
public class PolarisRoutingLoadBalancer extends DynamicServerListLoadBalancer<Server> {
public class PolarisLoadBalancer extends DynamicServerListLoadBalancer<Server> {
private final RouterAPI routerAPI;
public PolarisRoutingLoadBalancer(IClientConfig config, IRule rule, IPing ping,
ServerList<Server> serverList, RouterAPI routerAPI) {
private ConsumerAPI consumerAPI;
private PolarisLoadBalancerProperties polarisLoadBalancerProperties;
public PolarisLoadBalancer(IClientConfig config, IRule rule, IPing ping, ServerList<Server> serverList,
RouterAPI routerAPI, ConsumerAPI consumerAPI, PolarisLoadBalancerProperties properties) {
super(config, rule, ping, serverList, null, new PollingServerListUpdater());
this.routerAPI = routerAPI;
this.consumerAPI = consumerAPI;
this.polarisLoadBalancerProperties = properties;
}
@Override
public List<Server> getReachableServers() {
ServiceInstances serviceInstances;
if (polarisLoadBalancerProperties.getDiscoveryType().equals(ContextConstant.POLARIS)) {
serviceInstances = getPolarisDiscoveryServiceInstances();
}
else {
serviceInstances = getExtendDiscoveryServiceInstances();
}
if (serviceInstances == null || CollectionUtils.isEmpty(serviceInstances.getInstances())) {
return Collections.emptyList();
}
ProcessRoutersRequest processRoutersRequest = new ProcessRoutersRequest();
processRoutersRequest.setDstInstances(serviceInstances);
String srcNamespace = MetadataContext.LOCAL_NAMESPACE;
String srcService = MetadataContext.LOCAL_SERVICE;
Map<String, String> transitiveCustomMetadata = MetadataContextHolder.get()
.getAllTransitiveCustomMetadata();
String method = MetadataContextHolder.get()
.getSystemMetadata(SystemMetadataKey.PEER_PATH);
processRoutersRequest.setMethod(method);
if (StringUtils.isNotBlank(srcNamespace) && StringUtils.isNotBlank(srcService)) {
ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.setNamespace(srcNamespace);
serviceInfo.setService(srcService);
serviceInfo.setMetadata(transitiveCustomMetadata);
processRoutersRequest.setSourceService(serviceInfo);
}
ProcessRoutersResponse processRoutersResponse = routerAPI
.processRouters(processRoutersRequest);
ServiceInstances filteredServiceInstances = processRoutersResponse
.getServiceInstances();
List<Server> filteredInstances = new ArrayList<>();
for (Instance instance : filteredServiceInstances.getInstances()) {
filteredInstances.add(new PolarisServer(serviceInstances, instance));
}
return filteredInstances;
}
private ServiceInstances getPolarisDiscoveryServiceInstances() {
String serviceName = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.PEER_SERVICE);
if (StringUtils.isBlank(serviceName)) {
List<Server> allServers = super.getAllServers();
if (CollectionUtils.isEmpty(allServers)) {
return allServers;
return null;
}
ServiceInstances serviceInstances = null;
if (allServers.get(0) instanceof PolarisServer) {
serviceInstances = ((PolarisServer) allServers.get(0)).getServiceInstances();
serviceName = ((PolarisServer) super.getAllServers().get(0)).getServiceInstances().getService();
}
else {
return getAllInstances(MetadataContext.LOCAL_NAMESPACE, serviceName).toServiceInstances();
}
private ServiceInstances getExtendDiscoveryServiceInstances() {
List<Server> allServers = super.getAllServers();
if (CollectionUtils.isEmpty(allServers)) {
return null;
}
ServiceInstances serviceInstances;
String serviceName;
// notice the difference between different service registries
if (StringUtils.isNotBlank(
@ -82,7 +139,7 @@ public class PolarisRoutingLoadBalancer extends DynamicServerListLoadBalancer<Se
}
if (StringUtils.isBlank(serviceName)) {
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,
serviceName);
@ -101,32 +158,7 @@ public class PolarisRoutingLoadBalancer extends DynamicServerListLoadBalancer<Se
instances.add(instance);
}
serviceInstances = new DefaultServiceInstances(serviceKey, instances);
}
ProcessRoutersRequest processRoutersRequest = new ProcessRoutersRequest();
processRoutersRequest.setDstInstances(serviceInstances);
String srcNamespace = MetadataContext.LOCAL_NAMESPACE;
String srcService = MetadataContext.LOCAL_SERVICE;
Map<String, String> transitiveCustomMetadata = MetadataContextHolder.get()
.getAllTransitiveCustomMetadata();
String method = MetadataContextHolder.get()
.getSystemMetadata(SystemMetadataKey.PEER_PATH);
processRoutersRequest.setMethod(method);
if (StringUtils.isNotBlank(srcNamespace) && StringUtils.isNotBlank(srcService)) {
ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.setNamespace(srcNamespace);
serviceInfo.setService(srcService);
serviceInfo.setMetadata(transitiveCustomMetadata);
processRoutersRequest.setSourceService(serviceInfo);
}
ProcessRoutersResponse processRoutersResponse = routerAPI
.processRouters(processRoutersRequest);
ServiceInstances filteredServiceInstances = processRoutersResponse
.getServiceInstances();
List<Server> filteredInstances = new ArrayList<>();
for (Instance instance : filteredServiceInstances.getInstances()) {
filteredInstances.add(new PolarisServer(serviceInstances, instance));
}
return filteredInstances;
return serviceInstances;
}
@Override
@ -134,4 +166,17 @@ public class PolarisRoutingLoadBalancer extends DynamicServerListLoadBalancer<Se
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.
*/
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;
@ -40,16 +40,15 @@ import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@ConditionalOnPolarisEnabled
@ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.enabled",
matchIfMissing = true)
@ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.enabled", matchIfMissing = true)
@AutoConfigureAfter(RibbonAutoConfiguration.class)
@RibbonClients(defaultConfiguration = PolarisRibbonClientConfiguration.class)
public class PolarisRibbonAutoConfiguration {
public class PolarisLoadBalancerAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public PolarisRibbonProperties polarisRibbonProperties() {
return new PolarisRibbonProperties();
public PolarisLoadBalancerProperties polarisLoadBalancerProperties() {
return new PolarisLoadBalancerProperties();
}
@Bean(name = "polarisRoute")

@ -15,30 +15,34 @@
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.router.config;
package com.tencent.cloud.polaris.loadbalancer.config;
import com.tencent.cloud.common.constant.ContextConstant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Properties of Ribbon.
* Properties of Polaris loadbalancer.
*
* @author Haotian Zhang
*/
@ConfigurationProperties("spring.cloud.polaris.ribbon")
public class PolarisRibbonProperties {
@ConfigurationProperties("spring.cloud.polaris.loadbalancer")
public class PolarisLoadBalancerProperties {
/**
* If load-balance enabled.
*/
@Value("${spring.cloud.polaris.discovery.loadbalancer.enabled:#{true}}")
private Boolean loadbalancerEnabled;
private Boolean enabled = true;
/**
* Load balance strategy.
*/
@Value("${spring.cloud.polaris.loadbalancer.strategy:#{'weightedRandom'}}")
private String strategy;
private String strategy = "weightedRandom";
/**
* Type of discovery server.
*/
private String discoveryType = ContextConstant.POLARIS;
public String getStrategy() {
return strategy;
@ -48,17 +52,25 @@ public class PolarisRibbonProperties {
this.strategy = strategy;
}
public Boolean getLoadbalancerEnabled() {
return loadbalancerEnabled;
public Boolean getEnabled() {
return enabled;
}
public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}
public String getDiscoveryType() {
return discoveryType;
}
public void setLoadbalancerEnabled(Boolean loadbalancerEnabled) {
this.loadbalancerEnabled = loadbalancerEnabled;
public void setDiscoveryType(String discoveryType) {
this.discoveryType = discoveryType;
}
@Override
public String toString() {
return "PolarisRibbonProperties{" + "loadbalancerEnabled=" + loadbalancerEnabled
return "PolarisLoadBalancerProperties{" + "loadbalancerEnabled=" + enabled
+ ", strategy='" + strategy + '\'' + '}';
}

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

@ -15,7 +15,7 @@
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.router.rule;
package com.tencent.cloud.polaris.loadbalancer.rule;
import java.util.Arrays;

@ -4,13 +4,19 @@
"name": "spring.cloud.polaris.loadbalancer.enabled",
"type": "java.lang.Boolean",
"defaultValue": "true",
"description": "polaris loadbalancer"
"description": "polaris loadbalancer."
},
{
"name": "spring.cloud.polaris.loadbalancer.discoveryType",
"type": "java.lang.String",
"defaultValue": "POLARIS",
"description": "Type of discovery server."
},
{
"name": "spring.cloud.polaris.loadbalancer.strategy",
"type": "java.lang.String",
"defaultValue": "random",
"description": "retry,best_available,availability_filtering,round_robin,weighted_response_time,zone_avoidance,random,consistent_hash,weighted_random"
"description": "retry,best_available,availability_filtering,round_robin,weighted_response_time,zone_avoidance,random,consistent_hash,weighted_random."
}
]
}

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