加入收藏 | 设为首页 | 会员中心 | 我要投稿 莆田站长网 (https://www.0594zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > 建站资源 > 优化 > 正文

限流降级神器,带你解读阿里巴巴开源 Sentinel 实现原理

发布时间:2019-07-10 04:34:12 所属栏目:优化 来源:Java高级互联网架构
导读:副标题#e# Sentinel 是阿里中间件团队开源的,面向分布式服务架构的轻量级高可用流量控制组件,主要以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度来帮助用户保护服务的稳定性。 大家可能会问:Sentinel 和之前常用的熔断降级库 Netflix Hy
副标题[/!--empirenews.page--]

限流降级神器,带你解读阿里巴巴开源 Sentinel 实现原理

Sentinel 是阿里中间件团队开源的,面向分布式服务架构的轻量级高可用流量控制组件,主要以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度来帮助用户保护服务的稳定性。

大家可能会问:Sentinel 和之前常用的熔断降级库 Netflix Hystrix 有什么异同呢?Sentinel官网有一个对比的文章,这里摘抄一个总结的表格,具体的对比可以点此 链接 查看。

限流降级神器,带你解读阿里巴巴开源 Sentinel 实现原理

从对比的表格可以看到,Sentinel比Hystrix在功能性上还要强大一些,本文让我们一起来了解下Sentinel的源码,揭开Sentinel的神秘面纱。

项目结构

将Sentinel的源码fork到自己的github库中,接着把源码clone到本地,然后开始源码阅读之旅吧。

首先我们看一下Sentinel项目的整个结构:

限流降级神器,带你解读阿里巴巴开源 Sentinel 实现原理
  • sentinel-core 核心模块,限流、降级、系统保护等都在这里实现
  • sentinel-dashboard 控制台模块,可以对连接上的sentinel客户端实现可视化的管理
  • sentinel-transport 传输模块,提供了基本的监控服务端和客户端的API接口,以及一些基于不同库的实现
  • sentinel-extension 扩展模块,主要对DataSource进行了部分扩展实现
  • sentinel-adapter 适配器模块,主要实现了对一些常见框架的适配
  • sentinel-demo 样例模块,可参考怎么使用sentinel进行限流、降级等
  • sentinel-benchmark 基准测试模块,对核心代码的精确性提供基准测试

运行样例

基本上每个框架都会带有样例模块,有的叫example,有的叫demo,sentinel也不例外。

那我们从sentinel的demo中找一个例子运行下看看大致的情况吧,上面说过了sentinel主要的核心功能是做限流、降级和系统保护,那我们就从“限流”开始看sentinel的实现原理吧。

限流降级神器,带你解读阿里巴巴开源 Sentinel 实现原理

可以看到sentinel-demo模块中有很多不同的样例,我们找到basic模块下的flow包,这个包下面就是对应的限流的样例,但是限流也有很多种类型的限流,我们就找根据qps限流的类看吧,其他的限流方式原理上都大差不差。

  1. public class FlowQpsDemo { 
  2.  
  3. private static final String KEY = "abc"; 
  4.  
  5. private static AtomicInteger pass = new AtomicInteger(); 
  6.  
  7. private static AtomicInteger block = new AtomicInteger(); 
  8.  
  9. private static AtomicInteger total = new AtomicInteger(); 
  10.  
  11. private static volatile boolean stop = false; 
  12.  
  13. private static final int threadCount = 32; 
  14.  
  15. private static int seconds = 30; 
  16.  
  17. public static void main(String[] args) throws Exception { 
  18.  
  19. initFlowQpsRule(); 
  20. tick(); 
  21.  
  22. // first make the system run on a very low condition 
  23.  
  24. simulateTraffic(); 
  25.  
  26. System.out.println("===== begin to do flow control"); 
  27.  
  28. System.out.println("only 20 requests per second can pass"); 
  29.  
  30.  
  31. private static void initFlowQpsRule() { 
  32.  
  33. List<FlowRule> rules = new ArrayList<FlowRule>(); 
  34.  
  35. FlowRule rule1 = new FlowRule(); 
  36.  
  37. rule1.setResource(KEY); 
  38.  
  39. // set limit qps to 20 
  40.  
  41. rule1.setCount(20); 
  42.  
  43. // 设置限流类型:根据qps 
  44.  
  45. rule1.setGrade(RuleConstant.FLOW_GRADE_QPS); 
  46.  
  47. rule1.setLimitApp("default"); 
  48.  
  49. rules.add(rule1); 
  50.  
  51. // 加载限流的规则 
  52.  
  53. FlowRuleManager.loadRules(rules); 
  54.  
  55.  
  56. private static void simulateTraffic() { 
  57.  
  58. for (int i = 0; i < threadCount; i++) { 
  59.  
  60. Thread t = new Thread(new RunTask()); 
  61.  
  62. t.setName("simulate-traffic-Task"); 
  63.  
  64. t.start(); 
  65.  
  66.  
  67.  
  68. private static void tick() { 
  69.  
  70. Thread timer = new Thread(new TimerTask()); 
  71.  
  72. timer.setName("sentinel-timer-task"); 
  73.  
  74. timer.start(); 
  75.  
  76.  
  77. static class TimerTask implements Runnable { 
  78.  
  79. @Override 
  80.  
  81. public void run() { 
  82.  
  83. long start = System.currentTimeMillis(); 
  84.  
  85. System.out.println("begin to statistic!!!"); 
  86.  
  87. long oldTotal = 0; 
  88.  
  89. long oldPass = 0; 
  90.  
  91. long oldBlock = 0; 
  92.  
  93. while (!stop) { 
  94.  
  95. try { 
  96.  
  97. TimeUnit.SECONDS.sleep(1); 
  98.  
  99. } catch (InterruptedException e) { 
  100.  
  101.  
  102. long globalTotal = total.get(); 
  103.  
  104. long oneSecondTotal = globalTotal - oldTotal; 
  105.  
  106. oldTotal = globalTotal; 
  107.  
  108. long globalPass = pass.get(); 
  109.  
  110. long oneSecondPass = globalPass - oldPass; 
  111.  
  112. oldPass = globalPass; 
  113.  
  114. long globalBlock = block.get(); 
  115.  
  116. long oneSecondBlock = globalBlock - oldBlock; 
  117.  
  118. oldBlock = globalBlock; 
  119.  
  120. System.out.println(seconds + " send qps is: " + oneSecondTotal); 
  121.  
  122. System.out.println(TimeUtil.currentTimeMillis() + ", total:" + oneSecondTotal 
  123.  
  124. + ", pass:" + oneSecondPass 
  125.  
  126. + ", block:" + oneSecondBlock); 
  127.  
  128. if (seconds-- <= 0) { 
  129.  
  130. stop = true; 
  131.  
  132.  
  133.  
  134. long cost = System.currentTimeMillis() - start; 
  135.  
  136. System.out.println("time cost: " + cost + " ms"); 
  137.  
  138. System.out.println("total:" + total.get() + ", pass:" + pass.get() 
  139.  
  140. + ", block:" + block.get()); 
  141.  
  142. System.exit(0); 
  143.  
  144.  
  145.  
  146. static class RunTask implements Runnable { 
  147.  
  148. @Override 
  149.  
  150. public void run() { 
  151.  
  152. while (!stop) { 
  153.  
  154. Entry entry = null; 
  155.  
  156. try { 
  157.  
  158. entry = SphU.entry(KEY); 
  159.  
  160. // token acquired, means pass 
  161.  
  162. pass.addAndGet(1); 
  163.  
  164. } catch (BlockException e1) { 
  165.  
  166. block.incrementAndGet(); 
  167.  
  168. } catch (Exception e2) { 
  169.  
  170. // biz exception 
  171.  
  172. } finally { 
  173.  
  174. total.incrementAndGet(); 
  175.  
  176. if (entry != null) { 
  177.  
  178. entry.exit(); 
  179.  
  180.  
  181.  
  182. Random random2 = new Random(); 
  183.  
  184. try { 
  185.  
  186. TimeUnit.MILLISECONDS.sleep(random2.nextInt(50)); 
  187.  
  188. } catch (InterruptedException e) { 
  189.  
  190. // ignore 
  191.  
  192.  
  193.  
  194.  
  195.  

执行上面的代码后,打印出如下的结果:

限流降级神器,带你解读阿里巴巴开源 Sentinel 实现原理

可以看到,上面的结果中,pass的数量和我们的预期并不相同,我们预期的是每秒允许pass的请求数是20个,但是目前有很多pass的请求数是超过20个的。

原因是,我们这里测试的代码使用了多线程,注意看 threadCount 的值,一共有32个线程来模拟,而在RunTask的run方法中执行资源保护时,即在 SphU.entry 的内部是没有加锁的,所以就会导致在高并发下,pass的数量会高于20。

可以用下面这个模型来描述下,有一个TimeTicker线程在做统计,每1秒钟做一次。有N个RunTask线程在模拟请求,被访问的business code被资源key保护着,根据规则,每秒只允许20个请求通过。

由于pass、block、total等计数器是全局共享的,而多个RunTask线程在执行SphU.entry申请获取entry时,内部没有锁保护,所以会存在pass的个数超过设定的阈值。

限流降级神器,带你解读阿里巴巴开源 Sentinel 实现原理

那为了证明在单线程下限流的正确性与可靠性,那我们的模型就应该变成了这样:

限流降级神器,带你解读阿里巴巴开源 Sentinel 实现原理

那接下来我把 threadCount 的值改为1,只有一个线程来执行这个方法,看下具体的限流结果,执行上面的代码后打印的结果如下:

限流降级神器,带你解读阿里巴巴开源 Sentinel 实现原理

可以看到pass数基本上维持在20,但是第一次统计的pass值还是超过了20。这又是什么原因导致的呢?

其实仔细看下Demo中的代码可以发现,模拟请求是用的一个线程,统计结果是用的另外一个线程,统计线程每1秒钟统计一次结果,这两个线程之间是有时间上的误差的。从TimeTicker线程打印出来的时间戳可以看出来,虽然每隔一秒进行统计,但是当前打印时的时间和上一次的时间还是有误差的,不完全是1000ms的间隔。

要真正验证每秒限制20个请求,保证数据的精准性,需要做基准测试,这个不是本篇文章的重点,有兴趣的同学可以去了解下jmh,sentinel中的基准测试也是通过jmh做的。

深入原理

通过一个简单的示例程序,我们了解了sentinel可以对请求进行限流,除了限流外,还有降级和系统保护等功能。那现在我们就拨开云雾,深入源码内部去一窥sentinel的实现原理吧。

首先从入口开始: SphU.entry() 。这个方法会去申请一个entry,如果能够申请成功,则说明没有被限流,否则会抛出BlockException,表面已经被限流了。

从 SphU.entry() 方法往下执行会进入到 Sph.entry() ,Sph的默认实现类是 CtSph ,在CtSph中最终会执行到 entry(ResourceWrapperresourceWrapper,intcount,Object...args)throwsBlockException 这个方法。

我们来看一下这个方法的具体实现:

  1. public Entry entry(ResourceWrapper resourceWrapper, int count, Object... args) throws BlockException { 
  2.  
  3. Context context = ContextUtil.getContext(); 
  4.  
  5. if (context instanceof NullContext) { 
  6.  
  7. // Init the entry only. No rule checking will occur. 
  8.  
  9. return new CtEntry(resourceWrapper, null, context); 
  10.  
  11.  
  12. if (context == null) { 
  13.  
  14. context = MyContextUtil.myEnter(Constants.CONTEXT_DEFAULT_NAME, "", resourceWrapper.getType()); 
  15.  
  16.  
  17. // Global switch is close, no rule checking will do. 
  18.  
  19. if (!Constants.ON) { 
  20.  
  21. return new CtEntry(resourceWrapper, null, context); 
  22.  
  23.  
  24. // 获取该资源对应的SlotChain 
  25.  
  26. ProcessorSlot<Object> chain = lookProcessChain(resourceWrapper); 
  27.  
  28. /* 
  29.  
  30. * Means processor cache size exceeds {@link Constants.MAX_SLOT_CHAIN_SIZE}, so no 
  31.  
  32. * rule checking will be done. 
  33.  
  34. */ 
  35.  
  36. if (chain == null) { 
  37.  
  38. return new CtEntry(resourceWrapper, null, context); 
  39.  
  40.  
  41. Entry e = new CtEntry(resourceWrapper, chain, context); 
  42.  
  43. try { 
  44.  
  45. // 执行Slot的entry方法 
  46.  
  47. chain.entry(context, resourceWrapper, null, count, args); 
  48.  
  49. } catch (BlockException e1) { 
  50.  
  51. e.exit(count, args); 
  52.  
  53. // 抛出BlockExecption 
  54.  
  55. throw e1; 
  56.  
  57. } catch (Throwable e1) { 
  58.  
  59. RecordLog.info("Sentinel unexpected exception", e1); 
  60.  
  61.  
  62. return e; 
  63.  

这个方法可以分为以下几个部分:

  • 1.对参数和全局配置项做检测,如果不符合要求就直接返回了一个CtEntry对象,不会再进行后面的限流检测,否则进入下面的检测流程。
  • 2.根据包装过的资源对象获取对应的SlotChain
  • 3.执行SlotChain的entry方法
  • 3.1.如果SlotChain的entry方法抛出了BlockException,则将该异常继续向上抛出
  • 3.2.如果SlotChain的entry方法正常执行了,则最后会将该entry对象返回
  • 4.如果上层方法捕获了BlockException,则说明请求被限流了,否则请求能正常执行

其中比较重要的是第2、3两个步骤,我们来分解一下这两个步骤。

创建SlotChain

首先看一下lookProcessChain的方法实现:

  1. private ProcessorSlot<Object> lookProcessChain(ResourceWrapper resourceWrapper) { 
  2.  
  3. ProcessorSlotChain chain = chainMap.get(resourceWrapper); 
  4.  
  5. if (chain == null) { 
  6.  
  7. synchronized (LOCK) { 
  8.  
  9. chain = chainMap.get(resourceWrapper); 
  10.  
  11. if (chain == null) 
  12.  
  13. // Entry size limit. 
  14.  
  15. if (chainMap.size() >= Constants.MAX_SLOT_CHAIN_SIZE) { 
  16.  
  17. return null; 
  18.  
  19.  
  20. // 具体构造chain的方法 
  21.  
  22. chain = Env.slotsChainbuilder.build(); 
  23.  
  24. Map<ResourceWrapper, ProcessorSlotChain> newMap = new HashMap<ResourceWrapper, ProcessorSlotChain>(chainMap.size() + 1); 
  25.  
  26. newMap.putAll(chainMap); 
  27.  
  28. newMap.put(resourceWrapper, chain); 
  29.  
  30. chainMap = newMap; 
  31.  
  32.  
  33.  
  34.  
  35. return chain; 
  36.  

(编辑:莆田站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读