commit
9a0b47720c
@ -1,5 +1,4 @@
|
||||
server:
|
||||
session-timeout: 1800
|
||||
port: 48081
|
||||
spring:
|
||||
application:
|
@ -1,10 +1,11 @@
|
||||
server:
|
||||
session-timeout: 1800
|
||||
port: 48080
|
||||
spring:
|
||||
application:
|
||||
name: DiscoveryCallerService
|
||||
cloud:
|
||||
loadbalancer:
|
||||
configurations: default
|
||||
polaris:
|
||||
address: grpc://127.0.0.1:8091
|
||||
consul:
|
@ -1,38 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>polaris-gateway-example</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<version>${revision}</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>gateway-zuul-service</artifactId>
|
||||
<name>Spring Cloud Starter Tencent Polaris Gateway Zuul Example</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.tencent.cloud</groupId>
|
||||
<artifactId>spring-cloud-tencent-polaris-gateway</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -1,20 +0,0 @@
|
||||
server:
|
||||
session-timeout: 1800
|
||||
port: 48082
|
||||
spring:
|
||||
application:
|
||||
name: GatewayZuulService
|
||||
cloud:
|
||||
polaris:
|
||||
address: grpc://127.0.0.1:8091
|
||||
tencent:
|
||||
metadata:
|
||||
content:
|
||||
a: 1
|
||||
transitive:
|
||||
- a
|
||||
zuul:
|
||||
routes:
|
||||
GatewayCalleeService:
|
||||
serviceId: GatewayCalleeService
|
||||
path: /GatewayCalleeService/**
|
@ -1,11 +1,12 @@
|
||||
server:
|
||||
session-timeout: 1800
|
||||
port: 48081
|
||||
spring:
|
||||
application:
|
||||
name: RateLimitCalleeService
|
||||
cloud:
|
||||
loadbalancer:
|
||||
configurations: default
|
||||
polaris:
|
||||
address: grpc://127.0.0.1:8091
|
||||
discovery:
|
||||
namespace: Test
|
||||
namespace: default
|
25
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisLoadBalancerFeignClient.java → spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/ConditionalOnLoadBalancerPolaris.java
25
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-circuitbreaker/src/main/java/com/tencent/cloud/polaris/circuitbreaker/feign/PolarisLoadBalancerFeignClient.java → spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/ConditionalOnLoadBalancerPolaris.java
12
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisDiscoveryRibbonAutoConfiguration.java → spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/LoadBalancerPolarisAutoConfiguration.java
12
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/ribbon/PolarisDiscoveryRibbonAutoConfiguration.java → spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/loadbalancer/LoadBalancerPolarisAutoConfiguration.java
@ -0,0 +1,93 @@
|
||||
/*
|
||||
* 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.constant.LoadbalancerConstant;
|
||||
import com.tencent.cloud.polaris.PolarisProperties;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnDiscoveryEnabled
|
||||
public class PolarisLoadBalancerClientConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public ReactorLoadBalancer<ServiceInstance> polarisLoadBalancer(Environment environment,
|
||||
LoadBalancerClientFactory loadBalancerClientFactory,
|
||||
PolarisProperties polarisProperties,
|
||||
RouterAPI routerAPI) {
|
||||
String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
|
||||
return new PolarisLoadbalancer(
|
||||
name, loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class),
|
||||
polarisProperties, routerAPI);
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnReactiveDiscoveryEnabled
|
||||
@Order(LoadbalancerConstant.DISCOVERY_SERVICE_INSTANCE_SUPPLIER_ORDER)
|
||||
public static class ReactiveSupportConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(ReactiveDiscoveryClient.class)
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "default", matchIfMissing = true)
|
||||
public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(
|
||||
ConfigurableApplicationContext context) {
|
||||
return ServiceInstanceListSupplier.builder().withDiscoveryClient()
|
||||
.build(context);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnBlockingDiscoveryEnabled
|
||||
@Order(LoadbalancerConstant.DISCOVERY_SERVICE_INSTANCE_SUPPLIER_ORDER + 1)
|
||||
public static class BlockingSupportConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(DiscoveryClient.class)
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "default", matchIfMissing = true)
|
||||
public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(
|
||||
ConfigurableApplicationContext context) {
|
||||
return ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient()
|
||||
.build(context);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,114 @@
|
||||
/*
|
||||
* 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.metadata.context.MetadataContextHolder;
|
||||
import com.tencent.cloud.polaris.PolarisProperties;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.tencent.cloud.polaris.pojo.PolarisServiceInstance;
|
||||
import com.tencent.polaris.api.pojo.DefaultServiceInstances;
|
||||
import com.tencent.polaris.api.pojo.Instance;
|
||||
import com.tencent.polaris.api.pojo.ServiceInstances;
|
||||
import com.tencent.polaris.api.pojo.ServiceKey;
|
||||
import com.tencent.polaris.api.rpc.Criteria;
|
||||
import com.tencent.polaris.router.api.core.RouterAPI;
|
||||
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest;
|
||||
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.loadbalancer.DefaultResponse;
|
||||
import org.springframework.cloud.client.loadbalancer.EmptyResponse;
|
||||
import org.springframework.cloud.client.loadbalancer.Request;
|
||||
import org.springframework.cloud.client.loadbalancer.Response;
|
||||
import org.springframework.cloud.loadbalancer.core.NoopServiceInstanceListSupplier;
|
||||
import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
|
||||
import org.springframework.cloud.loadbalancer.core.RoundRobinLoadBalancer;
|
||||
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
|
||||
*/
|
||||
public class PolarisLoadbalancer extends RoundRobinLoadBalancer implements ReactorServiceInstanceLoadBalancer {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(PolarisLoadbalancer.class);
|
||||
|
||||
private final String serviceId;
|
||||
|
||||
private ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider;
|
||||
|
||||
private final PolarisProperties discoveryProperties;
|
||||
|
||||
private final RouterAPI routerAPI;
|
||||
|
||||
public PolarisLoadbalancer(String serviceId,
|
||||
ObjectProvider<ServiceInstanceListSupplier> supplierObjectProvider,
|
||||
PolarisProperties discoveryProperties, RouterAPI routerAPI) {
|
||||
super(supplierObjectProvider, serviceId);
|
||||
this.serviceId = serviceId;
|
||||
this.supplierObjectProvider = supplierObjectProvider;
|
||||
this.discoveryProperties = discoveryProperties;
|
||||
this.routerAPI = routerAPI;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Response<ServiceInstance>> choose(Request request) {
|
||||
if (!discoveryProperties.getLoadbalancerEnabled()) {
|
||||
return super.choose(request);
|
||||
}
|
||||
ServiceInstanceListSupplier supplier = supplierObjectProvider.getIfAvailable(NoopServiceInstanceListSupplier::new);
|
||||
return supplier.get().next().map(this::getInstanceResponse);
|
||||
}
|
||||
|
||||
private Response<ServiceInstance> getInstanceResponse(List<ServiceInstance> serviceInstances) {
|
||||
if (serviceInstances.isEmpty()) {
|
||||
log.warn("No servers available for service: " + this.serviceId);
|
||||
return new EmptyResponse();
|
||||
}
|
||||
|
||||
ProcessLoadBalanceRequest request = new ProcessLoadBalanceRequest();
|
||||
request.setDstInstances(convertToPolarisServiceInstances(serviceInstances));
|
||||
request.setLbPolicy(discoveryProperties.getPolicy());
|
||||
request.setCriteria(new Criteria());
|
||||
|
||||
try {
|
||||
ProcessLoadBalanceResponse response = routerAPI.processLoadBalance(request);
|
||||
return new DefaultResponse(new PolarisServiceInstance(response.getTargetInstance()));
|
||||
}
|
||||
catch (Exception e) {
|
||||
log.warn("PolarisLoadbalancer error", e);
|
||||
return new EmptyResponse();
|
||||
}
|
||||
}
|
||||
|
||||
private static ServiceInstances convertToPolarisServiceInstances(List<ServiceInstance> serviceInstances) {
|
||||
ServiceKey serviceKey = new ServiceKey(MetadataContextHolder.LOCAL_NAMESPACE, serviceInstances.get(0).getServiceId());
|
||||
List<Instance> polarisInstances = serviceInstances
|
||||
.stream()
|
||||
.map(serviceInstance -> ((PolarisServiceInstance) serviceInstance).getPolarisInstance())
|
||||
.collect(Collectors.toList());
|
||||
return new DefaultServiceInstances(serviceKey, polarisInstances);
|
||||
}
|
||||
|
||||
}
|
@ -1,42 +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.ribbon;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import com.netflix.loadbalancer.ServerList;
|
||||
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author Haotian Zhang, Andrew Shan, Jie Cheng
|
||||
*/
|
||||
@Configuration
|
||||
public class PolarisRibbonServerListConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public ServerList<Server> ribbonServerList(PolarisDiscoveryHandler polarisDiscoveryHandler,
|
||||
IClientConfig iClientConfig) {
|
||||
PolarisServerList serverList = new PolarisServerList(polarisDiscoveryHandler);
|
||||
serverList.initWithNiwsConfig(iClientConfig);
|
||||
return serverList;
|
||||
}
|
||||
}
|
@ -1,74 +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.ribbon;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.AbstractServerList;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import com.tencent.cloud.polaris.pojo.PolarisServer;
|
||||
import com.tencent.polaris.api.pojo.Instance;
|
||||
import com.tencent.polaris.api.pojo.ServiceInstances;
|
||||
import com.tencent.polaris.api.rpc.InstancesResponse;
|
||||
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Haotian Zhang, Andrew Shan, Jie Cheng
|
||||
*/
|
||||
public class PolarisServerList extends AbstractServerList<Server> {
|
||||
|
||||
private String serviceId;
|
||||
|
||||
private PolarisDiscoveryHandler polarisDiscoveryHandler;
|
||||
|
||||
public PolarisServerList(PolarisDiscoveryHandler polarisDiscoveryHandler) {
|
||||
this.polarisDiscoveryHandler = polarisDiscoveryHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Server> getInitialListOfServers() {
|
||||
return getServers();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Server> getUpdatedListOfServers() {
|
||||
return getServers();
|
||||
}
|
||||
|
||||
private List<Server> getServers() {
|
||||
InstancesResponse filteredInstances = polarisDiscoveryHandler.getInstances(serviceId);
|
||||
ServiceInstances serviceInstances = filteredInstances.toServiceInstances();
|
||||
List<Server> polarisServers = new ArrayList<>();
|
||||
for (Instance instance : serviceInstances.getInstances()) {
|
||||
polarisServers.add(new PolarisServer(serviceInstances, instance));
|
||||
}
|
||||
return polarisServers;
|
||||
}
|
||||
|
||||
public String getServiceId() {
|
||||
return serviceId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initWithNiwsConfig(IClientConfig iClientConfig) {
|
||||
this.serviceId = iClientConfig.getClientName();
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.tencent.cloud.polaris.discovery.PolarisDiscoveryAutoConfiguration,\
|
||||
com.tencent.cloud.polaris.ribbon.PolarisDiscoveryRibbonAutoConfiguration,\
|
||||
com.tencent.cloud.polaris.registry.PolarisServiceRegistryAutoConfiguration
|
||||
|
||||
|
@ -0,0 +1,112 @@
|
||||
/*
|
||||
* 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.router;
|
||||
|
||||
import com.tencent.cloud.constant.LoadbalancerConstant;
|
||||
import com.tencent.cloud.polaris.router.PolarisRouterServiceInstanceListSupplier;
|
||||
import com.tencent.polaris.api.exception.PolarisException;
|
||||
import com.tencent.polaris.client.api.SDKContext;
|
||||
import com.tencent.polaris.factory.api.RouterAPIFactory;
|
||||
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.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.client.ConditionalOnBlockingDiscoveryEnabled;
|
||||
import org.springframework.cloud.client.ConditionalOnReactiveDiscoveryEnabled;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
|
||||
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
|
||||
/**
|
||||
* Auto-configuration Ribbon for Polaris.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
@Configuration()
|
||||
@EnableConfigurationProperties
|
||||
@ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.enabled", matchIfMissing = true)
|
||||
public class PolarisRouterAutoConfiguration {
|
||||
|
||||
@Bean(name = "polarisRoute")
|
||||
@ConditionalOnMissingBean
|
||||
public RouterAPI polarisRouter(SDKContext polarisContext) throws PolarisException {
|
||||
return RouterAPIFactory.createRouterAPIByContext(polarisContext);
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnReactiveDiscoveryEnabled
|
||||
@Order(LoadbalancerConstant.ROUTER_SERVICE_INSTANCE_SUPPLIER_ORDER - 1000)
|
||||
public static class ReactiveSupportConfiguration {
|
||||
|
||||
// @Bean
|
||||
// @ConditionalOnBean(ReactiveDiscoveryClient.class)
|
||||
// @ConditionalOnMissingBean
|
||||
// @ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "default", matchIfMissing = true)
|
||||
// public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(RouterAPI routerAPI,
|
||||
// ConfigurableApplicationContext context) {
|
||||
// return new PolarisRouterServiceInstanceListSupplier(ServiceInstanceListSupplier.builder().withDiscoveryClient()
|
||||
// .build(context), routerAPI);
|
||||
// }
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(ReactiveDiscoveryClient.class)
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "zone-preference", matchIfMissing = true)
|
||||
public ServiceInstanceListSupplier polarisRouterDiscoveryClientServiceInstanceListSupplier(RouterAPI routerAPI,
|
||||
ConfigurableApplicationContext context) {
|
||||
return new PolarisRouterServiceInstanceListSupplier(ServiceInstanceListSupplier.builder().withDiscoveryClient()
|
||||
.withZonePreference()
|
||||
.build(context), routerAPI);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnBlockingDiscoveryEnabled
|
||||
@Order(LoadbalancerConstant.ROUTER_SERVICE_INSTANCE_SUPPLIER_ORDER + 1 - 1000)
|
||||
public static class BlockingSupportConfiguration {
|
||||
|
||||
// @Bean
|
||||
// @ConditionalOnBean(DiscoveryClient.class)
|
||||
// @ConditionalOnMissingBean
|
||||
// @ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "default", matchIfMissing = true)
|
||||
// public ServiceInstanceListSupplier discoveryClientServiceInstanceListSupplier(
|
||||
// RouterAPI routerAPI,
|
||||
// ConfigurableApplicationContext context) {
|
||||
// return new PolarisRouterServiceInstanceListSupplier(ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient()
|
||||
// .build(context), routerAPI);
|
||||
// }
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(DiscoveryClient.class)
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnProperty(value = "spring.cloud.loadbalancer.configurations", havingValue = "zone-preference", matchIfMissing = true)
|
||||
public ServiceInstanceListSupplier polarisRouterDiscoveryClientServiceInstanceListSupplier(RouterAPI routerAPI,
|
||||
ConfigurableApplicationContext context) {
|
||||
return new PolarisRouterServiceInstanceListSupplier(ServiceInstanceListSupplier.builder().withBlockingDiscoveryClient()
|
||||
.withZonePreference().build(context), routerAPI);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
95
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRoutingLoadBalancer.java → spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplier.java
95
spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRoutingLoadBalancer.java → spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-router/src/main/java/com/tencent/cloud/polaris/router/PolarisRouterServiceInstanceListSupplier.java
@ -1,56 +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.router.config;
|
||||
|
||||
import com.tencent.polaris.api.exception.PolarisException;
|
||||
import com.tencent.polaris.client.api.SDKContext;
|
||||
import com.tencent.polaris.factory.api.RouterAPIFactory;
|
||||
import com.tencent.polaris.router.api.core.RouterAPI;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration;
|
||||
import org.springframework.cloud.netflix.ribbon.RibbonClients;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Auto-configuration Ribbon for Polaris.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableConfigurationProperties
|
||||
@ConditionalOnProperty(value = "spring.cloud.polaris.loadbalancer.enabled", matchIfMissing = true)
|
||||
@AutoConfigureAfter(RibbonAutoConfiguration.class)
|
||||
@RibbonClients(defaultConfiguration = PolarisRibbonClientConfiguration.class)
|
||||
public class PolarisRibbonAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public PolarisRibbonProperties polarisRibbonProperties() {
|
||||
return new PolarisRibbonProperties();
|
||||
}
|
||||
|
||||
@Bean(name = "polarisRoute")
|
||||
@ConditionalOnMissingBean
|
||||
public RouterAPI polarisRouter(SDKContext polarisContext) throws PolarisException {
|
||||
return RouterAPIFactory.createRouterAPIByContext(polarisContext);
|
||||
}
|
||||
}
|
@ -1,56 +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.router.config;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.ILoadBalancer;
|
||||
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.polaris.router.api.core.RouterAPI;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
@Configuration
|
||||
public class PolarisRibbonClientConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public IRule polarisRibbonRule(PolarisRibbonProperties polarisRibbonProperties) {
|
||||
switch (PolarisLoadBalanceRule.fromStrategy(polarisRibbonProperties.getPolicy())) {
|
||||
case WEIGHTED_RANDOM_RULE:
|
||||
default:
|
||||
return new PolarisWeightedRandomRule();
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public ILoadBalancer polarisRoutingLoadBalancer(IClientConfig iClientConfig, IRule iRule, IPing iPing,
|
||||
ServerList<Server> serverList, RouterAPI polarisRouter) {
|
||||
return new PolarisRoutingLoadBalancer(iClientConfig, iRule, iPing, serverList, polarisRouter);
|
||||
}
|
||||
}
|
@ -1,64 +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.router.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
@ConfigurationProperties("spring.cloud.polaris.ribbon")
|
||||
public class PolarisRibbonProperties {
|
||||
|
||||
/**
|
||||
* 是否开启负载均衡
|
||||
*/
|
||||
@Value("${spring.cloud.polaris.loadbalancer.enabled:#{true}}")
|
||||
private Boolean loadbalancerEnabled;
|
||||
|
||||
/**
|
||||
* loadbalnce strategy
|
||||
*/
|
||||
@Value("${spring.cloud.polaris.loadbalancer.strategy:#{'weightedRandom'}}")
|
||||
private String policy;
|
||||
|
||||
public String getPolicy() {
|
||||
return policy;
|
||||
}
|
||||
|
||||
public void setPolicy(String policy) {
|
||||
this.policy = policy;
|
||||
}
|
||||
|
||||
public Boolean getLoadbalancerEnabled() {
|
||||
return loadbalancerEnabled;
|
||||
}
|
||||
|
||||
public void setLoadbalancerEnabled(Boolean loadbalancerEnabled) {
|
||||
this.loadbalancerEnabled = loadbalancerEnabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PolarisRibbonProperties{" +
|
||||
"loadbalancerEnabled=" + loadbalancerEnabled +
|
||||
", policy='" + policy + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -1,53 +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.router.rule;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public enum PolarisLoadBalanceRule {
|
||||
|
||||
/**
|
||||
* 加权随机
|
||||
*/
|
||||
WEIGHTED_RANDOM_RULE("weighted_random");
|
||||
|
||||
/**
|
||||
* 策略
|
||||
*/
|
||||
String policy;
|
||||
|
||||
PolarisLoadBalanceRule(String strategy) {
|
||||
this.policy = strategy;
|
||||
}
|
||||
|
||||
public static PolarisLoadBalanceRule fromStrategy(String strategy) {
|
||||
return Arrays.stream(values()).filter(t -> t.getPolicy().equals(strategy)).findAny()
|
||||
.orElse(WEIGHTED_RANDOM_RULE);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link #policy}的getter方法。
|
||||
*/
|
||||
public String getPolicy() {
|
||||
return policy;
|
||||
}
|
||||
|
||||
}
|
@ -1,67 +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.router.rule;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import com.tencent.cloud.polaris.pojo.PolarisServer;
|
||||
import com.tencent.polaris.api.config.consumer.LoadBalanceConfig;
|
||||
import com.tencent.polaris.api.pojo.Instance;
|
||||
import com.tencent.polaris.router.api.core.RouterAPI;
|
||||
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest;
|
||||
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisWeightedRandomRule extends AbstractLoadBalancerRule {
|
||||
|
||||
private static final String POLICY = LoadBalanceConfig.LOAD_BALANCE_WEIGHTED_RANDOM;
|
||||
|
||||
@Autowired
|
||||
private RouterAPI polarisRouter;
|
||||
|
||||
@Override
|
||||
public void initWithNiwsConfig(IClientConfig clientConfig) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Server choose(Object key) {
|
||||
List<Server> allServers = getLoadBalancer().getReachableServers();
|
||||
if (CollectionUtils.isEmpty(allServers)) {
|
||||
return null;
|
||||
}
|
||||
Server server = allServers.get(0);
|
||||
if (!(server instanceof PolarisServer)) {
|
||||
throw new IllegalStateException("PolarisDiscoveryRule only support PolarisServer instances");
|
||||
}
|
||||
PolarisServer polarisServer = (PolarisServer) server;
|
||||
ProcessLoadBalanceRequest request = new ProcessLoadBalanceRequest();
|
||||
request.setDstInstances(polarisServer.getServiceInstances());
|
||||
request.setLbPolicy(POLICY);
|
||||
ProcessLoadBalanceResponse processLoadBalanceResponse = polarisRouter.processLoadBalance(request);
|
||||
Instance targetInstance = processLoadBalanceResponse.getTargetInstance();
|
||||
return new PolarisServer(polarisServer.getServiceInstances(), targetInstance);
|
||||
}
|
||||
}
|
@ -1,2 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.tencent.cloud.polaris.router.config.PolarisRibbonAutoConfiguration
|
||||
com.tencent.cloud.polaris.router.PolarisRouterAutoConfiguration
|
||||
|
@ -1,109 +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.pojo;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import com.tencent.polaris.api.pojo.Instance;
|
||||
import com.tencent.polaris.api.pojo.ServiceInstances;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Polaris implementation of {@link Server}
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class PolarisServer extends Server {
|
||||
|
||||
private final ServiceInstances serviceInstances;
|
||||
|
||||
private final Instance instance;
|
||||
|
||||
private final MetaInfo metaInfo;
|
||||
|
||||
public PolarisServer(ServiceInstances serviceInstances, Instance instance) {
|
||||
super(instance.getHost(), instance.getPort());
|
||||
if (StringUtils.equalsIgnoreCase(instance.getProtocol(), "https")) {
|
||||
setSchemea("https");
|
||||
} else {
|
||||
setSchemea("http");
|
||||
}
|
||||
this.serviceInstances = serviceInstances;
|
||||
this.instance = instance;
|
||||
this.metaInfo = new MetaInfo() {
|
||||
@Override
|
||||
public String getAppName() {
|
||||
return instance.getService();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServerGroup() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getServiceIdForDiscovery() {
|
||||
return instance.getService();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInstanceId() {
|
||||
return instance.getId();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public Instance getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MetaInfo getMetaInfo() {
|
||||
return metaInfo;
|
||||
}
|
||||
|
||||
public Map<String, String> getMetadata() {
|
||||
return instance.getMetadata();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
if (!super.equals(o)) {
|
||||
return false;
|
||||
}
|
||||
PolarisServer that = (PolarisServer) o;
|
||||
return Objects.equal(instance, that.instance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hashCode(super.hashCode(), instance);
|
||||
}
|
||||
|
||||
public ServiceInstances getServiceInstances() {
|
||||
return serviceInstances;
|
||||
}
|
||||
}
|
@ -1,79 +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.gateway.core.zuul.filter;
|
||||
|
||||
import com.netflix.zuul.ZuulFilter;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import com.tencent.cloud.metadata.constant.MetadataConstant;
|
||||
import com.tencent.cloud.metadata.context.MetadataContext;
|
||||
import com.tencent.cloud.metadata.context.MetadataContextHolder;
|
||||
import com.tencent.cloud.metadata.util.JacksonUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.RIBBON_ROUTING_FILTER_ORDER;
|
||||
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.ROUTE_TYPE;
|
||||
|
||||
/**
|
||||
* Zuul filter used for writing metadata in HTTP request header.
|
||||
*
|
||||
* @author skyehtzhang
|
||||
*/
|
||||
public class Metadata2HeaderZuulFilter extends ZuulFilter {
|
||||
|
||||
@Override
|
||||
public String filterType() {
|
||||
return ROUTE_TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int filterOrder() {
|
||||
return RIBBON_ROUTING_FILTER_ORDER - 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldFilter() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object run() {
|
||||
// get request context
|
||||
RequestContext requestContext = RequestContext.getCurrentContext();
|
||||
|
||||
// get metadata of current thread
|
||||
MetadataContext metadataContext = MetadataContextHolder.get();
|
||||
|
||||
// add new metadata and cover old
|
||||
Map<String, String> customMetadata = metadataContext.getAllTransitiveCustomMetadata();
|
||||
if (!CollectionUtils.isEmpty(customMetadata)) {
|
||||
String metadataStr = JacksonUtils.serialize2Json(customMetadata);
|
||||
try {
|
||||
requestContext.addZuulRequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA,
|
||||
URLEncoder.encode(metadataStr, "UTF-8"));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
requestContext.addZuulRequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA, metadataStr);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -1,64 +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.gateway.core.zuul.filter;
|
||||
|
||||
import com.netflix.zuul.ZuulFilter;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import com.tencent.cloud.metadata.constant.MetadataConstant;
|
||||
import com.tencent.cloud.metadata.context.MetadataContextHolder;
|
||||
|
||||
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_DECORATION_FILTER_ORDER;
|
||||
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;
|
||||
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.REQUEST_URI_KEY;
|
||||
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SERVICE_ID_KEY;
|
||||
|
||||
/**
|
||||
* Zuul filter used for setting peer info in context.
|
||||
*
|
||||
* @author Haotian Zhang
|
||||
*/
|
||||
public class MetadataFirstZuulFilter extends ZuulFilter {
|
||||
@Override
|
||||
public String filterType() {
|
||||
return PRE_TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int filterOrder() {
|
||||
return PRE_DECORATION_FILTER_ORDER + 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldFilter() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object run() {
|
||||
// get request context
|
||||
RequestContext requestContext = RequestContext.getCurrentContext();
|
||||
|
||||
// TODO 对端命名空间暂时与本地命名空间相同
|
||||
MetadataContextHolder.get().putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_NAMESPACE,
|
||||
MetadataContextHolder.get().getSystemMetadata(MetadataConstant.SystemMetadataKey.LOCAL_NAMESPACE));
|
||||
MetadataContextHolder.get().putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_SERVICE,
|
||||
(String) requestContext.get(SERVICE_ID_KEY));
|
||||
MetadataContextHolder.get().putSystemMetadata(MetadataConstant.SystemMetadataKey.PEER_PATH,
|
||||
(String) requestContext.get(REQUEST_URI_KEY));
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue