Search in sources :

Example 6 with GrayConfiguration

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);
}
Also used : GrayConfiguration(com.huawei.route.common.gray.label.entity.GrayConfiguration) CurrentTag(com.huawei.route.common.gray.label.entity.CurrentTag)

Example 7 with GrayConfiguration

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;
    }
}
Also used : Instances(com.huawei.route.common.gray.addr.entity.Instances) GrayConfig(com.huawei.route.common.gray.config.GrayConfig) Request(feign.Request) GrayConfiguration(com.huawei.route.common.gray.label.entity.GrayConfiguration) FeignResolvedUrl(com.huawei.gray.feign.context.FeignResolvedUrl) RuleType(com.huawei.gray.feign.rule.RuleType) Rule(com.huawei.route.common.gray.label.entity.Rule) URL(java.net.URL) Route(com.huawei.route.common.gray.label.entity.Route)

Example 8 with GrayConfiguration

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()));
}
Also used : GrayConfiguration(com.huawei.route.common.gray.label.entity.GrayConfiguration) Yaml(org.yaml.snakeyaml.Yaml) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) Rule(com.huawei.route.common.gray.label.entity.Rule) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 9 with GrayConfiguration

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());
}
Also used : GrayConfiguration(com.huawei.route.common.gray.label.entity.GrayConfiguration) CurrentTag(com.huawei.route.common.gray.label.entity.CurrentTag)

Example 10 with GrayConfiguration

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();
}
Also used : GrayConfiguration(com.huawei.route.common.gray.label.entity.GrayConfiguration) CurrentTag(com.huawei.route.common.gray.label.entity.CurrentTag)

Aggregations

GrayConfiguration (com.huawei.route.common.gray.label.entity.GrayConfiguration)12 CurrentTag (com.huawei.route.common.gray.label.entity.CurrentTag)7 Rule (com.huawei.route.common.gray.label.entity.Rule)4 Route (com.huawei.route.common.gray.label.entity.Route)3 GrayConfig (com.huawei.route.common.gray.config.GrayConfig)2 HashMap (java.util.HashMap)2 List (java.util.List)2 RuleStrategyEnum (com.huawei.gray.dubbo.strategy.RuleStrategyEnum)1 FieldPrivilegedAction (com.huawei.gray.dubbo.utils.FieldPrivilegedAction)1 FeignResolvedUrl (com.huawei.gray.feign.context.FeignResolvedUrl)1 RuleType (com.huawei.gray.feign.rule.RuleType)1 Instances (com.huawei.route.common.gray.addr.entity.Instances)1 Request (feign.Request)1 Field (java.lang.reflect.Field)1 URL (java.net.URL)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 URL (org.apache.dubbo.common.URL)1 ApplicationConfig (org.apache.dubbo.config.ApplicationConfig)1 Invocation (org.apache.dubbo.rpc.Invocation)1