Search in sources :

Example 1 with DefaultServiceInstances

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

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

the class PolarisLoadBalancerCompositeRuleTest method assembleServiceInstances.

private ServiceInstances assembleServiceInstances() {
    ServiceKey serviceKey = new ServiceKey(testNamespace, testCalleeService);
    List<Instance> instances = new LinkedList<>();
    instances.add(new DefaultInstance());
    instances.add(new DefaultInstance());
    instances.add(new DefaultInstance());
    instances.add(new DefaultInstance());
    instances.add(new DefaultInstance());
    return new DefaultServiceInstances(serviceKey, instances);
}
Also used : DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) Instance(com.tencent.polaris.api.pojo.Instance) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) DefaultServiceInstances(com.tencent.polaris.api.pojo.DefaultServiceInstances) LinkedList(java.util.LinkedList)

Example 3 with DefaultServiceInstances

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

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

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

the class PolarisRoutingLoadBalancer method getReachableServers.

@Override
public List<Server> getReachableServers() {
    List<Server> allServers = super.getAllServers();
    if (CollectionUtils.isEmpty(allServers)) {
        return allServers;
    }
    ServiceInstances serviceInstances = null;
    if (allServers.get(0) instanceof PolarisServer) {
        serviceInstances = ((PolarisServer) allServers.get(0)).getServiceInstances();
    } else {
        String serviceName;
        // notice the difference between different service registries
        if (StringUtils.isNotBlank(allServers.get(0).getMetaInfo().getServiceIdForDiscovery())) {
            serviceName = allServers.get(0).getMetaInfo().getServiceIdForDiscovery();
        } else {
            serviceName = allServers.get(0).getMetaInfo().getAppName();
        }
        if (StringUtils.isBlank(serviceName)) {
            throw new IllegalStateException("PolarisRoutingLoadBalancer only Server with AppName or ServiceIdForDiscovery attribute");
        }
        ServiceKey serviceKey = new ServiceKey(MetadataContextHolder.LOCAL_NAMESPACE, serviceName);
        List<Instance> instances = new ArrayList<>(8);
        for (Server server : allServers) {
            DefaultInstance instance = new DefaultInstance();
            instance.setNamespace(MetadataContextHolder.LOCAL_NAMESPACE);
            instance.setService(serviceName);
            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);
    }
    ProcessRoutersRequest processRoutersRequest = new ProcessRoutersRequest();
    processRoutersRequest.setDstInstances(serviceInstances);
    String srcNamespace = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.LOCAL_NAMESPACE);
    String srcService = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.LOCAL_SERVICE);
    Map<String, String> transitiveCustomMetadata = MetadataContextHolder.get().getAllTransitiveCustomMetadata();
    String method = MetadataContextHolder.get().getSystemMetadata(SystemMetadataKey.PEER_PATH);
    processRoutersRequest.setMethod(method);
    if (StringUtils.isNotBlank(srcNamespace) && StringUtils.isNotBlank(srcService)) {
        ServiceInfo serviceInfo = new ServiceInfo();
        serviceInfo.setNamespace(srcNamespace);
        serviceInfo.setService(srcService);
        serviceInfo.setMetadata(transitiveCustomMetadata);
        processRoutersRequest.setSourceService(serviceInfo);
    }
    ProcessRoutersResponse processRoutersResponse = routerAPI.processRouters(processRoutersRequest);
    ServiceInstances filteredServiceInstances = processRoutersResponse.getServiceInstances();
    List<Server> filteredInstances = new ArrayList<>();
    for (Instance instance : filteredServiceInstances.getInstances()) {
        filteredInstances.add(new PolarisServer(serviceInstances, instance));
    }
    return filteredInstances;
}
Also used : Server(com.netflix.loadbalancer.Server) PolarisServer(com.tencent.cloud.polaris.pojo.PolarisServer) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) Instance(com.tencent.polaris.api.pojo.Instance) ProcessRoutersRequest(com.tencent.polaris.router.api.rpc.ProcessRoutersRequest) ArrayList(java.util.ArrayList) ServiceKey(com.tencent.polaris.api.pojo.ServiceKey) ServiceInfo(com.tencent.polaris.api.pojo.ServiceInfo) ProcessRoutersResponse(com.tencent.polaris.router.api.rpc.ProcessRoutersResponse) DefaultServiceInstances(com.tencent.polaris.api.pojo.DefaultServiceInstances) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) DefaultInstance(com.tencent.polaris.api.pojo.DefaultInstance) DefaultServiceInstances(com.tencent.polaris.api.pojo.DefaultServiceInstances) PolarisServer(com.tencent.cloud.polaris.pojo.PolarisServer)

Aggregations

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