feat:added ribbon lb polaris weighted round robin rule (#1080)
parent
8f60cbdf58
commit
abaf02827e
@ -0,0 +1,84 @@
|
|||||||
|
/*
|
||||||
|
* 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 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.pojo.Instance;
|
||||||
|
import com.tencent.polaris.api.pojo.ServiceInstances;
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract polaris load balancer rule.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:veteranchen@tencent.com">veteranchen</a>
|
||||||
|
*/
|
||||||
|
public abstract class AbstractPolarisRule extends PredicateBasedRule {
|
||||||
|
private final RouterAPI routerAPI;
|
||||||
|
private CompositePredicate compositePredicate;
|
||||||
|
|
||||||
|
public AbstractPolarisRule(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 = setProcessLoadBalanceRequest(request);
|
||||||
|
ProcessLoadBalanceResponse processLoadBalanceResponse = routerAPI.processLoadBalance(request);
|
||||||
|
Instance targetInstance = processLoadBalanceResponse.getTargetInstance();
|
||||||
|
return new PolarisServer(serviceInstances, targetInstance);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract ProcessLoadBalanceRequest setProcessLoadBalanceRequest(ProcessLoadBalanceRequest request);
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* 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.polaris.api.config.consumer.LoadBalanceConfig;
|
||||||
|
import com.tencent.polaris.router.api.core.RouterAPI;
|
||||||
|
import com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Polaris weighted load balancer.
|
||||||
|
*
|
||||||
|
* @author <a href="mailto:veteranchen@tencent.com">veteranchen</a>
|
||||||
|
*/
|
||||||
|
public class PolarisWeightedRoundRobinRule extends AbstractPolarisRule {
|
||||||
|
|
||||||
|
public PolarisWeightedRoundRobinRule(RouterAPI routerAPI) {
|
||||||
|
super(routerAPI);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected ProcessLoadBalanceRequest setProcessLoadBalanceRequest(ProcessLoadBalanceRequest request) {
|
||||||
|
request.setLbPolicy(LoadBalanceConfig.LOAD_BALANCE_WEIGHTED_ROUND_ROBIN);
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue