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