Search in sources :

Example 1 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(DubboCache.getLabelName());
    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)

Example 2 with GrayConfiguration

use of com.huawei.route.common.gray.label.entity.GrayConfiguration 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 3 with GrayConfiguration

use of com.huawei.route.common.gray.label.entity.GrayConfiguration in project Sermant by huaweicloud.

the class RegistrationServiceImpl method getVersion.

/**
 * 拦截点前执行
 *
 * @param obj 增强的类
 * @param arguments 增强方法的所有参数
 */
@Override
public void getVersion(Object obj, Object[] arguments) throws NoSuchFieldException, IllegalAccessException {
    if (arguments.length <= 2 || !(arguments[2] instanceof List<?>)) {
        return;
    }
    Field field = obj.getClass().getDeclaredField("microservice");
    AccessController.doPrivileged(new FieldPrivilegedAction(field));
    Microservice microservice = (Microservice) field.get(obj);
    GrayConfiguration grayConfiguration = LabelCache.getLabel(DubboCache.getLabelName());
    CurrentTag currentTag = grayConfiguration.getCurrentTag();
    currentTag.setRegisterVersion(microservice.getVersion());
    List<MicroserviceInstance> instances = (List<MicroserviceInstance>) arguments[2];
    for (MicroserviceInstance instance : instances) {
        for (String endpoint : instance.getEndpoints()) {
            AddrCache.setRegisterVersionCache(endpoint.substring(GrayConstant.DUBBO_PREFIX.length()), instance.getVersion());
        }
    }
}
Also used : Field(java.lang.reflect.Field) Microservice(org.apache.servicecomb.service.center.client.model.Microservice) GrayConfiguration(com.huawei.route.common.gray.label.entity.GrayConfiguration) CurrentTag(com.huawei.route.common.gray.label.entity.CurrentTag) MicroserviceInstance(org.apache.servicecomb.service.center.client.model.MicroserviceInstance) List(java.util.List) FieldPrivilegedAction(com.huawei.gray.dubbo.utils.FieldPrivilegedAction)

Example 4 with GrayConfiguration

use of com.huawei.route.common.gray.label.entity.GrayConfiguration in project Sermant by huaweicloud.

the class AbstractDirectoryServiceImpl method selectInvokers.

/**
 * 筛选灰度invoker
 *
 * @param obj RegistryDirectory
 * @param arguments 参数
 * @param result invokers
 * @return invokers
 * @see com.alibaba.dubbo.registry.integration.RegistryDirectory
 * @see org.apache.dubbo.registry.integration.RegistryDirectory
 * @see com.alibaba.dubbo.rpc.Invoker
 * @see org.apache.dubbo.rpc.Invoker
 */
@Override
public Object selectInvokers(Object obj, Object[] arguments, Object result) {
    if (arguments != null && arguments.length > 0) {
        GrayConfiguration grayConfiguration = LabelCache.getLabel(GrayConstant.GRAY_LABEL_CACHE_NAME);
        if (GrayConfiguration.isInValid(grayConfiguration)) {
            return result;
        }
        Map<String, String> queryMap = ReflectUtils.getQueryMap(obj);
        if (CollectionUtils.isEmpty(queryMap)) {
            return result;
        }
        if (!CONSUMER_VALUE.equals(queryMap.get(CONSUMER_KEY))) {
            return result;
        }
        String serviceInterface = queryMap.get(INTERFACE_KEY);
        Object invocation = arguments[0];
        String interfaceName = serviceInterface + "." + ReflectUtils.getMethodName(invocation);
        String targetService = DubboCache.INSTANCE.getApplication(serviceInterface);
        List<Rule> rules = RouterUtil.getValidRules(grayConfiguration, targetService, interfaceName);
        List<Route> routes = RouterUtil.getRoutes(rules, ReflectUtils.getArguments(invocation));
        if (CollectionUtils.isEmpty(routes)) {
            return result;
        }
        return RuleStrategyHandler.WEIGHT.getTargetInvoker(routes, (List<Object>) result, grayConfiguration.getVersionFrom());
    }
    return result;
}
Also used : GrayConfiguration(com.huawei.route.common.gray.label.entity.GrayConfiguration) Rule(com.huawei.route.common.gray.label.entity.Rule) Route(com.huawei.route.common.gray.label.entity.Route)

Example 5 with GrayConfiguration

use of com.huawei.route.common.gray.label.entity.GrayConfiguration in project Sermant by huaweicloud.

the class ApplicationConfigServiceImpl method getName.

/**
 * 获取dubbo服务名,并设置灰度参数
 *
 * @param obj 增强的类
 * @see com.alibaba.dubbo.config.ApplicationConfig
 * @see org.apache.dubbo.config.ApplicationConfig
 */
@Override
public void getName(Object obj) {
    String name = ReflectUtils.getName(obj);
    if (StringUtils.isBlank(name)) {
        return;
    }
    DubboCache.INSTANCE.setAppName(name);
    String version = grayConfig.getGrayVersion(GrayConstant.GRAY_DEFAULT_VERSION);
    Map<String, String> versionMap = new HashMap<>();
    versionMap.put(GrayConstant.GRAY_VERSION_KEY, version);
    String ldc = grayConfig.getLdc(GrayConstant.GRAY_DEFAULT_LDC);
    versionMap.put(GrayConstant.GRAY_LDC_KEY, ldc);
    Map<String, String> parameters = ReflectUtils.getParameters(obj);
    if (parameters == null) {
        ReflectUtils.setParameters(obj, versionMap);
    } else {
        parameters.putAll(versionMap);
    }
    GrayConfiguration grayConfiguration = LabelCache.getLabel(GrayConstant.GRAY_LABEL_CACHE_NAME);
    CurrentTag currentTag = grayConfiguration.getCurrentTag();
    if (currentTag == null) {
        currentTag = new CurrentTag();
    }
    currentTag.setVersion(version);
    currentTag.setLdc(ldc);
    grayConfiguration.setCurrentTag(currentTag);
}
Also used : HashMap(java.util.HashMap) 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