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);
}
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));
}
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());
}
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();
}
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());
}
Aggregations