Search in sources :

Example 1 with GrayConfig

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);
    }
}
Also used : ApplicationConfig(org.apache.dubbo.config.ApplicationConfig) GrayConfig(com.huawei.route.common.gray.config.GrayConfig) HashMap(java.util.HashMap) GrayConfiguration(com.huawei.route.common.gray.label.entity.GrayConfiguration) CurrentTag(com.huawei.route.common.gray.label.entity.CurrentTag)

Example 2 with GrayConfig

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

Aggregations

GrayConfig (com.huawei.route.common.gray.config.GrayConfig)2 GrayConfiguration (com.huawei.route.common.gray.label.entity.GrayConfiguration)2 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 CurrentTag (com.huawei.route.common.gray.label.entity.CurrentTag)1 Route (com.huawei.route.common.gray.label.entity.Route)1 Rule (com.huawei.route.common.gray.label.entity.Rule)1 Request (feign.Request)1 URL (java.net.URL)1 HashMap (java.util.HashMap)1 ApplicationConfig (org.apache.dubbo.config.ApplicationConfig)1