parent
2e8074b163
commit
b10ef1bf70
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* 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.common.metadata.MetadataContextHolder;
|
||||
|
||||
/**
|
||||
* PolarisLoadBalancerRingHashKeyProvider.
|
||||
*
|
||||
* @author seanthefish
|
||||
*/
|
||||
public final class PolarisLoadBalancerRingHashKeyProvider {
|
||||
|
||||
private static final String LOAD_BALANCER_HASH_KEY = "LOAD_BALANCER_HASH_KEY";
|
||||
|
||||
private PolarisLoadBalancerRingHashKeyProvider() {
|
||||
}
|
||||
|
||||
public static void hashKey(String key) {
|
||||
MetadataContextHolder.get().setLoadbalancer(LOAD_BALANCER_HASH_KEY, key);
|
||||
}
|
||||
|
||||
static String getHashKey() {
|
||||
return MetadataContextHolder.get().getLoadbalancerMetadata().get(LOAD_BALANCER_HASH_KEY);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,94 @@
|
||||
/*
|
||||
* 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.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import com.netflix.client.config.IClientConfig;
|
||||
import com.netflix.loadbalancer.AbstractServerPredicate;
|
||||
import com.netflix.loadbalancer.AvailabilityPredicate;
|
||||
import com.netflix.loadbalancer.CompositePredicate;
|
||||
import com.netflix.loadbalancer.PredicateBasedRule;
|
||||
import com.netflix.loadbalancer.Server;
|
||||
import com.tencent.cloud.common.pojo.PolarisServer;
|
||||
import com.tencent.polaris.api.config.consumer.LoadBalanceConfig;
|
||||
import com.tencent.polaris.api.pojo.Instance;
|
||||
import com.tencent.polaris.api.pojo.ServiceInstances;
|
||||
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.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* Polaris weighted load balancer.
|
||||
*
|
||||
* @author lepdou 2022-05-17
|
||||
*/
|
||||
public class PolarisRingHashRule extends PredicateBasedRule {
|
||||
|
||||
private final RouterAPI routerAPI;
|
||||
private CompositePredicate compositePredicate;
|
||||
|
||||
public PolarisRingHashRule(RouterAPI routerAPI) {
|
||||
this.routerAPI = routerAPI;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initWithNiwsConfig(IClientConfig clientConfig) {
|
||||
AvailabilityPredicate availabilityPredicate = new AvailabilityPredicate(this, clientConfig);
|
||||
compositePredicate = CompositePredicate.withPredicates(availabilityPredicate).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbstractServerPredicate getPredicate() {
|
||||
return compositePredicate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Server choose(Object key) {
|
||||
List<Server> servers = getLoadBalancer().getReachableServers();
|
||||
if (CollectionUtils.isEmpty(servers)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// filter circuit breaker servers by ribbon
|
||||
if (compositePredicate != null) {
|
||||
servers = compositePredicate.getEligibleServers(servers);
|
||||
}
|
||||
|
||||
ServiceInstances serviceInstances = LoadBalancerUtils.transferServersToServiceInstances(servers);
|
||||
|
||||
ProcessLoadBalanceRequest request = new ProcessLoadBalanceRequest();
|
||||
request.setDstInstances(serviceInstances);
|
||||
request.setLbPolicy(LoadBalanceConfig.LOAD_BALANCE_RING_HASH);
|
||||
Criteria criteria = new Criteria();
|
||||
String hashKey = Optional.ofNullable(PolarisLoadBalancerRingHashKeyProvider.getHashKey()).orElse("");
|
||||
criteria.setHashKey(hashKey);
|
||||
request.setCriteria(criteria);
|
||||
|
||||
ProcessLoadBalanceResponse processLoadBalanceResponse = routerAPI.processLoadBalance(request);
|
||||
|
||||
Instance targetInstance = processLoadBalanceResponse.getTargetInstance();
|
||||
|
||||
return new PolarisServer(serviceInstances, targetInstance);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue