use of com.huawei.route.common.gray.config.GrayConfig in project Sermant by huaweicloud.
the class InterfaceConfigServiceImpl method getName.
@Override
public void getName(Object result) {
if (result instanceof ApplicationConfig) {
ApplicationConfig config = (ApplicationConfig) result;
GrayConfig grayConfig = PluginConfigManager.getPluginConfig(GrayConfig.class);
String version = grayConfig.getGrayVersion(GrayConstant.GRAY_DEFAULT_VERSION);
Map<String, String> versionMap = new HashMap<String, String>();
versionMap.put(GrayConstant.GRAY_VERSION_KEY, version);
Map<String, String> parameters = config.getParameters();
if (parameters == null) {
config.setParameters(versionMap);
parameters = config.getParameters();
} else if (parameters.get(GrayConstant.GRAY_VERSION_KEY) == null) {
config.getParameters().putAll(versionMap);
}
String ldc = grayConfig.getLdc(GrayConstant.GRAY_DEFAULT_LDC);
parameters.put(GrayConstant.GRAY_LDC_KEY, ldc);
DubboCache.setAppName(config.getName());
GrayConfiguration grayConfiguration = LabelCache.getLabel(DubboCache.getLabelName());
CurrentTag currentTag = grayConfiguration.getCurrentTag();
if (currentTag == null) {
currentTag = new CurrentTag();
}
currentTag.setVersion(parameters.get(GrayConstant.GRAY_VERSION_KEY));
currentTag.setLdc(ldc);
grayConfiguration.setCurrentTag(currentTag);
}
}
use of com.huawei.route.common.gray.config.GrayConfig 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