public void notify(BitList> invokers) {
String providerApplication = url.getRemoteApplication();
synchronized (this) {
// 刚开始application为null
if (!providerApplication.equals(application)) {
// dubbo-springboot-demo-provider.tag-router
String key = providerApplication + RULE_SUFFIX;
// TreePathDynamicConfiguration#addListener
// pathKey = /dubbo/config/dubbo/dubbo-springboot-demo-provider.tag-router,在pathKey中添加监听器TagStateRouter
this.getRuleRepository().addListener(key, this);
// 添加过就不会再添加
application = providerApplication;
// 从pathKey中获取标签规则
String rawRule = this.getRuleRepository().getRule(key, DynamicConfiguration.DEFAULT_GROUP);
// 触发监听器处理规则
this.process(new ConfigChangedEvent(key, DynamicConfiguration.DEFAULT_GROUP, rawRule));
}
}
}
public StateRouterResult> route(BitList> invokers, URL url,
Invocation invocation, boolean needToPrintMessage) throws RpcException {
// 将标签路由规则进行拷贝,防止变更
final TagRouterRule tagRouterRuleCopy = tagRouterRule;
// 标签规则不可用处理(暂不分析)
if (tagRouterRuleCopy == null || !tagRouterRuleCopy.isValid() || !tagRouterRuleCopy.isEnabled()) {
return new StateRouterResult<>(filterUsingStaticTag(invokers, url, invocation),
needToPrintMessage ? "Disable Tag Router. Reason: tagRouterRule is invalid or disabled" : null);
}
// 获取tag (RpcInvocation#getAttachment),返回tag1
String tag = StringUtils.isEmpty(invocation.getAttachment(TAG_KEY)) ? url.getParameter(TAG_KEY) :
invocation.getAttachment(TAG_KEY);
if (StringUtils.isNotEmpty(tag)) {
// 获取规则中指定tag的provider列表(此时size=1)
List addresses = tagRouterRuleCopy.getTagnameToAddresses().get(tag);
// 过滤后result的size=0
result = filterInvoker(invokers, invoker -> addressMatches(invoker.getUrl(), addresses));
}
}
本篇文章通过demo实践dubbo如何通过标签路由进行流量隔离,并对其中实现进行了分析(不断摩擦,加深理解)
欢迎点赞 收藏 ⭐留言 如有错误敬请指正!