Search in sources :

Example 36 with Instance

use of com.tencent.polaris.api.pojo.Instance 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 37 with Instance

use of com.tencent.polaris.api.pojo.Instance in project spring-cloud-tencent by Tencent.

the class PolarisLoadBalancer method getExtendDiscoveryServiceInstances.

private ServiceInstances getExtendDiscoveryServiceInstances() {
    List<Server> allServers = super.getAllServers();
    if (CollectionUtils.isEmpty(allServers)) {
        return null;
    }
    ServiceInstances serviceInstances;
    if (StringUtils.isBlank(name)) {
        throw new IllegalStateException("PolarisLoadBalancer only Server with AppName or ServiceIdForDiscovery attribute");
    }
    ServiceKey serviceKey = new ServiceKey(MetadataContext.LOCAL_NAMESPACE, name);
    List<Instance> instances = new ArrayList<>(8);
    for (Server server : allServers) {
        DefaultInstance instance = new DefaultInstance();
        instance.setNamespace(MetadataContext.LOCAL_NAMESPACE);
        instance.setService(name);
        instance.setHealthy(server.isAlive());
        instance.setProtocol(server.getScheme());
        instance.setId(server.getId());
        instance.setHost(server.getHost());
        instance.setPort(server.getPort());
        instance.setZone(server.getZone());
        instance.setWeight(100);
        instances.add(instance);
    }
    serviceInstances = new DefaultServiceInstances(serviceKey, instances);
    return serviceInstances;
}
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) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) ArrayList(java.util.ArrayList) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) DefaultServiceInstances(com.tencent.polaris.api.pojo.DefaultServiceInstances)

Example 38 with Instance

use of com.tencent.polaris.api.pojo.Instance 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 39 with Instance

use of com.tencent.polaris.api.pojo.Instance 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 40 with Instance

use of com.tencent.polaris.api.pojo.Instance in project spring-cloud-tencent by Tencent.

the class PolarisServiceRegistry method getStatus.

@Override
public Object getStatus(Registration registration) {
    String serviceName = registration.getServiceId();
    InstancesResponse instancesResponse = polarisDiscoveryHandler.getInstances(serviceName);
    Instance[] instances = instancesResponse.getInstances();
    if (null == instances || instances.length == 0) {
        return null;
    }
    for (Instance instance : instances) {
        if (instance.getHost().equalsIgnoreCase(registration.getHost()) && instance.getPort() == polarisDiscoveryProperties.getPort()) {
            return instance.isHealthy() ? "UP" : "DOWN";
        }
    }
    return null;
}
Also used : Instance(com.tencent.polaris.api.pojo.Instance) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Aggregations

Instance (com.tencent.polaris.api.pojo.Instance)53 ArrayList (java.util.ArrayList)15 InstancesResponse (com.tencent.polaris.api.rpc.InstancesResponse)14 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)12 Server (com.netflix.loadbalancer.Server)9 ConsumerAPI (com.tencent.polaris.api.core.ConsumerAPI)9 PolarisServer (com.tencent.cloud.common.pojo.PolarisServer)6 Configuration (com.tencent.polaris.api.config.Configuration)6 GetOneInstanceRequest (com.tencent.polaris.api.rpc.GetOneInstanceRequest)6 HashMap (java.util.HashMap)6 DefaultServiceInstances (com.tencent.polaris.api.pojo.DefaultServiceInstances)5 ServiceKey (com.tencent.polaris.api.pojo.ServiceKey)5 ServiceCallResult (com.tencent.polaris.api.rpc.ServiceCallResult)5 HashSet (java.util.HashSet)5 LinkedList (java.util.LinkedList)5 Map (java.util.Map)5 PolarisException (com.tencent.polaris.api.exception.PolarisException)4 ResultKey (com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreakResult.ResultKey)4 DefaultInstance (com.tencent.polaris.api.pojo.DefaultInstance)4 StatusDimension (com.tencent.polaris.api.pojo.StatusDimension)4