Search in sources :

Example 46 with Instance

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

the class GetOneInstanceExample method main.

public static void main(String[] args) throws Exception {
    InitResult initResult = ExampleUtils.initConsumerConfiguration(args, false);
    String namespace = initResult.getNamespace();
    String service = initResult.getService();
    try (ConsumerAPI consumerAPI = ExampleUtils.createConsumerAPI(initResult.getConfig())) {
        System.out.println("namespace " + namespace + ", service " + service);
        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());
    }
}
Also used : InitResult(com.tencent.polaris.discovery.example.utils.ExampleUtils.InitResult) Instance(com.tencent.polaris.api.pojo.Instance) GetOneInstanceRequest(com.tencent.polaris.api.rpc.GetOneInstanceRequest) ConsumerAPI(com.tencent.polaris.api.core.ConsumerAPI) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Example 47 with Instance

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

the class SyncFlow method commonSyncGetOneInstance.

/**
 * 获取单个实例
 *
 * @param request 请求对象
 * @return 实例应答
 * @throws PolarisException 异常
 */
public InstancesResponse commonSyncGetOneInstance(CommonInstancesRequest request) throws PolarisException {
    syncGetServiceInstances(request);
    ServiceInstances dstInstances = request.getDstInstances();
    if (CollectionUtils.isEmpty(dstInstances.getInstances())) {
        return new InstancesResponse(dstInstances);
    }
    ServiceInstances routerInstances = BaseFlow.processServiceRouters(request.getRouteInfo(), request.getDstInstances(), extensions.getConfigRouterChainGroup());
    LoadBalancer loadBalancer = extensions.getLoadBalancer();
    Instance instance = BaseFlow.processLoadBalance(loadBalancer, request.getCriteria(), routerInstances);
    return new InstancesResponse(dstInstances, instance);
}
Also used : ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) Instance(com.tencent.polaris.api.pojo.Instance) LoadBalancer(com.tencent.polaris.api.plugin.loadbalance.LoadBalancer) InstancesResponse(com.tencent.polaris.api.rpc.InstancesResponse)

Example 48 with Instance

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

the class Utils method checkAddInstances.

public static List<Instance> checkAddInstances(ServiceInstancesByProto oldVal, ServiceInstancesByProto newVal) {
    Set<Instance> oldIns = new HashSet<>(oldVal.getInstances());
    Set<Instance> newIns = new HashSet<>(newVal.getInstances());
    List<Instance> ret = new LinkedList<>();
    for (Instance instance : newIns) {
        if (!oldIns.contains(instance)) {
            ret.add(instance);
        }
    }
    return ret;
}
Also used : Instance(com.tencent.polaris.api.pojo.Instance) LinkedList(java.util.LinkedList) HashSet(java.util.HashSet)

Example 49 with Instance

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

the class NearbyRouter method router.

