use of com.huawei.route.common.gray.label.entity.Route in project Sermant by huaweicloud.
the class WeightRuleStrategy method getTargetInvoker.
@Override
public List<Invoker<?>> getTargetInvoker(List<Route> routes, Invocation invocation, List<Invoker<?>> invokers, VersionFrom versionFrom) {
if (routes.get(0).getWeight() == null) {
// 规定第一个规则的流量为空,则设置为100
routes.get(0).setWeight(ONO_HUNDRED);
}
String targetVersion = null;
Set<String> notMatchVersions = new HashSet<String>();
int begin = 1;
int num = new Random().nextInt(ONO_HUNDRED) + 1;
for (Route route : routes) {
@SuppressWarnings("checkstyle:RegexpSingleline") Integer weight = route.getWeight();
if (weight == null) {
continue;
}
String currentVersion = route.getTags().getVersion();
if (num >= begin && num <= begin + weight - 1) {
targetVersion = currentVersion;
break;
}
begin += weight;
notMatchVersions.add(currentVersion);
}
VersionStrategy versionStrategy = VersionChooser.INSTANCE.choose(versionFrom);
InvokerStrategy invokerStrategy = InvokerChooser.INSTANCE.choose(targetVersion);
List<Invoker<?>> resultList = new ArrayList<Invoker<?>>();
for (Invoker<?> invoker : invokers) {
if (invokerStrategy.isMatch(invoker, targetVersion, notMatchVersions, versionStrategy)) {
resultList.add(invoker);
}
}
return CollectionUtils.isEmpty(resultList) ? invokers : resultList;
}
use of com.huawei.route.common.gray.label.entity.Route 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.Route in project Sermant by huaweicloud.
the class RouterUtil method getValidRules.
/**
* 获取有效规则
*
* @param grayConfiguration 灰度配置
* @param targetService 目标服务
* @param path 请求路径
* @return 有效规则
*/
public static List<Rule> getValidRules(GrayConfiguration grayConfiguration, String targetService, String path) {
if (GrayConfiguration.isInValid(grayConfiguration)) {
return Collections.emptyList();
}
Map<String, List<Rule>> routeRule = grayConfiguration.getRouteRule();
if (CollectionUtils.isEmpty(routeRule) || CollectionUtils.isEmpty(routeRule.get(targetService))) {
return Collections.emptyList();
}
List<Rule> list = new ArrayList<Rule>();
for (Rule rule : routeRule.get(targetService)) {
if (!isValidRule(rule, CurrentInstance.getInstance().getAppName(), path)) {
continue;
}
// 去掉无效的规则
Map<String, List<MatchRule>> headerRules = rule.getMatch().getHeaders();
if (headerRules != null) {
removeInValidRules(headerRules);
rule.getMatch().setHeaders(headerRules);
}
Map<String, List<MatchRule>> paramRules = rule.getMatch().getParameters();
if (paramRules != null) {
removeInValidRules(paramRules);
rule.getMatch().setParameters(paramRules);
}
Map<String, List<MatchRule>> cookieRules = rule.getMatch().getCookie();
if (cookieRules != null) {
removeInValidRules(cookieRules);
rule.getMatch().setCookie(cookieRules);
}
// 去掉无效的路由
Iterator<Route> routeIterator = rule.getRoute().iterator();
while (routeIterator.hasNext()) {
if (isInValidRoute(routeIterator.next())) {
routeIterator.remove();
}
}
list.add(rule);
}
Collections.sort(list, new Comparator<Rule>() {
@Override
public int compare(Rule o1, Rule o2) {
return o1.getPrecedence() - o2.getPrecedence();
}
});
return list;
}
use of com.huawei.route.common.gray.label.entity.Route in project Sermant by huaweicloud.
the class WeightRuleStrategy method getTargetServiceInstance.
@Override
public Optional<Instances> getTargetServiceInstance(List<Route> list, String targetService, Map<String, Collection<String>> headers) {
Map<String, List<Instances>> map = AddrCache.getAddr(targetService, null);
if (CollectionUtils.isEmpty(map)) {
return Optional.empty();
}
// 剔除不合法的路由规则,和不在地址列表中的版本应用地址
Iterator<Route> iterator = list.iterator();
while (iterator.hasNext()) {
Route route = iterator.next();
if (route.getTags() == null || !map.containsKey(route.getTags().getVersion())) {
iterator.remove();
}
}
if (CollectionUtils.isEmpty(map)) {
return Optional.empty();
}
if (list.get(0).getWeight() == null) {
// 规定第一个规则不为空,则设置为100
list.get(0).setWeight(ONO_HUNDRED);
}
int begin = 1;
int num = new Random().nextInt(ONO_HUNDRED) + 1;
for (Route route : list) {
Integer weight = route.getWeight();
if (weight == null) {
continue;
}
String tagVersion = route.getTags().getVersion();
if (num >= begin && num <= begin + weight - 1) {
return Optional.of(map.get(tagVersion).get(0));
}
begin += weight;
}
return Optional.empty();
}
use of com.huawei.route.common.gray.label.entity.Route 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;
}
}
Aggregations