-
Notifications
You must be signed in to change notification settings - Fork 492
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:added ribbon lb polaris weighted round robin rule (#1080)
- Loading branch information
Showing
10 changed files
with
185 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...oadbalancer/src/main/java/com/tencent/cloud/polaris/loadbalancer/AbstractPolarisRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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:[email protected]">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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...r/src/main/java/com/tencent/cloud/polaris/loadbalancer/PolarisWeightedRoundRobinRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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:[email protected]">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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.