use of com.huawei.gray.feign.rule.RuleType 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;
}
}
Aggregations