Search in sources :

Example 41 with Instance

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

Example 42 with Instance

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

use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.

the class Consumer method invokeByNameResolution.

private static String invokeByNameResolution(String namespace, String service, String value, ConsumerAPI consumerAPI) {
    System.out.println("namespace " + namespace + ", service " + service);
    // 1. we need to do naming resolution to get a load balanced host and port
    GetOneInstanceRequest getOneInstanceRequest = new GetOneInstanceRequest();
    getOneInstanceRequest.setNamespace(namespace);
    getOneInstanceRequest.setService(service);
    InstancesResponse oneInstance = consumerAPI.getOneInstance(getOneInstanceRequest);
    Instance[] instances = oneInstance.getInstances();
    System.out.println("instances count is " + instances.length);
    Instance targetInstance = instances[0];
    System.out.printf("target instance is %s:%d%n", targetInstance.getHost(), targetInstance.getPort());
    // 2. invoke the server by the resolved address
    String urlStr = String.format("http://%s:%d%s?value=%s", targetInstance.getHost(), targetInstance.getPort(), PATH, value);
    long startMillis = System.currentTimeMillis();
    HttpResult httpResult = httpGet(urlStr);
    long delay = System.currentTimeMillis() - startMillis;
    System.out.printf("invoke %s, code is %d, delay is %d%n", urlStr, httpResult.code, delay);
    // 3. report the invoke result to polaris-java, to eliminate the fail address
    RetStatus status = RetStatus.RetSuccess;
    if (httpResult.code != 200) {
        status = RetStatus.RetFail;
    }
    ServiceCallResult result = new ServiceCallResult();
    result.setNamespace(namespace);
    result.setService(service);
    result.setHost(targetInstance.getHost());
    result.setPort(targetInstance.getPort());
    result.setRetCode(httpResult.code);
    result.setDelay(delay);
    result.setRetStatus(status);
    consumerAPI.updateServiceCallResult(result);
    System.out.println("success to call updateServiceCallResult");
    return httpResult.message;
}
Also used : RetStatus(com.tencent.polaris.api.pojo.RetStatus) ServiceCallResult(com.tencent.polaris.api.rpc.ServiceCallResult) Instance(com.tencent.polaris.api.pojo.Instance) GetOneInstanceRequest(com.tencent.polaris.api.rpc.GetOneInstanceRequest) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Example 44 with Instance

use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.

the class WeightedRandomBalance method sumTotalWeight.

private int sumTotalWeight(ServiceInstances svcInstances) {
    List<Instance> instances = svcInstances.getInstances();
    int totalWeight = 0;
    if (CollectionUtils.isNotEmpty(instances)) {
        for (Instance instance : instances) {
            totalWeight += instance.getWeight();
        }
    }
    return totalWeight;
}
Also used : Instance(com.tencent.polaris.api.pojo.Instance)

Example 45 with Instance

use of com.tencent.polaris.api.pojo.Instance in project polaris-java by polarismesh.

the class ConsistentHashLoadBalance method buildConsistentHashRing.

/**
 * 构建一致性hash环
 *
 * @param serviceInstances 服务实例
 * @return 一致性hash环
 */
private TreeMap<Integer, Instance> buildConsistentHashRing(ServiceInstances serviceInstances) {
    List<Instance> instances = serviceInstances.getInstances();
    TreeMap<Integer, Instance> virtualNodeRing = new TreeMap<>();
    for (Instance instance : instances) {
        for (int i = 0; i < VIRTUAL_NODE_SIZE; i++) {
            // 新增虚拟节点
            virtualNodeRing.put(hashStrategy.getHashCode(instance.getHost() + ":" + instance.getPort() + VIRTUAL_NODE_SUFFIX + i), instance);
        }
    }
    return virtualNodeRing;
}
Also used : Instance(com.tencent.polaris.api.pojo.Instance) TreeMap(java.util.TreeMap)

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