use of com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse in project polaris-java by polarismesh.
the class DefaultRouterAPI method processLoadBalance.
@Override
public ProcessLoadBalanceResponse processLoadBalance(ProcessLoadBalanceRequest request) throws PolarisException {
checkAvailable("EngineAPI");
RouterValidator.validateProcessLoadBalanceRequest(request);
String lbPolicy = request.getLbPolicy();
if (StringUtils.isBlank(lbPolicy)) {
lbPolicy = extensions.getConfiguration().getConsumer().getLoadbalancer().getType();
}
LoadBalancer loadBalancer = (LoadBalancer) extensions.getPlugins().getPlugin(PluginTypes.LOAD_BALANCER.getBaseType(), lbPolicy);
Instance instance = BaseFlow.processLoadBalance(loadBalancer, request.getCriteria(), request.getDstInstances());
return new ProcessLoadBalanceResponse(instance);
}
use of com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse in project polaris-java-agent by polarismesh.
the class PolarisOperator method loadBalance.
public Instance loadBalance(String service, String hashKey, List<Instance> instances) {
ServiceKey serviceKey = new ServiceKey(polarisConfig.getNamespace(), service);
DefaultServiceInstances defaultServiceInstances = new DefaultServiceInstances(serviceKey, instances);
ProcessLoadBalanceRequest processLoadBalanceRequest = new ProcessLoadBalanceRequest();
processLoadBalanceRequest.setDstInstances(defaultServiceInstances);
Criteria criteria = new Criteria();
criteria.setHashKey(hashKey);
processLoadBalanceRequest.setCriteria(criteria);
ProcessLoadBalanceResponse processLoadBalanceResponse = routerAPI.processLoadBalance(processLoadBalanceRequest);
return processLoadBalanceResponse.getTargetInstance();
}
use of com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse in project polaris-java-agent by polarismesh.
the class PolarisServiceRouter method getLoadBalancedServiceInstance.
/**
* 对服务实例进行负载均衡
*
* @param dstInstances
* @return
*/
public static Instance getLoadBalancedServiceInstance(ServiceInstances dstInstances) {
LogUtils.logInvoke(PolarisServiceRouter.class, "getLoadBalancedServiceInstance");
// 执行负载均衡
ProcessLoadBalanceRequest processLoadBalanceRequest = new ProcessLoadBalanceRequest();
processLoadBalanceRequest.setDstInstances(dstInstances);
processLoadBalanceRequest.setLbPolicy(LoadBalanceConfig.LOAD_BALANCE_WEIGHTED_RANDOM);
ProcessLoadBalanceResponse processLoadBalanceResponse = PolarisAPIFactory.getRouterApi().processLoadBalance(processLoadBalanceRequest);
LOGGER.info("success to loadBalanced by Polaris");
return processLoadBalanceResponse.getTargetInstance();
}
use of com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse in project spring-cloud-tencent by Tencent.
the class PolarisWeightedRule method choose.
@Override
public Server choose(Object key) {
List<Server> servers = getLoadBalancer().getReachableServers();
if (CollectionUtils.isEmpty(servers)) {
return null;
}
ServiceInstances serviceInstances = LoadBalancerUtils.transferServersToServiceInstances(servers);
ProcessLoadBalanceRequest request = new ProcessLoadBalanceRequest();
request.setDstInstances(serviceInstances);
request.setLbPolicy(LoadBalanceConfig.LOAD_BALANCE_WEIGHTED_RANDOM);
ProcessLoadBalanceResponse processLoadBalanceResponse = routerAPI.processLoadBalance(request);
Instance targetInstance = processLoadBalanceResponse.getTargetInstance();
return new PolarisServer(serviceInstances, targetInstance);
}
use of com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse in project spring-cloud-tencent by Tencent.
the class PolarisWeightedRandomRule method choose.
@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);
}
Aggregations