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