{"id":1955,"date":"2023-04-01T10:18:35","date_gmt":"2023-04-01T02:18:35","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1955"},"modified":"2023-04-22T08:41:18","modified_gmt":"2023-04-22T00:41:18","slug":"spring-cloud-gateway-fusing-degradation-and-flow-limiting","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/spring-cloud-gateway-fusing-degradation-and-flow-limiting\/","title":{"rendered":"Spring Cloud Gateway \u7194\u65ad\u3001\u964d\u7ea7\u3001\u9650\u6d41"},"content":{"rendered":"<ul>\n<li><strong>\u7194\u65ad\u964d\u7ea7<\/strong>\uff1a\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u7f51\u5173\u4f5c\u4e3a\u6d41\u91cf\u7684\u5165\u53e3\uff0c\u5927\u91cf\u8bf7\u6c42\u8fdb\u5165\u7f51\u5173\uff0c\u5411\u540e\u7aef\u8fdc\u7a0b\u7cfb\u7edf\u6216\u670d\u52a1\u53d1\u8d77\u8c03\u7528\uff0c\u540e\u7aef\u670d\u52a1\u4e0d\u53ef\u907f\u514d\u7684\u4f1a\u4ea7\u751f\u8c03\u7528\u5931\u8d25\uff08\u8d85\u65f6\u6216\u8005\u5f02\u5e38\uff09\uff0c\u5931\u8d25\u65f6\u4e0d\u80fd\u8ba9\u8bf7\u6c42\u5806\u79ef\u5728\u7f51\u5173\u4e0a\uff0c\u9700\u8981\u5feb\u901f\u5931\u8d25\u5e76\u8fd4\u56de\u56de\u53bb\uff0c\u8fd9\u5c31\u9700\u8981\u5728\u7f51\u5173\u4e0a\u505a\u7194\u65ad\u3001\u964d\u7ea7\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u9650\u6d41<\/strong>\uff1a\u7f51\u5173\u4e0a\u6709\u5927\u91cf\u8bf7\u6c42\uff0c\u5bf9\u6307\u5b9a\u670d\u52a1\u8fdb\u884c\u9650\u6d41\uff0c\u53ef\u4ee5\u5f88\u5927\u7a0b\u5ea6\u4e0a\u63d0\u9ad8\u670d\u52a1\u7684\u53ef\u7528\u6027\u4e0e\u7a33\u5b9a\u6027\uff0c\u9650\u6d41\u7684\u76ee\u7684\u662f\u901a\u8fc7\u5bf9\u5e76\u53d1\u8bbf\u95ee\/\u8bf7\u6c42\u8fdb\u884c\u9650\u901f\uff0c\u6216\u5bf9\u4e00\u4e2a\u65f6\u95f4\u7a97\u53e3\u5185\u7684\u8bf7\u6c42\u8fdb\u884c\u9650\u901f\u6765\u4fdd\u62a4\u7cfb\u7edf\u3002\u4e00\u65e6\u8fbe\u5230\u9650\u5236\u901f\u7387\u5219\u53ef\u4ee5\u62d2\u7edd\u670d\u52a1\u3001\u6392\u961f\u6216\u7b49\u5f85\u3001\u964d\u7ea7\u3002<\/li>\n<\/ul>\n<h2>Spring Cloud Gateway \u96c6\u6210\u7194\u65ad\u3001\u9650\u6d41<\/h2>\n<p><!-- more --><\/p>\n<h3>\u96c6\u6210\u7194\u65ad<\/h3>\n<p>\u96c6\u6210 Hystrix \u7194\u65ad\u964d\u7ea7\uff0c\u5f15\u7528hystrix\u4f9d\u8d56\uff0c\u5728filters\u4e0b\u52a0\u5165\u7194\u65ad\u964d\u7ea7\u914d\u7f6e\uff0c\u8bbe\u7f6e\u964d\u7ea7\u540e\u8fd4\u56de\u7684\u8def\u7531\uff0c\u540c\u65f6\u914d\u7f6e\u9ed8\u8ba4\u4f7f\u7528\u4fe1\u53f7\u91cf\u9694\u79bb\u30013\u79d2\u4e3b\u52a8\u8d85\u65f6<\/p>\n<pre><code class=\"language-xml\">&lt;!-- \u7194\u65ad\u3001\u964d\u7ea7 --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.cloud&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-cloud-starter-netflix-hystrix&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>application.yml<\/p>\n<pre><code class=\"language-yml\">filters:\n# \u7194\u65ad\u964d\u7ea7\u914d\u7f6e\n- name: Hystrix\n  args:\n    name : default\n    fallbackUri: &#039;forward:\/defaultfallback&#039;\n\n# hystrix \u4fe1\u53f7\u91cf\u9694\u79bb\uff0c3\u79d2\u540e\u81ea\u52a8\u8d85\u65f6\nhystrix:\n  command:\n    default:\n      execution:\n        isolation:\n          strategy: SEMAPHORE\n          thread:\n            timeoutInMilliseconds: 3000\n  shareSecurityContext: true<\/code><\/pre>\n<p>\u65b0\u5efa\u4e00\u4e2aDefaultHystrixController \u7684RestController\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">\/**\n * \u9ed8\u8ba4\u964d\u7ea7\u5904\u7406\n *\/\n@RestController\npublic class DefaultHystrixController {\n\n    @RequestMapping(&quot;\/defaultfallback&quot;)\n    public Map&lt;String,String&gt; defaultfallback() {\n        System.out.println(&quot;\u964d\u7ea7\u64cd\u4f5c...&quot;);\n        Map&lt;String, String&gt; map = new HashMap&lt;&gt;();\n        map.put(&quot;resultCode&quot;, &quot;fail&quot;);\n        map.put(&quot;resultMessage&quot;, &quot;\u670d\u52a1\u5f02\u5e38&quot;);\n        map.put(&quot;resultObj&quot;, &quot;null&quot;);\n        return map;\n    }\n}<\/code><\/pre>\n<p>\u540c\u65f6\u5728<code>consumer-service<\/code>\u7684<code>IndexController<\/code>\u4e2d\u6dfb\u52a0\u4e00\u4e2atimeout\u65b9\u6cd5\uff0c\u8ba9\u7ebf\u7a0b\u77615\u79d2\uff0c\u5927\u4e8e\u7f51\u5173\u4e2d\u8bbe\u7f6e\u8d85\u65f6\u76843\u79d2\uff0c\u56e0\u6b64\u4f1a\u89e6\u53d1\u7194\u65ad\u964d\u7ea7\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">@RestController\npublic class IndexController {\n\n    @RequestMapping(&quot;\/hello&quot;)\n    public String hello(String name) {\n        return &quot;hi &quot; + name;\n    }\n\n    @RequestMapping(&quot;\/timeout&quot;)\n    public String timeout() {\n        try {\n            \/\/\u77615\u79d2\uff0c\u7f51\u5173Hystrix3\u79d2\u8d85\u65f6\uff0c\u4f1a\u89e6\u53d1\u7194\u65ad\u964d\u7ea7\u64cd\u4f5c\n            Thread.sleep(2000);\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n        return &quot;timeout&quot;;\n    }\n}<\/code><\/pre>\n<p>\u542f\u52a8\u9879\u76ee\uff0c\u8bbf\u95ee <a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/localhost:9999\/consumer\/timeout\">http:\/\/localhost:9999\/consumer\/timeout<\/a> \uff0c\u7ed3\u679c\u5982\u4e0b\uff0c\u89e6\u53d1\u4e86\u964d\u7ea7<\/p>\n<pre><code class=\"language-json\">{&quot;resultObj&quot;:&quot;null&quot;,&quot;resultCode&quot;:&quot;fail&quot;,&quot;resultMessage&quot;:&quot;\u670d\u52a1\u5f02\u5e38&quot;}<\/code><\/pre>\n<p>\u628a\u7f51\u5173\u7684\u8d85\u65f6\u914d\u7f6e\u6539\u4e3a6000\uff0c\u4e5f\u5c31\u662f6\u79d2\uff0c\u91cd\u542f\u7f51\u5173\uff0c\u518d\u6b21\u8bbf\u95ee <a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/localhost:9999\/consumer\/timeout\">http:\/\/localhost:9999\/consumer\/timeout<\/a> \uff0c\u7ed3\u679c\u6b63\u5e38\u8fd4\u56de\uff0c\u7f51\u5173\u4e0a\u96c6\u6210\u4e86 Hystrix \u7194\u65ad\u964d\u7ea7\uff0c\u53ef\u4ee5\u8986\u76d6\u5927\u90e8\u5206\u540e\u7aef\u670d\u52a1\u4e0d\u53ef\u7528\u573a\u666f<\/p>\n<blockquote>\n<p>\u6ce8\uff1a\u4e5f\u53ef\u5728<code>@FeignClient<\/code>\u6ce8\u89e3\u4e0a\u4f7f\u7528<code>fallback=Xxx.class<\/code>\u53c2\u6570\u5b9e\u73b0\u9ed8\u8ba4\u7194\u65ad\u964d\u7ea7<\/p>\n<\/blockquote>\n<pre><code class=\"language-java\">@FeignClient(name = &quot;consumer-service&quot;, fallback = HelloFeignFallbackService.class)\npublic interface HelloFeignService {\n\n    @RequestMapping(value = &quot;\/hello&quot;, method = RequestMethod.GET)\n    String hello(@RequestParam(&quot;name&quot;) String name);\n}<\/code><\/pre>\n<pre><code class=\"language-java\">\/**\n * hystrix\u670d\u52a1\u964d\u7ea7\u5904\u7406\uff0c\u9632\u6b62\u56e0\u8d85\u65f6\u3001\u5f02\u5e38\u7b49\u5bfc\u81f4\u7684\u670d\u52a1\u8c03\u7528\u96ea\u5d29\n *\/\n@Service\npublic class HelloFeignFallbackService implements HelloFeignService {\n    @Override\n    public String hello(String name) {\n        return &quot;\u672a\u627e\u5230&quot; + name;\n    }\n}<\/code><\/pre>\n<h3>\u96c6\u6210\u9650\u6d41<\/h3>\n<p>Spring Cloud Gateway\u9ed8\u8ba4\u96c6\u6210\u4e86Redis\u9650\u6d41\uff0c\u53ef\u4ee5\u5bf9\u4e0d\u540c\u670d\u52a1\u505a\u4e0d\u540c\u7ef4\u5ea6\u7684\u9650\u6d41\uff0c\u5982\uff1aIP\u9650\u6d41\u3001\u7528\u6237\u9650\u6d41 \u3001\u63a5\u53e3\u9650\u6d41<\/p>\n<p>\u672c\u6587\u6f14\u793a\u7684\u662f IP\u9650\u6d41 \uff0c\u5148\u6dfb\u52a0redis\u4f9d\u8d56\uff0c\u6dfb\u52a0KeyResolver\uff0c\u518d\u6dfb\u52a0\u914d\u7f6e\uff0c\u9700\u542f\u52a8redis<\/p>\n<pre><code class=\"language-xml\">&lt;!-- \u9650\u6d41 --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-data-redis-reactive&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u901a\u8fc7KeyResolver\u6765\u6307\u5b9a\u9650\u6d41\u7684Key\uff0c\u65b0\u5efa\u4e00\u4e2a RateLimiterConfig \u7c7b\uff0cIP\u9650\u6d41\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-java\">\/**\n * \u8def\u7531\u9650\u6d41\u914d\u7f6e\n *\/\n@Configuration\npublic class RateLimiterConfig {\n\n    @Bean(value = &quot;remoteAddrKeyResolver&quot;)\n    public KeyResolver remoteAddrKeyResolver() {\n        return exchange -&gt; Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());\n    }\n}<\/code><\/pre>\n<p>application.yml\u4e0b\u6dfb\u52a0\u9650\u6d41\u914d\u7f6e\uff1a<\/p>\n<pre><code class=\"language-yml\">spring:\n  redis:\n    host: localhost\nfilters:\n# redis\u9650\u6d41\uff0cfilter\u540d\u79f0\u5fc5\u987b\u662fRequestRateLimiter\n- name: RequestRateLimiter\n  args:\n    # \u4f7f\u7528SpEL\u540d\u79f0\u5f15\u7528Bean\uff0c\u4e0e\u4e0a\u9762\u65b0\u5efa\u7684RateLimiterConfig\u7c7b\u4e2d\u7684bean\u7684name\u76f8\u540c\n    key-resolver: &#039;#{@remoteAddrKeyResolver}&#039;\n    # \u6bcf\u79d2\u6700\u5927\u8bbf\u95ee\u6b21\u6570\n    redis-rate-limiter.replenishRate: 20\n    # \u4ee4\u724c\u6876\u6700\u5927\u5bb9\u91cf\n    redis-rate-limiter.burstCapacity: 20<\/code><\/pre>\n<ul>\n<li>filter\u540d\u79f0\u5fc5\u987b\u662f<code>RequestRateLimiter<\/code><\/li>\n<li><code>redis-rate-limiter.replenishRate<\/code>\uff1a\u5141\u8bb8\u7528\u6237\u6bcf\u79d2\u5904\u7406\u591a\u5c11\u4e2a\u8bf7\u6c42<\/li>\n<li><code>redis-rate-limiter.burstCapacity<\/code>\uff1a\u4ee4\u724c\u6876\u7684\u5bb9\u91cf\uff0c\u5141\u8bb8\u5728\u4e00\u79d2\u949f\u5185\u5b8c\u6210\u7684\u6700\u5927\u8bf7\u6c42\u6570<\/li>\n<li><code>key-resolver<\/code>\uff1a\u4f7f\u7528SpEL\u6309\u540d\u79f0\u5f15\u7528bean<\/li>\n<\/ul>\n<p>\u6d4b\u8bd5\u7f51\u5173\u662f\u5426\u505a\u5230\u4e86\u9650\u6d41\uff0c\u4f7f\u7528 jmeter \u6d4b\u8bd5\u5de5\u5177\uff0c\u6d4b\u8bd5\u914d\u7f6e\u5982\u4e0b\uff0c100\u4e2a\u7ebf\u7a0b\uff0c2\u79d2\u5185\uff0c\u5faa\u73af2\u6b21\uff0c\u603b\u5171200\u4e2a\u8bf7\u6c42\uff0c\u8bf7\u6c42\u5730\u5740\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/localhost:9999\/consumer\/hello?name=joe\">http:\/\/localhost:9999\/consumer\/hello?name=joe<\/a><\/p>\n<p>\uff081\uff09\u7b2c\u4e00\u6b21\u8bbe\u7f6e\u9650\u6d41\u6570 2\u300110<\/p>\n<pre><code>redis-rate-limiter.replenishRate: 2\nredis-rate-limiter.burstCapacity: 10<\/code><\/pre>\n<p>\u6d4b\u8bd5\u7ed3\u679c\u5982\u4e0b\uff0c\u53ea\u670914\u4e2a\u8bf7\u6c42\u54cd\u5e94\u56de\u53bb\uff0c\u5176\u4ed6\u5168\u88ab\u62d2\u7edd\u4e86<\/p>\n<p>\uff082\uff09\u7b2c\u4e8c\u6b21\u8bbe\u7f6e\u9650\u6d41\u6570 20\u300120<\/p>\n<pre><code>redis-rate-limiter.replenishRate: 20\nredis-rate-limiter.burstCapacity: 20<\/code><\/pre>\n<p>\u6d4b\u8bd5\u7ed3\u679c\u5982\u4e0b\uff0c\u6709\u5927\u91cf\u8bf7\u6c42\u8fdb\u884c\u4e86\u54cd\u5e94\uff0c\u90e8\u5206\u62d2\u7edd<\/p>\n<p>\uff083\uff09\u8fd8\u53ef\u4ee5\u4f7f\u7528\u7528\u6237\u9650\u6d41\u3001\u63a5\u53e3\u9650\u6d41<\/p>\n<p>\u7528\u6237\u9650\u6d41\uff0c\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u9650\u6d41\uff0c\u8bf7\u6c42\u8def\u5f84\u4e2d\u5fc5\u987b\u643a\u5e26userId\u53c2\u6570<\/p>\n<pre><code class=\"language-java\">@Bean\nKeyResolver userKeyResolver() {\n    return exchange -&gt; Mono.just(exchange.getRequest().getQueryParams().getFirst(&quot;userId&quot;));\n}<\/code><\/pre>\n<p>\u63a5\u53e3\u9650\u6d41\uff0c\u83b7\u53d6\u8bf7\u6c42\u5730\u5740\u7684uri\u4f5c\u4e3a\u9650\u6d41key<\/p>\n<pre><code class=\"language-java\">@Bean\nKeyResolver apiKeyResolver() {\n    return exchange -&gt; Mono.just(exchange.getRequest().getPath().value());\n}<\/code><\/pre>\n<p>\u5728\u771f\u5b9e\u573a\u666f\u4e2d\uff0c\u9650\u6d41\u6570\u7684\u8c03\u6574\u9700\u8981\u4f9d\u8d56\u914d\u7f6e\u4e2d\u5fc3\uff0c\u5f53\u7f51\u7ad9\u505a\u6d3b\u52a8\u65f6\uff0c\u52a8\u6001\u8c03\u6574\u9650\u6d41\u6570\uff0c\u65b0\u670d\u52a1\u4e0a\u7ebf\u65f6\uff0c\u901a\u8fc7\u914d\u7f6e\u4e2d\u5fc3\u505a\u52a8\u6001\u8def\u7531\u7b49<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7194\u65ad\u964d\u7ea7\uff1a\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u7f51\u5173\u4f5c\u4e3a\u6d41\u91cf\u7684\u5165\u53e3\uff0c\u5927\u91cf\u8bf7\u6c42\u8fdb\u5165\u7f51\u5173\uff0c\u5411\u540e\u7aef\u8fdc\u7a0b\u7cfb\u7edf\u6216\u670d\u52a1\u53d1\u8d77\u8c03\u7528\uff0c\u540e\u7aef\u670d\u52a1\u4e0d\u53ef\u907f\u514d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[439],"class_list":["post-1955","post","type-post","status-publish","format-standard","hentry","category-spring-cloud","tag-spring-cloud-gateway"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1955","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1955"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1955\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}