Search in sources :

Example 11 with CurrentTag

use of com.huawei.route.common.gray.label.entity.CurrentTag in project Sermant by huaweicloud.

the class AddrCache method getAddr.

/**
 * 获取路由服务返回的实例
 *
 * @param targetService 服务名
 * @param application 应用服务名
 * @param ldc ldc
 * @param version 版本号
 * @return 实例
 */
public static Instances getAddr(String targetService, String ldc, String version, String application) {
    if (ldc == null || version == null) {
        return new Instances();
    }
    List<Instances> instanceList = getAddrFromCache(targetService);
    if (CollectionUtils.isEmpty(instanceList)) {
        return new Instances();
    }
    Instances[] arr = new Instances[INSTANCE_LENGTH];
    for (Instances instance : instanceList) {
        CurrentTag currentTag = instance.getCurrentTag();
        if (currentTag == null) {
            continue;
        }
        String instanceLdc = instance.getLdc();
        String instanceVersion = application == null ? currentTag.getVersion() : currentTag.getValidVersion(application);
        if (ldc.equals(instanceLdc) && version.equals(instanceVersion)) {
            // 优先返回这个实例
            return instance;
        }
        if (!ldc.equals(instanceLdc) && version.equals(instanceVersion)) {
            // 第二优先级
            arr[0] = instance;
            continue;
        }
        if (ldc.equals(instanceLdc)) {
            // 第三优先级
            arr[1] = instance;
        }
    // 这里的优先级可考虑优化成分数的形式,然后取分数最高的
    }
    if (arr[0] != null) {
        return arr[0];
    }
    if (arr[1] != null) {
        return arr[1];
    }
    return new Instances();
}
Also used : Instances(com.huawei.route.common.gray.addr.entity.Instances) CurrentTag(com.huawei.route.common.gray.label.entity.CurrentTag)

Aggregations

CurrentTag (com.huawei.route.common.gray.label.entity.CurrentTag)11 GrayConfiguration (com.huawei.route.common.gray.label.entity.GrayConfiguration)7 HashMap (java.util.HashMap)3 Instances (com.huawei.route.common.gray.addr.entity.Instances)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 FieldPrivilegedAction (com.huawei.gray.dubbo.utils.FieldPrivilegedAction)1 GrayConfig (com.huawei.route.common.gray.config.GrayConfig)1 Field (java.lang.reflect.Field)1 Collection (java.util.Collection)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ApplicationConfig (org.apache.dubbo.config.ApplicationConfig)1 Microservice (org.apache.servicecomb.service.center.client.model.Microservice)1 MicroserviceInstance (org.apache.servicecomb.service.center.client.model.MicroserviceInstance)1 ServiceInstance (org.springframework.cloud.client.ServiceInstance)1 CachingServiceInstanceListSupplier (org.springframework.cloud.loadbalancer.core.CachingServiceInstanceListSupplier)1 DiscoveryClientServiceInstanceListSupplier (org.springframework.cloud.loadbalancer.core.DiscoveryClientServiceInstanceListSupplier)1 Flux (reactor.core.publisher.Flux)1