@Override
public RouteResult router(RouteInfo routeInfo, ServiceInstances serviceInstances) throws PolarisException {
    // 先获取最低可用就近级别
    LocationLevel minAvailableLevel = config.getMatchLevel();
    if (null == minAvailableLevel) {
        minAvailableLevel = defaultMinLevel;
    }
    LocationLevel minLevel = minAvailableLevel;
    if (null != routeInfo.getNextRouterInfo()) {
        if (null != routeInfo.getNextRouterInfo().getLocationLevel()) {
            minLevel = routeInfo.getNextRouterInfo().getLocationLevel();
        }
        if (null != routeInfo.getNextRouterInfo().getMinAvailableLevel()) {
            minAvailableLevel = routeInfo.getNextRouterInfo().getMinAvailableLevel();
        }
    }
    LocationLevel maxLevel = config.getMaxMatchLevel();
    if (null == maxLevel) {
        maxLevel = LocationLevel.all;
    }
    Map<LocationLevel, String> clientLocationInfo = locationInfo.get();
    if (minLevel.ordinal() >= maxLevel.ordinal()) {
        List<Instance> instances = selectInstances(serviceInstances, minAvailableLevel, clientLocationInfo);
        if (CollectionUtils.isEmpty(instances)) {
            throw new PolarisException(ErrorCode.LOCATION_MISMATCH, String.format("can not find any instance by level %s", minLevel.name()));
        }
        // 已经循环了一圈
        return new RouteResult(selectInstances(serviceInstances, minAvailableLevel, clientLocationInfo), RouteResult.State.Next);
    }
    CheckResult checkResult = new CheckResult();
    for (int i = minLevel.ordinal(); i <= maxLevel.ordinal(); i++) {
        LocationLevel curLevel = LocationLevel.values()[i];
        checkResult = hasHealthyInstances(serviceInstances, routeInfo.getStatusDimensions(), curLevel, clientLocationInfo);
        checkResult.curLevel = curLevel;
        if (!CollectionUtils.isEmpty(checkResult.instances)) {
            break;
        } else {
            minAvailableLevel = curLevel;
        }
    }
    if (CollectionUtils.isEmpty(checkResult.instances)) {
        throw new PolarisException(ErrorCode.LOCATION_MISMATCH, String.format("can not find any instance by level %s", checkResult.curLevel.name()));
    }
    if (!config.isEnableDegradeByUnhealthyPercent() || checkResult.curLevel == LocationLevel.all) {
        return new RouteResult(checkResult.instances, RouteResult.State.Next);
    }
    int healthyInstanceCount = checkResult.healthyInstanceCount;
    double actualHealthyPercent = (double) healthyInstanceCount / (double) serviceInstances.getInstances().size();
    if (actualHealthyPercent <= healthyPercentToDegrade) {
        LOG.debug("[shouldDegrade] enableDegradeByUnhealthyPercent = {},unhealthyPercentToDegrade={}," + "healthyPercent={},isStrict={},matchLevel={}", config.isEnableDegradeByUnhealthyPercent(), config.getUnhealthyPercentToDegrade(), actualHealthyPercent, config.isStrictNearby(), checkResult.curLevel);
        RouteResult result = new RouteResult(checkResult.instances, RouteResult.State.Retry);
        result.getNextRouterInfo().setLocationLevel(nextLevel(checkResult.curLevel));
        result.getNextRouterInfo().setMinAvailableLevel(minAvailableLevel);
        return result;
    }
    return new RouteResult(checkResult.instances, RouteResult.State.Next);
}
Also used : PolarisException(com.tencent.polaris.api.exception.PolarisException) Utils.isHealthyInstance(com.tencent.polaris.client.util.Utils.isHealthyInstance) Instance(com.tencent.polaris.api.pojo.Instance) RouteResult(com.tencent.polaris.api.plugin.route.RouteResult) LocationLevel(com.tencent.polaris.api.plugin.route.LocationLevel)

Example 50 with Instance

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

the class NearbyRouter method selectInstances.

private List<Instance> selectInstances(ServiceInstances svcInstances, LocationLevel targetLevel, Map<LocationLevel, String> clientInfo) {
    List<Instance> instances = new ArrayList<>();
    String clientZone = "";
    String clientRegion = "";
    String clientCampus = "";
    if (null != clientInfo) {
        clientZone = clientInfo.get(LocationLevel.zone);
        clientRegion = clientInfo.get(LocationLevel.region);
        clientCampus = clientInfo.get(LocationLevel.campus);
    }
    for (Instance instance : svcInstances.getInstances()) {
        switch(targetLevel) {
            case zone:
                if (clientZone.equals("") || clientZone.equals(instance.getZone())) {
                    instances.add(instance);
                }
                break;
            case campus:
                if (clientCampus.equals("") || clientCampus.equals(instance.getCampus())) {
                    instances.add(instance);
                }
                break;
            case region:
                if (clientRegion.equals("") || clientRegion.equals(instance.getRegion())) {
                    instances.add(instance);
                }
                break;
            default:
                instances.add(instance);
                break;
        }
    }
    return instances;
}
Also used : Utils.isHealthyInstance(com.tencent.polaris.client.util.Utils.isHealthyInstance) Instance(com.tencent.polaris.api.pojo.Instance) ArrayList(java.util.ArrayList)

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