Sentinel 的核心骨架,将不同的 Slot 按照顺序串在一起(责任链模式),从而将不同的功能(限流、降级、系统保护)组合在一起。slot chain 其实可以分为两部分:统计数据构建部分(statistic)和判断部分(rule checking)。核心结构:

业务埋点示例
1// 资源的唯一标识 2String resourceName = "testSentinel"; 3Entry entry = null; 4String retVal; 5try { 6 entry = SphU.entry(resourceName, EntryType.IN); 7 // TODO 业务逻辑 8 retVal = "passed"; 9} catch (BlockException e) { 10 // TODO 降级逻辑 11 retVal = "blocked"; 12} catch (Exception e) { 13 // 异常数统计埋点 14 Tracer.trace(e); 15 throw new RuntimeException(e); 16} finally { 17 if (entry != null) { 18 entry.exit(); 19 } 20}
这段代码是Sentinel业务埋点示例,通过示例我们可以看出Sentinel对资源的控制入口是SphU.entry(resourceName, EntryType.IN);,源码如下:
1public static Entry entry(String name, EntryType type) throws BlockException { 2 return Env.sph.entry(name, type, 1, OBJECTS0); 3}
这里第一个参数是受保护资源的唯一名称;第二个参数表示流量类型:
EntryType.IN:是指进入我们系统的入口流量,比如 http 请求或者是其他的 rpc 之类的请求,设置为IN主要是为了保护自己系统。EntryType.OUT:是指我们系统调用其他第三方服务的出口流量,设置为OUT是为了保护第三方系统。
这段代码没什么逻辑,只是转发了下,跟进源码可以发现最终逻辑实在CtSph#entryWithPriority(ResourceWrapper, int, boolean, Object...)方法中。
Sentinel 骨架代码
Sentinel的核心是资源,这里的资源可以是任何东西,服务,服务里的方法,甚至是一段代码。而SphU.entry(resourceName);这段代码的主要作用是 :
- 定义一个Sentinel资源
- 检验资源所对应的规则是否生效
核心代码如下:
1private Entry entryWithPriority(ResourceWrapper resourceWrapper, int count, boolean prioritized, Object... args) 2 throws BlockException { 3 // 获取当前线程上下文,Context是通过ThreadLocal维护,每一个Context都会有一个EntranceNode实例,它是dashboard【簇点链路】中的根节点,主要是用来区分调用链路的 4 Context context = ContextUtil.getContext(); 5 if (context instanceof NullContext) { 6 // 如果是 NullContext,表示 Context 个数超过了阈值,这个时候 Sentinel 不会应用规则,即不会触发限流降级等规则,也不会触发QPS等数据统计。 7 // 阈值大小 =Constants.MAX_CONTEXT_NAME_SIZE = 2000,具体可以查看 ContextUtil#trueEnter。 8 return new CtEntry(resourceWrapper, null, context); 9 } 10 11 if (context == null) { 12 // 如果没有设置上下文,即使用默认上下文,默认上下文的名称是 sentinel_default_context 13 context = InternalContextUtil.internalEnter(Constants.CONTEXT_DEFAULT_NAME); 14 } 15 16 if (!Constants.ON) { 17 // Sentinel 的全局控制开关,一旦关闭则不进行任何检查 18 return new CtEntry(resourceWrapper, null, context); 19 } 20 21 // 通过Sentinel的官方文档我们可以知道,Sentinel的核心功能是基于一系列的功能插槽来实现的,而组织这些功能插槽使用的是责任链模式。 22 // 这里是通过资源(每个资源是唯一的),获取第一个功能插,即该资源对应的规则入口。 23 ProcessorSlot<Object> chain = lookProcessChain(resourceWrapper); 24 25 /* 26 * Means amount of resources (slot chain) exceeds {@link Constants.MAX_SLOT_CHAIN_SIZE}, 27 * so no rule checking will be done. 28 */ 29 // 如果一个服务中,资源数量操过阈值(最大的插槽链),则返回null,即不会再应用规则,直接返回。 30 // 阈值大小 = Constants.MAX_SLOT_CHAIN_SIZE = 6000 31 if (chain == null) { 32 return new CtEntry(resourceWrapper, null, context); 33 } 34 35 // 构建Sentinel调用链入口 36 Entry e = new CtEntry(resourceWrapper, chain, context); 37 try { 38 // 开始执行插槽链,如果某个插槽匹配上了某个规则,如限流规则,就会抛出BlockException异常,这时表示请求被拒绝了。 39 // 业务层面会去捕获这个异常,然后做熔断,降级操作。 40 chain.entry(context, resourceWrapper, null, count, prioritized, args); 41 } catch (BlockException e1) { 42 e.exit(count, args); 43 throw e1; 44 } catch (Throwable e1) { 45 // Sentinel内部异常 46 RecordLog.info("Sentinel unexpected exception", e1); 47 } 48 return e; 49}
核心逻辑如下:
- 通过当前线程的上下文,获取到当前线程的【簇点链路】入口。
- 判断全局开关是否关闭。
- 通过唯一的资源标识获取到对应的功能插槽链(ProcessorSlot)的第一个插槽。
- 构建Sentinel调用链入口,并执行调用链
- 如果抛出BlockException表示触发了资源限制规则,需要进行熔断降级。
这里有两个需要注意的地方:
- 【簇点链路】入口Context的数量是有限制的,最大2000个,通常情况下,我们都不需要显示设置 context,使用默认的就好了,这样Context数量限制基本上不会触发。
SphU.entry(resourceName, EntryType.IN),这里的资源的唯一标识resourceName也是有限制的,最大是6000。当Sentinel与 Servlet 的整合后,CommonFilter会将所有的对外接口定义成Sentinel的资源,资源名称就是接口地址,所以要控制好服务接口数量。

ContextUtil#enter
ContextUtil#enter(String name, String origin)的主要作用就是创建当前线程的上下文Context,每个上下文会对应一个EntranceNode(入口节点)实例,通常情况下我们不需要显示调用该方法。
name:上下文的唯一标识,也是入口节点的资源名称。orgin:表示来源,通常是服务消费者或调用者的应用名称,当我们需要对不同来源的消费者或调用者进行限制时就会用到这个参数。
源码如下:
1public static Context enter(String name, String origin) { 2 if (Constants.CONTEXT_DEFAULT_NAME.equals(name)) { 3 throw new ContextNameDefineException( 4 "The " + Constants.CONTEXT_DEFAULT_NAME + " can't be permit to defined!"); 5 } 6 return trueEnter(name, origin); 7} 8 9protected static Context trueEnter(String name, String origin) { 10 // 通过ThreadLocal获取当前线程的上下文 11 Context context = contextHolder.get(); 12 // 如果没获取到需要新创建一个上下文 13 if (context == null) { 14 Map<String, DefaultNode> localCacheNameMap = contextNameNodeMap; 15 // 根据上下文名称获取入口节点 16 DefaultNode node = localCacheNameMap.get(name); 17 // 入口节点节点也为空需要新创建入口节点 18 if (node == null) { 19 // 判断是否超过最大长度限制(乐观锁机制) 20 if (localCacheNameMap.size() > Constants.MAX_CONTEXT_NAME_SIZE) { 21 setNullContext(); 22 return NULL_CONTEXT; 23 } else { 24 try { 25 LOCK.lock(); 26 // 双重判断 27 node = contextNameNodeMap.get(name); 28 if (node == null) { 29 if (contextNameNodeMap.size() > Constants.MAX_CONTEXT_NAME_SIZE) { 30 setNullContext(); 31 return NULL_CONTEXT; 32 } else { 33 // 新建入口节点 34 node = new EntranceNode(new StringResourceWrapper(name, EntryType.IN), null); 35 // 将入口节点添加到全局根节点下(machine-root) 36 Constants.ROOT.addChild(node); 37 // 类似写复制容器机制 38 Map<String, DefaultNode> newMap = new HashMap<>(contextNameNodeMap.size() + 1); 39 newMap.putAll(contextNameNodeMap); 40 newMap.put(name, node); 41 contextNameNodeMap = newMap; 42 } 43 } 44 } finally { 45 LOCK.unlock(); 46 } 47 } 48 } 49 context = new Context(node, name); 50 context.setOrigin(origin); 51 contextHolder.set(context); 52 } 53 54 return context; 55}
如果我们再代码中显示调用这个方法:
1ContextUtil.enter("context1", "service-1"); 2... 3ContextUtil.exit(); 4 5ContextUtil.enter("context2", "service-1"); 6... 7ContextUtil.exit();
那么会创建如下一个树结构图:

这里有两点需要注意:
- 也就是上面说的数量限制,2000。
- ContextUtil是通过ThreadLocal来维护当前线程的上下文的,所以当遇到异步线程时需要手动调用
ContextUtil.runOnContext(context, f)方法来完成父线程和子线程的上下文切换。
文档中的Demo:
1public void someAsync() { 2 try { 3 AsyncEntry entry = SphU.asyncEntry(resourceName); 4 5 // Asynchronous invocation. 6 doAsync(userId, result -> { 7 // 在异步回调中进行上下文变换,通过 AsyncEntry 的 getAsyncContext 方法获取异步 Context 8 ContextUtil.runOnContext(entry.getAsyncContext(), () -> { 9 try { 10 // 此处嵌套正常的资源调用. 11 handleResult(result); 12 } finally { 13 entry.exit(); 14 } 15 }); 16 }); 17 } catch (BlockException ex) { 18 // Request blocked. 19 // Handle the exception (e.g. retry or fallback). 20 } 21}
lookProcessChain
Sentinel的核心功能是使用的是责任链模式实现,lookProcessChain(resourceWrapper)的主要作用就是用来构造责任链,源码如下:
1ProcessorSlot<Object> lookProcessChain(ResourceWrapper resourceWrapper) { 2 // 根据资源的唯一标识来做本地缓存 3 ProcessorSlotChain chain = chainMap.get(resourceWrapper); 4 if (chain == null) { 5 synchronized (LOCK) { 6 chain = chainMap.get(resourceWrapper); 7 if (chain == null) { 8 // 限制资源资对应调用链的总数,一个资源对应一条调用链 9 if (chainMap.size() >= Constants.MAX_SLOT_CHAIN_SIZE) { 10 return null; 11 } 12 // 构建一个新的插槽链 13 chain = SlotChainProvider.newSlotChain(); 14 // 写复制容器做法 15 Map<ResourceWrapper, ProcessorSlotChain> newMap = new HashMap<ResourceWrapper, ProcessorSlotChain>( 16 chainMap.size() + 1); 17 newMap.putAll(chainMap); 18 newMap.put(resourceWrapper, chain); 19 chainMap = newMap; 20 } 21 } 22 } 23 return chain; 24}
进一步跟进方法会发现,责任链是由SlotChainBuilder#build()````去构建的,默认实现类是DefaultSlotChainBuilder```,源码如下:
1public class DefaultSlotChainBuilder implements SlotChainBuilder { 2 3 @Override 4 public ProcessorSlotChain build() { 5 ProcessorSlotChain chain = new DefaultProcessorSlotChain(); 6 7 // 找到ProcessorSlot所有的实现类,并排序 8 List<ProcessorSlot> sortedSlotList = SpiLoader.loadPrototypeInstanceListSorted(ProcessorSlot.class); 9 for (ProcessorSlot slot : sortedSlotList) { 10 if (!(slot instanceof AbstractLinkedProcessorSlot)) { 11 RecordLog.warn("The ProcessorSlot(" + slot.getClass().getCanonicalName() + ") is not an instance of AbstractLinkedProcessorSlot, can't be added into ProcessorSlotChain"); 12 continue; 13 } 14 // 将功能槽放到责任链最后 15 chain.addLast((AbstractLinkedProcessorSlot<?>) slot); 16 } 17 18 return chain; 19 } 20}
老版本直接是硬编码方式:
1public class DefaultSlotChainBuilder implements SlotChainBuilder { 2 3 @Override 4 public ProcessorSlotChain build() { 5 ProcessorSlotChain chain = new DefaultProcessorSlotChain(); 6 chain.addLast(new NodeSelectorSlot()); 7 chain.addLast(new ClusterBuilderSlot()); 8 chain.addLast(new LogSlot()); 9 chain.addLast(new StatisticSlot()); 10 chain.addLast(new AuthoritySlot()); 11 chain.addLast(new SystemSlot()); 12 chain.addLast(new FlowSlot()); 13 chain.addLast(new DegradeSlot()); 14 15 return chain; 16 } 17}
以下内容来自文档:
NodeSelectorSlot: 负责收集资源的路径,并将这些资源的调用路径,以树状结构存储起来,用于根据调用路径来限流降级;ClusterBuilderSlot: 则用于存储资源的统计信息以及调用者信息,例如该资源的 RT, QPS, thread count 等等,这些信息将用作为多维度限流,降级的依据;StatisticSlot: 则用于记录、统计不同纬度的 runtime 指标监控信息;FlowSlot: 则用于根据预设的限流规则以及前面 slot 统计的状态,来进行流量控制;AuthoritySlot: 则根据配置的黑白名单和调用来源信息,来做黑白名单控制;DegradeSlot: 则通过统计信息以及预设的规则,来做熔断降级;SystemSlot: 则通过系统的状态,例如 load1 等,来控制总的入口流量;
总结
- Sentinel 通过责任链模式,将各功能块隔离,即清晰划分出了各功能块的职责边界,也非常方便扩展。新增功能直接新增功能插槽就行了,不需要改以前代码。
- Sentinel 的本地缓存使用的是
HashMap,通过加锁和写复制的思想来解决HashMap的线程安全性问题,在读远大于写的场景这种方式非常非常值得借鉴。
参考
https://github.com/alibaba/Sentinel/wiki/Sentinel-%E6%A0%B8%E5%BF%83%E7%B1%BB%E8%A7%A3%E6%9E%90