【sentinel】深入浅出之原理篇实体模型

Sentinel中,有一些概念需要搞懂,不然很多点理解起来比较困难。
这里写得很粗,详细可看逅弈大佬的Sentinel 原理-实体类

  • Resource
    资源,具体代表被限流的方法或者字符串,屏蔽了具体的细节,可以是方法名,或者是url,也可以自定义对应的名称
  • Node
    保存节点数据请求参数,QPS等其他信息
  • Entry
    入口凭证,拿到Entry代表未限流
  • Context
    上下文,执行环境

Resource说明

resource定义主要声明了资源的名称和类型,类型有两种,IN,OUT,分别代表调入或者调出。例如:A->B->C,对于B来讲,A的请求类型为IN,C的请求为OUT。
在Sentinel中,resource实现有两种

  • MethodResourceWrapper
    方法的资源包装类,对应资源是Method
  • StringResourceWrapper
    字符串的资源包装类,对应资源为String
public abstract class ResourceWrapper {
  protected String name;
  protected EntryType type = EntryType.OUT;
public class MethodResourceWrapper extends ResourceWrapper {

    private transient Method method;

    public MethodResourceWrapper(Method method, EntryType type) {
        this.method = method;
        this.name = MethodUtil.resolveMethodName(method);
        this.type = type;
    }
}
public class StringResourceWrapper extends ResourceWrapper {

    public StringResourceWrapper(String name, EntryType type) {
        if (name == null) {
            throw new IllegalArgumentException("Resource name cannot be null");
        }
        this.name = name;
        this.type = type;
    }
}

Node说明

Node 中保存了资源的实时统计数据,例如:passQps,blockQps,rt等实时数据。正是有了这些统计数据后, Sentinel 才能进行限流、降级等一系列的操作。


public interface Node {
    long totalRequest();
    long totalSuccess();
    long blockRequest();
    long totalException();
    long passQps();
    long blockQps();
    long totalQps();
    long successQps();
    long maxSuccessQps();
    long exceptionQps();
    long avgRt();
    long minRt();
    int curThreadNum();
    long previousBlockQps();
    long previousPassQps();
    Map<Long, MetricNode> metrics();
    void addPassRequest(int count);
    void addRtAndSuccess(long rt, int success);
    void increaseBlockQps(int count);
    void increaseExceptionQps(int count);
    void increaseThreadNum();
    void decreaseThreadNum();
    void reset();
    void debug();
}

而Node的实现类有以下四种:

  • StatisticNode
    基础节点
  • DefaultNode
    默认Node节点
  • ClusterNode
  • EntranceNode
    Node入口节点,上下文初始化的时候加载出来的,作为入口初始化的Node,全局唯一。
    node.png

Entry说明

入口凭证,在SphU#entry()调用后,就会返回一个Entry,代表可以请求。Entry包含一个 Resource资源,originNode原始节点,curNode当前节点,createTime创建时间

public abstract class Entry implements AutoCloseable {
   private static final Object[] OBJECTS0 = new Object[0];
   private long createTime;
   private Node curNode;
   private Node originNode;
   private Throwable error;
   protected ResourceWrapper resourceWrapper;
}

实现类CtEntry

class CtEntry extends Entry {
    protected Entry parent = null;
    protected Entry child = null;
    protected ProcessorSlot<Object> chain;
    protected Context context;
}

Context上下文

上下文,记录当前执行环境的一些配置和执行情况。

public class Context {

   private final String name;

   private DefaultNode entranceNode;//当前调用链的入口节点

   private Entry curEntry;//当前调用链的当前entry

   private String origin = "";//当前调用链的调用源

   private final boolean async;
}

Metric度量信息 主要是滑动窗口实现,统计数据


public interface Metric {
    long success();
    long maxSuccess();
    long exception();
    long block();
    long pass();
    long rt();
    long minRt();
    List<MetricNode> details();
    MetricBucket[] windows();
    void addException(int n);
    void addBlock(int n);
    void addSuccess(int n);
    void addPass(int n);
    void addRT(long rt);
    double getWindowIntervalInSec();
    int getSampleCount();
    void debugQps();
    long previousWindowBlock();
    long previousWindowPass();
}

实现类:

public class ArrayMetric implements Metric {
    private final MetricsLeapArray data;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容