This commit is contained in:
周伟
2022-05-11 19:04:14 +08:00
parent 9440ac7291
commit d9c5ffd627
826 changed files with 0 additions and 481675 deletions

View File

@@ -25,13 +25,7 @@
<meta name="generator" content="Hexo 4.2.0">
</head>
<body>
<div class="book-container">
<div class="book-sidebar">
@@ -55,289 +49,148 @@
<li><a href="/" class="current-tab">首页</a></li>
</ul>
<ul class="uncollapsible">
<li><a href="../">上一级</a></li>
</ul>
<ul class="uncollapsible">
<li>
<a href="/专栏/深入理解 Sentinel/01 开篇词:一次服务雪崩问题排查经历.md.html">01 开篇词:一次服务雪崩问题排查经历.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/02 为什么需要服务降级以及常见的几种降级方式.md.html">02 为什么需要服务降级以及常见的几种降级方式.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/03 为什么选择 SentinelSentinel 与 Hystrix 的对比.md.html">03 为什么选择 SentinelSentinel 与 Hystrix 的对比.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/04 Sentinel 基于滑动窗口的实时指标数据统计.md.html">04 Sentinel 基于滑动窗口的实时指标数据统计.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/05 Sentinel 的一些概念与核心类介绍.md.html">05 Sentinel 的一些概念与核心类介绍.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/06 Sentinel 中的责任链模式与 Sentinel 的整体工作流程.md.html">06 Sentinel 中的责任链模式与 Sentinel 的整体工作流程.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/07 Java SPI 及 SPI 在 Sentinel 中的应用.md.html">07 Java SPI 及 SPI 在 Sentinel 中的应用.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/08 资源指标数据统计的实现全解析(上).md.html">08 资源指标数据统计的实现全解析(上).md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/09 资源指标数据统计的实现全解析(下).md.html">09 资源指标数据统计的实现全解析(下).md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/10 限流降级与流量效果控制器(上).md.html">10 限流降级与流量效果控制器(上).md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/11 限流降级与流量效果控制器(中).md.html">11 限流降级与流量效果控制器(中).md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/12 限流降级与流量效果控制器(下).md.html">12 限流降级与流量效果控制器(下).md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/13 熔断降级与系统自适应限流.md.html">13 熔断降级与系统自适应限流.md.html</a>
</li>
<li>
<a class="current-tab" href="/专栏/深入理解 Sentinel/14 黑白名单限流与热点参数限流.md.html">14 黑白名单限流与热点参数限流.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/15 自定义 ProcessorSlot 实现开关降级.md.html">15 自定义 ProcessorSlot 实现开关降级.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/16 Sentinel 动态数据源:规则动态配置.md.html">16 Sentinel 动态数据源:规则动态配置.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/17 Sentinel 主流框架适配.md.html">17 Sentinel 主流框架适配.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/18 Sentinel 集群限流的实现(上).md.html">18 Sentinel 集群限流的实现(上).md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/19 Sentinel 集群限流的实现(下).md.html">19 Sentinel 集群限流的实现(下).md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/20 结束语Sentinel 对应用的性能影响如何?.md.html">20 结束语Sentinel 对应用的性能影响如何?.md.html</a>
</li>
<li>
<a href="/专栏/深入理解 Sentinel/21 番外篇Sentinel 1.8.0 熔断降级新特性解读.md.html">21 番外篇Sentinel 1.8.0 熔断降级新特性解读.md.html</a>
</li>
</ul>
</div>
</div>
<div class="sidebar-toggle" onclick="sidebar_toggle()" onmouseover="add_inner()" onmouseleave="remove_inner()">
<div class="sidebar-toggle-inner"></div>
</div>
<script>
function add_inner() {
@@ -347,9 +200,6 @@
inner.classList.add('show')
}
function remove_inner() {
let inner = document.querySelector('.sidebar-toggle-inner')
@@ -357,9 +207,6 @@
inner.classList.remove('show')
}
function sidebar_toggle() {
let sidebar_toggle = document.querySelector('.sidebar-toggle')
@@ -389,9 +236,6 @@
}
function open_sidebar() {
let sidebar = document.querySelector('.book-sidebar')
@@ -415,13 +259,7 @@ function hide_canvas() {
overlay.classList.remove('show')
}
</script>
<div class="off-canvas-content">
<div class="columns">
@@ -491,9 +329,6 @@ function hide_canvas() {
private int strategy = RuleConstant.AUTHORITY_WHITE;
}
</code></pre>
<ul>
@@ -523,9 +358,6 @@ rule.setStrategy(RuleConstant.AUTHORITY_WHITE);
rule.setLimitApp(&quot;serviceA,serviceC&quot;);
AuthorityRuleManager.loadRules(Collections.singletonList(rule));
</code></pre>
<p>上述规则用于限制资源 &quot;GET:/hello&quot; 只允许服务 A 和服务 C 访问。</p>
@@ -539,9 +371,6 @@ AuthorityRuleManager.loadRules(Collections.singletonList(rule));
<p>AuthoritySlot 的实现源码如下:</p>
<pre><code class="language-java">public class AuthoritySlot extends AbstractLinkedProcessorSlot&lt;DefaultNode&gt; {
@Override
public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count, boolean prioritized, Object... args)
@@ -553,9 +382,6 @@ AuthorityRuleManager.loadRules(Collections.singletonList(rule));
fireEntry(context, resourceWrapper, node, count, prioritized, args);
}
@Override
public void exit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) {
@@ -563,9 +389,6 @@ AuthorityRuleManager.loadRules(Collections.singletonList(rule));
fireExit(context, resourceWrapper, count, args);
}
void checkBlackWhiteAuthority(ResourceWrapper resource, Context context) throws AuthorityException {
// (1)
@@ -603,9 +426,6 @@ AuthorityRuleManager.loadRules(Collections.singletonList(rule));
}
}
</code></pre>
<ul>
@@ -693,9 +513,6 @@ AuthorityRuleManager.loadRules(Collections.singletonList(rule));
return true;
}
</code></pre>
<p>整个方法都比较简单,首先是从当前 Context 获取调用来源的名称,只有在调用来源不为空且规则配置了黑名单或者白名单的情况下,才会走黑白名单的过滤逻辑,这也说明,要实现黑白名单限流的前提是,每个服务消费端在发起请求时都必须要携带自身服务的名称,这取决于 Sentinel 主流框架适配器其次Sentinel 通过使用 indexOf 先简单匹配一次黑名单或白名单,再切割黑名单或白名单数组实现精确匹配,这有助于提升性能;如果当前请求来源存在名单中,则根据策略判断这份名称是黑名单还是白名单,再决定是否需要拒绝请求。</p>
@@ -727,23 +544,14 @@ AuthorityRuleManager.loadRules(Collections.singletonList(rule));
<p>ParamMapBucket 的源码如下:</p>
<pre><code class="language-java">public class ParamMapBucket {
// 数组类型为 CacheMap&lt;Object, AtomicInteger&gt;
private final CacheMap&lt;Object, AtomicInteger&gt;[] data;
public ParamMapBucket() {
this(DEFAULT_MAX_CAPACITY);
}
public ParamMapBucket(int capacity) {
RollingParamEvent[] events = RollingParamEvent.values();
@@ -761,13 +569,7 @@ AuthorityRuleManager.loadRules(Collections.singletonList(rule));
}
}
}
</code></pre>
<ul>
@@ -793,25 +595,16 @@ AuthorityRuleManager.loadRules(Collections.singletonList(rule));
// ....
}
public Map&lt;Object, Double&gt; getTopValues(RollingParamEvent event, int number) {
// .....
}
public long getRollingSum(RollingParamEvent event, Object value) {
// .....
}
public double getRollingAvg(RollingParamEvent event, Object value) {
// ....
@@ -819,9 +612,6 @@ AuthorityRuleManager.loadRules(Collections.singletonList(rule));
}
}
</code></pre>
<ul>
@@ -853,21 +643,12 @@ AuthorityRuleManager.loadRules(Collections.singletonList(rule));
<p>ParameterMetric 有三个静态字段,源码如下:</p>
<pre><code class="language-java">public class ParameterMetric {
private final Map&lt;ParamFlowRule, CacheMap&lt;Object, AtomicLong&gt;&gt; ruleTimeCounters = new HashMap&lt;&gt;();
private final Map&lt;ParamFlowRule, CacheMap&lt;Object, AtomicLong&gt;&gt; ruleTokenCounter = new HashMap&lt;&gt;();
private final Map&lt;Integer, CacheMap&lt;Object, AtomicInteger&gt;&gt; threadCountMap = new HashMap&lt;&gt;();
}
</code></pre>
<ul>
@@ -887,9 +668,6 @@ AuthorityRuleManager.loadRules(Collections.singletonList(rule));
private static final Map&lt;String, ParameterMetric&gt; metricsMap = new ConcurrentHashMap&lt;&gt;();
private static final Object LOCK = new Object();
public static void initParamMetricsFor(ResourceWrapper resourceWrapper,ParamFlowRule rule) {
if (resourceWrapper == null || resourceWrapper.getName() == null) {
@@ -927,9 +705,6 @@ AuthorityRuleManager.loadRules(Collections.singletonList(rule));
}
}
</code></pre>
<p>initParamMetricsFor 方法用于为资源创建 ParameterMetric 并初始化,该方法在资源被访问时由 ParamFlowSlot 调用,并且该方法只在为资源配置了参数限流规则的情况下被调用。</p>
@@ -939,9 +714,6 @@ AuthorityRuleManager.loadRules(Collections.singletonList(rule));
<p>sentinel-parameter-flow-control 模块通过 Java SPI 注册自定义的 SlotChainBuilder即注册 HotParamSlotChainBuilder将 ParamFlowSlot 放置在 StatisticSlot 的后面,这个 ParamFlowSlot 就是实现热点参数限流功能的切入点。</p>
<pre><code class="language-java">public class ParamFlowSlot extends AbstractLinkedProcessorSlot&lt;DefaultNode&gt; {
@Override
public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count,
@@ -961,9 +733,6 @@ AuthorityRuleManager.loadRules(Collections.singletonList(rule));
fireEntry(context, resourceWrapper, node, count, prioritized, args);
}
@Override
public void exit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) {
@@ -973,9 +742,6 @@ AuthorityRuleManager.loadRules(Collections.singletonList(rule));
}
}
</code></pre>
<p>既然是参数限流,那么肯定是需要能够拿到参数了,而 ProcessorSlot#entry 方法的最后一个参数就是请求传递过来的参数,通过 SphU#entry 方法一层层往下传递。例如:</p>
@@ -1019,9 +785,6 @@ AuthorityRuleManager.loadRules(Collections.singletonList(rule));
}
}
</code></pre>
<p>当 SphU#entry 调用到 ParamFlowSlot#entry 方法时ParamFlowSlot 调用 checkFlow 方法判断是否需要限流。checkFlow 方法的实现如下:</p>
@@ -1049,15 +812,9 @@ AuthorityRuleManager.loadRules(Collections.singletonList(rule));
for (ParamFlowRule rule : rules) {
applyRealParamIdx(rule, args.length);
// Initialize the parameter metrics.
ParameterMetricStorage.initParamMetricsFor(resourceWrapper, rule);
if (!ParamFlowChecker.passCheck(resourceWrapper, rule, count, args)) {
String triggeredParam = &quot;&quot;;
@@ -1077,9 +834,6 @@ AuthorityRuleManager.loadRules(Collections.singletonList(rule));
}
}
</code></pre>
<ul>
@@ -1113,9 +867,6 @@ AuthorityRuleManager.loadRules(Collections.singletonList(rule));
private int burstCount = 0;
}
</code></pre>
<ul>
@@ -1157,9 +908,6 @@ rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setCount(5);
ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
</code></pre>
<p>以此为例,我们分析 ParamFlowChecker#passCheck 方法源码passCheck 返回 true 表示放行,返回 false 表示拒绝。</p>
@@ -1209,9 +957,6 @@ ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
return passLocalCheck(resourceWrapper, rule, count, value);
}
</code></pre>
<ul>
@@ -1281,9 +1026,6 @@ ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
return true;
}
</code></pre>
<p>由于参数可能是基本数据类型,也可能是数组类型,或者引用类型,所以 passLocalCheck 方法分三种情况处理。我们只讨论其中一种情况,其它情况的实现类似。</p>
@@ -1329,15 +1071,9 @@ ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
return ++threadCount &lt;= threshold;
}
return true;
}
</code></pre>
<ul>
@@ -1421,9 +1157,6 @@ ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
return true;
}
//5
long passTime = currentTime - lastAddTokenTime.get();
@@ -1455,9 +1188,6 @@ ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
long newQps = toAddCount + restQps &gt; maxCount ? (maxCount - acquireCount)
: (restQps + toAddCount - acquireCount);
if (newQps &lt; 0) {
return false;
@@ -1513,9 +1243,6 @@ ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
}
}
</code></pre>
<ol>
@@ -1643,9 +1370,6 @@ ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
}
}
</code></pre>
<ol>
@@ -1687,9 +1411,6 @@ ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
</div>
</div>
</div>
</div>
@@ -1697,9 +1418,6 @@ ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
</div>
</div>
<a class="off-canvas-overlay" onclick="hide_canvas()"></a>
</div>
@@ -1715,17 +1433,11 @@ ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-NPSEEVD756');
@@ -1751,9 +1463,6 @@ ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
setCookie("lastPath", path)
}
function setCookie(cname, cvalue) {
var d = new Date();
@@ -1765,9 +1474,6 @@ ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
document.cookie = cname + "=" + cvalue + "; " + expires + ";path = /";
}
function getCookie(cname) {
var name = cname + "=";
@@ -1785,12 +1491,6 @@ ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
return "";
}
</script>
</html>