Search in sources :

Example 1 with PolarisServer

use of com.tencent.cloud.common.pojo.PolarisServer in project spring-cloud-tencent by Tencent.

the class PolarisLoadBalancerCompositeRule method doRouter.

List<Server> doRouter(List<Server> allServers, Object key) {
    ServiceInstances serviceInstances = LoadBalancerUtils.transferServersToServiceInstances(allServers);
    // filter instance by routers
    ProcessRoutersRequest processRoutersRequest = buildProcessRoutersRequest(serviceInstances, key);
    ProcessRoutersResponse processRoutersResponse = routerAPI.processRouters(processRoutersRequest);
    List<Server> filteredInstances = new ArrayList<>();
    ServiceInstances filteredServiceInstances = processRoutersResponse.getServiceInstances();
    for (Instance instance : filteredServiceInstances.getInstances()) {
        filteredInstances.add(new PolarisServer(serviceInstances, instance));
    }
    return filteredInstances;
}
Also used : ProcessRoutersResponse(com.tencent.polaris.router.api.rpc.ProcessRoutersResponse) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) Server(com.netflix.loadbalancer.Server) PolarisServer(com.tencent.cloud.common.pojo.PolarisServer) Instance(com.tencent.polaris.api.pojo.Instance) ProcessRoutersRequest(com.tencent.polaris.router.api.rpc.ProcessRoutersRequest) ArrayList(java.util.ArrayList) PolarisServer(com.tencent.cloud.common.pojo.PolarisServer)

Example 2 with PolarisServer

use of com.tencent.cloud.common.pojo.PolarisServer in project spring-cloud-tencent by Tencent.

the class PolarisServerList method getServers.

private List<Server> getServers() {
    InstancesResponse allInstances = polarisDiscoveryHandler.getHealthyInstances(serviceId);
    ServiceInstances serviceInstances = allInstances.toServiceInstances();
    List<Server> polarisServers = new ArrayList<>();
    for (Instance instance : serviceInstances.getInstances()) {
        polarisServers.add(new PolarisServer(serviceInstances, instance));
    }
    return polarisServers;
}
Also used : 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) ArrayList(java.util.ArrayList) PolarisServer(com.tencent.cloud.common.pojo.PolarisServer) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Example 3 with PolarisServer

use of com.tencent.cloud.common.pojo.PolarisServer in project spring-cloud-tencent by Tencent.

the class PolarisLoadBalancer method getReachableServers.

@Override
public List<Server> getReachableServers() {
    ServiceInstances serviceInstances;
    if (polarisLoadBalancerProperties.getDiscoveryType().equals(ContextConstant.POLARIS)) {
        serviceInstances = getPolarisDiscoveryServiceInstances();
    } else {
        serviceInstances = getExtendDiscoveryServiceInstances();
    }
    if (serviceInstances == null || CollectionUtils.isEmpty(serviceInstances.getInstances())) {
        return Collections.emptyList();
    }
    List<Server> servers = new LinkedList<>();
    for (Instance instance : serviceInstances.getInstances()) {
        servers.add(new PolarisServer(serviceInstances, instance));
    }
    return servers;
}
Also used : DefaultServiceInstances(com.tencent.polaris.api.pojo.DefaultServiceInstances) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) Server(com.netflix.loadbalancer.Server) PolarisServer(com.tencent.cloud.common.pojo.PolarisServer) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) Instance(com.tencent.polaris.api.pojo.Instance) PolarisServer(com.tencent.cloud.common.pojo.PolarisServer) LinkedList(java.util.LinkedList)

Example 4 with PolarisServer

use of com.tencent.cloud.common.pojo.PolarisServer in project spring-cloud-tencent by Tencent.

the class LoadBalancerUtils method transferServersToServiceInstances.

public static ServiceInstances transferServersToServiceInstances(List<Server> servers) {
    List<Instance> instances = new ArrayList<>(servers.size());
    String serviceName = null;
    for (Server server : servers) {
        if (server instanceof PolarisServer) {
            Instance instance = ((PolarisServer) server).getInstance();
            instances.add(instance);
            if (serviceName == null) {
                serviceName = instance.getService();
            }
        }
    }
    ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, serviceName);
    return new DefaultServiceInstances(serviceKey, instances);
}
Also used : PolarisServer(com.tencent.cloud.common.pojo.PolarisServer) Server(com.netflix.loadbalancer.Server) Instance(com.tencent.polaris.api.pojo.Instance) ArrayList(java.util.ArrayList) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) DefaultServiceInstances(com.tencent.polaris.api.pojo.DefaultServiceInstances) PolarisServer(com.tencent.cloud.common.pojo.PolarisServer)

Example 5 with PolarisServer

use of com.tencent.cloud.common.pojo.PolarisServer 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)

Aggregations

Server (com.netflix.loadbalancer.Server)6 PolarisServer (com.tencent.cloud.common.pojo.PolarisServer)6 Instance (com.tencent.polaris.api.pojo.Instance)5 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)5 DefaultServiceInstances (com.tencent.polaris.api.pojo.DefaultServiceInstances)3 ArrayList (java.util.ArrayList)3 DefaultInstance (com.tencent.polaris.api.pojo.DefaultInstance)2 LinkedList (java.util.LinkedList)2 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)1 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)1 ProcessLoadBalanceRequest (com.tencent.polaris.router.api.rpc.ProcessLoadBalanceRequest)1 ProcessLoadBalanceResponse (com.tencent.polaris.router.api.rpc.ProcessLoadBalanceResponse)1 ProcessRoutersRequest (com.tencent.polaris.router.api.rpc.ProcessRoutersRequest)1 ProcessRoutersResponse (com.tencent.polaris.router.api.rpc.ProcessRoutersResponse)1