一个Context生命周期内Context只能初始化一次,存到ThreadLocal中,并且只有在非NULL时才会进行初始化。如果想在调用SphU.entry()或SphO.entry()前,自定义一个context,则通过ContextUtil.enter()方法来创建。context保存在ThreadLocal中,每次执行的时候会优先到ThreadLocal中获取,为null时会创建一个context。当Entry执行exit方法时,如果entry的parent节点为null,表示当前context中最外层的entry了,此时将threadLocals中的context清空。
3、Entry
每次执行SphU.entry()或SphO.entry()都会返回一个Entry,Entry表示一次资源操作,内部会保存单签invocation信息。在一个context声明周期中多次资源操作,也就是对应多个Entry,parent/child结构保存在Entry实例中,Entry类CtEntry结构如下:
class CtEntry extends Entry {
protected Entry parent = null;
protected Entry child = null;
protected ProcessorSlot chain;
protected Context context;
}
public abstract class Entry implements AutoCloseable {
private long createTime;
private Node curNode;
/**
- {@link Node} of the specific origin, Usually the origin is the Service Consumer.
*/
private Node originNode;
private Throwable error; // 是否出现异常
protected ResourceWrapper resourceWrapper; // 资源信息
}
4、DefaultNode
Node默认实现类DefaultNode,该类还有一个子类EntranceNode;context有一个entranceNode属性,Entry中有一个curNode属性。
-
<