Search in sources :

Example 1 with Instances

use of com.huawei.route.common.gray.addr.entity.Instances in project Sermant by huaweicloud.

the class ServiceInstanceListSupplierServiceImpl method after.

@Override
public void after(Object obj, Method method, Object[] arguments, Object result) {
    String serviceName = "";
    if (obj instanceof DiscoveryClientServiceInstanceListSupplier) {
        serviceName = ((DiscoveryClientServiceInstanceListSupplier) obj).getServiceId();
    } else if (obj instanceof CachingServiceInstanceListSupplier) {
        serviceName = ((CachingServiceInstanceListSupplier) obj).getServiceId();
    } else {
        return;
    }
    Flux<List<ServiceInstance>> fluxinsts = (Flux<List<ServiceInstance>>) result;
    List<Instances> ins = new ArrayList<Instances>();
    List<ServiceInstance> insts = fluxinsts.toIterable().iterator().next();
    for (ServiceInstance inst : insts) {
        Instances in = new Instances();
        in.setIp(inst.getHost());
        in.setServiceName(serviceName);
        in.setPort(inst.getPort());
        Map<String, String> meta = inst.getMetadata();
        CurrentTag currentTag = new CurrentTag();
        currentTag.setRegisterVersion(meta.get(GrayConstant.REG_VERSION_KEY));
        currentTag.setVersion(meta.get(GrayConstant.GRAY_VERSION_KEY));
        currentTag.setLdc(meta.get(GrayConstant.GRAY_LDC_KEY));
        in.setCurrentTag(currentTag);
        ins.add(in);
    }
    Map<String, List<Instances>> instmap = new ConcurrentHashMap<String, List<Instances>>();
    instmap.put(serviceName, ins);
    AddrCache.setCache(instmap);
}
Also used : CachingServiceInstanceListSupplier(org.springframework.cloud.loadbalancer.core.CachingServiceInstanceListSupplier) Flux(reactor.core.publisher.Flux) ArrayList(java.util.ArrayList) CurrentTag(com.huawei.route.common.gray.label.entity.CurrentTag) ServiceInstance(org.springframework.cloud.client.ServiceInstance) Instances(com.huawei.route.common.gray.addr.entity.Instances) DiscoveryClientServiceInstanceListSupplier(org.springframework.cloud.loadbalancer.core.DiscoveryClientServiceInstanceListSupplier) ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with Instances

use of com.huawei.route.common.gray.addr.entity.Instances in project Sermant by huaweicloud.

the class DefaultHttpClientServiceImpl method before.

@Override
public void before(Object obj, Method method, Object[] arguments, BeforeResult beforeResult) throws Exception {
    GrayConfig grayConfig = PluginConfigManager.getPluginConfig(GrayConfig.class);
    Request request = (Request) arguments[0];
    String targetAppName = HostContext.get();
    // 根据灰度规则重构请求地址
    GrayConfiguration grayConfiguration = LabelCache.getLabel(grayConfig.getSpringCloudKey());
    if (GrayConfiguration.isInValid(grayConfiguration)) {
        return;
    }
    // 获得url路径参数解析前的原始path
    URL url = new URL(request.url());
    String path = url.getPath();
    FeignResolvedUrl feignResolvedUrl = PathVarServiceImpl.URL_CONTEXT.get();
    if (feignResolvedUrl != null) {
        try {
            path = path.replace(feignResolvedUrl.getUrl().split("[?]")[0], feignResolvedUrl.getOriginUrl()).split("[?]")[0];
        } finally {
            PathVarServiceImpl.URL_CONTEXT.remove();
        }
    }
    // 获取匹配规则并替换url
    List<Rule> rules = RouterUtil.getValidRules(grayConfiguration, targetAppName, path);
    List<Route> routes = RouterUtil.getRoutes(rules, request);
    RuleType ruleType = CollectionUtils.isEmpty(routes) ? RuleType.UPSTREAM : RuleType.WEIGHT;
    Instances instance = ruleType.getTargetServiceInstance(routes, targetAppName, request.headers());
    if (instance != null) {
        String targetServiceHost = RouterUtil.getTargetHost(instance).orElse(null);
        String version = instance.getCurrentTag().getVersion();
        request = RouterUtil.rebuildUrl(targetServiceHost, version, request);
        arguments[0] = request;
    }
}
Also used : Instances(com.huawei.route.common.gray.addr.entity.Instances) GrayConfig(com.huawei.route.common.gray.config.GrayConfig) Request(feign.Request) GrayConfiguration(com.huawei.route.common.gray.label.entity.GrayConfiguration) FeignResolvedUrl(com.huawei.gray.feign.context.FeignResolvedUrl) RuleType(com.huawei.gray.feign.rule.RuleType) Rule(com.huawei.route.common.gray.label.entity.Rule) URL(java.net.URL) Route(com.huawei.route.common.gray.label.entity.Route)

Example 3 with Instances

use of com.huawei.route.common.gray.addr.entity.Instances 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

Instances (com.huawei.route.common.gray.addr.entity.Instances)3 CurrentTag (com.huawei.route.common.gray.label.entity.CurrentTag)2 FeignResolvedUrl (com.huawei.gray.feign.context.FeignResolvedUrl)1 RuleType (com.huawei.gray.feign.rule.RuleType)1 GrayConfig (com.huawei.route.common.gray.config.GrayConfig)1 GrayConfiguration (com.huawei.route.common.gray.label.entity.GrayConfiguration)1 Route (com.huawei.route.common.gray.label.entity.Route)1 Rule (com.huawei.route.common.gray.label.entity.Rule)1 Request (feign.Request)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)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