聊聊sentinel的DefaultSlotsChainBuilder

本文主要研究一下sentinel的DefaultSlotsChainBuilder

DefaultSlotsChainBuilder

com/alibaba/csp/sentinel/slots/DefaultSlotsChainBuilder.java

public class DefaultSlotsChainBuilder implements SlotsChainBuilder {

    @Override
    public ProcessorSlotChain build() {
        ProcessorSlotChain chain = new DefaultProcessorSlotChain();
        chain.addLast(new NodeSelectorSlot());
        chain.addLast(new ClusterBuilderSlot());
        chain.addLast(new LogSlot());
        chain.addLast(new StatisticSlot());
        chain.addLast(new SystemSlot());
        chain.addLast(new AuthoritySlot());
        chain.addLast(new FlowSlot());
        chain.addLast(new DegradeSlot());

        return chain;
    }

}
  • 实现build方法,返回DefaultProcessorSlotChain,通过它addLast方法构造链式调用

DefaultProcessorSlotChain

com/alibaba/csp/sentinel/slotchain/DefaultProcessorSlotChain.java

public class DefaultProcessorSlotChain extends ProcessorSlotChain {

    AbstractLinkedProcessorSlot first = new AbstractLinkedProcessorSlot() {

        @Override
        public void entry(Context context, ResourceWrapper resourceWrapper, Object t, int count, Object... args)
            throws Throwable {
            super.fireEntry(context, resourceWrapper, t, count, args);
        }

        @Override
        public void exit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) {
            super.fireExit(context, resourceWrapper, count, args);
        }

    };
    AbstractLinkedProcessorSlot end = first;

    @Override
    public void addFirst(AbstractLinkedProcessorSlot protocolProcessor) {
        protocolProcessor.setNext(first.getNext());
        first.setNext(protocolProcessor);
        if (end == first) {
            end = protocolProcessor;
        }
    }

    @Override
    public void addLast(AbstractLinkedProcessorSlot protocolProcessor) {
        end.setNext(protocolProcessor);
        end = protocolProcessor;
    }

    /**
     * Same as {@link #addLast(AbstractLinkedProcessorSlot)}.
     *
     * @param next processor to be added.
     */
    @Override
    public void setNext(AbstractLinkedProcessorSlot next) {
        addLast(next);
    }

    @Override
    public AbstractLinkedProcessorSlot getNext() {
        return first.getNext();
    }

    @Override
    public void entry(Context context, ResourceWrapper resourceWrapper, Object t, int count, Object... args)
        throws Throwable {
        first.transformEntry(context, resourceWrapper, t, count, args);
    }

    @Override
    public void exit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) {
        first.exit(context, resourceWrapper, count, args);
    }

} 
 
  • 主要有两个属性,一个是first,一个是end,它们是AbstractLinkedProcessorSlot类型
  • 方法主要有addFirst,addLast,setNext
  • 这里默认设置了first,之后主要是通过getNext方法来开启链式调用,getNext方法返回的是first的next

AbstractLinkedProcessorSlot

com/alibaba/csp/sentinel/slotchain/AbstractLinkedProcessorSlot.java

public abstract class AbstractLinkedProcessorSlot implements ProcessorSlot {

    private AbstractLinkedProcessorSlot next = null;

    @Override
    public void fireEntry(Context context, ResourceWrapper resourceWrapper, Object obj, int count, Object... args)
        throws Throwable {
        if (next != null) {
            next.transformEntry(context, resourceWrapper, obj, count, args);
        }
    }

    @SuppressWarnings("unchecked")
    void transformEntry(Context context, ResourceWrapper resourceWrapper, Object o, int count, Object... args)
        throws Throwable {
        T t = (T)o;
        entry(context, resourceWrapper, t, count, args);
    }

    @Override
    public void fireExit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) {
        if (next != null) {
            next.exit(context, resourceWrapper, count, args);
        }
    }

    public AbstractLinkedProcessorSlot getNext() {
        return next;
    }

    public void setNext(AbstractLinkedProcessorSlot next) {
        this.next = next;
    }

}
  • AbstractLinkedProcessorSlot是所有slot的基类,有个next属性,用来标识下一个slot
  • entry是整个链式调用的入口,默认是会触发fireEntry,通过判断next不为null,则调用next的transformEntry,进而调用next的entry方法
  • 子类的entry方法会做一些前置校验工作,通过之后调用fireEntry方法,进而完成整个链式调用

小结

sentinel使用了责任链模式来进行各种slot逻辑的执行,其构建是通过DefaultSlotsChainBuilder来进行build。

doc

  • slotchain

你可能感兴趣的:(sentinel)