Search in sources :

Example 6 with CurrentTag

use of com.huawei.route.common.gray.label.entity.CurrentTag 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 7 with CurrentTag

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

the class UpstreamRuleStrategy method getTargetServiceInstance.

@Override
public Optional<Instances> getTargetServiceInstance(List<Route> list, String targetService, Map<String, Collection<String>> headers) {
    if (headers.get(GrayConstant.GRAY_TAG).isEmpty()) {
        return Optional.empty();
    }
    String tagVersion;
    CurrentTag currentTag = JSONObject.parseObject(new ArrayList<String>(headers.get(GrayConstant.GRAY_TAG)).get(0), CurrentTag.class);
    if (currentTag != null && !StringUtils.isBlank(currentTag.getVersion())) {
        tagVersion = currentTag.getVersion();
    } else {
        tagVersion = GrayConstant.GRAY_DEFAULT_VERSION;
    }
    return Optional.ofNullable(AddrCache.getAddr(targetService, RouterUtil.getLdc(headers), tagVersion, null));
}
Also used : CurrentTag(com.huawei.route.common.gray.label.entity.CurrentTag) ArrayList(java.util.ArrayList)

Example 8 with CurrentTag

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

the class ApplicationConfigServiceTest method testGrayConfiguration.

private void testGrayConfiguration() {
    GrayConfiguration grayConfiguration = LabelCache.getLabel(GrayConstant.GRAY_LABEL_CACHE_NAME);
    CurrentTag currentTag = grayConfiguration.getCurrentTag();
    Assert.assertNotNull(currentTag);
    Assert.assertEquals(config.getGrayVersion(), currentTag.getVersion());
    Assert.assertEquals(config.getLdc(), currentTag.getLdc());
}
Also used : GrayConfiguration(com.huawei.route.common.gray.label.entity.GrayConfiguration) CurrentTag(com.huawei.route.common.gray.label.entity.CurrentTag)

Example 9 with CurrentTag

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

the class RouterUtil method getLdc.

/**
 * 获取当前服务的ldc
 *
 * @return LDC
 */
public static String getLdc() {
    GrayConfiguration grayConfiguration = LabelCache.getLabel(CurrentInstance.getInstance().getAppName());
    if (GrayConfiguration.isInValid(grayConfiguration)) {
        return GrayConstant.GRAY_DEFAULT_LDC;
    }
    CurrentTag currentTag = grayConfiguration.getCurrentTag();
    if (currentTag == null || StringUtils.isBlank(currentTag.getLdc())) {
        return GrayConstant.GRAY_DEFAULT_LDC;
    }
    return currentTag.getLdc();
}
Also used : GrayConfiguration(com.huawei.route.common.gray.label.entity.GrayConfiguration) CurrentTag(com.huawei.route.common.gray.label.entity.CurrentTag)

Example 10 with CurrentTag

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

the class RouterUtil method rebuildUrl.

/**
 * 更换旧的URL中的IP和端口,
 *
 * @param host 新的请求地址
 * @param version 灰度后的版本
 * @param request feign请求
 * @return 是否更换成功
 */
public static Request rebuildUrl(String host, String version, Request request) {
    if (StringUtils.isBlank(version) || request == null) {
        return request;
    }
    String[] arr = host.split(COLON);
    if (arr.length != EXPECT_LENGTH) {
        return request;
    }
    final String newUrl = replaceUrl(request.url(), arr[0], Integer.parseInt(arr[1])).toString();
    Map<String, Collection<String>> headers = request.headers();
    // 由于headers有可能是一个UnmodifiableMap,所以只能new一个出来然后putAll
    Map<String, Collection<String>> headerMap = new HashMap<String, Collection<String>>(headers);
    if (!headerMap.containsKey(GrayConstant.GRAY_LDC)) {
        headerMap.put(GrayConstant.GRAY_LDC, Collections.singletonList(getLdc(headers)));
    }
    CurrentTag currentTag = new CurrentTag();
    currentTag.setVersion(version);
    currentTag.setLdc(getLdc(headers));
    headerMap.put(GrayConstant.GRAY_TAG, Collections.singletonList(JSONObject.toJSONString(currentTag)));
    return Request.create(request.method(), newUrl, headerMap, request.body(), request.charset());
}
Also used : HashMap(java.util.HashMap) CurrentTag(com.huawei.route.common.gray.label.entity.CurrentTag) Collection(java.util.Collection)

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