Search in sources :

Example 1 with ProcessLoadBalanceResponse

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);
}
Also used : Instance(com.tencent.polaris.api.pojo.Instance) ProcessLoadBalanceResponse(com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse) LoadBalancer(com.tencent.polaris.api.plugin.loadbalance.LoadBalancer)

Example 2 with ProcessLoadBalanceResponse

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();
}
Also used : ProcessLoadBalanceRequest(com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest) ProcessLoadBalanceResponse(com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) DefaultServiceInstances(com.tencent.polaris.api.pojo.DefaultServiceInstances) Criteria(com.tencent.polaris.api.rpc.Criteria)

Example 3 with ProcessLoadBalanceResponse

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();
}
Also used : ProcessLoadBalanceRequest(com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest) ProcessLoadBalanceResponse(com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse)

Example 4 with ProcessLoadBalanceResponse

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);
}
Also used : ProcessLoadBalanceRequest(com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) PolarisServer(com.tencent.cloud.common.pojo.PolarisServer) Server(com.netflix.loadbalancer.Server) Instance(com.tencent.polaris.api.pojo.Instance) ProcessLoadBalanceResponse(com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse) PolarisServer(com.tencent.cloud.common.pojo.PolarisServer)

Example 5 with ProcessLoadBalanceResponse

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);
}
Also used : ProcessLoadBalanceRequest(com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest) Server(com.netflix.loadbalancer.Server) PolarisServer(com.tencent.cloud.polaris.pojo.PolarisServer) Instance(com.tencent.polaris.api.pojo.Instance) ProcessLoadBalanceResponse(com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse) PolarisServer(com.tencent.cloud.polaris.pojo.PolarisServer)

Aggregations

ProcessLoadBalanceResponse (com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse)6 ProcessLoadBalanceRequest (com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest)5 Instance (com.tencent.polaris.api.pojo.Instance)3 Server (com.netflix.loadbalancer.Server)2 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)2 PolarisServer (com.tencent.cloud.common.pojo.PolarisServer)1 PolarisServer (com.tencent.cloud.polaris.pojo.PolarisServer)1 ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)1 LoadBalancer (com.tencent.polaris.api.plugin.loadbalance.LoadBalancer)1 DefaultServiceInstances (com.tencent.polaris.api.pojo.DefaultServiceInstances)1 ServiceInfo (com.tencent.polaris.api.pojo.ServiceInfo)1 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)1 Criteria (com.tencent.polaris.api.rpc.Criteria)1 GetAllInstancesRequest (com.tencent.polaris.api.rpc.GetAllInstancesRequest)1 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)1 SDKContext (com.tencent.polaris.client.api.SDKContext)1 RouterAPI (com.tencent.polaris.router.api.core.RouterAPI)1 ProcessRoutersRequest (com.tencent.polaris.router.api.rpc.ProcessRoutersRequest)1 RouterNamesGroup (com.tencent.polaris.router.api.rpc.ProcessRoutersRequest.RouterNamesGroup)1 ProcessRoutersResponse (com.tencent.polaris.router.api.rpc.ProcessRoutersResponse)1