use of com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest 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.ProcessLoadBalanceRequest 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.ProcessLoadBalanceRequest 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.ProcessLoadBalanceRequest 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);
}
use of com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest in project polaris-java by polarismesh.
the class RouterExample method main.
public static void main(String[] args) throws Exception {
InitResult initResult = ExampleUtils.initConsumerConfiguration(args);
String namespace = initResult.getNamespace();
String service = initResult.getService();
try (SDKContext sdkContext = SDKContext.initContext()) {
ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByContext(sdkContext);
RouterAPI routerAPI = RouterAPIFactory.createRouterAPIByContext(sdkContext);
// 1. 拉取全量服务实例
GetAllInstancesRequest getAllInstancesRequest = new GetAllInstancesRequest();
getAllInstancesRequest.setNamespace(namespace);
getAllInstancesRequest.setService(service);
InstancesResponse allInstanceResp = consumerAPI.getAllInstance(getAllInstancesRequest);
ServiceInstances dstInstances = allInstanceResp.toServiceInstances();
// 2. 执行服务路由
ProcessRoutersRequest processRoutersRequest = new ProcessRoutersRequest();
// 被调服务
System.out.printf("instances count before routing is %s%n", dstInstances.getInstances().size());
// 主调方信息
ServiceInfo srcSourceInfo = new ServiceInfo();
Map<String, String> labels = new HashMap<>();
labels.put("env", "test");
srcSourceInfo.setMetadata(labels);
RouterNamesGroup routerNamesGroup = new RouterNamesGroup();
List<String> coreRouters = new ArrayList<>();
coreRouters.add(ServiceRouterConfig.DEFAULT_ROUTER_RULE);
coreRouters.add(ServiceRouterConfig.DEFAULT_ROUTER_METADATA);
coreRouters.add(ServiceRouterConfig.DEFAULT_ROUTER_NEARBY);
// 设置走规则路由
routerNamesGroup.setCoreRouters(coreRouters);
processRoutersRequest.setDstInstances(dstInstances);
processRoutersRequest.setSourceService(srcSourceInfo);
processRoutersRequest.setRouters(routerNamesGroup);
ProcessRoutersResponse processRoutersResponse = routerAPI.processRouters(processRoutersRequest);
System.out.printf("instances count after routing is %s%n", processRoutersResponse.getServiceInstances().getInstances().size());
// 3. 执行负载均衡
ProcessLoadBalanceRequest processLoadBalanceRequest = new ProcessLoadBalanceRequest();
processLoadBalanceRequest.setDstInstances(processRoutersResponse.getServiceInstances());
processLoadBalanceRequest.setLbPolicy(LoadBalanceConfig.LOAD_BALANCE_WEIGHTED_RANDOM);
ProcessLoadBalanceResponse processLoadBalanceResponse = routerAPI.processLoadBalance(processLoadBalanceRequest);
System.out.printf("instances after lb is %s%n", processLoadBalanceResponse.getTargetInstance());
}
}
Aggregations