Search in sources :

Example 1 with ProcessLoadBalanceRequest

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();
}
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 2 with ProcessLoadBalanceRequest

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

Example 3 with ProcessLoadBalanceRequest

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);
}
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 4 with ProcessLoadBalanceRequest

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);
}
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)

Example 5 with ProcessLoadBalanceRequest

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());
    }
}
Also used : HashMap(java.util.HashMap) RouterNamesGroup(com.tencent.polaris.router.api.rpc.ProcessRoutersRequest.RouterNamesGroup) ProcessRoutersRequest(com.tencent.polaris.router.api.rpc.ProcessRoutersRequest) ProcessLoadBalanceResponse(com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse) ArrayList(java.util.ArrayList) RouterAPI(com.tencent.polaris.router.api.core.RouterAPI) SDKContext(com.tencent.polaris.client.api.SDKContext) ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) ProcessRoutersResponse(com.tencent.polaris.router.api.rpc.ProcessRoutersResponse) ProcessLoadBalanceRequest(com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) InitResult(com.tencent.polaris.router.example.ExampleUtils.InitResult) GetAllInstancesRequest(com.tencent.polaris.api.rpc.GetAllInstancesRequest) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Aggregations

ProcessLoadBalanceRequest (com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest)5 ProcessLoadBalanceResponse (com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse)5 Server (com.netflix.loadbalancer.Server)2 Instance (com.tencent.polaris.api.pojo.Instance)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 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 InitResult (com.tencent.polaris.router.example.ExampleUtils.InitResult)1