桥泰

导航

 

1、 凡是资源都需要进入
SphU.entry
Env.sph.entryWithType
CtSph.entry
CtSph#entryWithPriority
每个资源都必须有一个处理链lookProcessChain(resourceWrapper)
每个资源通过newSlotChain()进行创建,并将旧的添加到链条上,再补上新链条
this.lookProcessChain里面有Map<ResourceWrapper, ProcessorSlotChain> chainMap缓存起来链条
一个chain调用链,chain = SlotChainProvider.newSlotChain();
DefaultSlotChainBuilder#build()创建一堆Slot出来
loadInstanceListSorted()
this.load();
读取META-INF/services/下的文件(sentinel-core包下),加载自定义+默认加载的插槽类
将插槽类实例化为对象
chain.addLast(各个插槽类的实例化对象)

然后Entry e = new CtEntry(resourceWrapper, chain, context);
资源名、链条、上下文都关联起来
CtEntry#setUpEntryFor(context)当前
context.getCurEntry就是入口节点,把当前curEntry,作为父
把自己作为父的儿子,就在链上新增了一节
this.parent = context.getCurEntry();
if (this.parent != null) {
((CtEntry)this.parent).child = this;
}
// 把上下文的curEntry设置为自己
context.setCurEntry(this);

总体思路:上一个curEntry成为了父亲,那么我是不是就是成为了curEntry

进入链条
chain.entry(context, resourceWrapper, (Object)null, count, prioritized, args);
chain其实就是DefaultProcessorSlotChain类的对象

DefaultProcessorSlotChain#entry
找first对象,它是AbstractLinkedProcessorSlot对象

每个链在建立关系时,只记住它的下一个插槽即可
DefaultProcessorSlotChain.first.super.fireEntry()
AbstractLinkedProcessorSlot#fireEntry.this.next.transformEntry()


this.first ===> DefaultProcessorSlotChain
next ===>NodeSelectSlot


 

 

 

官方处理架构图

 

 

 

 

 

 

 

 

com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot#entry
创建一个DefaultNode节点,此node对应key为contextName,而不是资源名称
不同请求链的上下文不一样,不同的入口链路的上下文不一样,同一个入口的DefaultNode是一样的,创建过一次就放入缓存中了
com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot#entry
((DefaultNode)context.getLastNode()).addChild(node);
DefaultNode变成EntranceNode的儿子了
由于node已经变成儿子了,
com.alibaba.csp.sentinel.slots.nodeselector.NodeSelectorSlot#entry
那么当前节点就是上下文的当前节点,代码实现context.setCurNode(node);

进入下一个插槽
this.fireEntry(context, resourceWrapper, node, count, prioritized, args);
插槽为 com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot#entry
它有个成员变量ClusterNode clusterNode = null;
ClusterNode和插槽绑定了,插槽对于资源都是唯一的,每一个资源都有自己的链路,每个插槽都是唯一的,意味着这个插槽的ClusterNode也是唯一的,同一个资源只会有一个ClusterNode

设置node和clusterNode的绑定关系

 

posted on 2025-06-10 16:58  桥泰  阅读(24)  评论(0)    收藏  举报