commit
3db52ef22c
@ -1,3 +1,5 @@
|
||||
# Change Log
|
||||
---
|
||||
|
||||
- [fix:Router support request label.](https://github.com/Tencent/spring-cloud-tencent/pull/165)
|
||||
|
||||
|
@ -0,0 +1,184 @@
|
||||
/*
|
||||
* 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 java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
|
||||
import com.netflix.loadbalancer.AvailabilityFilteringRule;
|
||||
import com.netflix.loadbalancer.BestAvailableRule;
|
||||
import com.netflix.loadbalancer.ILoadBalancer;
|
||||
import com.netflix.loadbalancer.RandomRule;
|
||||
import com.netflix.loadbalancer.RetryRule;
|
||||
import com.netflix.loadbalancer.RoundRobinRule;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import com.netflix.loadbalancer.WeightedResponseTimeRule;
|
||||
import com.netflix.loadbalancer.ZoneAvoidanceRule;
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.pojo.PolarisServer;
|
||||
import com.tencent.cloud.polaris.loadbalancer.LoadBalancerUtils;
|
||||
import com.tencent.cloud.polaris.loadbalancer.PolarisWeightedRule;
|
||||
import com.tencent.cloud.polaris.loadbalancer.config.PolarisLoadBalancerProperties;
|
||||
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.router.api.core.RouterAPI;
|
||||
import com.tencent.polaris.router.api.rpc.ProcessRoutersRequest;
|
||||
import com.tencent.polaris.router.api.rpc.ProcessRoutersResponse;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
*
|
||||
* Service routing entrance.
|
||||
*
|
||||
* Rule routing needs to rely on request parameters for server filtering,
|
||||
* and {@link com.netflix.loadbalancer.ServerListFilter#getFilteredListOfServers(List)}
|
||||
* The interface cannot obtain the context object of the request granularity,
|
||||
* so the routing capability cannot be achieved through ServerListFilter.
|
||||
*
|
||||
* And {@link com.netflix.loadbalancer.IRule#choose(Object)} provides the ability to pass in context parameters,
|
||||
* so routing capabilities are implemented through IRule.
|
||||
*
|
||||
* @author Haotian Zhang, lepdou
|
||||
*/
|
||||
public class PolarisLoadBalancerCompositeRule extends AbstractLoadBalancerRule {
|
||||
|
||||
private final static String STRATEGY_RANDOM = "random";
|
||||
private final static String STRATEGY_ROUND_ROBIN = "roundRobin";
|
||||
private final static String STRATEGY_WEIGHT = "polarisWeighted";
|
||||
private final static String STRATEGY_RETRY = "retry";
|
||||
private final static String STRATEGY_RESPONSE_TIME_WEIGHTED = "responseTimeWeighted";
|
||||
private final static String STRATEGY_BEST_AVAILABLE = "bestAvailable";
|
||||
private final static String STRATEGY_ZONE_AVOIDANCE = "zoneAvoidance";
|
||||
private final static String STRATEGY_AVAILABILITY_FILTERING = "availabilityFilteringRule";
|
||||
|
||||
private final PolarisLoadBalancerProperties loadBalancerProperties;
|
||||
private final RouterAPI routerAPI;
|
||||
|
||||
private final AbstractLoadBalancerRule delegateRule;
|
||||
|
||||
public PolarisLoadBalancerCompositeRule(RouterAPI routerAPI, PolarisLoadBalancerProperties polarisLoadBalancerProperties,
|
||||
IClientConfig iClientConfig) {
|
||||
this.routerAPI = routerAPI;
|
||||
this.loadBalancerProperties = polarisLoadBalancerProperties;
|
||||
|
||||
delegateRule = getRule();
|
||||
delegateRule.initWithNiwsConfig(iClientConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initWithNiwsConfig(IClientConfig clientConfig) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Server choose(Object key) {
|
||||
// 1. get all servers
|
||||
List<Server> allServers = getLoadBalancer().getReachableServers();
|
||||
if (CollectionUtils.isEmpty(allServers)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 2. filter by router
|
||||
List<Server> serversAfterRouter = doRouter(allServers, key);
|
||||
|
||||
// 3. filter by load balance.
|
||||
// A LoadBalancer needs to be regenerated for each request,
|
||||
// because the list of servers may be different after filtered by router
|
||||
ILoadBalancer loadBalancer = new SimpleLoadBalancer();
|
||||
loadBalancer.addServers(serversAfterRouter);
|
||||
delegateRule.setLoadBalancer(loadBalancer);
|
||||
|
||||
return delegateRule.choose(key);
|
||||
}
|
||||
|
||||
private List<Server> doRouter(List<Server> allServers, Object key) {
|
||||
ServiceInstances serviceInstances = LoadBalancerUtils.transferServersToServiceInstances(allServers);
|
||||
|
||||
// filter instance by routers
|
||||
ProcessRoutersRequest processRoutersRequest = buildProcessRoutersRequest(serviceInstances, key);
|
||||
|
||||
ProcessRoutersResponse processRoutersResponse = routerAPI.processRouters(processRoutersRequest);
|
||||
|
||||
List<Server> filteredInstances = new ArrayList<>();
|
||||
ServiceInstances filteredServiceInstances = processRoutersResponse.getServiceInstances();
|
||||
for (Instance instance : filteredServiceInstances.getInstances()) {
|
||||
filteredInstances.add(new PolarisServer(serviceInstances, instance));
|
||||
}
|
||||
return filteredInstances;
|
||||
}
|
||||
|
||||
private ProcessRoutersRequest buildProcessRoutersRequest(ServiceInstances serviceInstances, Object key) {
|
||||
ProcessRoutersRequest processRoutersRequest = new ProcessRoutersRequest();
|
||||
processRoutersRequest.setDstInstances(serviceInstances);
|
||||
|
||||
Map<String, String> routerMetadata;
|
||||
if (key instanceof PolarisRouterContext) {
|
||||
routerMetadata = ((PolarisRouterContext) key).getLabels();
|
||||
}
|
||||
else {
|
||||
routerMetadata = Collections.emptyMap();
|
||||
}
|
||||
|
||||
String srcNamespace = MetadataContext.LOCAL_NAMESPACE;
|
||||
String srcService = MetadataContext.LOCAL_SERVICE;
|
||||
|
||||
if (StringUtils.isNotBlank(srcNamespace) && StringUtils.isNotBlank(srcService)) {
|
||||
ServiceInfo serviceInfo = new ServiceInfo();
|
||||
serviceInfo.setNamespace(srcNamespace);
|
||||
serviceInfo.setService(srcService);
|
||||
serviceInfo.setMetadata(routerMetadata);
|
||||
processRoutersRequest.setSourceService(serviceInfo);
|
||||
}
|
||||
|
||||
return processRoutersRequest;
|
||||
}
|
||||
|
||||
public AbstractLoadBalancerRule getRule() {
|
||||
String loadBalanceStrategy = loadBalancerProperties.getStrategy();
|
||||
if (org.springframework.util.StringUtils.isEmpty(loadBalanceStrategy)) {
|
||||
return new RoundRobinRule();
|
||||
}
|
||||
|
||||
switch (loadBalanceStrategy) {
|
||||
case STRATEGY_RANDOM:
|
||||
return new RandomRule();
|
||||
case STRATEGY_WEIGHT:
|
||||
return new PolarisWeightedRule(routerAPI);
|
||||
case STRATEGY_RETRY:
|
||||
return new RetryRule();
|
||||
case STRATEGY_RESPONSE_TIME_WEIGHTED:
|
||||
return new WeightedResponseTimeRule();
|
||||
case STRATEGY_BEST_AVAILABLE:
|
||||
return new BestAvailableRule();
|
||||
case STRATEGY_ROUND_ROBIN:
|
||||
return new RoundRobinRule();
|
||||
case STRATEGY_AVAILABILITY_FILTERING:
|
||||
return new AvailabilityFilteringRule();
|
||||
case STRATEGY_ZONE_AVOIDANCE:
|
||||
default:
|
||||
return new ZoneAvoidanceRule();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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 java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* the context for router.
|
||||
*
|
||||
*@author lepdou 2022-05-17
|
||||
*/
|
||||
public class PolarisRouterContext {
|
||||
|
||||
private Map<String, String> labels;
|
||||
|
||||
public Map<String, String> getLabels() {
|
||||
if (CollectionUtils.isEmpty(labels)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
return Collections.unmodifiableMap(labels);
|
||||
}
|
||||
|
||||
public void setLabels(Map<String, String> labels) {
|
||||
this.labels = labels;
|
||||
}
|
||||
|
||||
public void putLabel(String key, String value) {
|
||||
if (labels == null) {
|
||||
labels = new HashMap<>();
|
||||
}
|
||||
labels.put(key, value);
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* 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 java.util.List;
|
||||
|
||||
import com.netflix.loadbalancer.ILoadBalancer;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
|
||||
/**
|
||||
* Simple load balancer only for getting and setting servers.
|
||||
*
|
||||
*@author lepdou 2022-05-17
|
||||
*/
|
||||
public class SimpleLoadBalancer implements ILoadBalancer {
|
||||
private List<Server> servers;
|
||||
|
||||
@Override
|
||||
public void addServers(List<Server> newServers) {
|
||||
this.servers = newServers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Server chooseServer(Object key) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markServerDown(Server server) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Server> getServerList(boolean availableOnly) {
|
||||
return servers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Server> getReachableServers() {
|
||||
return servers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Server> getAllServers() {
|
||||
return servers;
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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.tencent.cloud.polaris.router.feign.PolarisFeignLoadBalancer;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.cloud.netflix.ribbon.ServerIntrospector;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* configuration for feign component.
|
||||
*
|
||||
*@author lepdou 2022-05-16
|
||||
*/
|
||||
@Configuration
|
||||
public class FeignConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public PolarisFeignLoadBalancer polarisFeignLoadBalancer(ILoadBalancer lb, IClientConfig clientConfig,
|
||||
ServerIntrospector serverIntrospector) {
|
||||
return new PolarisFeignLoadBalancer(lb, clientConfig, serverIntrospector);
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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.IRule;
|
||||
import com.tencent.cloud.polaris.loadbalancer.config.PolarisLoadBalancerProperties;
|
||||
import com.tencent.cloud.polaris.router.PolarisLoadBalancerCompositeRule;
|
||||
import com.tencent.polaris.router.api.core.RouterAPI;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Auto configuration for ribbon components.
|
||||
* @author lepdou 2022-05-17
|
||||
*/
|
||||
@Configuration
|
||||
public class RibbonConfiguration {
|
||||
|
||||
@Bean
|
||||
public IRule polarisLoadBalancerCompositeRule(RouterAPI routerAPI,
|
||||
PolarisLoadBalancerProperties polarisLoadBalancerProperties,
|
||||
IClientConfig iClientConfig) {
|
||||
return new PolarisLoadBalancerCompositeRule(routerAPI, polarisLoadBalancerProperties, iClientConfig);
|
||||
}
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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.cloud.common.metadata.config.MetadataLocalProperties;
|
||||
import com.tencent.cloud.polaris.router.feign.PolarisCachingSpringLoadBalanceFactory;
|
||||
import com.tencent.cloud.polaris.router.feign.RouterLabelInterceptor;
|
||||
import com.tencent.cloud.polaris.router.resttemplate.PolarisLoadBalancerBeanPostProcessor;
|
||||
import com.tencent.cloud.polaris.router.spi.RouterLabelResolver;
|
||||
|
||||
import org.springframework.cloud.netflix.ribbon.RibbonClients;
|
||||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE;
|
||||
|
||||
/**
|
||||
* router module auto configuration.
|
||||
*
|
||||
*@author lepdou 2022-05-11
|
||||
*/
|
||||
@Configuration
|
||||
@RibbonClients(defaultConfiguration = {FeignConfiguration.class, RibbonConfiguration.class})
|
||||
public class RouterAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public RouterLabelInterceptor routerLabelInterceptor(@Nullable RouterLabelResolver resolver,
|
||||
MetadataLocalProperties metadataLocalProperties) {
|
||||
return new RouterLabelInterceptor(resolver, metadataLocalProperties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PolarisCachingSpringLoadBalanceFactory polarisCachingSpringLoadBalanceFactory(SpringClientFactory factory) {
|
||||
return new PolarisCachingSpringLoadBalanceFactory(factory);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Order(HIGHEST_PRECEDENCE)
|
||||
public PolarisLoadBalancerBeanPostProcessor polarisLoadBalancerBeanPostProcessor() {
|
||||
return new PolarisLoadBalancerBeanPostProcessor();
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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.feign;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.ILoadBalancer;
|
||||
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancedRetryFactory;
|
||||
import org.springframework.cloud.netflix.ribbon.ServerIntrospector;
|
||||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
||||
import org.springframework.cloud.openfeign.ribbon.CachingSpringLoadBalancerFactory;
|
||||
import org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer;
|
||||
import org.springframework.util.ConcurrentReferenceHashMap;
|
||||
|
||||
/**
|
||||
* Extends CachingSpringLoadBalancerFactory to be able to create PolarisFeignLoadBalance.
|
||||
*
|
||||
*@author lepdou 2022-05-16
|
||||
*/
|
||||
public class PolarisCachingSpringLoadBalanceFactory extends CachingSpringLoadBalancerFactory {
|
||||
|
||||
private final Map<String, FeignLoadBalancer> cache = new ConcurrentReferenceHashMap<>();
|
||||
|
||||
public PolarisCachingSpringLoadBalanceFactory(SpringClientFactory factory) {
|
||||
super(factory);
|
||||
}
|
||||
|
||||
public PolarisCachingSpringLoadBalanceFactory(SpringClientFactory factory,
|
||||
LoadBalancedRetryFactory loadBalancedRetryPolicyFactory) {
|
||||
super(factory, loadBalancedRetryPolicyFactory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FeignLoadBalancer create(String clientName) {
|
||||
FeignLoadBalancer client = this.cache.get(clientName);
|
||||
if (client != null) {
|
||||
return client;
|
||||
}
|
||||
|
||||
IClientConfig config = this.factory.getClientConfig(clientName);
|
||||
ILoadBalancer lb = this.factory.getLoadBalancer(clientName);
|
||||
ServerIntrospector serverIntrospector = this.factory.getInstance(clientName, ServerIntrospector.class);
|
||||
|
||||
FeignLoadBalancer loadBalancer = new PolarisFeignLoadBalancer(lb, config, serverIntrospector);
|
||||
|
||||
//There is a concurrency problem here.
|
||||
//When the concurrency is high, it may cause a service to create multiple FeignLoadBalancers.
|
||||
//But there is no concurrency control in CachingSpringLoadBalancerFactory,
|
||||
//so no locks will be added here for the time being
|
||||
cache.putIfAbsent(clientName, loadBalancer);
|
||||
|
||||
return loadBalancer;
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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.feign;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.ILoadBalancer;
|
||||
import com.netflix.loadbalancer.reactive.LoadBalancerCommand;
|
||||
import com.tencent.cloud.common.util.JacksonUtils;
|
||||
import com.tencent.cloud.polaris.router.PolarisRouterContext;
|
||||
import com.tencent.cloud.polaris.router.RouterConstants;
|
||||
|
||||
import org.springframework.cloud.netflix.ribbon.ServerIntrospector;
|
||||
import org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* In order to pass router context for {@link com.tencent.cloud.polaris.router.PolarisLoadBalancerCompositeRule}.
|
||||
*
|
||||
*@author lepdou 2022-05-16
|
||||
*/
|
||||
public class PolarisFeignLoadBalancer extends FeignLoadBalancer {
|
||||
|
||||
public PolarisFeignLoadBalancer(ILoadBalancer lb, IClientConfig clientConfig, ServerIntrospector serverIntrospector) {
|
||||
super(lb, clientConfig, serverIntrospector);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void customizeLoadBalancerCommandBuilder(RibbonRequest request, IClientConfig config,
|
||||
LoadBalancerCommand.Builder<RibbonResponse> builder) {
|
||||
Map<String, Collection<String>> headers = request.getRequest().headers();
|
||||
Collection<String> labelHeaderValues = headers.get(RouterConstants.ROUTER_LABEL_HEADER);
|
||||
|
||||
if (CollectionUtils.isEmpty(labelHeaderValues)) {
|
||||
builder.withServerLocator(null);
|
||||
return;
|
||||
}
|
||||
|
||||
PolarisRouterContext routerContext = new PolarisRouterContext();
|
||||
|
||||
labelHeaderValues.forEach(labelHeaderValue -> {
|
||||
Map<String, String> labels = JacksonUtils.deserialize2Map(labelHeaderValue);
|
||||
if (!CollectionUtils.isEmpty(labels)) {
|
||||
routerContext.setLabels(labels);
|
||||
}
|
||||
});
|
||||
|
||||
builder.withServerLocator(routerContext);
|
||||
}
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
/*
|
||||
* 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.feign;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
|
||||
import com.tencent.cloud.common.util.JacksonUtils;
|
||||
import com.tencent.cloud.polaris.router.RouterConstants;
|
||||
import com.tencent.cloud.polaris.router.spi.RouterLabelResolver;
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* Resolver labels from request.
|
||||
*
|
||||
*@author lepdou 2022-05-12
|
||||
*/
|
||||
public class RouterLabelInterceptor implements RequestInterceptor, Ordered {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(RouterLabelInterceptor.class);
|
||||
|
||||
private final RouterLabelResolver resolver;
|
||||
private final MetadataLocalProperties metadataLocalProperties;
|
||||
|
||||
public RouterLabelInterceptor(RouterLabelResolver resolver,
|
||||
MetadataLocalProperties metadataLocalProperties) {
|
||||
this.resolver = resolver;
|
||||
this.metadataLocalProperties = metadataLocalProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(RequestTemplate requestTemplate) {
|
||||
Map<String, String> labels = new HashMap<>();
|
||||
|
||||
// labels from downstream
|
||||
Map<String, String> transitiveLabels = MetadataContextHolder.get()
|
||||
.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
||||
labels.putAll(transitiveLabels);
|
||||
|
||||
// labels from request
|
||||
if (resolver != null) {
|
||||
try {
|
||||
Map<String, String> customResolvedLabels = resolver.resolve(requestTemplate);
|
||||
if (!CollectionUtils.isEmpty(customResolvedLabels)) {
|
||||
labels.putAll(customResolvedLabels);
|
||||
}
|
||||
}
|
||||
catch (Throwable t) {
|
||||
LOGGER.error("[SCT][Router] revoke RouterLabelResolver occur some exception. ", t);
|
||||
}
|
||||
}
|
||||
|
||||
//local service labels
|
||||
labels.putAll(metadataLocalProperties.getContent());
|
||||
|
||||
// pass label by header
|
||||
requestTemplate.header(RouterConstants.ROUTER_LABEL_HEADER, JacksonUtils.serialize2Json(labels));
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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.resttemplate;
|
||||
|
||||
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
|
||||
import com.tencent.cloud.polaris.router.spi.RouterLabelResolver;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerRequestFactory;
|
||||
|
||||
/**
|
||||
* Replace LoadBalancerInterceptor with PolarisLoadBalancerInterceptor.
|
||||
* PolarisLoadBalancerInterceptor can pass routing context information.
|
||||
*
|
||||
*@author lepdou 2022-05-18
|
||||
*/
|
||||
public class PolarisLoadBalancerBeanPostProcessor implements BeanPostProcessor, BeanFactoryAware {
|
||||
|
||||
private BeanFactory factory;
|
||||
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
this.factory = beanFactory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
if (bean instanceof LoadBalancerInterceptor) {
|
||||
LoadBalancerRequestFactory requestFactory = this.factory.getBean(LoadBalancerRequestFactory.class);
|
||||
LoadBalancerClient loadBalancerClient = this.factory.getBean(LoadBalancerClient.class);
|
||||
RouterLabelResolver routerLabelResolver = this.factory.getBean(RouterLabelResolver.class);
|
||||
MetadataLocalProperties metadataLocalProperties = this.factory.getBean(MetadataLocalProperties.class);
|
||||
|
||||
return new PolarisLoadBalancerInterceptor(loadBalancerClient, requestFactory,
|
||||
routerLabelResolver, metadataLocalProperties);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
}
|
@ -0,0 +1,120 @@
|
||||
/*
|
||||
* 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.resttemplate;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.metadata.MetadataContextHolder;
|
||||
import com.tencent.cloud.common.metadata.config.MetadataLocalProperties;
|
||||
import com.tencent.cloud.polaris.router.PolarisRouterContext;
|
||||
import com.tencent.cloud.polaris.router.spi.RouterLabelResolver;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerRequestFactory;
|
||||
import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient;
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.http.client.ClientHttpRequestExecution;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* PolarisLoadBalancerInterceptor extends LoadBalancerInterceptor capabilities.
|
||||
* Parses the label from the request and puts it into the RouterContext for routing.
|
||||
*
|
||||
*@author lepdou 2022-05-18
|
||||
*/
|
||||
public class PolarisLoadBalancerInterceptor extends LoadBalancerInterceptor {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(PolarisLoadBalancerInterceptor.class);
|
||||
|
||||
private final LoadBalancerClient loadBalancer;
|
||||
private final LoadBalancerRequestFactory requestFactory;
|
||||
private final RouterLabelResolver resolver;
|
||||
private final MetadataLocalProperties metadataLocalProperties;
|
||||
|
||||
private final boolean isRibbonLoadBalanceClient;
|
||||
|
||||
public PolarisLoadBalancerInterceptor(LoadBalancerClient loadBalancer,
|
||||
LoadBalancerRequestFactory requestFactory,
|
||||
RouterLabelResolver resolver,
|
||||
MetadataLocalProperties metadataLocalProperties) {
|
||||
super(loadBalancer, requestFactory);
|
||||
this.loadBalancer = loadBalancer;
|
||||
this.requestFactory = requestFactory;
|
||||
this.resolver = resolver;
|
||||
this.metadataLocalProperties = metadataLocalProperties;
|
||||
|
||||
this.isRibbonLoadBalanceClient = loadBalancer instanceof RibbonLoadBalancerClient;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
|
||||
final URI originalUri = request.getURI();
|
||||
String serviceName = originalUri.getHost();
|
||||
Assert.state(serviceName != null,
|
||||
"Request URI does not contain a valid hostname: " + originalUri);
|
||||
|
||||
if (isRibbonLoadBalanceClient) {
|
||||
PolarisRouterContext routerContext = genRouterContext(request, body);
|
||||
|
||||
return ((RibbonLoadBalancerClient) loadBalancer).execute(serviceName,
|
||||
this.requestFactory.createRequest(request, body, execution), routerContext);
|
||||
}
|
||||
|
||||
return this.loadBalancer.execute(serviceName,
|
||||
this.requestFactory.createRequest(request, body, execution));
|
||||
}
|
||||
|
||||
private PolarisRouterContext genRouterContext(HttpRequest request, byte[] body) {
|
||||
Map<String, String> labels = new HashMap<>();
|
||||
|
||||
// labels from downstream
|
||||
Map<String, String> transitiveLabels = MetadataContextHolder.get()
|
||||
.getFragmentContext(MetadataContext.FRAGMENT_TRANSITIVE);
|
||||
labels.putAll(transitiveLabels);
|
||||
|
||||
// labels from request
|
||||
if (resolver != null) {
|
||||
try {
|
||||
Map<String, String> customResolvedLabels = resolver.resolve(request, body);
|
||||
if (!CollectionUtils.isEmpty(customResolvedLabels)) {
|
||||
labels.putAll(customResolvedLabels);
|
||||
}
|
||||
}
|
||||
catch (Throwable t) {
|
||||
LOGGER.error("[SCT][Router] revoke RouterLabelResolver occur some exception. ", t);
|
||||
}
|
||||
}
|
||||
|
||||
//local service labels
|
||||
labels.putAll(metadataLocalProperties.getContent());
|
||||
|
||||
PolarisRouterContext routerContext = new PolarisRouterContext();
|
||||
routerContext.setLabels(labels);
|
||||
|
||||
return routerContext;
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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.spi;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import feign.RequestTemplate;
|
||||
|
||||
import org.springframework.http.HttpRequest;
|
||||
|
||||
/**
|
||||
* The spi for resolving labels from request.
|
||||
*
|
||||
* @author lepdou 2022-05-11
|
||||
*/
|
||||
public interface RouterLabelResolver {
|
||||
|
||||
/**
|
||||
* resolve labels from feign request.
|
||||
* @param requestTemplate the feign request.
|
||||
* @return resolved labels
|
||||
*/
|
||||
Map<String, String> resolve(RequestTemplate requestTemplate);
|
||||
|
||||
/**
|
||||
* resolve labels from rest template request.
|
||||
* @param request the rest template request.
|
||||
* @param body the rest template request body.
|
||||
* @return resolved labels
|
||||
*/
|
||||
Map<String, String> resolve(HttpRequest request, byte[] body);
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.tencent.cloud.polaris.router.config.RouterAutoConfiguration
|
@ -0,0 +1,53 @@
|
||||
/*
|
||||
* 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.example;
|
||||
|
||||
/**
|
||||
* demo object.
|
||||
* @author lepdou 2022-05-12
|
||||
*/
|
||||
public class User {
|
||||
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" +
|
||||
"name='" + name + '\'' +
|
||||
", age=" + age +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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.example;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.tencent.cloud.polaris.router.spi.RouterLabelResolver;
|
||||
import feign.RequestTemplate;
|
||||
|
||||
import org.springframework.http.HttpRequest;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
*
|
||||
* Customize the business tag information obtained from the request
|
||||
*
|
||||
*@author lepdou 2022-05-12
|
||||
*/
|
||||
@Component
|
||||
public class CustomRouterLabelResolver implements RouterLabelResolver {
|
||||
private final Gson gson = new Gson();
|
||||
|
||||
@Override
|
||||
public Map<String, String> resolve(RequestTemplate requestTemplate) {
|
||||
Map<String, String> labels = new HashMap<>();
|
||||
|
||||
User user = gson.fromJson(new String(requestTemplate.body()), User.class);
|
||||
|
||||
labels.put("user", user.getName());
|
||||
|
||||
return labels;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> resolve(HttpRequest request, byte[] body) {
|
||||
Map<String, String> labels = new HashMap<>();
|
||||
User user = gson.fromJson(new String(body), User.class);
|
||||
|
||||
labels.put("user", user.getName());
|
||||
return labels;
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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.example;
|
||||
|
||||
/**
|
||||
* demo object.
|
||||
* @author lepdou 2022-05-12
|
||||
*/
|
||||
public class User {
|
||||
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* 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.netflix.loadbalancer.Server;
|
||||
import com.tencent.cloud.common.metadata.MetadataContext;
|
||||
import com.tencent.cloud.common.pojo.PolarisServer;
|
||||
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;
|
||||
|
||||
/**
|
||||
* load balancer utils.
|
||||
*
|
||||
*@author lepdou 2022-05-17
|
||||
*/
|
||||
public class LoadBalancerUtils {
|
||||
|
||||
public static ServiceInstances transferServersToServiceInstances(List<Server> servers) {
|
||||
List<Instance> instances = new ArrayList<>(servers.size());
|
||||
String serviceName = null;
|
||||
|
||||
for (Server server : servers) {
|
||||
if (server instanceof PolarisServer) {
|
||||
Instance instance = ((PolarisServer) server).getInstance();
|
||||
instances.add(instance);
|
||||
|
||||
if (serviceName == null) {
|
||||
serviceName = instance.getService();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, serviceName);
|
||||
|
||||
return new DefaultServiceInstances(serviceKey, instances);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue