{"id":10021277,"date":"2026-02-26T13:42:01","date_gmt":"2026-02-26T05:42:01","guid":{"rendered":"https:\/\/itmax.cn\/?p=10021277"},"modified":"2026-02-26T13:42:01","modified_gmt":"2026-02-26T05:42:01","slug":"sentinel-java%e5%ae%a2%e6%88%b7%e7%ab%af%e9%99%90%e6%b5%81%e5%8e%9f%e7%90%86%e8%a7%a3%e6%9e%90%ef%bd%9c%e5%be%97%e7%89%a9%e6%8a%80%e6%9c%af","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10021277","title":{"rendered":"Sentinel Java\u5ba2\u6237\u7aef\u9650\u6d41\u539f\u7406\u89e3\u6790\uff5c\u5f97\u7269\u6280\u672f"},"content":{"rendered":"<h2>\u4e00\u3001\u4ece\u4e00\u6b21 HTTP \u8bf7\u6c42\u5f00\u59cb<\/h2>\n<p>\u5728\u4e00\u4e2a\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u670d\u52a1\u8282\u70b9\u901a\u5e38\u66b4\u9732\u4e86\u6210\u767e\u4e0a\u5343\u4e2a HTTP \u63a5\u53e3\u5bf9\u5916\u63d0\u4f9b\u670d\u52a1\u3002\u4e3a\u4e86\u4fdd\u8bc1\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\uff0c\u6838\u5fc3 HTTP \u63a5\u53e3\u5f80\u5f80\u9700\u8981\u914d\u7f6e\u9650\u6d41\u89c4\u5219\u3002\u7ed9 HTTP \u63a5\u53e3\u914d\u7f6e\u9650\u6d41\uff0c\u53ef\u4ee5\u9632\u6b62\u7a81\u53d1\u6216\u6076\u610f\u7684\u9ad8\u5e76\u53d1\u8bf7\u6c42\u8017\u5c3d\u670d\u52a1\u5668\u8d44\u6e90\uff08\u5982 CPU\u3001\u5185\u5b58\u3001\u6570\u636e\u5e93\u8fde\u63a5\u7b49\uff09\uff0c\u4ece\u800c\u907f\u514d\u670d\u52a1\u5d29\u6e83\u6216\u5f15\u53d1\u96ea\u5d29\u6548\u5e94\u3002<\/p>\n<h3>\u57fa\u7840\u793a\u4f8b<\/h3>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e0b\u9762\u8fd9\u6837\u4e00\u4e2a HTTP \u63a5\u53e3\uff0c\u9700\u8981\u7ed9\u5b83\u914d\u7f6e\u9650\u6d41\u89c4\u5219\uff1a<\/p>\n<pre><code>@RestController\n@RequiredArgsConstructor\n@RequestMapping(&quot;\/demo&quot;)\npublic class DemoController {\n\n    @RequestMapping(&quot;\/hello&quot;)\n    @SentinelResource(&quot;test_sentinel&quot;)\n    public String hello() {\n        return &quot;hello world&quot;;\n    }\n}<\/code><\/pre>\n<p>\u4f7f\u7528\u8d77\u6765\u975e\u5e38\u7b80\u5355\u3002\u9996\u5148\u6211\u4eec\u53ef\u4ee5\u9009\u62e9\u7ed9\u63a5\u53e3\u52a0\u4e0a\u00a0@SentinelResource\u00a0\u6ce8\u89e3\uff08\u4e5f\u53ef\u4ee5\u4e0d\u52a0\uff0c\u5982\u679c\u4e0d\u52a0 Sentinel \u5ba2\u6237\u7aef\u4f1a\u4f7f\u7528\u8bf7\u6c42\u8def\u5f84\u4f5c\u4e3a\u8d44\u6e90\u540d\uff0c\u8be6\u7ec6\u539f\u7406\u5728\u540e\u9762\u7ae0\u8282\u8bb2\u89e3\uff09\uff0c\u7136\u540e\u5230\u6d41\u63a7\u63a7\u5236\u53f0\u7ed9\u8be5\u8d44\u6e90\u914d\u7f6e\u6d41\u63a7\u89c4\u5219\u5373\u53ef\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"723\" height=\"500\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdnYTC\" alt=\"\" title=\"\"><\/p>\n<h2>\u4e8c\u3001\u9650\u6d41\u89c4\u5219\u7684\u52a0\u8f7d<\/h2>\n<p>\u9650\u6d41\u89c4\u5219\u7684\u751f\u6548\uff0c\u662f\u4ece\u9650\u6d41\u89c4\u5219\u7684\u52a0\u8f7d\u5f00\u59cb\u7684\u3002\u805a\u7126\u5230\u5ba2\u6237\u7aef\u7684\u00a0RuleLoader\u00a0\u7c7b\uff0c\u53ef\u4ee5\u770b\u5230\u5b83\u652f\u6301\u4e86\u591a\u79cd\u89c4\u5219\u7684\u52a0\u8f7d\uff1a<\/p>\n<ul>\n<li>\u6d41\u63a7\u89c4\u5219\uff1b<\/li>\n<li>\u96c6\u7fa4\u9650\u6d41\u89c4\u5219\uff1b<\/li>\n<li>\u7194\u65ad\u89c4\u5219\uff1b<\/li>\n<li>&#8230;&#8230;<\/li>\n<\/ul>\n<h3>RuleLoader \u6838\u5fc3\u903b\u8f91<\/h3>\n<p>RuleLoader\u00a0\u7c7b\u7684\u6838\u5fc3\u4f5c\u7528\u662f\u5c06\u8fd9\u4e9b\u89c4\u5219\u52a0\u8f7d\u5230\u7f13\u5b58\u4e2d\uff0c\u65b9\u4fbf\u540e\u7eed\u4f7f\u7528\uff1a<\/p>\n<pre><code>public class RuleLoader {\n\n    \/**\n     * \u52a0\u8f7d\u6240\u6709 Sentinel \u89c4\u5219\u5230\u5185\u5b58\u7f13\u5b58\n     *\n     * @param sentinelRules \u5305\u542b\u5404\u79cd\u89c4\u5219\u7684\u914d\u7f6e\u5bf9\u8c61\n     *\/\n    public static void loadRule(SentinelRules sentinelRules) {\n        if (sentinelRules == null) {\n            return;\n        }\n\n        \/\/ \u52a0\u8f7d\u6d41\u63a7\u89c4\u5219\n        FlowRuleManager.loadRules(sentinelRules.getFlowRules());\n        \/\/ \u52a0\u8f7d\u96c6\u7fa4\u6d41\u63a7\u89c4\u5219\n        RuleManager.loadClusterFlowRule(sentinelRules.getFlowRules());\n\n        \/\/ \u52a0\u8f7d\u53c2\u6570\u6d41\u63a7\u89c4\u5219\n        ParamFlowRuleManager.loadRules(sentinelRules.getParamFlowRules());\n        \/\/ \u52a0\u8f7d\u53c2\u6570\u96c6\u7fa4\u6d41\u63a7\u89c4\u5219\n        RuleManager.loadClusterParamFlowRule(sentinelRules.getParamFlowRules());\n\n        \/\/ \u52a0\u8f7d\u7194\u65ad\u89c4\u5219\n        DegradeRuleManager.loadRules(sentinelRules.getDegradeRules());\n\n        \/\/ \u52a0\u8f7d\u53c2\u6570\u7194\u65ad\u89c4\u5219\n        ParamDegradeRuleManager.loadRules(sentinelRules.getParamDegradeRules());\n\n        \/\/ \u52a0\u8f7d\u7cfb\u7edf\u9650\u6d41\u89c4\u5219\n        SystemRuleManager.loadRules(sentinelRules.getSystemRules());\n    }\n}<\/code><\/pre>\n<h3>\u6d41\u63a7\u89c4\u5219\u52a0\u8f7d\u8be6\u60c5<\/h3>\n<p>\u4ee5\u6d41\u63a7\u89c4\u5219\u7684\u52a0\u8f7d\u4e3a\u4f8b\u6df1\u5165FlowRuleManager.loadRules\u00a0\u65b9\u6cd5\u53ef\u4ee5\u770b\u5230\u5176\u5b8c\u6574\u7684\u52a0\u8f7d\u903b\u8f91\uff1a<\/p>\n<pre><code>public static void loadRules(List&lt;FlowRule&gt; rules) {\n    \/\/ \u901a\u8fc7\u52a8\u6001\u914d\u7f6e\u5c5e\u6027\u66f4\u65b0\u89c4\u5219\u503c\n    currentProperty.updateValue(rules);\n}<\/code><\/pre>\n<p>updateValue\u00a0\u65b9\u6cd5\u8d1f\u8d23\u901a\u77e5\u6240\u6709\u76d1\u542c\u5668\u914d\u7f6e\u53d8\u66f4\uff1a<\/p>\n<pre><code>public boolean updateValue(T newValue) {\n    \/\/ \u5982\u679c\u65b0\u65e7\u503c\u76f8\u540c\uff0c\u65e0\u9700\u66f4\u65b0\n    if (isEqual(value, newValue)) {\n        return false;\n    }\n    RecordLog.info(&quot;[DynamicSentinelProperty] Config will be updated to: &quot; + newValue);\n\n    \/\/ \u66f4\u65b0\u914d\u7f6e\u503c\n    value = newValue;\n    \/\/ \u901a\u77e5\u6240\u6709\u76d1\u542c\u5668\u914d\u7f6e\u5df2\u66f4\u65b0\n    for (PropertyListener&lt;T&gt; listener : listeners) {\n        listener.configUpdate(newValue);\n    }\n    return true;\n}<\/code><\/pre>\n<p>FlowPropertyListener\u00a0\u662f\u6d41\u63a7\u89c4\u5219\u53d8\u66f4\u7684\u5177\u4f53\u76d1\u542c\u5668\u5b9e\u73b0\uff1a<\/p>\n<pre><code>private static final class FlowPropertyListener implements PropertyListener&lt;List&lt;FlowRule&gt;&gt; {\n\n    @Override\n    public void configUpdate(List&lt;FlowRule&gt; value) {\n        \/\/ \u6784\u5efa\u6d41\u63a7\u89c4\u5219\u6620\u5c04\u8868\uff08\u6309\u8d44\u6e90\u540d\u5206\u7ec4\uff09\n        Map&lt;String, List&lt;FlowRule&gt;&gt; rules = FlowRuleUtil.buildFlowRuleMap(value);\n        if (rules != null) {\n            \/\/ \u6e05\u7a7a\u65e7\u89c4\u5219\n            flowRules.clear();\n            \/\/ \u52a0\u8f7d\u65b0\u89c4\u5219\n            flowRules.putAll(rules);\n        }\n        RecordLog.info(&quot;[FlowRuleManager] Flow rules received: &quot; + flowRules);\n    }\n}<\/code><\/pre>\n<h2>\u4e09\u3001SentinelServletFilter \u8fc7\u6ee4\u5668<\/h2>\n<p>\u5728 Sentinel \u4e2d\uff0c\u6240\u6709\u7684\u8d44\u6e90\u90fd\u5bf9\u5e94\u4e00\u4e2a\u8d44\u6e90\u540d\u79f0\u548c\u4e00\u4e2a\u00a0Entry\u3002Entry\u00a0\u53ef\u4ee5\u901a\u8fc7\u5bf9\u4e3b\u6d41\u6846\u67b6\u7684\u9002\u914d\u81ea\u52a8\u521b\u5efa\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u6ce8\u89e3\u7684\u65b9\u5f0f\u6216\u8c03\u7528 API \u663e\u5f0f\u521b\u5efa\u3002Entry\u00a0\u662f\u9650\u6d41\u7684\u5165\u53e3\u7c7b\uff0c\u901a\u8fc7\u00a0@SentinelResource\u00a0\u6ce8\u89e3\u7684\u9650\u6d41\u672c\u8d28\u4e0a\u4e5f\u662f\u901a\u8fc7 AOP \u7684\u65b9\u5f0f\u8fdb\u884c\u4e86\u5bf9\u00a0Entry\u00a0\u7c7b\u7684\u8c03\u7528\u3002<\/p>\n<h3>Entry \u7684\u7f16\u7a0b\u8303\u5f0f<\/h3>\n<p>Entry\u00a0\u7c7b\u7684\u6807\u51c6\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code>\/\/ \u8d44\u6e90\u540d\u53ef\u4f7f\u7528\u4efb\u610f\u6709\u4e1a\u52a1\u8bed\u4e49\u7684\u5b57\u7b26\u4e32\uff0c\u6bd4\u5982\u65b9\u6cd5\u540d\u3001\u63a5\u53e3\u540d\u6216\u5176\u5b83\u53ef\u552f\u4e00\u6807\u8bc6\u7684\u5b57\u7b26\u4e32\ntry (Entry entry = SphU.entry(&quot;resourceName&quot;)) {\n    \/\/ \u88ab\u4fdd\u62a4\u7684\u4e1a\u52a1\u903b\u8f91\n    \/\/ do something here...\n} catch (BlockException ex) {\n    \/\/ \u8d44\u6e90\u8bbf\u95ee\u963b\u6b62\uff0c\u88ab\u9650\u6d41\u6216\u88ab\u964d\u7ea7\n    \/\/ \u5728\u6b64\u5904\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u64cd\u4f5c\n}<\/code><\/pre>\n<h3>Servlet Filter \u62e6\u622a\u903b\u8f91<\/h3>\n<p>\u5bf9\u4e8e\u4e00\u4e2a HTTP \u8d44\u6e90\uff0c\u5728\u6ca1\u6709\u663e\u5f0f\u6807\u6ce8\u00a0@SentinelResource\u00a0\u6ce8\u89e3\u7684\u60c5\u51b5\u4e0b\uff0c\u4f1a\u6709\u4e00\u4e2a Servlet Filter \u7c7b\u00a0SentinelServletFilter\u00a0\u7edf\u4e00\u8fdb\u884c\u62e6\u622a\uff1a<\/p>\n<pre><code>public class SentinelServletFilter implements Filter {\n\n    @Override\n    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)\n            throws IOException, ServletException {\n        HttpServletRequest sRequest = (HttpServletRequest) request;\n        Entry urlEntry = null;\n\n        try {\n            \/\/ \u83b7\u53d6\u5e76\u6e05\u7406\u8bf7\u6c42\u8def\u5f84\n            String target = FilterUtil.filterTarget(sRequest);\n\n            \/\/ \u7edf\u4e00 URL \u6e05\u7406\u903b\u8f91\n            \/\/ \u5bf9\u4e8e RESTful API\uff0c\u5fc5\u987b\u5bf9 URL \u8fdb\u884c\u6e05\u7406\uff08\u4f8b\u5982\u5c06 \/foo\/1 \u548c \/foo\/2 \u7edf\u4e00\u4e3a \/foo\/:id\uff09\uff0c\n            \/\/ \u5426\u5219\u4e0a\u4e0b\u6587\u548c\u8d44\u6e90\u7684\u6570\u91cf\u4f1a\u8d85\u8fc7\u9608\u503c\n            SentinelUrlCleaner urlCleaner = SentinelUrlCleaner.SENTINEL_URL_CLEANER;\n            if (urlCleaner != null) {\n                target = urlCleaner.clean(sRequest, target);\n            }\n\n            \/\/ \u5982\u679c\u8bf7\u6c42\u8def\u5f84\u4e0d\u4e3a\u7a7a\u4e14\u975e\u5b89\u5168\u626b\u63cf\uff0c\u5219\u8fdb\u5165\u9650\u6d41\u903b\u8f91\n            if (!StringUtil.isEmpty(target) &amp;&amp; !isSecScan) {\n                \/\/ \u89e3\u6790\u6765\u6e90\u6807\u8bc6\uff08\u7528\u4e8e\u6765\u6e90\u9650\u6d41\uff09\n                String origin = parseOrigin(sRequest);\n                \/\/ \u786e\u5b9a\u4e0a\u4e0b\u6587\u540d\u79f0\n                String contextName = webContextUnify\n                    ? WebServletConfig.WEB_SERVLET_CONTEXT_NAME\n                    : target;\n\n                \/\/ \u4f7f\u7528 WEB_SERVLET_CONTEXT_NAME \u4f5c\u4e3a\u5f53\u524d Context \u7684\u540d\u5b57\n                ContextUtil.enter(contextName, origin);\n\n                \/\/ \u6839\u636e\u914d\u7f6e\u51b3\u5b9a\u662f\u5426\u5305\u542b HTTP \u65b9\u6cd5\n                if (httpMethodSpecify) {\n                    String pathWithHttpMethod = sRequest.getMethod().toUpperCase() + COLON + target;\n                    \/\/ \u5b9e\u9645\u8fdb\u5165\u5230\u9650\u6d41\u7edf\u8ba1\u5224\u65ad\u903b\u8f91\uff0c\u8d44\u6e90\u540d\u662f &quot;\u65b9\u6cd5:\u8def\u5f84&quot;\n                    urlEntry = SphU.entry(pathWithHttpMethod, ResourceTypeConstants.COMMON_WEB, EntryType.IN);\n                } else {\n                    \/\/ \u5b9e\u9645\u8fdb\u5165\u5230\u9650\u6d41\u7edf\u8ba1\u5224\u65ad\u903b\u8f91\uff0c\u8d44\u6e90\u540d\u662f\u8bf7\u6c42\u8def\u5f84\n                    urlEntry = SphU.entry(target, ResourceTypeConstants.COMMON_WEB, EntryType.IN);\n                }\n            }\n\n            \/\/ \u7ee7\u7eed\u6267\u884c\u540e\u7eed\u8fc7\u6ee4\u5668\n            chain.doFilter(request, response);\n\n        } catch (BlockException e) {\n            \/\/ \u5904\u7406\u88ab\u9650\u6d41\u7684\u60c5\u51b5\n            HttpServletResponse sResponse = (HttpServletResponse) response;\n            \/\/ \u8fd4\u56de\u9650\u6d41\u9875\u9762\u6216\u91cd\u5b9a\u5411\u5230\u5176\u4ed6 URL\n            WebCallbackManager.getUrlBlockHandler().blocked(sRequest, sResponse, e);\n\n        } catch (IOException | ServletException | RuntimeException e2) {\n            \/\/ \u8bb0\u5f55\u5f02\u5e38\u4fe1\u606f\u7528\u4e8e\u7edf\u8ba1\n            Tracer.traceEntry(e2, urlEntry);\n            throw e2;\n\n        } finally {\n            \/\/ \u91ca\u653e Entry \u8d44\u6e90\n            if (urlEntry != null) {\n                urlEntry.exit();\n            }\n            \/\/ \u9000\u51fa\u5f53\u524d\u4e0a\u4e0b\u6587\n            ContextUtil.exit();\n        }\n    }\n}<\/code><\/pre>\n<h2>\u56db\u3001SentinelResourceAspect \u5207\u9762<\/h2>\n<p>\u5982\u679c\u5728\u63a5\u53e3\u4e0a\u6807\u6ce8\u4e86\u00a0@SentinelResource\u00a0\u6ce8\u89e3\uff0c\u8fd8\u4f1a\u6709\u53e6\u5916\u7684\u903b\u8f91\u5904\u7406\u3002Sentinel \u5b9a\u4e49\u4e86\u4e00\u4e2a\u5355\u72ec\u7684 AOP \u5207\u9762\u00a0SentinelResourceAspect\u00a0\u4e13\u95e8\u7528\u4e8e\u5904\u7406\u6ce8\u89e3\u9650\u6d41\u3002<\/p>\n<h3>SentinelResource \u6ce8\u89e3\u5b9a\u4e49<\/h3>\n<p>\u5148\u6765\u770b\u770b\u00a0@SentinelResource\u00a0\u6ce8\u89e3\u7684\u5b8c\u6574\u5b9a\u4e49\uff1a<\/p>\n<pre><code>@Target({ElementType.METHOD, ElementType.TYPE})\n@Retention(RetentionPolicy.RUNTIME)\n@Inherited\npublic @interface SentinelResource {\n\n    \/**\n     * Sentinel \u8d44\u6e90\u7684\u540d\u79f0\uff08\u5373\u8d44\u6e90\u6807\u8bc6\uff09\n     * \u5fc5\u586b\u9879\uff0c\u4e0d\u80fd\u4e3a\u7a7a\n     *\/\n    String value() default &quot;&quot;;\n\n    \/**\n     * \u8d44\u6e90\u7684\u5165\u53e3\u7c7b\u578b\uff08\u5165\u7ad9 IN \u6216\u51fa\u7ad9 OUT\uff09\n     * \u9ed8\u8ba4\u4e3a\u51fa\u7ad9\uff08OUT\uff09\n     *\/\n    EntryType entryType() default EntryType.OUT;\n\n    \/**\n     * \u8d44\u6e90\u7684\u5206\u7c7b\uff08\u7c7b\u578b\uff09\n     * \u81ea 1.7.0 \u7248\u672c\u8d77\u652f\u6301\n     *\/\n    int resourceType() default 0;\n\n    \/**\n     * \u9650\u6d41\u6216\u7194\u65ad\u65f6\u8c03\u7528\u7684 block \u5f02\u5e38\u5904\u7406\u65b9\u6cd5\u7684\u540d\u79f0\n     * \u9ed8\u8ba4\u4e3a\u7a7a\uff08\u5373\u4e0d\u6307\u5b9a\uff09\n     *\/\n    String blockHandler() default &quot;&quot;;\n\n    \/**\n     * blockHandler \u6240\u5728\u7684\u7c7b\n     * \u5982\u679c\u4e0e\u539f\u65b9\u6cd5\u4e0d\u5728\u540c\u4e00\u4e2a\u7c7b\uff0c\u9700\u8981\u6307\u5b9a\u6b64\u53c2\u6570\n     *\/\n    Class&lt;?&gt;[] blockHandlerClass() default {};\n\n    \/**\n     * \u964d\u7ea7\uff08fallback\uff09\u65b9\u6cd5\u7684\u540d\u79f0\n     * \u9ed8\u8ba4\u4e3a\u7a7a\uff08\u5373\u4e0d\u6307\u5b9a\uff09\n     *\/\n    String fallback() default &quot;&quot;;\n\n    \/**\n     * \u7528\u4f5c\u901a\u7528\u7684\u9ed8\u8ba4\u964d\u7ea7\u65b9\u6cd5\n     * \u8be5\u65b9\u6cd5\u4e0d\u80fd\u63a5\u6536\u4efb\u4f55\u53c2\u6570\uff0c\u4e14\u8fd4\u56de\u7c7b\u578b\u9700\u4e0e\u539f\u65b9\u6cd5\u517c\u5bb9\n     *\/\n    String defaultFallback() default &quot;&quot;;\n\n    \/**\n     * fallback \u6240\u5728\u7684\u7c7b\n     * \u5982\u679c\u4e0e\u539f\u65b9\u6cd5\u4e0d\u5728\u540c\u4e00\u4e2a\u7c7b\uff0c\u9700\u8981\u6307\u5b9a\u6b64\u53c2\u6570\n     *\/\n    Class&lt;?&gt;[] fallbackClass() default {};\n\n    \/**\n     * \u9700\u8981\u88ab\u8ffd\u8e2a\u5e76\u89e6\u53d1 fallback \u7684\u5f02\u5e38\u7c7b\u578b\u5217\u8868\n     * \u9ed8\u8ba4\u4e3a Throwable\uff08\u5373\u6240\u6709\u5f02\u5e38\u90fd\u4f1a\u89e6\u53d1 fallback\uff09\n     *\/\n    Class&lt;? extends Throwable&gt;[] exceptionsToTrace() default {Throwable.class};\n\n    \/**\n     * \u6307\u5b9a\u9700\u8981\u5ffd\u7565\u7684\u5f02\u5e38\u7c7b\u578b\uff08\u5373\u8fd9\u4e9b\u5f02\u5e38\u4e0d\u4f1a\u89e6\u53d1 fallback\uff09\n     * \u6ce8\u610f\uff1aexceptionsToTrace \u548c exceptionsToIgnore \u4e0d\u5e94\u540c\u65f6\u4f7f\u7528\uff1b\n     * \u82e5\u540c\u65f6\u5b58\u5728\uff0cexceptionsToIgnore \u4f18\u5148\u7ea7\u66f4\u9ad8\n     *\/\n    Class&lt;? extends Throwable&gt;[] exceptionsToIgnore() default {};\n}<\/code><\/pre>\n<h3>\u5b9e\u9645\u4f7f\u7528\u793a\u4f8b<\/h3>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u4f7f\u7528\u793a\u4f8b\uff0c\u5c55\u793a\u4e86\u00a0@SentinelResource\u00a0\u6ce8\u89e3\u7684\u5404\u79cd\u914d\u7f6e\u65b9\u5f0f\uff1a<\/p>\n<pre><code>@RestController\npublic class SentinelController {\n\n    @Autowired\n    private ISentinelService service;\n\n    @GetMapping(value = &quot;\/hello\/{s}&quot;)\n    public String apiHello(@PathVariable long s) {\n        return service.hello(s);\n    }\n}\n\npublic interface ISentinelService {\n    String hello(long s);\n}\n\n@Service\n@Slf4j\npublic class SentinelServiceImpl implements ISentinelService {\n\n    \/**\n     * Sentinel \u63d0\u4f9b\u4e86 @SentinelResource \u6ce8\u89e3\u7528\u4e8e\u5b9a\u4e49\u8d44\u6e90\n     *\n     * @param s \u8f93\u5165\u53c2\u6570\n     * @return \u8fd4\u56de\u7ed3\u679c\n     *\/\n    @Override\n    \/\/ value\uff1a\u8d44\u6e90\u540d\u79f0\uff0c\u5fc5\u9700\u9879\uff08\u4e0d\u80fd\u4e3a\u7a7a\uff09\n    \/\/ blockHandler\uff1a\u5bf9\u5e94\u5904\u7406 BlockException \u7684\u51fd\u6570\u540d\u79f0\n    \/\/ fallback\uff1a\u7528\u4e8e\u5728\u629b\u51fa\u5f02\u5e38\u7684\u65f6\u5019\u63d0\u4f9b fallback \u5904\u7406\u903b\u8f91\n    @SentinelResource(value = &quot;hello&quot;, blockHandler = &quot;exceptionHandler&quot;, fallback = &quot;helloFallback&quot;)\n    public String hello(long s) {\n        log.error(&quot;hello:{}&quot;, s);\n        return String.format(&quot;Hello at %d&quot;, s);\n    }\n\n    \/**\n     * Fallback \u51fd\u6570\n     * \u51fd\u6570\u7b7e\u540d\u4e0e\u539f\u51fd\u6570\u4e00\u81f4\uff0c\u6216\u52a0\u4e00\u4e2a Throwable \u7c7b\u578b\u7684\u53c2\u6570\n     *\/\n    public String helloFallback(long s) {\n        log.error(&quot;helloFallback:{}&quot;, s);\n        return String.format(&quot;Halooooo %d&quot;, s);\n    }\n\n    \/**\n     * Block \u5f02\u5e38\u5904\u7406\u51fd\u6570\n     * \u53c2\u6570\u6700\u540e\u591a\u4e00\u4e2a BlockException\uff0c\u5176\u4f59\u4e0e\u539f\u51fd\u6570\u4e00\u81f4\n     *\/\n    public String exceptionHandler(long s, BlockException ex) {\n        \/\/ Do some log here.\n        log.error(&quot;exceptionHandler:{}&quot;, s);\n        ex.printStackTrace();\n        return &quot;Oops, error occurred at &quot; + s;\n    }\n}<\/code><\/pre>\n<h3>SentinelResourceAspect \u6838\u5fc3\u903b\u8f91<\/h3>\n<p>@SentinelResource\u00a0\u6ce8\u89e3\u7531\u00a0SentinelResourceAspect\u00a0\u5207\u9762\u5904\u7406\uff0c\u6838\u5fc3\u903b\u8f91\u5982\u4e0b\uff1a<\/p>\n<pre><code>@Aspect\npublic class SentinelResourceAspect extends AbstractSentinelAspectSupport {\n\n    @Pointcut(&quot;@annotation(com.alibaba.csp.sentinel.annotation.SentinelResource)&quot;)\n    public void sentinelResourceAnnotationPointcut() {\n    }\n\n    @Around(&quot;sentinelResourceAnnotationPointcut()&quot;)\n    public Object invokeResourceWithSentinel(ProceedingJoinPoint pjp) throws Throwable {\n        \/\/ \u83b7\u53d6\u76ee\u6807\u65b9\u6cd5\n        Method originMethod = resolveMethod(pjp);\n\n        \/\/ \u83b7\u53d6\u6ce8\u89e3\u4fe1\u606f\n        SentinelResource annotation = originMethod.getAnnotation(SentinelResource.class);\n        if (annotation == null) {\n            throw new IllegalStateException(&quot;Wrong state for SentinelResource annotation&quot;);\n        }\n\n        \/\/ \u83b7\u53d6\u8d44\u6e90\u914d\u7f6e\u4fe1\u606f\n        String resourceName = getResourceName(annotation.value(), originMethod);\n        EntryType entryType = annotation.entryType();\n        int resourceType = annotation.resourceType();\n\n        Entry entry = null;\n        try {\n            \/\/ \u521b\u5efa\u9650\u6d41\u5165\u53e3\n            entry = SphU.entry(resourceName, resourceType, entryType, pjp.getArgs());\n            \/\/ \u6267\u884c\u539f\u65b9\u6cd5\n            Object result = pjp.proceed();\n            return result;\n\n        } catch (BlockException ex) {\n            \/\/ \u5904\u7406\u88ab\u9650\u6d41\u5f02\u5e38\n            return handleBlockException(pjp, annotation, ex);\n\n        } catch (Throwable ex) {\n            \/\/ \u5904\u7406\u4e1a\u52a1\u5f02\u5e38\n            Class&lt;? extends Throwable&gt;[] exceptionsToIgnore = annotation.exceptionsToIgnore();\n            \/\/ \u4f18\u5148\u68c0\u67e5\u5ffd\u7565\u5217\u8868\n            if (exceptionsToIgnore.length &gt; 0 &amp;&amp; exceptionBelongsTo(ex, exceptionsToIgnore)) {\n                throw ex;\n            }\n            \/\/ \u68c0\u67e5\u5f02\u5e38\u662f\u5426\u5728\u8ffd\u8e2a\u5217\u8868\u4e2d\n            if (exceptionBelongsTo(ex, annotation.exceptionsToTrace())) {\n                traceException(ex);\n                \/\/ \u6267\u884c fallback \u903b\u8f91\n                return handleFallback(pjp, annotation, ex);\n            }\n\n            \/\/ \u6ca1\u6709 fallback \u51fd\u6570\u53ef\u4ee5\u5904\u7406\u8be5\u5f02\u5e38\uff0c\u76f4\u63a5\u629b\u51fa\n            throw ex;\n\n        } finally {\n            \/\/ \u91ca\u653e Entry \u8d44\u6e90\n            if (entry != null) {\n                entry.exit(1, pjp.getArgs());\n            }\n        }\n    }\n\n    \/**\n     * \u5904\u7406 BlockException\n     *\n     * blockHandler \/ blockHandlerClass \u8bf4\u660e\uff1a\n     * - blockHandler\uff1a\u5bf9\u5e94\u5904\u7406 BlockException \u7684\u51fd\u6570\u540d\u79f0\uff0c\u53ef\u9009\u9879\n     * - blockHandler \u51fd\u6570\u7b7e\u540d\uff1a\u4e0e\u539f\u65b9\u6cd5\u76f8\u5339\u914d\u5e76\u4e14\u6700\u540e\u52a0\u4e00\u4e2a\u989d\u5916\u7684\u53c2\u6570\uff0c\u7c7b\u578b\u4e3a BlockException\n     * - blockHandler \u51fd\u6570\u9ed8\u8ba4\u9700\u8981\u548c\u539f\u65b9\u6cd5\u5728\u540c\u4e00\u4e2a\u7c7b\u4e2d\n     * - \u82e5\u5e0c\u671b\u4f7f\u7528\u5176\u4ed6\u7c7b\u7684\u51fd\u6570\uff0c\u5219\u53ef\u4ee5\u6307\u5b9a blockHandlerClass \u4e3a\u5bf9\u5e94\u7684\u7c7b\u7684 Class \u5bf9\u8c61\n     * - \u6ce8\u610f\uff1ablockHandlerClass \u4e2d\u5bf9\u5e94\u7684\u51fd\u6570\u5fc5\u987b\u4e3a static \u51fd\u6570\uff0c\u5426\u5219\u65e0\u6cd5\u89e3\u6790\n     *\/\n    protected Object handleBlockException(ProceedingJoinPoint pjp, SentinelResource annotation, BlockException ex)\n            throws Throwable {\n\n        \/\/ \u6267\u884c blockHandler \u65b9\u6cd5\uff08\u5982\u679c\u914d\u7f6e\u4e86\u7684\u8bdd\uff09\n        Method blockHandlerMethod = extractBlockHandlerMethod(pjp, annotation.blockHandler(),\n                annotation.blockHandlerClass());\n\n        if (blockHandlerMethod != null) {\n            Object[] originArgs = pjp.getArgs();\n            \/\/ \u6784\u9020\u53c2\u6570\uff1a\u539f\u65b9\u6cd5\u53c2\u6570 + BlockException\n            Object[] args = Arrays.copyOf(originArgs, originArgs.length + 1);\n            args[args.length - 1] = ex;\n\n            try {\n                \/\/ \u6839\u636e static \u65b9\u6cd5\u4e0e\u5426\u8fdb\u884c\u4e0d\u540c\u7684\u8c03\u7528\n                if (isStatic(blockHandlerMethod)) {\n                    return blockHandlerMethod.invoke(null, args);\n                }\n                return blockHandlerMethod.invoke(pjp.getTarget(), args);\n            } catch (InvocationTargetException e) {\n                \/\/ \u629b\u51fa\u5b9e\u9645\u7684\u5f02\u5e38\n                throw e.getTargetException();\n            }\n        }\n\n        \/\/ \u5982\u679c\u6ca1\u6709 blockHandler\uff0c\u5219\u5c1d\u8bd5\u6267\u884c fallback\n        return handleFallback(pjp, annotation, ex);\n    }\n\n    \/**\n     * \u5904\u7406 Fallback \u903b\u8f91\n     *\n     * fallback \/ fallbackClass \u8bf4\u660e\uff1a\n     * - fallback\uff1afallback \u51fd\u6570\u540d\u79f0\uff0c\u53ef\u9009\u9879\uff0c\u7528\u4e8e\u5728\u629b\u51fa\u5f02\u5e38\u7684\u65f6\u5019\u63d0\u4f9b fallback \u5904\u7406\u903b\u8f91\n     * - fallback \u51fd\u6570\u53ef\u4ee5\u9488\u5bf9\u6240\u6709\u7c7b\u578b\u7684\u5f02\u5e38\uff08\u9664\u4e86 exceptionsToIgnore \u91cc\u9762\u6392\u9664\u6389\u7684\u5f02\u5e38\u7c7b\u578b\uff09\u8fdb\u884c\u5904\u7406\n     *\n     * fallback \u51fd\u6570\u7b7e\u540d\u548c\u4f4d\u7f6e\u8981\u6c42\uff1a\n     * - \u8fd4\u56de\u503c\u7c7b\u578b\u5fc5\u987b\u4e0e\u539f\u51fd\u6570\u8fd4\u56de\u503c\u7c7b\u578b\u4e00\u81f4\n     * - \u65b9\u6cd5\u53c2\u6570\u5217\u8868\u9700\u8981\u548c\u539f\u51fd\u6570\u4e00\u81f4\uff0c\u6216\u8005\u53ef\u4ee5\u989d\u5916\u591a\u4e00\u4e2a Throwable \u7c7b\u578b\u7684\u53c2\u6570\u7528\u4e8e\u63a5\u6536\u5bf9\u5e94\u7684\u5f02\u5e38\n     * - fallback \u51fd\u6570\u9ed8\u8ba4\u9700\u8981\u548c\u539f\u65b9\u6cd5\u5728\u540c\u4e00\u4e2a\u7c7b\u4e2d\n     * - \u82e5\u5e0c\u671b\u4f7f\u7528\u5176\u4ed6\u7c7b\u7684\u51fd\u6570\uff0c\u5219\u53ef\u4ee5\u6307\u5b9a fallbackClass \u4e3a\u5bf9\u5e94\u7684\u7c7b\u7684 Class \u5bf9\u8c61\n     * - \u6ce8\u610f\uff1afallbackClass \u4e2d\u5bf9\u5e94\u7684\u51fd\u6570\u5fc5\u987b\u4e3a static \u51fd\u6570\uff0c\u5426\u5219\u65e0\u6cd5\u89e3\u6790\n     *\/\n    protected Object handleFallback(ProceedingJoinPoint pjp, String fallback, String defaultFallback,\n                                    Class&lt;?&gt;[] fallbackClass, Throwable ex) throws Throwable {\n        Object[] originArgs = pjp.getArgs();\n\n        \/\/ \u6267\u884c fallback \u51fd\u6570\uff08\u5982\u679c\u914d\u7f6e\u4e86\u7684\u8bdd\uff09\n        Method fallbackMethod = extractFallbackMethod(pjp, fallback, fallbackClass);\n\n        if (fallbackMethod != null) {\n            \/\/ \u6784\u9020\u53c2\u6570\uff1a\u6839\u636e fallback \u65b9\u6cd5\u7684\u53c2\u6570\u6570\u91cf\u51b3\u5b9a\u662f\u5426\u6dfb\u52a0\u5f02\u5e38\u53c2\u6570\n            int paramCount = fallbackMethod.getParameterTypes().length;\n            Object[] args;\n            if (paramCount == originArgs.length) {\n                args = originArgs;\n            } else {\n                args = Arrays.copyOf(originArgs, originArgs.length + 1);\n                args[args.length - 1] = ex;\n            }\n\n            try {\n                \/\/ \u6839\u636e static \u65b9\u6cd5\u4e0e\u5426\u8fdb\u884c\u4e0d\u540c\u7684\u8c03\u7528\n                if (isStatic(fallbackMethod)) {\n                    return fallbackMethod.invoke(null, args);\n                }\n                return fallbackMethod.invoke(pjp.getTarget(), args);\n            } catch (InvocationTargetException e) {\n                \/\/ \u629b\u51fa\u5b9e\u9645\u7684\u5f02\u5e38\n                throw e.getTargetException();\n            }\n        }\n\n        \/\/ \u5982\u679c\u6ca1\u6709 fallback\uff0c\u5c1d\u8bd5\u4f7f\u7528 defaultFallback\n        return handleDefaultFallback(pjp, defaultFallback, fallbackClass, ex);\n    }\n}<\/code><\/pre>\n<h2>\u4e94\u3001\u6d41\u63a7\u5904\u7406\u6838\u5fc3\u903b\u8f91<\/h2>\n<p>\u4ece\u5165\u53e3\u51fd\u6570\u5f00\u59cb\uff0c\u6211\u4eec\u6df1\u5165\u5230\u6d41\u63a7\u5904\u7406\u7684\u6838\u5fc3\u903b\u8f91\u3002<\/p>\n<h3>\u5165\u53e3\u51fd\u6570\u8c03\u7528\u94fe<\/h3>\n<pre><code>public class SphU {\n\n    \/**\n     * \u521b\u5efa\u9650\u6d41\u5165\u53e3\n     *\n     * @param name \u8d44\u6e90\u540d\u79f0\n     * @param resourceType \u8d44\u6e90\u7c7b\u578b\n     * @param trafficType \u6d41\u91cf\u7c7b\u578b\uff08IN \u6216 OUT\uff09\n     * @param args \u53c2\u6570\u6570\u7ec4\n     * @return Entry \u5bf9\u8c61\n     * @throws BlockException \u5982\u679c\u88ab\u9650\u6d41\u5219\u629b\u51fa\u6b64\u5f02\u5e38\n     *\/\n    public static Entry entry(String name, int resourceType, EntryType trafficType, Object[] args)\n            throws BlockException {\n        return Env.sph.entryWithType(name, resourceType, trafficType, 1, args);\n    }\n\n    public static Entry entry(String name, EntryType trafficType, int batchCount) throws BlockException {\n        return Env.sph.entry(name, trafficType, batchCount, OBJECTS0);\n    }\n}<\/code><\/pre>\n<pre><code>public class CtSph implements Sph {\n\n    @Override\n    public Entry entry(String name, EntryType type, int count, Object... args) throws BlockException {\n        StringResourceWrapper resource = new StringResourceWrapper(name, type);\n        return entry(resource, count, args);\n    }\n\n    public Entry entry(ResourceWrapper resourceWrapper, int count, Object... args) throws BlockException {\n        return entryWithPriority(resourceWrapper, count, false, args);\n    }\n\n    \/**\n     * \u5e26\u4f18\u5148\u7ea7\u7684\u5165\u53e3\u65b9\u6cd5\uff0c\u8fd9\u662f\u9650\u6d41\u7684\u6838\u5fc3\u903b\u8f91\n     *\/\n    private Entry entryWithPriority(ResourceWrapper resourceWrapper, int count, boolean prioritized, Object... args)\n            throws BlockException {\n        Context context = ContextUtil.getContext();\n\n        \/\/ \u5982\u679c\u4e0a\u4e0b\u6587\u6570\u91cf\u8d85\u8fc7\u9608\u503c\uff0c\u5219\u4e0d\u8fdb\u884c\u89c4\u5219\u68c0\u67e5\n        if (context instanceof NullContext) {\n            \/\/ NullContext \u8868\u793a\u4e0a\u4e0b\u6587\u6570\u91cf\u8d85\u8fc7\u4e86\u9608\u503c\uff0c\u8fd9\u91cc\u53ea\u521d\u59cb\u5316 Entry\uff0c\u4e0d\u8fdb\u884c\u89c4\u5219\u68c0\u67e5\n            return new CtEntry(resourceWrapper, null, context);\n        }\n\n        \/\/ \u5982\u679c\u6ca1\u6709\u4e0a\u4e0b\u6587\uff0c\u4f7f\u7528\u9ed8\u8ba4\u4e0a\u4e0b\u6587\n        if (context == null) {\n            context = InternalContextUtil.internalEnter(Constants.CONTEXT_DEFAULT_NAME);\n        }\n\n        \/\/ \u5982\u679c\u5168\u5c40\u5f00\u5173\u5173\u95ed\uff0c\u5219\u4e0d\u8fdb\u884c\u89c4\u5219\u68c0\u67e5\n        if (!Constants.ON) {\n            return new CtEntry(resourceWrapper, null, context);\n        }\n\n        \/\/ \u83b7\u53d6\u6216\u521b\u5efa ProcessorSlotChain\uff08\u8d23\u4efb\u94fe\uff09\n        ProcessorSlot&lt;Object&gt; chain = lookProcessChain(resourceWrapper);\n\n        \/*\n         * \u5982\u679c\u8d44\u6e90\uff08slot chain\uff09\u6570\u91cf\u8d85\u8fc7 {@link Constants.MAX_SLOT_CHAIN_SIZE}\uff0c\n         * \u5219\u4e0d\u8fdb\u884c\u89c4\u5219\u68c0\u67e5\n         *\/\n        if (chain == null) {\n            return new CtEntry(resourceWrapper, null, context);\n        }\n\n        \/\/ \u521b\u5efa Entry \u5bf9\u8c61\n        Entry e = new CtEntry(resourceWrapper, chain, context);\n\n        try {\n            \/\/ \u6267\u884c\u8d23\u4efb\u94fe\u8fdb\u884c\u89c4\u5219\u68c0\u67e5\n            chain.entry(context, resourceWrapper, null, count, prioritized, args);\n        } catch (BlockException e1) {\n            \/\/ \u5982\u679c\u88ab\u9650\u6d41\uff0c\u91ca\u653e Entry \u5e76\u629b\u51fa\u5f02\u5e38\n            e.exit(count, args);\n            throw e1;\n        } catch (Throwable e1) {\n            \/\/ \u8fd9\u4e0d\u5e94\u8be5\u53d1\u751f\uff0c\u9664\u975e Sentinel \u5185\u90e8\u5b58\u5728\u9519\u8bef\n            log.warn(&quot;Sentinel unexpected exception,{}&quot;, e1.getMessage());\n        }\n        return e;\n    }\n}<\/code><\/pre>\n<h3>ProcessorSlotChain \u529f\u80fd\u63d2\u69fd\u94fe<\/h3>\n<p>lookProcessChain\u00a0\u65b9\u6cd5\u5b9e\u9645\u521b\u5efa\u4e86\u00a0ProcessorSlotChain\u00a0\u529f\u80fd\u63d2\u69fd\u94fe\u3002ProcessorSlotChain\u00a0\u91c7\u7528\u8d23\u4efb\u94fe\u6a21\u5f0f\uff0c\u5c06\u4e0d\u540c\u7684\u529f\u80fd\uff08\u9650\u6d41\u3001\u964d\u7ea7\u3001\u7cfb\u7edf\u4fdd\u62a4\uff09\u7ec4\u5408\u5728\u4e00\u8d77\u3002<\/p>\n<h4>SlotChain \u7684\u83b7\u53d6\u4e0e\u521b\u5efa<\/h4>\n<pre><code>ProcessorSlot&lt;Object&gt; lookProcessChain(ResourceWrapper resourceWrapper) {\n    \/\/ \u5148\u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\n    ProcessorSlotChain chain = chainMap.get(resourceWrapper);\n\n    if (chain == null) {\n        \/\/ \u53cc\u91cd\u68c0\u67e5\u9501\uff0c\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\n        synchronized (LOCK) {\n            chain = chainMap.get(resourceWrapper);\n            if (chain == null) {\n                \/\/ Entry \u5927\u5c0f\u9650\u5236\n                if (chainMap.size() &gt;= Constants.MAX_SLOT_CHAIN_SIZE) {\n                    return null;\n                }\n\n                \/\/ \u521b\u5efa\u65b0\u7684 SlotChain\n                chain = SlotChainProvider.newSlotChain();\n\n                \/\/ \u4f7f\u7528\u4e0d\u53ef\u53d8\u6a21\u5f0f\u66f4\u65b0\u7f13\u5b58\n                Map&lt;ResourceWrapper, ProcessorSlotChain&gt; newMap =\n                    new HashMap&lt;ResourceWrapper, ProcessorSlotChain&gt;(chainMap.size() + 1);\n                newMap.putAll(chainMap);\n                newMap.put(resourceWrapper, chain);\n                chainMap = newMap;\n            }\n        }\n    }\n    return chain;\n}<\/code><\/pre>\n<h4>SlotChain \u7684\u6784\u5efa<\/h4>\n<pre><code>public class DefaultSlotChainBuilder implements SlotChainBuilder {\n\n    @Override\n    public ProcessorSlotChain build() {\n        ProcessorSlotChain chain = new DefaultProcessorSlotChain();\n\n        \/\/ \u901a\u8fc7 SPI \u52a0\u8f7d\u6240\u6709 ProcessorSlot \u5e76\u6392\u5e8f\n        List&lt;ProcessorSlot&gt; sortedSlotList = SpiLoader.loadPrototypeInstanceListSorted(ProcessorSlot.class);\n\n        for (ProcessorSlot slot : sortedSlotList) {\n            \/\/ \u53ea\u5904\u7406\u7ee7\u627f\u81ea AbstractLinkedProcessorSlot \u7684 Slot\n            if (!(slot instanceof AbstractLinkedProcessorSlot)) {\n                RecordLog.warn(&quot;The ProcessorSlot(&quot; + slot.getClass().getCanonicalName() +\n                    &quot;) is not an instance of AbstractLinkedProcessorSlot, can't be added into ProcessorSlotChain&quot;);\n                continue;\n            }\n\n            \/\/ \u5c06 Slot \u6dfb\u52a0\u5230\u8d23\u4efb\u94fe\u5c3e\u90e8\n            chain.addLast((AbstractLinkedProcessorSlot&lt;?&gt;) slot);\n        }\n\n        return chain;\n    }\n}<\/code><\/pre>\n<h4>SlotChain \u7684\u529f\u80fd\u5212\u5206<\/h4>\n<p>Slot Chain \u53ef\u4ee5\u5206\u4e3a\u4e24\u90e8\u5206\uff1a<\/p>\n<ul>\n<li>\u7edf\u8ba1\u6570\u636e\u6784\u5efa\u90e8\u5206\uff08statistic\uff09\uff1a\u8d1f\u8d23\u6536\u96c6\u5404\u79cd\u6307\u6807\u6570\u636e\uff1b<\/li>\n<li>\u5224\u65ad\u90e8\u5206\uff08rule checking\uff09\uff1a\u6839\u636e\u89c4\u5219\u5224\u65ad\u662f\u5426\u9650\u6d41\u3002<\/li>\n<\/ul>\n<p><img decoding=\"async\" width=\"723\" height=\"510\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdnYTD\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5b98\u65b9\u67b6\u6784\u56fe\u5f88\u597d\u5730\u89e3\u91ca\u4e86\u5404\u4e2a Slot \u7684\u4f5c\u7528\u53ca\u5176\u8d1f\u8d23\u7684\u90e8\u5206\u3002\u76ee\u524d\u00a0ProcessorSlotChain\u00a0\u7684\u8bbe\u8ba1\u662f\u4e00\u4e2a\u8d44\u6e90\u5bf9\u5e94\u4e00\u4e2a\uff0c\u6784\u5efa\u597d\u540e\u7f13\u5b58\u8d77\u6765\uff0c\u65b9\u4fbf\u4e0b\u6b21\u76f4\u63a5\u53d6\u7528\u3002<\/p>\n<h4>\u5404 Slot \u7684\u6267\u884c\u987a\u5e8f<\/h4>\n<p>\u4ee5\u4e0b\u662f Sentinel \u4e2d\u5404\u4e2a Slot \u7684\u9ed8\u8ba4\u6267\u884c\u987a\u5e8f\uff1a<\/p>\n<pre><code>NodeSelectorSlot\n    \u2193\nClusterBuilderSlot\n    \u2193\nStatisticSlot\n    \u2193\nParamFlowSlot\n    \u2193\nSystemSlot\n    \u2193\nAuthoritySlot\n    \u2193\nFlowSlot\n    \u2193\nDegradeSlot<\/code><\/pre>\n<h3>NodeSelectorSlot &#8211; \u4e0a\u4e0b\u6587\u8282\u70b9\u9009\u62e9<\/h3>\n<p>\u8fd9\u4e2a\u529f\u80fd\u63d2\u69fd\u4e3b\u8981\u4e3a\u8d44\u6e90\u4e0b\u4e0d\u540c\u7684\u4e0a\u4e0b\u6587\u521b\u5efa\u5bf9\u5e94\u7684\u00a0DefaultNode\uff08\u5b9e\u9645\u7528\u4e8e\u7edf\u8ba1\u6307\u6807\u4fe1\u606f\uff09\u3002\u89e3\u91ca\u4e00\u4e0bSentinel\u4e2d\u7684Node\u662f\u4ec0\u4e48\uff0c\u7b80\u5355\u6765\u8bf4\u5c31\u662f\u6bcf\u4e2a\u8d44\u6e90\u7edf\u8ba1\u6307\u6807\u5b58\u653e\u7684\u5bb9\u5668\uff0c\u53ea\u4e0d\u8fc7\u5185\u90e8\u7531\u4e8e\u4e0d\u540c\u7684\u7edf\u8ba1\u53e3\u5f84\uff08\u79d2\u7ea7\u3001\u5206\u949f\u53ca\uff09\u800c\u5206\u522b\u6709\u4e0d\u540c\u7684\u7edf\u8ba1\u7a97\u53e3\u3002Node\u5728Sentinel\u4e0d\u662f\u5355\u4e00\u7684\u7ed3\u6784\uff0c\u800c\u662f\u603b\u4f53\u4e0a\u5f62\u6210\u7236\u5b50\u5173\u7cfb\u7684\u6811\u5f62\u7ed3\u6784\u3002<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"575\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdnYTE\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4e0d\u540c\u7684\u8c03\u7528\u4f1a\u6709\u4e0d\u540c\u7684 context \u540d\u79f0\uff0c\u5982\u5728\u5f53\u524d MVC \u573a\u666f\u4e0b\uff0c\u4e0a\u4e0b\u6587\u4e3a\u00a0sentinel_web_servlet_context\u3002<\/p>\n<pre><code>public class NodeSelectorSlot extends AbstractLinkedProcessorSlot&lt;Object&gt; {\n\n    \/**\n     * \u540c\u4e00\u4e2a\u8d44\u6e90\u5728\u4e0d\u540c\u4e0a\u4e0b\u6587\u4e2d\u7684 DefaultNode \u6620\u5c04\n     *\/\n    private volatile Map&lt;String, DefaultNode&gt; map = new HashMap&lt;String, DefaultNode&gt;(10);\n\n    @Override\n    public void entry(Context context, ResourceWrapper resourceWrapper, Object obj, int count,\n                      boolean prioritized, Object... args) throws Throwable {\n        \/\/ \u4ece\u6620\u5c04\u8868\u4e2d\u83b7\u53d6\u5f53\u524d\u4e0a\u4e0b\u6587\u5bf9\u5e94\u7684\u8282\u70b9\n        DefaultNode node = map.get(context.getName());\n\n        if (node == null) {\n            \/\/ \u53cc\u91cd\u68c0\u67e5\u9501\uff0c\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\n            synchronized (this) {\n                node = map.get(context.getName());\n                if (node == null) {\n                    \/\/ \u521b\u5efa\u65b0\u7684 DefaultNode\n                    node = new DefaultNode(resourceWrapper, null);\n\n                    \/\/ \u4f7f\u7528\u5199\u65f6\u590d\u5236\u66f4\u65b0\u7f13\u5b58\n                    HashMap&lt;String, DefaultNode&gt; cacheMap = new HashMap&lt;String, DefaultNode&gt;(map.size());\n                    cacheMap.putAll(map);\n                    cacheMap.put(context.getName(), node);\n                    map = cacheMap;\n\n                    \/\/ \u6784\u5efa\u8c03\u7528\u6811\n                    ((DefaultNode) context.getLastNode()).addChild(node);\n                }\n            }\n        }\n\n        \/\/ \u8bbe\u7f6e\u5f53\u524d\u4e0a\u4e0b\u6587\u7684\u5f53\u524d\u8282\u70b9\n        context.setCurNode(node);\n        \/\/ \u7ee7\u7eed\u6267\u884c\u540e\u7eed Slot\n        fireEntry(context, resourceWrapper, node, count, prioritized, args);\n    }\n\n    @Override\n    public void exit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) {\n        fireExit(context, resourceWrapper, count, args);\n    }\n}<\/code><\/pre>\n<h3>ClusterBuilderSlot &#8211; \u96c6\u7fa4\u8282\u70b9\u6784\u5efa<\/h3>\n<p>\u8fd9\u4e2a\u529f\u80fd\u69fd\u4e3b\u8981\u7528\u4e8e\u521b\u5efa\u00a0ClusterNode\u3002ClusterNode\u00a0\u548c\u00a0DefaultNode\u00a0\u7684\u533a\u522b\u662f\uff1a<\/p>\n<p>DefaultNode\u00a0\u662f\u7279\u5b9a\u4e8e\u4e0a\u4e0b\u6587\u7684\uff08context-specific\uff09\uff1b<\/p>\n<p>ClusterNode\u00a0\u662f\u4e0d\u533a\u5206\u4e0a\u4e0b\u6587\u7684\uff08context-independent\uff09\uff0c\u7528\u4e8e\u7edf\u8ba1\u8be5\u8d44\u6e90\u5728\u6240\u6709\u4e0a\u4e0b\u6587\u4e2d\u7684\u6574\u4f53\u6570\u636e\u3002<\/p>\n<pre><code>public class ClusterBuilderSlot extends AbstractLinkedProcessorSlot&lt;DefaultNode&gt; {\n\n    \/**\n     * \u5168\u5c40 ClusterNode \u6620\u5c04\u8868\n     *\/\n    private static volatile Map&lt;ResourceWrapper, ClusterNode&gt; clusterNodeMap = new HashMap&lt;&gt;();\n\n    private static final Object lock = new Object();\n\n    private volatile ClusterNode clusterNode = null;\n\n    @Override\n    public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count,\n                      boolean prioritized, Object... args) throws Throwable {\n        \/\/ \u521b\u5efa ClusterNode\uff08\u5982\u679c\u4e0d\u5b58\u5728\uff09\n        if (clusterNode == null) {\n            synchronized (lock) {\n                if (clusterNode == null) {\n                    \/\/ \u521b\u5efa\u96c6\u7fa4\u8282\u70b9\n                    clusterNode = new ClusterNode(resourceWrapper.getName(), resourceWrapper.getResourceType());\n\n                    \/\/ \u66f4\u65b0\u5168\u5c40\u6620\u5c04\u8868\n                    HashMap&lt;ResourceWrapper, ClusterNode&gt; newMap =\n                        new HashMap&lt;&gt;(Math.max(clusterNodeMap.size(), 16));\n                    newMap.putAll(clusterNodeMap);\n                    newMap.put(node.getId(), clusterNode);\n\n                    clusterNodeMap = newMap;\n                }\n            }\n        }\n\n        \/\/ \u5c06 ClusterNode \u8bbe\u7f6e\u5230 DefaultNode \u4e2d\n        node.setClusterNode(clusterNode);\n\n        \/\/ \u5982\u679c\u6709\u6765\u6e90\u6807\u8bc6\uff0c\u5219\u521b\u5efa origin node\n        if (!&quot;&quot;.equals(context.getOrigin())) {\n            Node originNode = node.getClusterNode().getOrCreateOriginNode(context.getOrigin());\n            context.getCurEntry().setOriginNode(originNode);\n        }\n\n        \/\/ \u7ee7\u7eed\u6267\u884c\u540e\u7eed Slot\n        fireEntry(context, resourceWrapper, node, count, prioritized, args);\n    }\n}<\/code><\/pre>\n<h3>StatisticSlot &#8211; \u7edf\u8ba1\u63d2\u69fd<\/h3>\n<p>StatisticSlot\u00a0\u662f Sentinel \u6700\u91cd\u8981\u7684\u7c7b\u4e4b\u4e00\uff0c\u7528\u4e8e\u6839\u636e\u89c4\u5219\u5224\u65ad\u7ed3\u679c\u8fdb\u884c\u76f8\u5e94\u7684\u7edf\u8ba1\u64cd\u4f5c\u3002<\/p>\n<h4>\u7edf\u8ba1\u903b\u8f91\u8bf4\u660e<\/h4>\n<p>entry \u7684\u65f6\u5019\uff1a<\/p>\n<p>\u4f9d\u6b21\u6267\u884c\u540e\u7eed\u7684\u5224\u65ad Slot\uff1b<\/p>\n<p>\u6bcf\u4e2a Slot \u89e6\u53d1\u6d41\u63a7\u4f1a\u629b\u51fa\u5f02\u5e38\uff08BlockException\u00a0\u7684\u5b50\u7c7b\uff09\uff1b<\/p>\n<p>\u82e5\u6709\u00a0BlockException\u00a0\u629b\u51fa\uff0c\u5219\u8bb0\u5f55 block \u6570\u636e\uff1b<\/p>\n<p>\u82e5\u65e0\u5f02\u5e38\u629b\u51fa\u5219\u7b97\u4f5c\u53ef\u901a\u8fc7\uff08pass\uff09\uff0c\u8bb0\u5f55 pass \u6570\u636e\u3002<\/p>\n<p><strong>exit \u7684\u65f6\u5019\uff1a<\/strong><\/p>\n<p>\u82e5\u65e0 error\uff08\u65e0\u8bba\u662f\u4e1a\u52a1\u5f02\u5e38\u8fd8\u662f\u6d41\u63a7\u5f02\u5e38\uff09\uff0c\u8bb0\u5f55 complete\uff08success\uff09\u4ee5\u53ca RT\uff0c\u7ebf\u7a0b\u6570 -1\u3002<\/p>\n<p><strong>\u8bb0\u5f55\u6570\u636e\u7684\u7ef4\u5ea6\uff1a<\/strong><\/p>\n<p>\u7ebf\u7a0b\u6570 +1\uff1b<\/p>\n<p>\u8bb0\u5f55\u5f53\u524d\u00a0DefaultNode\u00a0\u6570\u636e\uff1b<\/p>\n<p>\u8bb0\u5f55\u5bf9\u5e94\u7684\u00a0originNode\u00a0\u6570\u636e\uff08\u82e5\u5b58\u5728 origin\uff09\uff1b<\/p>\n<p>\u7d2f\u8ba1 IN \u7edf\u8ba1\u6570\u636e\uff08\u82e5\u6d41\u91cf\u7c7b\u578b\u4e3a IN\uff09\u3002<\/p>\n<pre><code>public class StatisticSlot extends AbstractLinkedProcessorSlot&lt;DefaultNode&gt; {\n\n    @Override\n    public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count,\n                      boolean prioritized, Object... args) throws Throwable {\n        try {\n            \/\/ \u6b64\u4f4d\u7f6e\u4f1a\u8c03\u7528 SlotChain \u4e2d\u540e\u7eed\u7684\u6240\u6709 Slot\uff0c\u5b8c\u6210\u6240\u6709\u89c4\u5219\u68c0\u6d4b\n            fireEntry(context, resourceWrapper, node, count, prioritized, args);\n\n            \/\/ \u8bf7\u6c42\u901a\u8fc7\uff0c\u589e\u52a0\u7ebf\u7a0b\u6570\u548c\u901a\u8fc7\u6570\n            \/\/ \u4ee3\u7801\u8fd0\u884c\u5230\u8fd9\u4e2a\u4f4d\u7f6e\uff0c\u5c31\u8bc1\u660e\u4e4b\u524d\u7684\u6240\u6709 Slot \u68c0\u6d4b\u90fd\u901a\u8fc7\u4e86\n            \/\/ \u6b64\u65f6\u5c31\u53ef\u4ee5\u7edf\u8ba1\u8bf7\u6c42\u7684\u76f8\u5e94\u6570\u636e\u4e86\n\n            \/\/ \u589e\u52a0\u7ebf\u7a0b\u6570\uff08+1\uff09\n            node.increaseThreadNum();\n            \/\/ \u589e\u52a0\u901a\u8fc7\u8bf7\u6c42\u7684\u6570\u91cf\uff08\u8fd9\u91cc\u6d89\u53ca\u5230\u6ed1\u52a8\u7a97\u53e3\u7b97\u6cd5\uff09\n            node.addPassRequest(count);\n\n            \/\/ \u7701\u7565\u5176\u4ed6\u7edf\u8ba1\u903b\u8f91...\n\n        } catch (PriorityWaitException ex) {\n            \/\/ \u5982\u679c\u662f\u4f18\u5148\u7ea7\u7b49\u5f85\u5f02\u5e38\uff0c\u8bb0\u5f55\u4f18\u5148\u7ea7\u7b49\u5f85\u6570\n            node.increaseThreadNum();\n            if (context.getCurEntry().getOriginNode() != null) {\n                context.getCurEntry().getOriginNode().increaseThreadNum();\n            }\n            if (resourceWrapper.getEntryType() == EntryType.IN) {\n                \/\/ \u8bb0\u5f55\u5165\u7ad9\u7edf\u8ba1\u6570\u636e\n                Constants.ENTRY_NODE.increaseThreadNum();\n            }\n            throw ex;\n\n        } catch (BlockException e) {\n            \/\/ \u5982\u679c\u88ab\u9650\u6d41\uff0c\u8bb0\u5f55\u88ab\u9650\u6d41\u6570\n            \/\/ \u7701\u7565 block \u7edf\u8ba1\u903b\u8f91...\n            throw e;\n\n        } catch (Throwable ex) {\n            \/\/ \u5982\u679c\u53d1\u751f\u4e1a\u52a1\u5f02\u5e38\uff0c\u8bb0\u5f55\u5f02\u5e38\u6570\n            \/\/ \u7701\u7565\u5f02\u5e38\u7edf\u8ba1\u903b\u8f91...\n            throw ex;\n        }\n    }\n\n    @Override\n    public void exit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) {\n        \/\/ \u82e5\u65e0 error\uff08\u65e0\u8bba\u662f\u4e1a\u52a1\u5f02\u5e38\u8fd8\u662f\u6d41\u63a7\u5f02\u5e38\uff09\uff0c\u8bb0\u5f55 complete\uff08success\uff09\u4ee5\u53ca RT\uff0c\u7ebf\u7a0b\u6570-1\n        \/\/ \u8bb0\u5f55\u6570\u636e\u7684\u7ef4\u5ea6\uff1a\u7ebf\u7a0b\u6570+1\u3001\u8bb0\u5f55\u5f53\u524d DefaultNode \u6570\u636e\u3001\u8bb0\u5f55\u5bf9\u5e94\u7684 originNode \u6570\u636e\uff08\u82e5\u5b58\u5728 origin\uff09\n        \/\/ \u3001\u7d2f\u8ba1 IN \u7edf\u8ba1\u6570\u636e\uff08\u82e5\u6d41\u91cf\u7c7b\u578b\u4e3a IN\uff09\n        \/\/ \u7701\u7565 exit \u7edf\u8ba1\u903b\u8f91...\n    }\n}<\/code><\/pre>\n<h4>StatisticNode \u6570\u636e\u7ed3\u6784<\/h4>\n<p>\u5230\u8fd9\u91cc\uff0cStatisticSlot\u00a0\u7684\u4f5c\u7528\u5df2\u7ecf\u6bd4\u8f83\u6e05\u6670\u4e86\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u9700\u8981\u5206\u6790\u5b83\u7684\u7edf\u8ba1\u6570\u636e\u7ed3\u6784\u3002fireEntry\u00a0\u8c03\u7528\u5411\u4e0b\u7684\u8282\u70b9\u548c\u4e4b\u524d\u7684\u65b9\u5f0f\u4e00\u6837\uff0c\u5269\u4e0b\u7684\u8282\u70b9\u4e3b\u8981\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>ParamFlowSlot\uff1b<\/li>\n<li>SystemSlot\uff1b<\/li>\n<li>AuthoritySlot\uff1b<\/li>\n<li>FlowSlot\uff1b<\/li>\n<li>DegradeSlot\uff1b<\/li>\n<\/ul>\n<p>\u5176\u4e2d\u6bd4\u8f83\u5e38\u89c1\u7684\u662f\u6d41\u63a7\u548c\u7194\u65ad\uff1aFlowSlot\u3001DegradeSlot\uff0c\u6240\u4ee5\u4e0b\u9762\u6211\u4eec\u7740\u91cd\u5206\u6790\u00a0FlowSlot\u3002<\/p>\n<h2>\u516d\u3001FlowSlot &#8211; \u6d41\u63a7\u63d2\u69fd<\/h2>\n<p>\u8fd9\u4e2a Slot \u4e3b\u8981\u6839\u636e\u9884\u8bbe\u7684\u8d44\u6e90\u7684\u7edf\u8ba1\u4fe1\u606f\uff0c\u6309\u7167\u56fa\u5b9a\u7684\u6b21\u5e8f\u4f9d\u6b21\u751f\u6548\u3002\u5982\u679c\u4e00\u4e2a\u8d44\u6e90\u5bf9\u5e94\u4e24\u6761\u6216\u8005\u591a\u6761\u6d41\u63a7\u89c4\u5219\uff0c\u5219\u4f1a\u6839\u636e\u5982\u4e0b\u6b21\u5e8f\u4f9d\u6b21\u68c0\u9a8c\uff0c\u76f4\u5230\u5168\u90e8\u901a\u8fc7\u6216\u8005\u6709\u4e00\u4e2a\u89c4\u5219\u751f\u6548\u4e3a\u6b62\u3002<\/p>\n<h3>FlowSlot \u6838\u5fc3\u903b\u8f91<\/h3>\n<pre><code>@SpiOrder(-2000)\npublic class FlowSlot extends AbstractLinkedProcessorSlot&lt;DefaultNode&gt; {\n\n    @Override\n    public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count,\n                      boolean prioritized, Object... args) throws Throwable {\n        \/\/ \u6267\u884c\u6d41\u63a7\u68c0\u67e5\n        checkFlow(resourceWrapper, context, node, count, prioritized);\n\n        \/\/ \u7ee7\u7eed\u6267\u884c\u540e\u7eed Slot\n        fireEntry(context, resourceWrapper, node, count, prioritized, args);\n    }\n\n    \/\/ \u7701\u7565\u5176\u4ed6\u65b9\u6cd5...\n}<\/code><\/pre>\n<h3>checkFlow \u65b9\u6cd5\u8be6\u89e3<\/h3>\n<pre><code>\/**\n * \u6267\u884c\u6d41\u63a7\u68c0\u67e5\n *\n * @param ruleProvider \u89c4\u5219\u63d0\u4f9b\u8005\u51fd\u6570\n * @param resource \u8d44\u6e90\u5305\u88c5\u5668\n * @param context \u4e0a\u4e0b\u6587\n * @param node \u8282\u70b9\n * @param count \u8bf7\u6c42\u6570\u91cf\n * @param prioritized \u662f\u5426\u4f18\u5148\n * @throws BlockException \u5982\u679c\u88ab\u9650\u6d41\u5219\u629b\u51fa\u5f02\u5e38\n *\/\npublic void checkFlow(Function&lt;String, Collection&lt;FlowRule&gt;&gt; ruleProvider, ResourceWrapper resource,\n                      Context context, DefaultNode node, int count, boolean prioritized) throws BlockException {\n    \/\/ \u5224\u65ad\u89c4\u5219\u548c\u8d44\u6e90\u4e0d\u80fd\u4e3a\u7a7a\n    if (ruleProvider == null || resource == null) {\n        return;\n    }\n\n    \/\/ \u83b7\u53d6\u6307\u5b9a\u8d44\u6e90\u7684\u6240\u6709\u6d41\u63a7\u89c4\u5219\n    Collection&lt;FlowRule&gt; rules = ruleProvider.apply(resource.getName());\n\n    \/\/ \u9010\u4e2a\u5e94\u7528\u6d41\u63a7\u89c4\u5219\u3002\u82e5\u65e0\u6cd5\u901a\u8fc7\u5219\u629b\u51fa\u5f02\u5e38\uff0c\u540e\u7eed\u89c4\u5219\u4e0d\u518d\u5e94\u7528\n    if (rules != null) {\n        for (FlowRule rule : rules) {\n            if (!canPassCheck(rule, context, node, count, prioritized)) {\n                \/\/ FlowException \u7ee7\u627f BlockException\n                throw new FlowException(rule.getLimitApp(), rule);\n            }\n        }\n    }\n}<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u91cc\u6211\u4eec\u5c31\u53ef\u4ee5\u5f97\u77e5\uff0c\u6d41\u63a7\u89c4\u5219\u662f\u901a\u8fc7\u00a0FlowRule\u00a0\u6765\u5b8c\u6210\u7684\uff0c\u6570\u636e\u6765\u6e90\u662f\u6211\u4eec\u4f7f\u7528\u7684\u6d41\u63a7\u63a7\u5236\u53f0\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4ee3\u7801\u8fdb\u884c\u8bbe\u7f6e\u3002<\/p>\n<h3>FlowRule \u6d41\u63a7\u89c4\u5219<\/h3>\n<p>\u6bcf\u6761\u6d41\u63a7\u89c4\u5219\u4e3b\u8981\u7531\u4e09\u4e2a\u8981\u7d20\u6784\u6210\uff1a<\/p>\n<ul>\n<li>grade\uff08\u9608\u503c\u7c7b\u578b\uff09\uff1a\u6309 QPS\uff08\u6bcf\u79d2\u8bf7\u6c42\u6570\uff09\u8fd8\u662f\u7ebf\u7a0b\u6570\u8fdb\u884c\u9650\u6d41;<\/li>\n<li>strategy\uff08\u8c03\u7528\u5173\u7cfb\u7b56\u7565\uff09\uff1a\u57fa\u4e8e\u8c03\u7528\u5173\u7cfb\u7684\u6d41\u63a7\u7b56\u7565;<\/li>\n<li>controlBehavior\uff08\u6d41\u63a7\u6548\u679c\uff09\uff1a\u5f53 QPS \u8d85\u8fc7\u9608\u503c\u65f6\u7684\u6d41\u91cf\u6574\u5f62\u884c\u4e3a\u3002<\/li>\n<\/ul>\n<pre><code>public class FlowRule extends AbstractRule {\n\n    public FlowRule() {\n        super();\n        \/\/ \u6765\u6e90\u9ed8\u8ba4 Default\n        setLimitApp(RuleConstant.LIMIT_APP_DEFAULT);\n    }\n\n    public FlowRule(String resourceName) {\n        super();\n        \/\/ \u8d44\u6e90\u540d\u79f0\n        setResource(resourceName);\n        setLimitApp(RuleConstant.LIMIT_APP_DEFAULT);\n    }\n\n    \/**\n     * \u6d41\u63a7\u7684\u9608\u503c\u7c7b\u578b\n     * 0: \u7ebf\u7a0b\u6570\n     * 1: QPS\n     *\/\n    private int grade = RuleConstant.FLOW_GRADE_QPS;\n\n    \/**\n     * \u6d41\u63a7\u9608\u503c\n     *\/\n    private double count;\n\n    \/**\n     * \u57fa\u4e8e\u8c03\u7528\u94fe\u7684\u6d41\u63a7\u7b56\u7565\n     * STRATEGY_DIRECT: \u76f4\u63a5\u6d41\u63a7\uff08\u6309\u6765\u6e90\uff09\n     * STRATEGY_RELATE: \u5173\u8054\u6d41\u63a7\uff08\u5173\u8054\u8d44\u6e90\uff09\n     * STRATEGY_CHAIN: \u94fe\u8def\u6d41\u63a7\uff08\u6309\u5165\u53e3\u8d44\u6e90\uff09\n     *\/\n    private int strategy = RuleConstant.STRATEGY_DIRECT;\n\n    \/**\n     * \u5173\u8054\u6d41\u63a7\u6a21\u5f0f\u4e0b\u7684\u5173\u8054\u8d44\u6e90\n     *\/\n    private String refResource;\n\n    \/**\n     * \u6d41\u63a7\u6548\u679c\uff08\u6d41\u91cf\u6574\u5f62\u884c\u4e3a\uff09\n     * 0: \u9ed8\u8ba4\uff08\u76f4\u63a5\u62d2\u7edd\uff09\n     * 1: \u9884\u70ed\uff08Warm Up\uff09\n     * 2: \u6392\u961f\u7b49\u5f85\uff08Rate Limiter\uff09\n     * 3: \u9884\u70ed + \u6392\u961f\u7b49\u5f85\uff08\u76ee\u524d\u63a7\u5236\u53f0\u6ca1\u6709\uff09\n     *\/\n    private int controlBehavior = RuleConstant.CONTROL_BEHAVIOR_DEFAULT;\n\n    \/**\n     * \u9884\u70ed\u65f6\u957f\uff08\u79d2\uff09\n     *\/\n    private int warmUpPeriodSec = 10;\n\n    \/**\n     * \u6392\u961f\u7b49\u5f85\u7684\u6700\u5927\u8d85\u65f6\u65f6\u95f4\uff08\u6beb\u79d2\uff09\n     *\/\n    private int maxQueueingTimeMs = 500;\n\n    \/**\n     * \u662f\u5426\u4e3a\u96c6\u7fa4\u6a21\u5f0f\n     *\/\n    private boolean clusterMode;\n\n    \/**\n     * \u96c6\u7fa4\u6a21\u5f0f\u914d\u7f6e\n     *\/\n    private ClusterFlowConfig clusterConfig;\n\n    \/**\n     * \u6d41\u91cf\u6574\u5f62\u63a7\u5236\u5668\n     *\/\n    private TrafficShapingController controller;\n\n    \/\/ \u7701\u7565 getter\/setter \u65b9\u6cd5...\n}<\/code><\/pre>\n<h2>\u4e03\u3001\u6ed1\u52a8\u7a97\u53e3\u7b97\u6cd5<\/h2>\n<p>\u4e0d\u7ba1\u6d41\u63a7\u89c4\u5219\u91c7\u7528\u4f55\u79cd\u6d41\u63a7\u7b97\u6cd5\uff0c\u5728\u5e95\u5c42\u90fd\u9700\u8981\u6709\u652f\u6301\u6307\u6807\u7edf\u8ba1\u7684\u6570\u636e\u7ed3\u6784\u4f5c\u4e3a\u652f\u6491\u3002\u5728 Sentinel \u4e2d\uff0c\u7528\u4e8e\u652f\u6491\u57fa\u4e8e QPS \u7b49\u9650\u6d41\u7684\u6570\u636e\u7ed3\u6784\u662f\u00a0StatisticNode\u3002<\/p>\n<h3>StatisticNode \u6570\u636e\u7ed3\u6784<\/h3>\n<pre><code>public class StatisticNode implements Node {\n\n    \/**\n     * \u4fdd\u5b58\u6700\u8fd1 1 \u79d2\u5185\u7684\u7edf\u8ba1\u6570\u636e\n     * \u6bcf\u4e2a\u6876\uff08bucket\uff09500ms\uff0c\u5171 2 \u4e2a\u6876\n     *\/\n    private transient volatile Metric rollingCounterInSecond =\n        new ArrayMetric(SampleCountProperty.SAMPLE_COUNT, IntervalProperty.INTERVAL);\n\n    \/**\n     * \u4fdd\u5b58\u6700\u8fd1 60 \u79d2\u7684\u7edf\u8ba1\u6570\u636e\n     * windowLengthInMs \u88ab\u7279\u610f\u8bbe\u7f6e\u4e3a 1000 \u6beb\u79d2\uff0c\u5373\u6bcf\u4e2a\u6876\u4ee3\u8868 1 \u79d2\n     * \u5171 60 \u4e2a\u6876\uff0c\u8fd9\u6837\u53ef\u4ee5\u83b7\u5f97\u6bcf\u79d2\u7cbe\u786e\u7684\u7edf\u8ba1\u4fe1\u606f\n     *\/\n    private transient Metric rollingCounterInMinute =\n        new ArrayMetric(60, 60 * 1000, false);\n\n    \/\/ \u7701\u7565\u5176\u4ed6\u5b57\u6bb5\u548c\u65b9\u6cd5...\n}<\/code><\/pre>\n<h3>ArrayMetric \u6838\u5fc3\u5b9e\u73b0<\/h3>\n<p>ArrayMetric\u00a0\u662f Sentinel \u4e2d\u6570\u636e\u91c7\u96c6\u7684\u6838\u5fc3\uff0c\u5185\u90e8\u4f7f\u7528\u4e86\u00a0BucketLeapArray\uff0c\u5373\u6ed1\u52a8\u7a97\u53e3\u7684\u601d\u60f3\u8fdb\u884c\u6570\u636e\u7684\u91c7\u96c6\u3002<\/p>\n<pre><code>public class ArrayMetric implements Metric {\n\n    \/**\n     * \u6ed1\u52a8\u7a97\u53e3\u6570\u7ec4\n     *\/\n    private final LeapArray&lt;MetricBucket&gt; data;\n\n    public ArrayMetric(int sampleCount, int intervalInMs) {\n        this.data = new OccupiableBucketLeapArray(sampleCount, intervalInMs);\n    }\n\n    public ArrayMetric(int sampleCount, int intervalInMs, boolean enableOccupy) {\n        if (enableOccupy) {\n            \/\/ \u53ef\u62a2\u5360\u7684\u6ed1\u52a8\u7a97\u53e3\uff0c\u652f\u6301\u501f\u7528\u672a\u6765\u7a97\u53e3\u7684\u914d\u989d\n            this.data = new OccupiableBucketLeapArray(sampleCount, intervalInMs);\n        } else {\n            \/\/ \u666e\u901a\u6ed1\u52a8\u7a97\u53e3\n            this.data = new BucketLeapArray(sampleCount, intervalInMs);\n        }\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u91cc\u6709\u4e24\u79cd\u5b9e\u73b0\uff1a<\/p>\n<ul>\n<li><code>BucketLeapArray<\/code>\uff1a\u666e\u901a\u6ed1\u52a8\u7a97\u53e3\uff0c\u6bcf\u4e2a\u65f6\u95f4\u6876\u4ec5\u8bb0\u5f55\u56fa\u5b9a\u65f6\u95f4\u7a97\u53e3\u5185\u7684\u6307\u6807\u6570\u636e\uff1b<\/li>\n<li><code>OccupiableBucketLeapArray<\/code>\uff1a\u6269\u5c55\u5b9e\u73b0\uff0c\u652f\u6301&#8221;\u62a2\u5360&#8221;\u672a\u6765\u65f6\u95f4\u7a97\u53e3\u7684\u4ee4\u724c\u6216\u5bb9\u91cf\uff0c\u5728\u6d41\u91cf\u7a81\u53d1\u65f6\u5141\u8bb8\u501f\u7528\u540e\u7eed\u7a97\u53e3\u7684\u914d\u989d\uff0c\u5b9e\u73b0\u66f4\u5e73\u6ed1\u7684\u9650\u6d41\u6548\u679c\u3002<\/li>\n<\/ul>\n<h3>BucketLeapArray &#8211; \u6ed1\u52a8\u7a97\u53e3\u5b9e\u73b0<\/h3>\n<h4>LeapArray \u6838\u5fc3\u5c5e\u6027<\/h4>\n<p>LeapArray\u00a0\u662f\u6ed1\u52a8\u7a97\u53e3\u7684\u57fa\u7840\u7c7b\uff0c\u5176\u6838\u5fc3\u5c5e\u6027\u5982\u4e0b\uff1a<\/p>\n<pre><code>\/**\n * \u7a97\u53e3\u5927\u5c0f\uff08\u957f\u5ea6\uff09\uff0c\u5355\u4f4d\uff1a\u6beb\u79d2\n * \u4f8b\u5982\uff1a1000ms\n *\/\nprivate int windowLengthInMs;\n\n\/**\n * \u6837\u672c\u6570\uff08\u6876\u7684\u6570\u91cf\uff09\n * \u4f8b\u5982\uff1a5\uff08\u8868\u793a 5 \u4e2a\u6876\uff0c\u6bcf\u4e2a 1000ms\uff0c\u603b\u5171 5 \u79d2\uff09\n *\/\nprivate int sampleCount;\n\n\/**\n * \u91c7\u96c6\u5468\u671f\uff08\u603b\u65f6\u95f4\u7a97\u53e3\u957f\u5ea6\uff09\uff0c\u5355\u4f4d\uff1a\u6beb\u79d2\n * \u4f8b\u5982\uff1a5 * 1000ms\uff085 \u79d2\uff09\n *\/\nprivate int intervalInMs;\n\n\/**\n * \u7a97\u53e3\u6570\u7ec4\uff0carray \u957f\u5ea6\u5c31\u662f\u6837\u672c\u6570 sampleCount\n *\/\nprotected final AtomicReferenceArray&lt;WindowWrap&lt;T&gt;&gt; array;\n\n\/**\n * \u66f4\u65b0\u7a97\u53e3\u6570\u636e\u7684\u9501\uff0c\u4fdd\u8bc1\u6570\u636e\u7684\u6b63\u786e\u6027\n *\/\nprivate final ReentrantLock updateLock;<\/code><\/pre>\n<h4>WindowWrap \u7a97\u53e3\u5305\u88c5\u5668<\/h4>\n<p>\u6bcf\u4e2a\u7a97\u53e3\u5305\u88c5\u5668\u5305\u542b\u4e09\u4e2a\u5c5e\u6027\uff1a<\/p>\n<pre><code> public class WindowWrap&lt;T&gt; {\n\n    \/**\n     * \u7a97\u53e3\u5927\u5c0f\uff08\u957f\u5ea6\uff09\uff0c\u5355\u4f4d\uff1a\u6beb\u79d2\n     * \u4e0e LeapArray \u4e2d\u7684 windowLengthInMs \u4e00\u81f4\n     *\/\n    private final long windowLengthInMs;\n\n    \/**\n     * \u7a97\u53e3\u5f00\u59cb\u65f6\u95f4\u6233\n     * \u5b83\u7684\u503c\u662f windowLengthInMs \u7684\u6574\u6570\u500d\n     *\/\n    private long windowStart;\n\n    \/**\n     * \u7a97\u53e3\u6570\u636e\uff08\u6cdb\u578b T\uff09\n     * Sentinel \u76ee\u524d\u53ea\u6709 MetricBucket \u7c7b\u578b\uff0c\u5b58\u50a8\u7edf\u8ba1\u6570\u636e\n     *\/\n    private T value;\n}<\/code><\/pre>\n<h4>MetricBucket \u6307\u6807\u6876<\/h4>\n<pre><code>public class MetricBucket {\n\n    \/**\n     * \u8ba1\u6570\u5668\u6570\u7ec4\n     * \u957f\u5ea6\u662f\u9700\u8981\u7edf\u8ba1\u7684\u4e8b\u4ef6\u79cd\u7c7b\u6570\uff0c\u76ee\u524d\u662f 6 \u4e2a\n     * LongAdder \u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u8ba1\u6570\u5668\uff0c\u6027\u80fd\u4f18\u4e8e AtomicLong\n     *\/\n    private final LongAdder[] counters;\n    \n    \/\/ \u7701\u7565\u5176\u4ed6\u5b57\u6bb5\u548c\u65b9\u6cd5...\n}<\/code><\/pre>\n<h4>\u6ed1\u52a8\u7a97\u53e3\u5de5\u4f5c\u539f\u7406<\/h4>\n<p>LeapArray\u00a0\u7edf\u8ba1\u6570\u636e\u7684\u57fa\u672c\u601d\u8def\uff1a<\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u957f\u5ea6\u4e3a\u00a0n\u00a0\u7684\u6570\u7ec4\uff0c\u6570\u7ec4\u5143\u7d20\u5c31\u662f\u7a97\u53e3\uff1b<\/p>\n<p>\u6bcf\u4e2a\u7a97\u53e3\u5305\u88c5\u4e86 1 \u4e2a\u6307\u6807\u6876\uff0c\u6876\u4e2d\u5b58\u653e\u4e86\u8be5\u7a97\u53e3\u65f6\u95f4\u8303\u56f4\u5185\u5bf9\u5e94\u7684\u8bf7\u6c42\u7edf\u8ba1\u6570\u636e\uff1b<\/p>\n<p>\u53ef\u4ee5\u60f3\u8c61\u6210\u4e00\u4e2a\u73af\u5f62\u6570\u7ec4\u5728\u65f6\u95f4\u8f74\u4e0a\u5411\u53f3\u6eda\u52a8\uff1b<\/p>\n<p>\u8bf7\u6c42\u5230\u8fbe\u65f6\uff0c\u4f1a\u547d\u4e2d\u6570\u7ec4\u4e2d\u7684\u4e00\u4e2a\u7a97\u53e3\uff0c\u8be5\u8bf7\u6c42\u7684\u6570\u636e\u5c31\u4f1a\u5b58\u5230\u547d\u4e2d\u7684\u8fd9\u4e2a\u7a97\u53e3\u5305\u542b\u7684\u6307\u6807\u6876\u4e2d\uff1b<\/p>\n<p>\u5f53\u6570\u7ec4\u8f6c\u6ee1\u4e00\u5708\u65f6\uff0c\u4f1a\u56de\u5230\u6570\u7ec4\u7684\u5f00\u5934\uff1b<\/p>\n<p>\u6b64\u65f6\u4e0b\u6807\u4e3a 0 \u7684\u5143\u7d20\u9700\u8981\u91cd\u590d\u4f7f\u7528\uff0c\u5b83\u91cc\u9762\u7684\u7a97\u53e3\u6570\u636e\u8fc7\u671f\u4e86\uff0c\u9700\u8981\u91cd\u7f6e\uff0c\u7136\u540e\u518d\u4f7f\u7528\u3002<\/p>\n<p><img decoding=\"async\" width=\"723\" height=\"433\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdnYTF\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h4>\u83b7\u53d6\u5f53\u524d\u7a97\u53e3<\/h4>\n<p>LeapArray\u00a0\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7a97\u53e3\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre><code> \/**\n * \u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233\u5bf9\u5e94\u7684\u7a97\u53e3\n *\n * @return \u5f53\u524d\u65f6\u95f4\u7684\u7a97\u53e3\n *\/\npublic WindowWrap&lt;T&gt; currentWindow() {\n    return currentWindow(TimeUtil.currentTimeMillis());\n}\n\n\/**\n * \u83b7\u53d6\u6307\u5b9a\u65f6\u95f4\u6233\u5bf9\u5e94\u7684\u7a97\u53e3\uff08\u6838\u5fc3\u65b9\u6cd5\uff09\n *\n * @param timeMillis \u65f6\u95f4\u6233\uff08\u6beb\u79d2\uff09\n * @return \u5bf9\u5e94\u7684\u7a97\u53e3\n *\/\npublic WindowWrap&lt;T&gt; currentWindow(long timeMillis) {\n    if (timeMillis &lt; 0) {\n        return null;\n    }\n\n    \/\/ \u8ba1\u7b97\u6570\u7ec4\u4e0b\u6807\n    int idx = calculateTimeIdx(timeMillis);\n\n    \/\/ \u8ba1\u7b97\u5f53\u524d\u8bf7\u6c42\u5bf9\u5e94\u7684\u7a97\u53e3\u5f00\u59cb\u65f6\u95f4\n    long windowStart = calculateWindowStart(timeMillis);\n\n    \/\/ \u65e0\u9650\u5faa\u73af\uff0c\u786e\u4fdd\u80fd\u591f\u83b7\u53d6\u5230\u7a97\u53e3\n    while (true) {\n        \/\/ \u53d6\u7a97\u53e3\n        WindowWrap&lt;T&gt; old = array.get(idx);\n\n        if (old == null) {\n            \/\/ \u7b2c\u4e00\u6b21\u4f7f\u7528\uff0c\u521b\u5efa\u65b0\u7a97\u53e3\n            WindowWrap&lt;T&gt; window = new WindowWrap&lt;T&gt;(windowLengthInMs, windowStart, newEmptyBucket(timeMillis));\n\n            \/\/ CAS \u64cd\u4f5c\uff0c\u786e\u4fdd\u53ea\u521d\u59cb\u5316\u4e00\u6b21\n            if (array.compareAndSet(idx, null, window)) {\n                \/\/ \u6210\u529f\u66f4\u65b0\uff0c\u8fd4\u56de\u521b\u5efa\u7684\u7a97\u53e3\n                return window;\n            } else {\n                \/\/ CAS \u5931\u8d25\uff0c\u8ba9\u51fa\u65f6\u95f4\u7247\uff0c\u7b49\u5f85\u5176\u4ed6\u7ebf\u7a0b\u5b8c\u6210\u521d\u59cb\u5316\n                Thread.yield();\n            }\n\n        } else if (windowStart == old.windowStart()) {\n            \/\/ \u547d\u4e2d\uff1a\u53d6\u51fa\u7684\u7a97\u53e3\u7684\u5f00\u59cb\u65f6\u95f4\u548c\u672c\u6b21\u8bf7\u6c42\u8ba1\u7b97\u51fa\u7684\u7a97\u53e3\u5f00\u59cb\u65f6\u95f4\u4e00\u81f4\n            return old;\n\n        } else if (windowStart &gt; old.windowStart()) {\n            \/\/ \u7a97\u53e3\u8fc7\u671f\uff1a\u672c\u6b21\u8bf7\u6c42\u8ba1\u7b97\u51fa\u7684\u7a97\u53e3\u5f00\u59cb\u65f6\u95f4\u5927\u4e8e\u53d6\u51fa\u7684\u7a97\u53e3\n            \/\/ \u8bf4\u660e\u53d6\u51fa\u7684\u7a97\u53e3\u8fc7\u671f\u4e86\uff0c\u9700\u8981\u91cd\u7f6e\n            if (updateLock.tryLock()) {\n                try {\n                    \/\/ \u6210\u529f\u83b7\u53d6\u9501\uff0c\u66f4\u65b0\u7a97\u53e3\u5f00\u59cb\u65f6\u95f4\uff0c\u8ba1\u6570\u5668\u91cd\u7f6e\n                    return resetWindowTo(old, windowStart);\n                } finally {\n                    updateLock.unlock();\n                }\n            } else {\n                \/\/ \u83b7\u53d6\u9501\u5931\u8d25\uff0c\u8ba9\u51fa\u65f6\u95f4\u7247\uff0c\u7b49\u5f85\u5176\u4ed6\u7ebf\u7a0b\u66f4\u65b0\n                Thread.yield();\n            }\n\n        } else if (windowStart &lt; old.windowStart()) {\n            \/\/ \u5f02\u5e38\u60c5\u51b5\uff1a\u673a\u5668\u65f6\u949f\u56de\u62e8\u7b49\n            \/\/ \u6b63\u5e38\u60c5\u51b5\u4e0d\u4f1a\u8fdb\u5165\u8be5\u5206\u652f\n            return new WindowWrap&lt;T&gt;(windowLengthInMs, windowStart, newEmptyBucket(timeMillis));\n        }\n    }\n}<\/code><\/pre>\n<h4>\u6570\u636e\u5b58\u50a8<\/h4>\n<p>\u5728\u83b7\u53d6\u5230\u7a97\u53e3\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u5b58\u50a8\u6570\u636e\u4e86\u3002ArrayMetric\u00a0\u5b9e\u73b0\u4e86\u00a0Metric\u00a0\u4e2d\u5b58\u53d6\u6570\u636e\u7684\u63a5\u53e3\u65b9\u6cd5\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a\u5b58\u50a8 RT\uff08\u54cd\u5e94\u65f6\u95f4\uff09<\/strong><\/p>\n<pre><code>\/**\n * \u6dfb\u52a0\u54cd\u5e94\u65f6\u95f4\u6570\u636e\n *\n * @param rt \u54cd\u5e94\u65f6\u95f4\uff08\u6beb\u79d2\uff09\n *\/\npublic void addRT(long rt) {\n    \/\/ \u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u7a97\u53e3\uff0cdata \u4e3a BucketLeapArray\n    WindowWrap&lt;MetricBucket&gt; wrap = data.currentWindow();\n\n    \/\/ \u8ba1\u6570\n    wrap.value().addRT(rt);\n}\n\n\/**\n * MetricBucket \u7684 addRT \u65b9\u6cd5\n *\n * @param rt \u54cd\u5e94\u65f6\u95f4\n *\/\npublic void addRT(long rt) {\n    \/\/ \u8bb0\u5f55 RT \u65f6\u95f4\u5bf9 rt \u503c\n    add(MetricEvent.RT, rt);\n\n    \/\/ \u8bb0\u5f55\u6700\u5c0f\u54cd\u5e94\u65f6\u95f4\uff08\u975e\u7ebf\u7a0b\u5b89\u5168\uff0c\u4f46\u6ca1\u5173\u7cfb\uff09\n    if (rt &lt; minRt) {\n        minRt = rt;\n    }\n}\n\n\/**\n * \u901a\u7528\u7684\u8ba1\u6570\u65b9\u6cd5\n *\n * @param event \u4e8b\u4ef6\u7c7b\u578b\n * @param n \u589e\u52a0\u7684\u6570\u91cf\n * @return \u5f53\u524d\u6876\n *\/\npublic MetricBucket add(MetricEvent event, long n) {\n    counters[event.ordinal()].add(n);\n    return this;\n}<\/code><\/pre>\n<h4>\u6570\u636e\u8bfb\u53d6<\/h4>\n<p><strong>\u793a\u4f8b\uff1a\u8bfb\u53d6 RT\uff08\u54cd\u5e94\u65f6\u95f4\uff09<\/strong><\/p>\n<pre><code>\/**\n * \u83b7\u53d6\u603b\u54cd\u5e94\u65f6\u95f4\n *\n * @return \u603b\u54cd\u5e94\u65f6\u95f4\n *\/\npublic long rt() {\n    \/\/ \u89e6\u53d1\u5f53\u524d\u7a97\u53e3\u66f4\u65b0\uff08\u5904\u7406\u8fc7\u671f\u7a97\u53e3\uff09\n    data.currentWindow();\n\n    long rt = 0;\n    \/\/ \u53d6\u51fa\u6240\u6709\u7684 bucket\n    List&lt;MetricBucket&gt; list = data.values();\n\n    for (MetricBucket window : list) {\n        rt += window.rt(); \/\/ \u6c42\u548c\n    }\n    return rt;\n}\n\n\/**\n * \u83b7\u53d6\u6240\u6709\u6709\u6548\u7684\u7a97\u53e3\n *\n * @return \u6709\u6548\u7a97\u53e3\u5217\u8868\n *\/\npublic List&lt;T&gt; values() {\n    return values(TimeUtil.currentTimeMillis());\n}\n\n\/**\n * \u83b7\u53d6\u6307\u5b9a\u65f6\u95f4\u4e4b\u524d\u7684\u6240\u6709\u6709\u6548\u7a97\u53e3\n *\n * @param timeMillis \u65f6\u95f4\u6233\n * @return \u6709\u6548\u7a97\u53e3\u5217\u8868\n *\/\npublic List&lt;T&gt; values(long timeMillis) {\n    if (timeMillis &lt; 0) {\n        return new ArrayList&lt;T&gt;(); \/\/ \u6b63\u5e38\u60c5\u51b5\u4e0d\u4f1a\u5230\u8fd9\u91cc\n    }\n\n    int size = array.length();\n    List&lt;T&gt; result = new ArrayList&lt;T&gt;(size);\n\n    for (int i = 0; i &lt; size; i++) {\n        WindowWrap&lt;T&gt; windowWrap = array.get(i);\n\n        \/\/ \u8fc7\u6ee4\u6389\u6ca1\u6709\u521d\u59cb\u5316\u8fc7\u7684\u7a97\u53e3\u548c\u8fc7\u671f\u7684\u7a97\u53e3\n        if (windowWrap == null || isWindowDeprecated(timeMillis, windowWrap)) {\n            continue;\n        }\n\n        result.add(windowWrap.value());\n    }\n    return result;\n}\n\n\/**\n * \u5224\u65ad\u7a97\u53e3\u662f\u5426\u8fc7\u671f\n *\n * @param time \u7ed9\u5b9a\u65f6\u95f4\uff08\u901a\u5e38\u662f\u5f53\u524d\u65f6\u95f4\uff09\n * @param windowWrap \u7a97\u53e3\u5305\u88c5\u5668\n * @return \u5982\u679c\u8fc7\u671f\u8fd4\u56de true\n *\/\npublic boolean isWindowDeprecated(long time, WindowWrap&lt;T&gt; windowWrap) {\n    \/\/ \u7ed9\u5b9a\u65f6\u95f4\u4e0e\u7a97\u53e3\u5f00\u59cb\u65f6\u95f4\u8d85\u8fc7\u4e86\u4e00\u4e2a\u91c7\u96c6\u5468\u671f\n    return time - windowWrap.windowStart() &gt; intervalInMs;\n}<\/code><\/pre>\n<h3>OccupiableBucketLeapArray &#8211; \u53ef\u62a2\u5360\u7a97\u53e3<\/h3>\n<h4>\u4e3a\u4ec0\u4e48\u9700\u8981 OccupiableBucketLeapArray\uff1f<\/h4>\n<p>\u5047\u8bbe\u4e00\u4e2a\u8d44\u6e90\u7684\u8bbf\u95ee QPS \u7a33\u5b9a\u662f 10\uff0c\u8bf7\u6c42\u662f\u5747\u5300\u5206\u5e03\u7684\uff1a<\/p>\n<p>\u5728\u65f6\u95f4 0.0-1.0 \u79d2\u533a\u95f4\u4e2d\uff0c\u901a\u8fc7\u4e86 10 \u4e2a\u8bf7\u6c42\uff1b<\/p>\n<p>\u5728 1.1 \u79d2\u7684\u65f6\u5019\uff0c\u89c2\u5bdf\u5230\u7684 QPS \u53ef\u80fd\u53ea\u6709 5\uff0c\u56e0\u4e3a\u6b64\u65f6\u7b2c\u4e00\u4e2a\u65f6\u95f4\u7a97\u53e3\u88ab\u91cd\u7f6e\u4e86\uff0c\u53ea\u6709\u7b2c\u4e8c\u4e2a\u65f6\u95f4\u7a97\u53e3\u6709\u503c\uff1b<\/p>\n<p>\u5f53\u5728\u79d2\u7ea7\u7edf\u8ba1\u7684\u60c5\u5f62\u4e0b\uff0c\u7528\u00a0BucketLeapArray\u00a0\u4f1a\u6709 0~50%\u7684\u6570\u636e\u8bef\u8fd9\u65f6\u5c31\u8981\u7528\u00a0OccupiableBucketLeapArray\u00a0\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<h4>OccupiableBucketLeapArray \u5b9e\u73b0<\/h4>\n<p>\u4ece\u4e0a\u9762\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5728\u79d2\u7ea7\u7edf\u8ba1\u00a0rollingCounterInSecond\u00a0\u4e2d\uff0c\u521d\u59cb\u5316\u5b9e\u4f8b\u65f6\u6709\u4e24\u79cd\u6784\u9020\u53c2\u6570\uff1a<\/p>\n<pre><code>public class OccupiableBucketLeapArray extends LeapArray&lt;MetricBucket&gt; {\n\n    \/**\n     * \u501f\u7528\u672a\u6765\u7a97\u53e3\u7684\u6570\u7ec4\n     *\/\n    private final FutureBucketLeapArray borrowArray;\n\n    public OccupiableBucketLeapArray(int sampleCount, int intervalInMs) {\n        super(sampleCount, intervalInMs);\n        \/\/ \u521b\u5efa\u501f\u7528\u7a97\u53e3\u6570\u7ec4\n        this.borrowArray = new FutureBucketLeapArray(sampleCount, intervalInMs);\n    }\n\n    \/**\n     * \u521b\u5efa\u65b0\u7684\u7a7a\u6876\n     * \u4f1a\u4ece borrowArray \u4e2d\u501f\u7528\u6570\u636e\n     *\/\n    @Override\n    public MetricBucket newEmptyBucket(long time) {\n        MetricBucket newBucket = new MetricBucket();\n\n        \/\/ \u83b7\u53d6\u501f\u7528\u7a97\u53e3\u7684\u6570\u636e\n        MetricBucket borrowBucket = borrowArray.getWindowValue(time);\n        if (borrowBucket != null) {\n            \/\/ \u5c06\u501f\u7528\u6570\u636e\u590d\u5236\u5230\u65b0\u6876\u4e2d\n            newBucket.reset(borrowBucket);\n        }\n\n        return newBucket;\n    }\n\n    \/**\n     * \u91cd\u7f6e\u7a97\u53e3\n     * \u4f1a\u4ece borrowArray \u4e2d\u501f\u7528 pass \u6570\u636e\n     *\/\n    @Override\n    protected WindowWrap&lt;MetricBucket&gt; resetWindowTo(WindowWrap&lt;MetricBucket&gt; w, long time) {\n        \/\/ \u66f4\u65b0\u5f00\u59cb\u65f6\u95f4\u5e76\u91cd\u7f6e\u503c\n        w.resetTo(time);\n\n        MetricBucket borrowBucket = borrowArray.getWindowValue(time);\n        if (borrowBucket != null) {\n            \/\/ \u91cd\u7f6e\u6876\u503c\u5e76\u6dfb\u52a0\u501f\u7528\u7684 pass \u6570\u636e\n            w.value().reset();\n            w.value().addPass((int) borrowBucket.pass());\n        } else {\n            w.value().reset();\n        }\n\n        return w;\n    }\n\n    \/**\n     * \u83b7\u53d6\u5f53\u524d\u7b49\u5f85\u4e2d\u7684\u8bf7\u6c42\u6570\u91cf\n     *\/\n    @Override\n    public long currentWaiting() {\n        borrowArray.currentWindow();\n        long currentWaiting = 0;\n        List&lt;MetricBucket&gt; list = borrowArray.values();\n\n        for (MetricBucket window : list) {\n            currentWaiting += window.pass();\n        }\n        return currentWaiting;\n    }\n\n    \/**\n     * \u6dfb\u52a0\u7b49\u5f85\u4e2d\u7684\u8bf7\u6c42\u6570\u91cf\n     *\n     * @param time \u65f6\u95f4\n     * @param acquireCount \u83b7\u53d6\u6570\u91cf\n     *\/\n    @Override\n    public void addWaiting(long time, int acquireCount) {\n        WindowWrap&lt;MetricBucket&gt; window = borrowArray.currentWindow(time);\n        window.value().add(MetricEvent.PASS, acquireCount);\n    }\n}<\/code><\/pre>\n<h2>\u516b\u3001\u603b\u7ed3<\/h2>\n<p>\u81f3\u6b64\uff0cSentinel \u7684\u57fa\u672c\u60c5\u51b5\u90fd\u5df2\u7ecf\u5206\u6790\u5b8c\u6210\u3002\u4ee5\u4e0a\u5185\u5bb9\u4e3b\u8981\u8bb2\u89e3\u4e86 Sentinel \u7684\u6838\u5fc3\u5904\u7406\u6d41\u7a0b\uff0c\u5305\u62ec\uff1a<\/p>\n<h3>\u6838\u5fc3\u6d41\u7a0b\u603b\u7ed3<\/h3>\n<ol>\n<li><strong>\u89c4\u5219\u52a0\u8f7d\uff1a<\/strong><\/li>\n<\/ol>\n<ul>\n<li>\u901a\u8fc7 <code>RuleLoader<\/code> \u5c06\u5404\u79cd\u89c4\u5219\uff08\u6d41\u63a7\u3001\u7194\u65ad\u3001\u7cfb\u7edf\u9650\u6d41\u7b49\uff09\u52a0\u8f7d\u5230\u5185\u5b58\u7f13\u5b58\u4e2d\u3002<\/li>\n<\/ul>\n<ol start=\"2\">\n<li><strong>\u8bf7\u6c42\u62e6\u622a\uff1a<\/strong><\/li>\n<\/ol>\n<ul>\n<li>\u901a\u8fc7\u00a0SentinelServletFilter\u00a0\u8fc7\u6ee4\u5668\u62e6\u622a HTTP \u8bf7\u6c42\uff1b<\/li>\n<li>\u901a\u8fc7SentinelResourceAspect\u5207\u9762\u5904\u7406\u00a0@SentinelResource\u00a0\u6ce8\u89e3\u3002<\/li>\n<\/ul>\n<ol start=\"3\">\n<li><strong>\u8d23\u4efb\u94fe\u5904\u7406\uff1a<\/strong><\/li>\n<\/ol>\n<ul>\n<li>\u4f7f\u7528\u00a0ProcessorSlotChain\u00a0\u8d23\u4efb\u94fe\u6a21\u5f0f\u7ec4\u5408\u591a\u4e2a\u529f\u80fd\u63d2\u69fd\uff1b<\/li>\n<li>\u6bcf\u4e2a\u63d2\u69fd\u8d1f\u8d23\u7279\u5b9a\u7684\u529f\u80fd\uff08\u7edf\u8ba1\u3001\u6d41\u63a7\u3001\u7194\u65ad\u7b49\uff09\u3002<\/li>\n<\/ul>\n<ol start=\"4\">\n<li><strong>\u6d41\u63a7\u5224\u65ad\uff1a<\/strong><\/li>\n<\/ol>\n<ul>\n<li>FlowSlot\u00a0\u6839\u636e\u6d41\u63a7\u89c4\u5219\u5224\u65ad\u662f\u5426\u9650\u6d41\uff1b<\/li>\n<li>\u901a\u8fc7\u6ed1\u52a8\u7a97\u53e3\u7b97\u6cd5\u7edf\u8ba1 QPS\u3001\u7ebf\u7a0b\u6570\u7b49\u6307\u6807\u3002<\/li>\n<\/ul>\n<ol start=\"5\">\n<li>\u5f02\u5e38\u5904\u7406\uff1a<\/li>\n<\/ol>\n<ul>\n<li>\u88ab\u9650\u6d41\u65f6\u629b\u51fa\u00a0BlockException\uff1b<\/li>\n<li>\u901a\u8fc7\u00a0blockHandler\u00a0\u6216\u00a0fallback\u00a0\u5904\u7406\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<h3>\u6838\u5fc3\u6280\u672f\u70b9<\/h3>\n<ol>\n<li><strong>\u8d23\u4efb\u94fe\u6a21\u5f0f\uff1a<\/strong><\/li>\n<\/ol>\n<ul>\n<li>\u901a\u8fc7 <code>ProcessorSlotChain<\/code> \u5c06\u4e0d\u540c\u7684\u9650\u6d41\u529f\u80fd\u7ec4\u5408\u5728\u4e00\u8d77\u3002<\/li>\n<\/ul>\n<ol start=\"2\">\n<li><strong>\u6ed1\u52a8\u7a97\u53e3\u7b97\u6cd5\uff1a<\/strong><\/li>\n<\/ol>\n<ul>\n<li>LeapArray\u00a0\u5b9e\u73b0\u73af\u5f62\u6ed1\u52a8\u7a97\u53e3\uff1b<\/li>\n<li>BucketLeapArray\u00a0\u666e\u901a\u6ed1\u52a8\u7a97\u53e3\uff1b<\/li>\n<li>OccupiableBucketLeapArray\u00a0\u53ef\u62a2\u5360\u7a97\u53e3\uff0c\u652f\u6301\u501f\u7528\u672a\u6765\u914d\u989d\u3002<\/li>\n<\/ul>\n<ol start=\"3\">\n<li><strong>\u6570\u636e\u7ed3\u6784\uff1a<\/strong><\/li>\n<\/ol>\n<ul>\n<li>DefaultNode\uff1a\u7279\u5b9a\u4e8e\u4e0a\u4e0b\u6587\u7684\u7edf\u8ba1\u8282\u70b9\uff1b<\/li>\n<li>ClusterNode\uff1a\u4e0d\u533a\u5206\u4e0a\u4e0b\u6587\u7684\u96c6\u7fa4\u7edf\u8ba1\u8282\u70b9\uff1b<\/li>\n<li>StatisticNode\uff1a\u6838\u5fc3\u7edf\u8ba1\u8282\u70b9\uff0c\u5305\u542b\u79d2\u7ea7\u548c\u5206\u949f\u7ea7\u7edf\u8ba1\u3002<\/li>\n<\/ul>\n<ol start=\"4\">\n<li><strong>\u9650\u6d41\u7b97\u6cd5\uff1a<\/strong><\/li>\n<\/ol>\n<ul>\n<li>QPS \u9650\u6d41\uff1a\u901a\u8fc7\u6ed1\u52a8\u7a97\u53e3\u7edf\u8ba1 QPS\uff1b<\/li>\n<li>\u7ebf\u7a0b\u6570\u9650\u6d41\uff1a\u901a\u8fc7\u539f\u5b50\u8ba1\u6570\u5668\u7edf\u8ba1\u7ebf\u7a0b\u6570\uff1b<\/li>\n<li>\u6d41\u63a7\u6548\u679c\uff1a\u5feb\u901f\u5931\u8d25\u3001\u9884\u70ed\u3001\u6392\u961f\u7b49\u5f85\u7b49\uff1b<\/li>\n<\/ul>\n<p>Sentinel \u901a\u8fc7\u7cbe\u5fc3\u8bbe\u8ba1\u7684\u67b6\u6784\uff0c\u5b9e\u73b0\u4e86\u9ad8\u6548\u3001\u7075\u6d3b\u3001\u53ef\u6269\u5c55\u7684\u6d41\u91cf\u63a7\u5236\u80fd\u529b\uff0c\u4e3a\u5fae\u670d\u52a1\u7cfb\u7edf\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u4fdd\u62a4\u673a\u5236\u3002<\/p>\n<h2>\u5f80\u671f\u56de\u987e<\/h2>\n<p>1.\u793e\u533a\u63a8\u8350\u91cd\u6392\u6280\u672f\uff1a\u53cc\u9636\u6bb5\u6846\u67b6\u7684\u5b9e\u8df5\u4e0e\u6f14\u8fdb\uff5c\u5f97\u7269\u6280\u672f<\/p>\n<p>2.Flink ClickHouse Sink\uff1a\u751f\u4ea7\u7ea7\u9ad8\u53ef\u7528\u5199\u5165\u65b9\u6848\uff5c\u5f97\u7269\u6280\u672f<\/p>\n<p>3.\u670d\u52a1\u62c6\u5206\u4e4b\u65c5\uff1a\u6d4b\u8bd5\u8fc7\u7a0b\u5168\u63ed\u79d8\uff5c\u5f97\u7269\u6280\u672f<\/p>\n<p>4.\u5927\u6a21\u578b\u7f51\u5173\uff1a\u5927\u6a21\u578b\u65f6\u4ee3\u7684\u667a\u80fd\u4ea4\u901a\u67a2\u7ebd\uff5c\u5f97\u7269\u6280\u672f<\/p>\n<p>5.\u4ece\u201c\u4eba\u6cbb\u201d\u5230\u201c\u673a\u6cbb\u201d\uff1a\u5f97\u7269\u79bb\u7ebf\u6570\u4ed3\u53d1\u5e03\u6d41\u6c34\u7ebf\u8d28\u91cf\u95e8\u7981\u5b9e\u8df5<\/p>\n<h2>\u6587 \/\u4e07\u94a7<\/h2>\n<p>\u5173\u6ce8\u5f97\u7269\u6280\u672f\uff0c\u6bcf\u5468\u66f4\u65b0\u6280\u672f\u5e72\u8d27<\/p>\n<p>\u8981\u662f\u89c9\u5f97\u6587\u7ae0\u5bf9\u4f60\u6709\u5e2e\u52a9\u7684\u8bdd\uff0c\u6b22\u8fce\u8bc4\u8bba\u8f6c\u53d1\u70b9\u8d5e\uff5e<\/p>\n<p>\u672a\u7ecf\u5f97\u7269\u6280\u672f\u8bb8\u53ef\u4e25\u7981\u8f6c\u8f7d\uff0c\u5426\u5219\u4f9d\u6cd5\u8ffd\u7a76\u6cd5\u5f8b\u8d23\u4efb\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000047622454\">https:\/\/segmentfault.com\/a\/1190000047622454<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u4ece\u4e00\u6b21 HTTP \u8bf7\u6c42\u5f00\u59cb \u5728\u4e00\u4e2a\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u670d\u52a1\u8282\u70b9\u901a\u5e38\u66b4\u9732\u4e86\u6210\u767e\u4e0a\u5343\u4e2a HTTP \u63a5\u53e3\u5bf9\u5916\u63d0\u4f9b\u670d\u52a1\u3002\u4e3a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10021277","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10021277"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10021277\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10021277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10021277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10021277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}