use of com.huawei.route.common.gray.label.entity.GrayConfiguration in project Sermant by huaweicloud.
the class RegistrationServiceImpl method setRegisterVersion.
/**
* 设置自身版本缓存
*
* @param version 版本
*/
@Override
public void setRegisterVersion(String version) {
GrayConfiguration grayConfiguration = LabelCache.getLabel(GrayConstant.GRAY_LABEL_CACHE_NAME);
CurrentTag currentTag = grayConfiguration.getCurrentTag();
currentTag.setRegisterVersion(version);
}
use of com.huawei.route.common.gray.label.entity.GrayConfiguration 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.label.entity.GrayConfiguration in project Sermant by huaweicloud.
the class GrayDynamicConfigListener method process.
@Override
public void process(DynamicConfigEvent event) {
if (event.getKey() == null || !event.getKey().equals(key)) {
return;
}
GrayConfiguration grayConfiguration = LabelCache.getLabel(labelName);
Map<String, List<Rule>> routeRule = new LinkedHashMap<String, List<Rule>>();
if (event.getEventType() == DynamicConfigEventType.DELETE) {
resetGrayConfiguration(grayConfiguration);
return;
}
Yaml yaml = new Yaml();
Map<String, Map<String, Map<String, String>>> load = yaml.load(event.getContent());
if (isInValidMap(load, grayConfiguration)) {
resetGrayConfiguration(grayConfiguration);
return;
}
Map<String, Map<String, String>> servicecomb = load.get(GrayConstant.GRAY_CONFIG_SERVICECOMB_KEY);
if (isInValidMap(servicecomb, grayConfiguration)) {
return;
}
Map<String, String> routeRuleMap = servicecomb.get(GrayConstant.GRAY_CONFIG_ROUTE_RULE_KEY);
if (isInValidMap(routeRuleMap, grayConfiguration)) {
return;
}
Yaml routeRuleYaml = new Yaml();
for (Entry<String, String> entry : routeRuleMap.entrySet()) {
List<Map<String, String>> routeRuleList = routeRuleYaml.load(entry.getValue());
if (CollectionUtils.isEmpty(routeRuleList)) {
continue;
}
Map<String, String> versionFromMap = routeRuleList.get(0);
String versionFrom = versionFromMap.get(GrayConstant.GRAY_CONFIG_VERSION_FROM_KEY);
if (StringUtils.isBlank(versionFrom)) {
grayConfiguration.setVersionFrom(VersionFrom.REGISTER_MSG);
} else {
grayConfiguration.setVersionFrom(VersionFrom.valueOf(versionFrom.toUpperCase(Locale.ROOT)));
routeRuleList.remove(0);
}
List<Rule> list = JSONArray.parseArray(JSONObject.toJSONString(routeRuleList), Rule.class);
if (CollectionUtils.isEmpty(list)) {
continue;
}
routeRule.put(entry.getKey(), list);
}
grayConfiguration.setRouteRule(routeRule);
grayConfiguration.setOn(!CollectionUtils.isEmpty(routeRule));
grayConfiguration.setValid(!CollectionUtils.isEmpty(routeRule));
LOGGER.info(String.format(Locale.ROOT, "Config [%s] has been %s ", event.getKey(), event.getEventType()));
}
use of com.huawei.route.common.gray.label.entity.GrayConfiguration 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.GrayConfiguration 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();
}
Aggregations