{"id":946,"date":"2024-11-11T14:18:29","date_gmt":"2024-11-11T06:18:29","guid":{"rendered":"http:\/\/www.huerpu.cc:7000\/?p=946"},"modified":"2024-11-11T14:19:17","modified_gmt":"2024-11-11T06:19:17","slug":"spring-cloudkilburn-2022-0-2%e7%89%88%e6%9c%ac%e7%b3%bb%e5%88%97%e6%95%99%e7%a8%8b%e4%ba%94-%e6%9c%8d%e5%8a%a1%e7%bd%91%e5%85%b3springcloud-gateway","status":"publish","type":"post","link":"http:\/\/www.huerpu.cc:7000\/?p=946","title":{"rendered":"Spring Cloud(Kilburn 2022.0.2\u7248\u672c)\u7cfb\u5217\u6559\u7a0b(\u4e94) \u670d\u52a1\u7f51\u5173(SpringCloud Gateway)"},"content":{"rendered":"<h1>Spring Cloud(Kilburn 2022.0.2\u7248\u672c)\u7cfb\u5217\u6559\u7a0b(\u4e94) \u670d\u52a1\u7f51\u5173(SpringCloud Gateway)<\/h1>\n<h2>\u4e00\u3001\u670d\u52a1\u7f51\u5173<\/h2>\n<h3>1.1 \u4ec0\u4e48\u662f\u7f51\u5173<\/h3>\n<p>\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\uff0c\u670d\u52a1\u7f51\u5173\u662f\u4e00\u4e2a\u81f3\u5173\u91cd\u8981\u7684\u7ec4\u4ef6\u3002\u5b83\u4f5c\u4e3a\u7cfb\u7edf\u7684\u5165\u53e3\uff0c\u8d1f\u8d23\u63a5\u6536\u5ba2\u6237\u7aef\u7684\u8bf7\u6c42\uff0c\u5e76\u5c06\u8fd9\u4e9b\u8bf7\u6c42\u8def\u7531\u5230\u76f8\u5e94\u7684\u540e\u7aef\u670d\u52a1\u3002<code>API<\/code>\u7f51\u5173\u662f\u4e00\u4e2a\u642d\u5efa\u5728\u5ba2\u6237\u7aef\u548c\u5fae\u670d\u52a1\u4e4b\u95f4\u7684\u670d\u52a1\uff0c\u53ef\u4ee5\u5728<code>API<\/code>\u7f51\u5173\u4e2d\u5904\u7406\u4e00\u4e9b\u975e\u4e1a\u52a1\u529f\u80fd\u7684\u903b\u8f91\uff0c\u4f8b\u5982\u6743\u9650\u9a8c\u8bc1\u3001\u76d1\u63a7\u3001\u7f13\u5b58\u3001\u8bf7\u6c42\u8def\u7531\u7b49\u3002<code>API<\/code>\u7f51\u5173\u5c31\u50cf\u6574\u4e2a\u5fae\u670d\u52a1\u7cfb\u7edf\u7684\u95e8\u9762\u4e00\u6837\uff0c\u662f\u7cfb\u7edf\u5bf9\u5916\u7684\u552f\u4e00\u5165\u53e3\u3002\u6709\u4e86\u5b83\uff0c\u5ba2\u6237\u7aef\u4f1a\u5148\u5c06\u8bf7\u6c42\u53d1\u9001\u5230<code>API<\/code>\u7f51\u5173\uff0c\u7531<code>API<\/code>\u7f51\u5173\u6839\u636e\u8bf7\u6c42\u7684\u6807\u8bc6\u4fe1\u606f\u5c06\u8bf7\u6c42\u8f6c\u53d1\u5230\u5fae\u670d\u52a1\u5b9e\u4f8b\u3002<code>Spring Cloud Gateway<\/code>\u662f<code>Spring Cloud<\/code>\u63d0\u4f9b\u7684\u4e00\u4e2a\u5f3a\u5927\u7684\u670d\u52a1\u7f51\u5173\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p><code>Spring Cloud Gateway<\/code> \u5e95\u5c42\u4f7f\u7528\u4e86\u9ad8\u6027\u80fd\u7684\u901a\u4fe1\u6846\u67b6Netty\u3002Netty \u662f\u9ad8\u6027\u80fd\u4e2d\u95f4\u4ef6\u7684\u901a\u8baf\u5e95\u5ea7\uff0c <code>rocketmq<\/code> \u3001<code>seata<\/code>\u3001<code>nacos<\/code> \u3001<code>sentinel<\/code>\u3001<code>redission<\/code> \u3001<code>dubbo<\/code> \u7b49\u592a\u591a\u3001\u592a\u591a\u7684\u7684\u5927\u540d\u9f0e\u9f0e\u7684\u4e2d\u95f4\u4ef6\uff0c\u65e0\u4e00\u4f8b\u5916\u90fd\u662f\u57fa\u4e8e<code>netty<\/code>\u3002<\/p>\n<p>\u4e0e <code>Spring Cloud Netflix Zuul<\/code> \u76f8\u6bd4\uff0c<code>Spring Cloud Gateway<\/code>\u5177\u6709\u66f4\u597d\u7684\u6027\u80fd\u548c\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff0c<code>Gateway<\/code> \u57fa\u4e8e<code> WebFlux <\/code>\u6784\u5efa\uff0c\u652f\u6301\u54cd\u5e94\u5f0f\u7f16\u7a0b\uff0c\u80fd\u591f\u66f4\u597d\u5730\u5904\u7406\u9ad8\u5e76\u53d1\u573a\u666f\u3002\u540c\u65f6\uff0c\u5b83\u7684\u8def\u7531\u548c\u8fc7\u6ee4\u914d\u7f6e\u66f4\u52a0\u7075\u6d3b\u548c\u7b80\u6d01\u3002<code>Spring Cloud Gateway<\/code>\u66ff\u6362\u6389<code>Zuul<\/code>\u7684\u6210\u672c\u4e0a\u662f\u975e\u5e38\u4f4e\u7684\uff0c\u51e0\u4e4e\u53ef\u4ee5\u65e0\u7f1d\u5207\u6362\u3002<code>Spring Cloud Gateway<\/code>\u51e0\u4e4e\u5305\u542b\u4e86<code>Zuul<\/code>\u7684\u6240\u6709\u529f\u80fd\u3002\u968f\u7740\u65f6\u95f4\u7684\u53d1\u5c55\uff0c<code>Spring Cloud Netflix Zuul<\/code>\u4f1a\u88ab<code>Spring Cloud Gateway<\/code>\u53d6\u4ee3\uff0c\u60f3\u5b66\u70b9\u65b0\u6280\u672f\u7684\u5c0f\u4f19\u4f34\uff0c\u5c31\u53bb\u73a9\u8f6c<code>Spring Cloud Gateway<\/code>\u5427\uff0c\u522b\u518d\u6b7b\u6b7b\u6293\u4f4f<code>Zuul<\/code>\u4e0d\u653e\u4e86\u3002<\/p>\n<h3>1.2 \u7f51\u5173\u7684\u4f5c\u7528<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u4f5c\u7528<\/th>\n<th style=\"text-align: center;\">\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">\u8def\u7531<\/td>\n<td style=\"text-align: center;\">\u63a5\u53e3\u670d\u52a1\u7684\u7edf\u4e00\u4ee3\u7406\uff0c\u5b9e\u73b0\u524d\u7aef\u5bf9\u63a5\u53e3\u670d\u52a1\u7684\u7edf\u4e00\u8bbf\u95ee\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">\u8fc7\u6ee4<\/td>\n<td style=\"text-align: center;\">\u5bf9\u7528\u6237\u8bf7\u6c42\u8fdb\u884c\u62e6\u622a\u3001\u8fc7\u6ee4\u3001\u7528\u6237\u9274\u6743\u3001\u76d1\u63a7\u7b49\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">\u9650\u6d41<\/td>\n<td style=\"text-align: center;\">\u9650\u5236\u7528\u6237\u7684\u8bbf\u95ee\u6d41\u91cf\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>1.3 \u5e38\u7528\u7684\u7f51\u5173<\/h3>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u7f51\u5173\u540d\u79f0<\/th>\n<th style=\"text-align: left;\">\u4f18\u70b9<\/th>\n<th style=\"text-align: left;\">\u7f3a\u70b9<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">Spring Cloud Gateway<\/td>\n<td style=\"text-align: left;\">1. \u57fa\u4e8e Spring \u6846\u67b6\uff0c\u4e0e Spring \u751f\u6001\u7cfb\u7edf\u96c6\u6210\u65b9\u4fbf\uff0c\u5982\u548c Spring Boot\u3001Spring Cloud \u7b49\u914d\u5408\u826f\u597d\u3002<br \/> 2. \u652f\u6301\u591a\u79cd\u8def\u7531\u5339\u914d\u65b9\u5f0f\uff0c\u5305\u62ec\u8def\u5f84\u3001\u8bf7\u6c42\u5934\u3001\u8bf7\u6c42\u53c2\u6570\u7b49\uff0c\u7075\u6d3b\u6027\u9ad8\u3002 <br \/>3. \u53ef\u4ee5\u65b9\u4fbf\u5730\u901a\u8fc7 Java \u4ee3\u7801\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u8fc7\u6ee4\u5668\uff0c\u7528\u4e8e\u5b9e\u73b0\u8bf8\u5982\u5b89\u5168\u8ba4\u8bc1\u3001\u65e5\u5fd7\u8bb0\u5f55\u3001\u8bf7\u6c42\u4fee\u6539\u7b49\u529f\u80fd\u3002<br \/> 4. \u5177\u5907\u8f83\u597d\u7684\u6027\u80fd\uff0c\u80fd\u5904\u7406\u9ad8\u5e76\u53d1\u8bf7\u6c42\u3002<\/td>\n<td style=\"text-align: left;\">1. \u5bf9\u4e8e\u4e0d\u719f\u6089 Spring \u6846\u67b6\u7684\u5f00\u53d1\u8005\u6709\u4e00\u5b9a\u7684\u5b66\u4e60\u6210\u672c\u3002 <br \/>2. \u914d\u7f6e\u76f8\u5bf9\u590d\u6742\uff0c\u5c24\u5176\u662f\u5927\u89c4\u6a21\u590d\u6742\u573a\u666f\u4e0b\uff0c\u8def\u7531\u548c\u8fc7\u6ee4\u5668\u7684\u914d\u7f6e\u53ef\u80fd\u53d8\u5f97\u7e41\u7410\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Spring Cloud Netflix Zuul<\/td>\n<td style=\"text-align: left;\">1. \u662f Spring Cloud \u65e9\u671f\u7684\u7f51\u5173\u89e3\u51b3\u65b9\u6848\uff0c\u548c Spring Cloud \u751f\u6001\u878d\u5408\u5ea6\u8f83\u597d\u3002 <br \/>2. \u652f\u6301\u52a8\u6001\u8def\u7531\u3001\u76d1\u63a7\u3001\u5f39\u6027\u548c\u5b89\u5168\u7b49\u529f\u80fd\uff0c\u80fd\u591f\u65b9\u4fbf\u5730\u5bf9\u8bf7\u6c42\u8fdb\u884c\u8def\u7531\u548c\u8fc7\u6ee4\u3002 <br \/>3. \u53ef\u4ee5\u901a\u8fc7\u7f16\u5199 Zuul Filter \u6765\u6269\u5c55\u529f\u80fd\u3002<\/td>\n<td style=\"text-align: left;\">1. \u76f8\u6bd4\u4e00\u4e9b\u65b0\u5174\u7684\u7f51\u5173\uff0c\u6027\u80fd\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u53ef\u80fd\u7a0d\u900a\u4e00\u7b79\u3002 <br \/>2. \u65b0\u529f\u80fd\u66f4\u65b0\u901f\u5ea6\u76f8\u5bf9\u8f83\u6162\uff0c\u793e\u533a\u6d3b\u8dc3\u5ea6\u4e0d\u5982\u4e00\u4e9b\u5176\u4ed6\u7f51\u5173\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Kong<\/td>\n<td style=\"text-align: left;\">1. \u5f00\u6e90\u7684\u3001\u8f7b\u91cf\u7ea7\u7684\u9ad8\u6027\u80fd API \u7f51\u5173\uff0c\u57fa\u4e8e Nginx \u548c OpenResty \u6784\u5efa\u3002 <br \/>2. \u652f\u6301\u63d2\u4ef6\u673a\u5236\uff0c\u53ef\u4ee5\u8f7b\u677e\u6269\u5c55\u529f\u80fd\uff0c\u5982\u8eab\u4efd\u9a8c\u8bc1\u3001\u9650\u6d41\u3001\u65e5\u5fd7\u8bb0\u5f55\u7b49\u3002 <br \/>3. \u53ef\u7528\u4e8e\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\uff0c\u5bf9\u524d\u540e\u7aef\u5206\u79bb\u7684\u5e94\u7528\u8fdb\u884c\u6709\u6548\u7684\u8bf7\u6c42\u8f6c\u53d1\u548c\u7ba1\u7406\u3002<br \/> 4. \u63d0\u4f9b\u4e86\u826f\u597d\u7684\u53ef\u6269\u5c55\u6027\u548c\u5206\u5e03\u5f0f\u90e8\u7f72\u80fd\u529b\u3002<\/td>\n<td style=\"text-align: left;\">1. \u6587\u6863\u5728\u67d0\u4e9b\u65b9\u9762\u53ef\u80fd\u4e0d\u591f\u8be6\u7ec6\uff0c\u5bf9\u4e8e\u521d\u6b21\u4f7f\u7528\u8005\u6709\u4e00\u5b9a\u7684\u5b66\u4e60\u95e8\u69db\u3002 <br \/>2. \u590d\u6742\u7684\u63d2\u4ef6\u914d\u7f6e\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\uff0c\u5982\u679c\u63d2\u4ef6\u4f7f\u7528\u4e0d\u5f53\u53ef\u80fd\u51fa\u73b0\u4e00\u4e9b\u96be\u4ee5\u6392\u67e5\u7684\u95ee\u9898\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Nginx + Lua<\/td>\n<td style=\"text-align: left;\">1. Nginx \u672c\u8eab\u6027\u80fd\u5353\u8d8a\uff0c\u5904\u7406\u9ad8\u5e76\u53d1\u80fd\u529b\u5f3a\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u5927\u91cf\u7684\u7f51\u7edc\u8bf7\u6c42\u3002 <br \/>2. Lua \u811a\u672c\u53ef\u4ee5\u65b9\u4fbf\u5730\u5d4c\u5165\u5230 Nginx \u4e2d\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u4e1a\u52a1\u903b\u8f91\uff0c\u5982\u52a8\u6001\u8def\u7531\u3001\u8bf7\u6c42\u8fc7\u6ee4\u7b49\u3002 <br \/>3. \u53ef\u4ee5\u7075\u6d3b\u5730\u8fdb\u884c\u914d\u7f6e\u548c\u4f18\u5316\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u89c4\u6a21\u7684\u7f51\u7edc\u67b6\u6784\u3002<\/td>\n<td style=\"text-align: left;\">1. \u5f00\u53d1\u548c\u7ef4\u62a4\u9700\u8981\u5bf9 Nginx \u548c Lua \u90fd\u6709\u6df1\u5165\u7684\u4e86\u89e3\uff0c\u6280\u672f\u95e8\u69db\u8f83\u9ad8\u3002 <br \/>2. Lua \u811a\u672c\u7f16\u5199\u4e0d\u5f53\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u7b49\u95ee\u9898\uff0c\u5f71\u54cd\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Traefik<\/td>\n<td style=\"text-align: left;\">1. \u81ea\u52a8\u53d1\u73b0\u540e\u7aef\u670d\u52a1\uff0c\u652f\u6301\u591a\u79cd\u5bb9\u5668\u7f16\u6392\u5de5\u5177(\u5982 Docker\u3001Kubernetes \u7b49)\uff0c\u914d\u7f6e\u7b80\u5355\u3002 <br \/>2. \u5177\u6709\u52a8\u6001\u914d\u7f6e\u66f4\u65b0\u80fd\u529b\uff0c\u65e0\u9700\u91cd\u542f\u5373\u53ef\u5e94\u7528\u65b0\u7684\u914d\u7f6e\u3002 <br \/>3. \u63d0\u4f9b\u4e86\u7b80\u6d01\u76f4\u89c2\u7684 Web UI \u7528\u4e8e\u76d1\u63a7\u548c\u7ba1\u7406\u3002 <br \/>4. \u6027\u80fd\u8f83\u597d\uff0c\u80fd\u6709\u6548\u5904\u7406\u5927\u89c4\u6a21\u6d41\u91cf\u3002<\/td>\n<td style=\"text-align: left;\">1. \u4e00\u4e9b\u9ad8\u7ea7\u529f\u80fd\u53ef\u80fd\u9700\u8981\u989d\u5916\u7684\u914d\u7f6e\u548c\u5b66\u4e60\uff0c\u5bf9\u590d\u6742\u573a\u666f\u7684\u652f\u6301\u53ef\u80fd\u9700\u8981\u66f4\u591a\u63a2\u7d22\u3002<br \/> 2. \u5728\u5904\u7406\u67d0\u4e9b\u7279\u6b8a\u7684\u4e1a\u52a1\u903b\u8f91\u5b9a\u5236\u65b9\u9762\u53ef\u80fd\u4e0d\u5982\u4e00\u4e9b\u57fa\u4e8e\u7f16\u7a0b\u5b9e\u73b0\u7684\u7f51\u5173\u7075\u6d3b\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><code>SpringCloud GateWay<\/code>\u7684\u4e3b\u8981\u529f\u80fd\u4e4b\u4e00\u662f\u8f6c\u53d1\u8bf7\u6c42\uff0c\u8f6c\u53d1\u89c4\u5219\u7684\u5b9a\u4e49\u4e3b\u8981\u5305\u542b\u4e09\u5927\u6838\u5fc3\u6982\u5ff5<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u672f\u8bed<\/th>\n<th style=\"text-align: center;\">\u529f\u80fd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">Route(\u8def\u7531)<\/td>\n<td style=\"text-align: center;\">\u8def\u7531\u662f\u7f51\u5173\u7684\u57fa\u672c\u5355\u5143\uff0c\u7531ID\u3001URI\u3001\u4e00\u7ec4Predicate\u3001\u4e00\u7ec4Filter\u7ec4\u6210\uff0c\u6839\u636ePredicate\u8fdb\u884c\u5339\u914d\u8f6c\u53d1\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Predicate(\u8c13\u8bed\u3001\u65ad\u8a00)<\/td>\n<td style=\"text-align: center;\">\u8def\u7531\u8f6c\u53d1\u7684\u5224\u65ad\u6761\u4ef6\uff0c\u76ee\u524dSpringCloud Gateway\u652f\u6301\u591a\u79cd\u65b9\u5f0f\uff0c\u5e38\u89c1\u5982\uff1aPath\u3001Query\u3001Method\u3001Header\u7b49\uff0c\u5199\u6cd5\u5fc5\u987b\u9075\u5faa key=vlue\u7684\u5f62\u5f0f\u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">Filter(\u8fc7\u6ee4\u5668)<\/td>\n<td style=\"text-align: center;\">\u8fc7\u6ee4\u5668\u662f\u8def\u7531\u8f6c\u53d1\u8bf7\u6c42\u65f6\u6240\u7ecf\u8fc7\u7684\u8fc7\u6ee4\u903b\u8f91\uff0c\u53ef\u7528\u4e8e\u4fee\u6539\u8bf7\u6c42\u3001\u54cd\u5e94\u5185\u5bb9\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u4e8c\u3001SpringCloud Gateway\u8def\u7531<\/h2>\n<p>\u590d\u5236<code>eurekaClient<\/code>\u5de5\u7a0b\uff0c\u4fee\u6539\u540d\u79f0\u4e3a<code>hepGatewayEntrance<\/code>\u3002<\/p>\n<p>\u4fee\u6539<code>pom.xml<\/code>\u6587\u4ef6\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;artifactId&gt;hepGatewayEntrance&lt;\/artifactId&gt;\n&lt;name&gt;hepGatewayEntrance&lt;\/name&gt;\n&lt;description&gt;hepGatewayEntrance&lt;\/description&gt;<\/code><\/pre>\n<p>\u5f15\u5165\u4ee5\u4e0b\u4f9d\u8d56\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.cloud&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-cloud-starter-gateway&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p>\u4fee\u6539\u7cfb\u7edf\u7684<code>hosts<\/code>\uff0c<code>Windows10<\/code>\u7684<code>hosts<\/code>\u6587\u4ef6\u8def\u5f84\u4e3a\uff1a<code>C:\\Windows\\System32\\drivers\\etc\\hosts<\/code>\u3002<code>Linux<\/code>\u548c<code>macOS<\/code>\u7684\u6587\u4ef6\u8def\u5f84\u4e3a<code>\/etc\/hosts<\/code>\u3002\u589e\u52a0\u4e00\u884c\uff1a<code>127.0.0.1 hepGatewayEntrance<\/code>\u3002<\/p>\n<h3>2.1 \u57fa\u7840URI\u8def\u7531\u914d\u7f6e<\/h3>\n<p>\u4fee\u6539<code>application.yml<\/code>\u6587\u4ef6\u4e3a\uff1a<\/p>\n<pre><code class=\"language-yml\">server:\n  port: 8005 # \u7aef\u53e3\u53f7\n\nspring:\n  main:\n    web-application-type: reactive\n  cloud:\n    gateway:\n      discovery:\n        locator:\n          enabled: true\n      routes:\n        - id: hep-api-userService\n          uri: http:\/\/localhost:8000\n          predicates:\n            - Path=\/**\n  application:\n    name: hepGatewayEntrance #Gateway\u540d\u79f0\n\neureka:\n  instance:\n    prefer-ip-address: false\n    hostname: hepGatewayEntrance\n    lease-renewal-interval-in-seconds: 5\n    lease-expiration-duration-in-seconds: 10\n  client:\n    healthcheck:\n      enabled: true\n    fetch-registry: true\n    register-with-eureka: true\n    service-url:\n      defaultZone: http:\/\/eureka:eurekapwd@eurekaServer:8761\/eureka\/ \n    registry-fetch-interval-seconds: 5<\/code><\/pre>\n<p><code>Spring Cloud Gateway<\/code>\u914d\u7f6e\u5404\u5b57\u6bb5\u542b\u4e49\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\n<p><code>id<\/code>\uff1a\u81ea\u5b9a\u4e49\u7684\u8def\u7531<code> ID<\/code>\uff0c\u4fdd\u6301\u552f\u4e00<\/p>\n<\/li>\n<li>\n<p><code>uri<\/code>\uff1a\u76ee\u6807\u670d\u52a1\u5730\u5740<\/p>\n<\/li>\n<li>\n<p><code>Predicate<\/code>\u65ad\u8a00\uff1a \u8fd9\u662f\u4e00\u4e2a<code>Java 8 Function Predicate<\/code>\u3002\u8f93\u5165\u7c7b\u578b\u662f<code>Spring Framework ServerWebExchange<\/code>\u3002\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u5339\u914d\u6765\u81eaHTTP\u8bf7\u6c42\u7684\u4efb\u4f55\u5185\u5bb9\uff0c\u4f8b\u5982<code>Header<\/code>\u6216\u53c2\u6570\u3002<code>Predicate<\/code>\u63a5\u53d7\u4e00\u4e2a\u8f93\u5165\u53c2\u6570\uff0c\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\u7ed3\u679c\u3002<code>Spring Cloud Gateway<\/code>\u5185\u7f6e\u4e86\u8bb8\u591a<code>Predicate<\/code>\uff0c\u8fd9\u4e9b<code>Predicate<\/code>\u7684\u6e90\u7801\u5728<code>org.springframework.cloud.gateway.handler.predicate<\/code>\u5305\u4e2d\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u4e0a\u9762\u8fd9\u6bb5\u914d\u7f6e\u7684\u610f\u601d\u662f\uff0c\u914d\u7f6e\u4e86\u4e00\u4e2a<code> id<\/code> \u4e3a <code>hep-api-userService<\/code>\u7684<code>URI<\/code>\u4ee3\u7406\u89c4\u5219\uff0c\u8def\u7531\u7684\u89c4\u5219\u4e3a\uff1a\u5f53\u8bbf\u95ee\u5730\u5740<code>http:\/\/localhost:8005\/getUserById<\/code>\u65f6\uff0c\u4f1a\u8def\u7531\u5230\u4e0a\u6e38\u5730\u5740<code>http:\/\/localhost:8000\/getUserById<\/code>\u3002<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/11\/11\/-2024-11-07-111135.png\" alt=\"\u5c4f\u5e55\u622a\u56fe 2024-11-07 111135\" \/><\/p>\n<p>\u5e38\u89c1\u7684 <code>Predicate<\/code> \u65ad\u8a00\u5982\u4e0b<\/p>\n<table>\n<thead>\n<tr>\n<th>\u65ad\u8a00<\/th>\n<th>\u793a\u4f8b<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Path<\/td>\n<td>&#8211; Path=\/user\/**<\/td>\n<td>\u5f53\u8bf7\u6c42\u8def\u5f84\u4e0e \/user\/** \u5339\u914d\u65f6\uff0c\u8be5\u8bf7\u6c42\u624d\u80fd\u88ab\u8f6c\u53d1\u5230 <code>http:\/\/localhost:8000\/<\/code> \u4e0a\u3002<\/td>\n<\/tr>\n<tr>\n<td>Before<\/td>\n<td>&#8211; Before=2024-10-20T11:47:34.255+08:00[Asia\/Shanghai]<\/td>\n<td>\u5728 2024 \u5e74 10 \u6708 20 \u65e5 11 \u65f6 47 \u5206 34.255 \u79d2\u4e4b\u524d\u7684\u8bf7\u6c42\uff0c\u624d\u4f1a\u88ab\u8f6c\u53d1\u5230 <code>http:\/\/localhost:8000\/<\/code> \u4e0a\u3002<\/td>\n<\/tr>\n<tr>\n<td>After<\/td>\n<td>&#8211; After=2024-10-20T11:47:34.255+08:00[Asia\/Shanghai]<\/td>\n<td>\u5728 2024 \u5e74 10 \u6708 20 \u65e5 11 \u65f6 47 \u5206 34.255 \u79d2\u4e4b\u540e\u7684\u8bf7\u6c42\uff0c\u624d\u4f1a\u88ab\u8f6c\u53d1\u5230<code>http:\/\/localhost:8000\/<\/code> \u4e0a\u3002<\/td>\n<\/tr>\n<tr>\n<td>Between<\/td>\n<td>&#8211; Between=2024-10-20T15:18:33.226+08:00[Asia\/Shanghai],2024-10-20T15:23:33.226+08:00[Asia\/Shanghai]<\/td>\n<td>\u5728 2024 \u5e74 10 \u6708 20 \u65e5 15 \u65f6 18 \u5206 33.226 \u79d2 \u5230 2024\u5e74 10 \u6708 20 \u65e5 15 \u65f6 23 \u5206 33.226 \u79d2\u4e4b\u95f4\u7684\u8bf7\u6c42\uff0c\u624d\u4f1a\u88ab\u8f6c\u53d1\u5230<code>http:\/\/localhost:8000\/<\/code>\u670d\u52a1\u5668\u4e0a\u3002<\/td>\n<\/tr>\n<tr>\n<td>Cookie<\/td>\n<td>&#8211; Cookie=name,c.biancheng.net<\/td>\n<td>\u643a\u5e26 Cookie \u4e14 Cookie \u7684\u5185\u5bb9\u4e3a name=c.biancheng.net \u7684\u8bf7\u6c42\uff0c\u624d\u4f1a\u88ab\u8f6c\u53d1\u5230<code>http:\/\/localhost:8000\/<\/code>\u4e0a\u3002<\/td>\n<\/tr>\n<tr>\n<td>Header<\/td>\n<td>&#8211; Header=X-Request-Id,\\d+<\/td>\n<td>\u8bf7\u6c42\u5934\u4e0a\u643a\u5e26\u5c5e\u6027 X-Request-Id \u4e14\u5c5e\u6027\u503c\u4e3a\u6574\u6570\u7684\u8bf7\u6c42\uff0c\u624d\u4f1a\u88ab\u8f6c\u53d1\u5230<code>http:\/\/localhost:8000\/<\/code>\u4e0a\u3002<\/td>\n<\/tr>\n<tr>\n<td>Method<\/td>\n<td>&#8211; Method=GET<\/td>\n<td>\u53ea\u6709 GET \u8bf7\u6c42\u624d\u4f1a\u88ab\u8f6c\u53d1\u5230<code>http:\/\/localhost:8000\/<\/code>\u4e0a\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5f53\u7136\u6211\u4eec\u4e5f\u53ef\u4ee5\u628a<code>EurekaClient<\/code>\u7684\u63a5\u53e3\u4fee\u6539\u4e00\u4e0b\uff0c\u4f7f\u5176\u524d\u9762\u589e\u52a0\u4e00\u4e2a<code>\/user\/<\/code>\u7684\u8def\u5f84<\/p>\n<pre><code class=\"language-java\">@RestController\n@RequestMapping(&quot;\/user&quot;)\npublic class UserController {\n\n    @RequestMapping(&quot;\/getUserById&quot;)\n    public String getUserById() throws Exception{\n        return &quot;{id:1,name:jason,age:23,sex: man}&quot;;\n    }\n}<\/code><\/pre>\n<p>\u5728<code>Spring Cloud Gateway<\/code>\u4e2d\u4e5f\u8bbe\u7f6e\u4e00\u4e0b<code>\/user\/<\/code>\u8def\u5f84<code>Path=\/user\/**<\/code><\/p>\n<pre><code class=\"language-yml\">spring:\n  main:\n    web-application-type: reactive\n  cloud:\n    gateway:\n      discovery:\n        locator:\n          enabled: true\n      routes:\n        - id: hep-api-userService\n          uri: http:\/\/localhost:8000\n          predicates:\n            - Path=\/user\/**<\/code><\/pre>\n<p>\u5f53\u8bbf\u95ee\u5730\u5740<code>http:\/\/localhost:8005\/user\/getUserById<\/code>\u65f6\uff0c\u4f1a\u8def\u7531\u5230\u4e0a\u6e38\u5730\u5740<code>http:\/\/localhost:8000\/user\/getUserById<\/code>\u3002<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/11\/11\/-2024-11-07-111401.png\" alt=\"\u5c4f\u5e55\u622a\u56fe 2024-11-07 111401\" \/><\/p>\n<h3>2.2 \u52a8\u6001\u8def\u7531\u914d\u7f6e<\/h3>\n<p><code>Spring Cloud Gateway<\/code> \u4f1a\u6839\u636e<code>Eureka Server<\/code>\u670d\u52a1\u6ce8\u518c\u4e2d\u5fc3\u4e2d\u7ef4\u62a4\u7684\u670d\u52a1\u5217\u8868\uff0c\u4ee5\u670d\u52a1\u540d<code>(spring.application.name)<\/code>\u4f5c\u4e3a\u8def\u5f84\u521b\u5efa\u52a8\u6001\u8def\u7531\u8fdb\u884c\u8f6c\u53d1\uff0c\u4ece\u800c\u5b9e\u73b0\u52a8\u6001\u8def\u7531\u529f\u80fd\u3002<\/p>\n<p>\u6211\u4eec\u628a<code>Spring Cloud Gateway<\/code>\u7684<code>uri<\/code>\u4fee\u6539\u4e3a<code>uri: lb:\/\/EUREKACLIENT<\/code>\uff0c\u5176\u4e2d<code>lb<\/code>\u6807\u8bc6\u8d1f\u8f7d\u5747\u8861\uff0c\u4e3a<code>uri<\/code>\u7684\u534f\u8bae\uff0c\u8868\u793a\u5f00\u542f <code>Spring Cloud Gateway<\/code> \u7684\u8d1f\u8f7d\u5747\u8861\u529f\u80fd\uff0c<code>EUREKACLIENT<\/code>\u662f\u670d\u52a1\u7684\u540d\u79f0\uff0c\u6ce8\u610f\u4e00\u822c\u662f\u5927\u5199\u3002<\/p>\n<pre><code class=\"language-yml\">spring:\n  main:\n    web-application-type: reactive\n  cloud:\n    gateway:\n      discovery:\n        locator:\n          enabled: true\n      routes:\n        - id: hep-api-userService\n          uri: lb:\/\/EUREKACLIENT\n          predicates:\n            - Path=\/user\/**<\/code><\/pre>\n<h2>\u4e09\u3001SpringCloud Gateway \u8fc7\u6ee4\u5668<\/h2>\n<h4>3.1 GatewayFilter \u7f51\u5173\u8fc7\u6ee4\u5668<\/h4>\n<p>\u6211\u4eec\u6765\u4f7f\u7528\u4e00\u4e0b<code>PrefixPath GatewayFilter<\/code>\u5427\uff0c\u5f53\u6211\u4eec\u589e\u52a0\u4e00\u9879<code>PrefixPath=\/user<\/code>\uff0c\u5e76\u628a<code>Path\u4e2d<\/code>\u7684<code>\/user\/<\/code>\u53bb\u6389\uff0c\u6539\u6210<code>Path=\/**<\/code><\/p>\n<pre><code class=\"language-yml\">spring:\n  main:\n    web-application-type: reactive\n  cloud:\n    gateway:\n      discovery:\n        locator:\n          enabled: true\n      routes:\n        - id: hep-api-userService\n          uri: lb:\/\/EUREKACLIENT\n          predicates:\n            - Path=\/**\n          filters:\n            - PrefixPath=\/user<\/code><\/pre>\n<p>\u6240\u6709\u5339\u914d\u8be5\u8def\u7531\u7684\u8bf7\u6c42\u8def\u5f84\u90fd\u4f1a\u88ab\u6dfb\u52a0<code>\/user<\/code>\u524d\u7f00\u3002\u4f8b\u5982\uff0c\u4e00\u4e2a\u539f\u672c\u8bf7\u6c42<code>\/getUserById<\/code>\u7684\u8bf7\u6c42\uff0c\u7ecf\u8fc7\u8be5\u8fc7\u6ee4\u5668\u540e\uff0c\u5b9e\u9645\u8bf7\u6c42\u8def\u5f84\u4f1a\u53d8\u4e3a<code>\/user\/getUserById<\/code>\uff0c\u7136\u540e\u88ab\u8f6c\u53d1\u5230<code>http:\/\/localhost:8000\/user\/getUserById<\/code>\u3002<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/11\/11\/-2024-11-07-114112.png\" alt=\"\u5c4f\u5e55\u622a\u56fe 2024-11-07 114112\" \/><\/p>\n<p><code>Spring Cloud Gateway<\/code> \u5185\u7f6e\u4e86\u591a\u8fbe 31 \u79cd <code>GatewayFilter<\/code>\uff0c\u4e0b\u8868\u4e2d\u5217\u4e3e\u4e86\u51e0\u79cd\u5e38\u7528\u7684\u7f51\u5173\u8fc7\u6ee4\u5668\u53ca\u5176\u4f7f\u7528\u793a\u4f8b\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u8def\u7531\u8fc7\u6ee4\u5668<\/th>\n<th>\u63cf\u8ff0<\/th>\n<th>\u53c2\u6570<\/th>\n<th>\u4f7f\u7528\u793a\u4f8b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>AddRequestHeader<\/td>\n<td>\u62e6\u622a\u4f20\u5165\u7684\u8bf7\u6c42\uff0c\u5e76\u5728\u8bf7\u6c42\u4e0a\u6dfb\u52a0\u4e00\u4e2a\u6307\u5b9a\u7684\u8bf7\u6c42\u5934\u53c2\u6570\u3002<\/td>\n<td>name\uff1a\u9700\u8981\u6dfb\u52a0\u7684\u8bf7\u6c42\u5934\u53c2\u6570\u7684 key\uff1b value\uff1a\u9700\u8981\u6dfb\u52a0\u7684\u8bf7\u6c42\u5934\u53c2\u6570\u7684 value\u3002<\/td>\n<td>&#8211; AddRequestHeader=my-request-header,1024<\/td>\n<\/tr>\n<tr>\n<td>AddRequestParameter<\/td>\n<td>\u62e6\u622a\u4f20\u5165\u7684\u8bf7\u6c42\uff0c\u5e76\u5728\u8bf7\u6c42\u4e0a\u6dfb\u52a0\u4e00\u4e2a\u6307\u5b9a\u7684\u8bf7\u6c42\u53c2\u6570\u3002<\/td>\n<td>name\uff1a\u9700\u8981\u6dfb\u52a0\u7684\u8bf7\u6c42\u53c2\u6570\u7684 key\uff1b value\uff1a\u9700\u8981\u6dfb\u52a0\u7684\u8bf7\u6c42\u53c2\u6570\u7684 value\u3002<\/td>\n<td>&#8211; AddRequestParameter=my-request-param,c.biancheng.net<\/td>\n<\/tr>\n<tr>\n<td>AddResponseHeader<\/td>\n<td>\u62e6\u622a\u54cd\u5e94\uff0c\u5e76\u5728\u54cd\u5e94\u4e0a\u6dfb\u52a0\u4e00\u4e2a\u6307\u5b9a\u7684\u54cd\u5e94\u5934\u53c2\u6570\u3002<\/td>\n<td>name\uff1a\u9700\u8981\u6dfb\u52a0\u7684\u54cd\u5e94\u5934\u7684 key\uff1b value\uff1a\u9700\u8981\u6dfb\u52a0\u7684\u54cd\u5e94\u5934\u7684 value\u3002<\/td>\n<td>&#8211; AddResponseHeader=my-response-header,c.biancheng.net<\/td>\n<\/tr>\n<tr>\n<td>PrefixPath<\/td>\n<td>\u62e6\u622a\u4f20\u5165\u7684\u8bf7\u6c42\uff0c\u5e76\u5728\u8bf7\u6c42\u8def\u5f84\u589e\u52a0\u4e00\u4e2a\u6307\u5b9a\u7684\u524d\u7f00\u3002<\/td>\n<td>prefix\uff1a\u9700\u8981\u589e\u52a0\u7684\u8def\u5f84\u524d\u7f00\u3002<\/td>\n<td>&#8211; PrefixPath=\/consumer<\/td>\n<\/tr>\n<tr>\n<td>PreserveHostHeader<\/td>\n<td>\u8f6c\u53d1\u8bf7\u6c42\u65f6\uff0c\u4fdd\u6301\u5ba2\u6237\u7aef\u7684 Host \u4fe1\u606f\u4e0d\u53d8\uff0c\u7136\u540e\u5c06\u5b83\u4f20\u9012\u5230\u63d0\u4f9b\u5177\u4f53\u670d\u52a1\u7684\u5fae\u670d\u52a1\u4e2d\u3002<\/td>\n<td>\u65e0<\/td>\n<td>&#8211; PreserveHostHeader<\/td>\n<\/tr>\n<tr>\n<td>RemoveRequestHeader<\/td>\n<td>\u79fb\u9664\u8bf7\u6c42\u5934\u4e2d\u6307\u5b9a\u7684\u53c2\u6570\u3002<\/td>\n<td>name\uff1a\u9700\u8981\u79fb\u9664\u7684\u8bf7\u6c42\u5934\u7684 key\u3002<\/td>\n<td>&#8211; RemoveRequestHeader=my-request-header<\/td>\n<\/tr>\n<tr>\n<td>RemoveResponseHeader<\/td>\n<td>\u79fb\u9664\u54cd\u5e94\u5934\u4e2d\u6307\u5b9a\u7684\u53c2\u6570\u3002<\/td>\n<td>name\uff1a\u9700\u8981\u79fb\u9664\u7684\u54cd\u5e94\u5934\u3002<\/td>\n<td>&#8211; RemoveResponseHeader=my-response-header<\/td>\n<\/tr>\n<tr>\n<td>RemoveRequestParameter<\/td>\n<td>\u79fb\u9664\u6307\u5b9a\u7684\u8bf7\u6c42\u53c2\u6570\u3002<\/td>\n<td>name\uff1a\u9700\u8981\u79fb\u9664\u7684\u8bf7\u6c42\u53c2\u6570\u3002<\/td>\n<td>&#8211; RemoveRequestParameter=my-request-param<\/td>\n<\/tr>\n<tr>\n<td>RequestSize<\/td>\n<td>\u914d\u7f6e\u8bf7\u6c42\u4f53\u7684\u5927\u5c0f\uff0c\u5f53\u8bf7\u6c42\u4f53\u8fc7\u5927\u65f6\uff0c\u5c06\u4f1a\u8fd4\u56de 413 Payload Too Large\u3002<\/td>\n<td>maxSize\uff1a\u8bf7\u6c42\u4f53\u7684\u5927\u5c0f\u3002<\/td>\n<td>&#8211; name: RequestSize   args:    maxSize: 5000000<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>31 \u79cd <code>GatewayFilter<\/code>\u5217\u8868<\/p>\n<pre><code class=\"language-text\">1. AddRequestHeader \u5728\u8bf7\u6c42\u5934\u4e2d\u6dfb\u52a0\u53c2\u6570\n2. AddRequestParameter \u6dfb\u52a0\u8bf7\u6c42\u53c2\u6570\n3. AddResponseHeader\n4. The DedupeResponseHeader GatewayFilter Factory\n5. The Hystrix GatewayFilter Factory\n6. Spring Cloud CircuitBreaker GatewayFilter Factory\n7. The FallbackHeaders GatewayFilter Factory\n8. The MapRequestHeader GatewayFilter Factory\n9. The PrefixPath GatewayFilter Factory\n10. The PreserveHostHeader GatewayFilter Factory\n11. The RequestRateLimiter GatewayFilter Factory\n12. The RedirectTo GatewayFilter Factory\n13. The RemoveRequestHeader GatewayFilter Factory\n14. RemoveResponseHeader GatewayFilter Factory\n15. The RemoveRequestParameter GatewayFilter Factory\n16. The RewritePath GatewayFilter Factory\n17. RewriteLocationResponseHeader GatewayFilter Factory\n18. The RewriteResponseHeader GatewayFilter Factory\n19. The SaveSession GatewayFilter Factory\n20. The SecureHeaders GatewayFilter Factory\n21. The SetPath GatewayFilter Factory\n22. The SetRequestHeader GatewayFilter Factory\n23. The SetResponseHeader GatewayFilter Factory\n24. The SetStatus GatewayFilter Factory\n25. The StripPrefix GatewayFilter Factory\n26. The Retry GatewayFilter Factory\n27. The RequestSize GatewayFilter Factory\n28. The SetRequestHostHeader GatewayFilter Factory\n29. Modify a Request Body GatewayFilter Factory\n30. Modify a Response Body GatewayFilter Factory\n31. Default Filters<\/code><\/pre>\n<h4>3.2 GlobalFilter \u5168\u5c40\u8fc7\u6ee4\u5668<\/h4>\n<p><code>GlobalFilter<\/code> \u662f\u4e00\u79cd\u4f5c\u7528\u4e8e\u6240\u6709\u7684\u8def\u7531\u4e0a\u7684\u5168\u5c40\u8fc7\u6ee4\u5668\uff0c\u901a\u8fc7\u5b83\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e9b\u7edf\u4e00\u5316\u7684\u4e1a\u52a1\u529f\u80fd\uff0c\u4f8b\u5982\u6743\u9650\u8ba4\u8bc1\u3001IP \u8bbf\u95ee\u9650\u5236\u7b49\u3002\u5f53\u67d0\u4e2a\u8bf7\u6c42\u88ab\u8def\u7531\u5339\u914d\u65f6\uff0c\u90a3\u4e48\u6240\u6709\u7684 <code>GlobalFilter<\/code> \u4f1a\u548c\u8be5\u8def\u7531\u81ea\u8eab\u914d\u7f6e\u7684 <code>GatewayFilter<\/code> \u7ec4\u5408\u6210\u4e00\u4e2a\u8fc7\u6ee4\u5668\u94fe\u3002 <code>GlobalFilter<\/code> \u5168\u5c40\u8fc7\u6ee4\u5668\u6709<code>Gateway Metrics Filter<\/code>\u3001<code>Local Response Cache Filter<\/code>\u3001<code>Forward Routing Filter<\/code>\u3001<code>Netty Routing Filter<\/code>\u3001<code>Netty Write Response Filter<\/code>\u3001<code>ReactiveLoadBalancerClientFilter<\/code>\u3001<code>RouteToRequestUrl Filter<\/code>\u3001<code>Websocket Routing Filter<\/code>\u7b49\u3002<\/p>\n<h4>3.3 GlobalFilter\u548cGatewayFilter\u7684\u533a\u522b<\/h4>\n<p><code>FilteringWebHandler<\/code>\u5728\u5904\u7406\u8bf7\u6c42\u65f6\uff0c\u4f1a\u5c06<code>GlobalFilter<\/code>\u88c5\u9970\u4e3a<code>GatewayFilter<\/code>\uff0c\u7136\u540e\u653e\u5230\u540c\u4e00\u4e2a\u8fc7\u6ee4\u5668\u94fe\u4e2d\uff0c\u6392\u5e8f\u4ee5\u540e\u4f9d\u6b21\u6267\u884c\u3002<\/p>\n<p><code>Spring Cloud Gateway<\/code> \u6839\u636e\u4f5c\u7528\u8303\u56f4\u5212\u5206\u4e3a <code>GatewayFilter<\/code> \u548c <code>GlobalFilter<\/code>\uff0c\u4e8c\u8005\u533a\u522b\u5982\u4e0b\uff1a<\/p>\n<p><code>GatewayFilter<\/code>\uff1a\u7f51\u5173\u8fc7\u6ee4\u5668\uff0c\u9700\u8981\u901a\u8fc7 <code>spring.cloud.routes.filters<\/code> \u914d\u7f6e\u5728\u5177\u4f53\u8def\u7531\u4e0b\uff0c\u53ea\u4f5c\u7528\u5728\u5f53\u524d\u8def\u7531\u4e0a\u6216\u901a\u8fc7 <code>spring.cloud.default-filters<\/code> \u914d\u7f6e\u5728\u5168\u5c40\uff0c\u4f5c\u7528\u5728\u6240\u6709\u8def\u7531\u4e0a\u3002<br \/>\n<code>GlobalFilter<\/code>\uff1a\u5168\u5c40\u8fc7\u6ee4\u5668\uff0c\u4e0d\u9700\u8981\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6e\uff0c\u4f5c\u7528\u5728\u6240\u6709\u7684\u8def\u7531\u4e0a\uff0c\u6700\u7ec8\u901a\u8fc7 <code>GatewayFilterAdapter<\/code> \u5305\u88c5\u6210 <code>GatewayFilterChain<\/code> \u53ef\u8bc6\u522b\u7684\u8fc7\u6ee4\u5668\uff0c\u5b83\u4e3a\u8bf7\u6c42\u4e1a\u52a1\u4ee5\u53ca\u8def\u7531\u7684 URI \u8f6c\u6362\u4e3a\u771f\u5b9e\u4e1a\u52a1\u670d\u52a1\u8bf7\u6c42\u5730\u5740\u7684\u6838\u5fc3\u8fc7\u6ee4\u5668\uff0c\u4e0d\u9700\u8981\u914d\u7f6e\u7cfb\u7edf\u521d\u59cb\u5316\u65f6\u52a0\u8f7d\uff0c\u5e76\u4f5c\u7528\u5728\u6bcf\u4e2a\u8def\u7531\u4e0a\u3002<\/p>\n<h2>\u56db\u3001\u81ea\u5b9a\u4e49\u8fc7\u6ee4\u5668<\/h2>\n<p>\u65e0\u8bba\u662f<code>GatewayFilter<\/code>\u8fd8\u662f<code>GlobalFilter<\/code>\u90fd\u652f\u6301\u81ea\u5b9a\u4e49\uff0c\u53ea\u4e0d\u8fc7<strong>\u7f16\u7801<\/strong>\u65b9\u5f0f\u3001<strong>\u4f7f\u7528<\/strong>\u65b9\u5f0f\u7565\u6709\u5dee\u522b\u3002<\/p>\n<h4>4.1 \u81ea\u5b9a\u4e49GatewayFilter<\/h4>\n<p>\u81ea\u5b9a\u4e49<code>GatewayFilter<\/code>\u4e0d\u662f\u76f4\u63a5\u5b9e\u73b0<code>GatewayFilter<\/code>\uff0c\u800c\u662f\u5b9e\u73b0<code>AbstractGatewayFilterFactory<\/code>\u3002\u6700\u7b80\u5355\u7684\u65b9\u5f0f\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre><code class=\"language-java\">@Component\npublic class PrintLogGatewayFilterFactory extends AbstractGatewayFilterFactory&lt;Object&gt; {\n    @Override\n    public GatewayFilter apply(Object config) {\n        return new GatewayFilter() {\n            @Override\n            public Mono&lt;Void&gt; filter(ServerWebExchange exchange, GatewayFilterChain chain) {\n                \/\/ \u83b7\u53d6\u8bf7\u6c42\n                ServerHttpRequest request = exchange.getRequest();\n                \/\/ \u7f16\u5199\u8fc7\u6ee4\u5668\u903b\u8f91\n                System.out.println(&quot;PrintLog executed.&quot;);\n                \/\/ \u653e\u884c\n                return chain.filter(exchange);\n            }\n        };\n    }\n}<\/code><\/pre>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a\u8be5\u7c7b\u7684\u540d\u79f0\u4e00\u5b9a\u8981\u4ee5<code>GatewayFilterFactory<\/code>\u4e3a\u540e\u7f00\u54c8\u3002<\/p>\n<p>\u7136\u540e\u5728<code>yaml<\/code>\u914d\u7f6e\u4e2d\u8fd9\u6837\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-yaml\">spring:\n  cloud:\n    gateway:\n      default-filters:\n        - PrintLog # \u6b64\u5904\u76f4\u63a5\u4ee5\u81ea\u5b9a\u4e49\u7684GatewayFilterFactory\u7c7b\u540d\u79f0\u524d\u7f00\u7c7b\u58f0\u660e\u8fc7\u6ee4\u5668<\/code><\/pre>\n<p>\u53e6\u5916\uff0c\u8fd9\u79cd\u8fc7\u6ee4\u5668\u8fd8\u53ef\u4ee5\u652f\u6301\u52a8\u6001\u914d\u7f6e\u53c2\u6570\uff0c\u4e0d\u8fc7\u5b9e\u73b0\u8d77\u6765\u6bd4\u8f83\u590d\u6742\uff0c\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-java\">@Component\npublic class PrintLogGatewayFilterFactory \/\/ \u7236\u7c7b\u6cdb\u578b\u662f\u5185\u90e8\u7c7b\u7684Config\u7c7b\u578b\n        extends AbstractGatewayFilterFactory&lt;PrintAnyGatewayFilterFactory.Config&gt; {\n\n    @Override\n    public GatewayFilter apply(Config config) {\n        \/\/ OrderedGatewayFilter\u662fGatewayFilter\u7684\u5b50\u7c7b\uff0c\u5305\u542b\u4e24\u4e2a\u53c2\u6570\uff1a\n        \/\/ - GatewayFilter\uff1a\u8fc7\u6ee4\u5668\n        \/\/ - int order\u503c\uff1a\u503c\u8d8a\u5c0f\uff0c\u8fc7\u6ee4\u5668\u6267\u884c\u4f18\u5148\u7ea7\u8d8a\u9ad8\n        return new OrderedGatewayFilter(new GatewayFilter() {\n            @Override\n            public Mono&lt;Void&gt; filter(ServerWebExchange exchange, GatewayFilterChain chain) {\n                \/\/ \u83b7\u53d6config\u503c\n                String a = config.getA();\n                String b = config.getB();\n                String c = config.getC();\n                \/\/ \u7f16\u5199\u8fc7\u6ee4\u5668\u903b\u8f91\n                System.out.println(&quot;a = &quot; + a);\n                System.out.println(&quot;b = &quot; + b);\n                System.out.println(&quot;c = &quot; + c);\n                \/\/ \u653e\u884c\n                return chain.filter(exchange);\n            }\n        }, 100);\n    }\n\n    \/\/ \u81ea\u5b9a\u4e49\u914d\u7f6e\u5c5e\u6027\uff0c\u6210\u5458\u53d8\u91cf\u540d\u79f0\u5f88\u91cd\u8981\uff0c\u4e0b\u9762\u4f1a\u7528\u5230\n    @Data\n    static class Config{\n        private String a;\n        private String b;\n        private String c;\n    }\n    \/\/ \u5c06\u53d8\u91cf\u540d\u79f0\u4f9d\u6b21\u8fd4\u56de\uff0c\u987a\u5e8f\u5f88\u91cd\u8981\uff0c\u5c06\u6765\u8bfb\u53d6\u53c2\u6570\u65f6\u9700\u8981\u6309\u987a\u5e8f\u83b7\u53d6\n    @Override\n    public List&lt;String&gt; shortcutFieldOrder() {\n        return List.of(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;);\n    }\n    \/\/ \u8fd4\u56de\u5f53\u524d\u914d\u7f6e\u7c7b\u7684\u7c7b\u578b\uff0c\u4e5f\u5c31\u662f\u5185\u90e8\u7684Config\n    @Override\n    public Class&lt;Config&gt; getConfigClass() {\n        return Config.class;\n    }\n\n}<\/code><\/pre>\n<p>\u7136\u540e\u5728<code>yaml<\/code>\u6587\u4ef6\u4e2d\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-yaml\">spring:\n  cloud:\n    gateway:\n      default-filters:\n        - PrintAny=1,2,3 # \u6ce8\u610f\uff0c\u8fd9\u91cc\u591a\u4e2a\u53c2\u6570\u4ee5&quot;,&quot;\u9694\u5f00\uff0c\u5c06\u6765\u4f1a\u6309\u7167shortcutFieldOrder()\u65b9\u6cd5\u8fd4\u56de\u7684\u53c2\u6570\u987a\u5e8f\u4f9d\u6b21\u590d\u5236<\/code><\/pre>\n<p>\u4e0a\u9762\u8fd9\u79cd\u914d\u7f6e\u65b9\u5f0f\u53c2\u6570\u5fc5\u987b\u4e25\u683c\u6309\u7167<code>shortcutFieldOrder()<\/code>\u65b9\u6cd5\u7684\u8fd4\u56de\u53c2\u6570\u540d\u987a\u5e8f\u6765\u8d4b\u503c\u3002<\/p>\n<p>\u8fd8\u6709\u4e00\u79cd\u7528\u6cd5\uff0c\u65e0\u9700\u6309\u7167\u8fd9\u4e2a\u987a\u5e8f\uff0c\u5c31\u662f\u624b\u52a8\u6307\u5b9a\u53c2\u6570\u540d\uff1a<\/p>\n<pre><code class=\"language-yaml\">spring:\n  cloud:\n    gateway:\n      default-filters:\n        - name: PrintAny\n          args: # \u624b\u52a8\u6307\u5b9a\u53c2\u6570\u540d\uff0c\u65e0\u9700\u6309\u7167\u53c2\u6570\u987a\u5e8f\n            a: 1\n            b: 2\n            c: 3<\/code><\/pre>\n<h4>4.2 \u81ea\u5b9a\u4e49GlobalFilter<\/h4>\n<p>\u81ea\u5b9a\u4e49<code>GlobalFilter<\/code>\u5219\u7b80\u5355\u5f88\u591a\uff0c\u76f4\u63a5\u5b9e\u73b0<code>GlobalFilter<\/code>\u5373\u53ef\uff0c\u800c\u4e14\u4e5f\u65e0\u6cd5\u8bbe\u7f6e\u52a8\u6001\u53c2\u6570\uff1a<\/p>\n<pre><code class=\"language-java\">@Component\npublic class PrintLogGlobalFilter implements GlobalFilter, Ordered {\n    @Override\n    public Mono&lt;Void&gt; filter(ServerWebExchange exchange, GatewayFilterChain chain) {\n        \/\/ \u7f16\u5199\u8fc7\u6ee4\u5668\u903b\u8f91\n        System.out.println(&quot;\u672a\u767b\u5f55\uff0c\u65e0\u6cd5\u8bbf\u95ee&quot;);\n        \/\/ \u653e\u884c\n        \/\/ return chain.filter(exchange);\n\n        \/\/ \u62e6\u622a\n        ServerHttpResponse response = exchange.getResponse();\n        response.setRawStatusCode(401);\/\/\u8bbe\u7f6e401\u65e0\u6cd5\u8bbf\u95ee\n        return response.setComplete();\n    }\n\n    @Override\n    public int getOrder() {\n        \/\/ \u8fc7\u6ee4\u5668\u6267\u884c\u987a\u5e8f\uff0c\u503c\u8d8a\u5c0f\uff0c\u4f18\u5148\u7ea7\u8d8a\u9ad8\n        return 0;\n    }\n}<\/code><\/pre>\n<h2>\u4e94\u3001SpringCloud Gateway \u9650\u6d41<\/h2>\n<p><code>Gateway<\/code>\u662f\u57fa\u4e8e\u4ee4\u724c\u6876\u7b97\u6cd5\uff0c\u4f7f\u7528<code>redis<\/code>\u4f5c\u4e3a\u201c\u6876\u201d\u7ed3\u5408\u8fc7\u6ee4\u5668\u5b9e\u73b0\u4e86\u7f51\u5173\u9650\u6d41\u3002<\/p>\n<p>\u5728<code>pom.xml<\/code>\u6587\u4ef6\u4e2d\u589e\u52a0\u4f9d\u8d56\uff1a<\/p>\n<pre><code class=\"language-xml\">&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<h4>5.1 IP\u9650\u6d41<\/h4>\n<p>\u4e3a\u4e86\u7b80\u4fbf\u4e00\u70b9\uff0c\u6211\u4eec\u5c31\u4e0d\u5355\u72ec\u521b\u5efa\u4e00\u4e2a<code>Config<\/code>\u7684<code>java<\/code>\u7c7b\u4e86\uff0c\u76f4\u63a5\u5728\u542f\u52a8\u7c7b\u4e2d\u521b\u5efa\u4e00\u4e2a<code>ipKeyResolver<\/code>\uff0c\u5e76\u6307\u5b9a<code>@Bean(name = &quot;ipKeyResolver&quot;)<\/code>\u3002<\/p>\n<pre><code class=\"language-java\">package cc.huerpu.eurekaclient;\n\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.cloud.client.discovery.EnableDiscoveryClient;\nimport org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;\nimport org.springframework.cloud.gateway.route.RouteLocator;\nimport org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.web.server.ServerWebExchange;\nimport reactor.core.publisher.Mono;\nimport java.util.Objects;\n\n@SpringBootApplication\n@EnableDiscoveryClient\npublic class HepGatewayEntranceApplication {\n\n    public static void main(String[] args) {\n        SpringApplication.run(HepGatewayEntranceApplication.class, args);\n    }\n\n    @Bean(name = &quot;ipKeyResolver&quot;)\n    public KeyResolver ipKeyResolver() {\n        return new KeyResolver() {\n            @Override\n            public Mono&lt;String&gt; resolve(ServerWebExchange exchange) {\n                String hostName = Objects.requireNonNull(exchange.getRequest()\n                        .getRemoteAddress()).getHostName();\n                System.out.println(&quot;hostName:&quot; + hostName);\n                return Mono.just(hostName);\n            }\n        };\n    }\n\n}<\/code><\/pre>\n<p>\u4f7f\u7528 <code>RequestRateLimiter<\/code> \u8fc7\u6ee4\u5668\u53ef\u4ee5\u5b9e\u73b0\u9650\u6d41\u529f\u80fd\u3002\u9996\u5148\u9700\u8981\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6e\u9650\u6d41\u89c4\u5219\u3002\u5728<code>application.yml<\/code>\u6587\u4ef6\u4e2d\u6307\u5b9a\u8fc7\u6ee4\u5668\u4e3a<code>RequestRateLimiter<\/code>\uff0c<code>key-resolver<\/code>\u7528\u4e8e\u9650\u6d41\u7684\u952e\u7684\u89e3\u6790\u5668\u7684<code>Bean<\/code>\u5bf9\u8c61\u7684\u540d\u5b57\u3002\u5b83\u4f7f\u7528 <code>SpEL<\/code> \u8868\u8fbe\u5f0f\u6839\u636e<code>#{@ipKeyResolver}<\/code>\u4ece <code>Spring<\/code> \u5bb9\u5668\u4e2d\u83b7\u53d6<code>Bean<\/code>\u5bf9\u8c61\uff0c<code>redis-rate-limiter.replenishRate<\/code>\u6307\u5b9a\u4ee4\u724c\u6876\u6bcfs\u7684\u586b\u5145\u901f\u5ea6\uff0c<code>redis-rate-limiter.burstCapacity<\/code>\u6307\u5b9a\u4ee4\u724c\u6876\u5bb9\u91cf\uff0c<code>redis-rate-limiter.requestedTokens<\/code>\u6307\u5b9a\u6bcf\u4e2a\u8bf7\u6c42\u6d88\u8017\u591a\u5c11\u4e2a\u4ee4\u724c\uff0c\u5f53\u7136\u8fd8\u8981\u914d\u7f6e\u597d<code>redis<\/code>\u76f8\u5173\u4fe1\u606f\u3002<\/p>\n<pre><code class=\"language-yml\">server:\n  port: 8005 # \u7aef\u53e3\u53f7\n\nspring:\n  main:\n    web-application-type: reactive\n  cloud:\n    redis:\n      host: 127.0.0.1\n      port: 6379\n      password: \n      database: 0\n    gateway:\n      filter:\n        request-rate-limiter:\n          deny-empty-key: true #\u5982\u679ckeyResolver\u8fd4\u56de\u7a7akey\uff0c\u5219\u62d2\u7edd\u8be5\u8bf7\u6c42403\uff0c\u9ed8\u8ba4true\u8868\u793a\u62d2\u7edd\uff0cfalse\u5219\u8868\u793a\u5141\u8bb8\u8bbf\u95ee\n      discovery:\n        locator:\n          enabled: true #\u542f\u7528\u57fa\u4e8e\u670d\u52a1\u53d1\u73b0\u7684\u8def\u7531\n      routes:\n        - id: hep-api-userService\n          uri: lb:\/\/EUREKACLIENT\n          predicates:\n            - Path=\/**\n          filters:\n          - name: RequestRateLimiter\n            args:\n                key-resolver: &#039;#{@ipKeyResolver}&#039;\n                redis-rate-limiter.replenishRate: 1 #\u4ee4\u724c\u6876\u6bcfs\u7684\u586b\u5145\u901f\u5ea6\n                redis-rate-limiter.burstCapacity: 2 #\u4ee4\u724c\u6876\u5bb9\u91cf\n                redis-rate-limiter.requestedTokens: 1 #\u6bcf\u4e2a\u8bf7\u6c42\u6d88\u8017\u591a\u5c11\u4e2a\u4ee4\u724c\n          - PrefixPath=\/user\n\n  application:\n    name: hepGatewayEntrance # Gateway\u540d\u79f0<\/code><\/pre>\n<p>\u5f53\u9650\u6d41\u7684\u65f6\u5019\u4f1a\u8fd4\u56de<code>429 Too Many Request<\/code>\uff0c\u800c\u6b63\u5f53\u8bbf\u95ee\u4f1a\u8fd4\u56de\u6b63\u786e\u7684\u7ed3\u679c\u6570\u636e\u3002<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/11\/11\/b02b899bdbb7ee38a127be3b466dffed.jpg\" alt=\"\u672a\u547d\u540d\" \/><\/p>\n<p>\u6211\u4eec\u4f7f\u7528<code>Postman<\/code>\u8fdb\u884c\u4e86100\u6b21\u7684\u8fde\u7eed\u8bf7\u6c42\uff0c\u53ef\u4ee5\u770b\u5230\u6709\u6210\u529f\u7684\uff0c\u4e5f\u6709<code>429 Too Many Request<\/code>\u7684\uff0c\u8fd9\u4e2a\u9650\u6d41\u7684\u8868\u8c61\u5c31\u8dc3\u7136\u7eb8\u4e0a\u4e86\u3002\u5f53\u6211\u4eec\u53bb<code>Redis<\/code>\u670d\u52a1\u5668\u67e5\u770b\u7684\u65f6\u5019\uff0c\u4e5f\u53ef\u4ee5\u770b\u5230\u914d\u7f6e\u7684<code>db0<\/code>\u6570\u636e\u5e93\u4e2d\u6709\u9650\u6d41\u7684\u76f8\u5173\u6570\u636e\u4fe1\u606f\u3002<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/11\/11\/2.jpg\" alt=\"\u672a\u547d\u540d2\" \/><\/p>\n<h4>5.2 \u7528\u6237\u9650\u6d41<\/h4>\n<p>\u6839\u636e\u7528\u6237\u6765\u505a\u9650\u6d41\u9700\u8981\u83b7\u53d6\u5f53\u524d\u8bf7\u6c42\u7528\u6237\u7684userId\uff0c\u6211\u4eec\u5728\u542f\u52a8\u7c7b\u4e2d\u521b\u5efa\u4e00\u4e2a<code>userIdResolver<\/code>\uff0c\u5e76\u6307\u5b9a<code>@Bean(name = &quot;userIdResolver&quot;)<\/code>\u3002<\/p>\n<pre><code class=\"language-java\">package cc.huerpu.eurekaclient;\n\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.cloud.client.discovery.EnableDiscoveryClient;\nimport org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;\nimport org.springframework.cloud.gateway.route.RouteLocator;\nimport org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.web.server.ServerWebExchange;\nimport reactor.core.publisher.Mono;\n\nimport java.util.Objects;\n\n@SpringBootApplication\n@EnableDiscoveryClient\npublic class HepGatewayEntranceApplication {\n\n    public static void main(String[] args) {\n        SpringApplication.run(HepGatewayEntranceApplication.class, args);\n    }\n\n    @Bean(name = &quot;userIdResolver&quot;)\n    public KeyResolver userKeyResolver() {\n        return exchange -&gt; Mono.just(Objects.requireNonNull(exchange.getRequest().getQueryParams().getFirst(&quot;userId&quot;)));\n    }\n\n}<\/code><\/pre>\n<p>\u5728<code>application.yml<\/code>\u6587\u4ef6\u4e2d\u6307\u5b9a<code>key-resolver: &#039;#{@userIdResolver}&#039;<\/code>\u3002<\/p>\n<pre><code class=\"language-yml\">server:\n  port: 8005 # \u7aef\u53e3\u53f7\n\nspring:\n  main:\n    web-application-type: reactive\n  cloud:\n    redis:\n      host: 127.0.0.1\n      port: 6379\n      password: \n      database: 0\n    gateway:\n      filter:\n        request-rate-limiter:\n          deny-empty-key: true #\u5982\u679ckeyResolver\u8fd4\u56de\u7a7akey\uff0c\u5219\u62d2\u7edd\u8be5\u8bf7\u6c42403\uff0c\u9ed8\u8ba4true\u8868\u793a\u62d2\u7edd\uff0cfalse\u5219\u8868\u793a\u5141\u8bb8\u8bbf\u95ee\n      discovery:\n        locator:\n          enabled: true #\u542f\u7528\u57fa\u4e8e\u670d\u52a1\u53d1\u73b0\u7684\u8def\u7531\n      routes:\n        - id: hep-api-userService\n          uri: lb:\/\/EUREKACLIENT\n          predicates:\n            - Path=\/**\n          filters:\n          - name: RequestRateLimiter\n            args:\n                key-resolver: &#039;#{@userIdResolver}&#039;\n                redis-rate-limiter.replenishRate: 1 #\u4ee4\u724c\u6876\u6bcfs\u7684\u586b\u5145\u901f\u5ea6\n                redis-rate-limiter.burstCapacity: 2 #\u4ee4\u724c\u6876\u5bb9\u91cf\n                redis-rate-limiter.requestedTokens: 1 #\u6bcf\u4e2a\u8bf7\u6c42\u6d88\u8017\u591a\u5c11\u4e2a\u4ee4\u724c\n          - PrefixPath=\/user\n\n  application:\n    name: hepGatewayEntrance # Gateway\u540d\u79f0<\/code><\/pre>\n<p>\u6211\u4eec\u5728\u8bf7\u6c42\u53c2\u6570\u4e2d\u589e\u52a0userId\u53c2\u6570\u5e76\u8bf7\u6c42<code>http:\/\/localhost:8005\/getUserById?userId=1111<\/code>\uff0c\u53ef\u4ee5\u770b\u5230\u5f53\u9650\u6d41\u7684\u65f6\u5019\u4f1a\u8fd4\u56de<code>429 Too Many Request<\/code>\uff0c\u800c\u6b63\u5f53\u8bbf\u95ee\u4f1a\u8fd4\u56de\u6b63\u786e\u7684\u7ed3\u679c\u6570\u636e\u3002<\/p>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2024\/11\/11\/userId.jpg\" alt=\"userId\" \/><\/p>\n<h4>5.3 \u63a5\u53e3\u9650\u6d41<\/h4>\n<p>\u63a5\u53e3\u9650\u6d41\u83b7\u53d6\u8bf7\u6c42\u5730\u5740\u7684<code>uri<\/code>\u4f5c\u4e3a\u9650\u6d41\u7684<code>key<\/code>\uff0c\u6211\u4eec\u6307\u5b9a\u4e00\u4e2a\u540d\u4e3a<code>@Bean(name = &quot;apiKeyResolver&quot;)<\/code>\u7684<code>Bean<\/code>\u3002<\/p>\n<pre><code class=\"language-java\">package cc.huerpu.eurekaclient;\n\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.cloud.client.discovery.EnableDiscoveryClient;\nimport org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;\nimport org.springframework.cloud.gateway.route.RouteLocator;\nimport org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;\nimport org.springframework.context.annotation.Bean;\nimport org.springframework.web.server.ServerWebExchange;\nimport reactor.core.publisher.Mono;\n\nimport java.util.Objects;\n\n@SpringBootApplication\n@EnableDiscoveryClient\npublic class HepGatewayEntranceApplication {\n\n    public static void main(String[] args) {\n        SpringApplication.run(HepGatewayEntranceApplication.class, args);\n    }\n\n    @Bean(name = &quot;apiKeyResolver&quot;)\n    public KeyResolver apiKeyResolver() {\n        return exchange -&gt; Mono.just(exchange.getRequest().getPath().value());\n    }\n\n}<\/code><\/pre>\n<p>\u5728<code>application.yml<\/code>\u6587\u4ef6\u4e2d\u6307\u5b9a<code>key-resolver: &#039;#{@apiKeyResolver}&#039;<\/code>\u3002<\/p>\n<pre><code class=\"language-yml\">server:\n  port: 8005 # \u7aef\u53e3\u53f7\n\nspring:\n  main:\n    web-application-type: reactive\n  cloud:\n    redis:\n      host: 127.0.0.1\n      port: 6379\n      password: \n      database: 0\n    gateway:\n      filter:\n        request-rate-limiter:\n          deny-empty-key: true #\u5982\u679ckeyResolver\u8fd4\u56de\u7a7akey\uff0c\u5219\u62d2\u7edd\u8be5\u8bf7\u6c42403\uff0c\u9ed8\u8ba4true\u8868\u793a\u62d2\u7edd\uff0cfalse\u5219\u8868\u793a\u5141\u8bb8\u8bbf\u95ee\n      discovery:\n        locator:\n          enabled: true #\u542f\u7528\u57fa\u4e8e\u670d\u52a1\u53d1\u73b0\u7684\u8def\u7531\n      routes:\n        - id: hep-api-userService\n          uri: lb:\/\/EUREKACLIENT\n          predicates:\n            - Path=\/**\n          filters:\n          - name: RequestRateLimiter\n            args:\n                key-resolver: &#039;#{@apiKeyResolver}&#039;\n                redis-rate-limiter.replenishRate: 1 #\u4ee4\u724c\u6876\u6bcfs\u7684\u586b\u5145\u901f\u5ea6\n                redis-rate-limiter.burstCapacity: 2 #\u4ee4\u724c\u6876\u5bb9\u91cf\n                redis-rate-limiter.requestedTokens: 1 #\u6bcf\u4e2a\u8bf7\u6c42\u6d88\u8017\u591a\u5c11\u4e2a\u4ee4\u724c\n          - PrefixPath=\/user\n\n  application:\n    name: hepGatewayEntrance # Gateway\u540d\u79f0<\/code><\/pre>\n<p>\u5f88\u7b80\u5355\u7684\uff0c\u8fd9\u91cc\u5c31\u4e0d\u622a\u56fe\u4e86\u54c8\u3002<\/p>\n<blockquote>\n<p>Reference\uff1a<\/p>\n<p>\u4ee3\u7801\u5171\u4eab\u5730\u5740\uff1a<a href=\"https:\/\/github.com\/jinglisen\/SpringCloud-Kilburn-2022\/tree\/master\">https:\/\/github.com\/jinglisen\/SpringCloud-Kilburn-2022\/tree\/master<\/a><\/p>\n<p>Spring Cloud Gateway(Kilburn 2022.0.2\u7248\u672c)\u5b98\u65b9\u6587\u6863\uff1a<a href=\"https:\/\/docs.spring.io\/spring-cloud-gateway\/docs\/4.0.9\/reference\/html\/\">https:\/\/docs.spring.io\/spring-cloud-gateway\/docs\/4.0.9\/reference\/html\/<\/a><br \/>\nSpring Cloud Gateway\u5b98\u65b9\u6587\u6863\uff1a<a href=\"https:\/\/cloud.spring.io\/spring-cloud-gateway\/reference\/html\/\">https:\/\/cloud.spring.io\/spring-cloud-gateway\/reference\/html\/<\/a><\/p>\n<p>Spring Cloud Gateway\u4e2d\u6587\u6587\u6863\uff1a<a href=\"https:\/\/springdoc.cn\/spring-cloud-gateway\/\">https:\/\/springdoc.cn\/spring-cloud-gateway\/<\/a><\/p>\n<p>SpringCloud-Gateway\u642d\u5efa\u4fdd\u59c6\u7ea7\u6559\u7a0b\uff1a<a href=\"https:\/\/www.cnblogs.com\/sun-10387834\/p\/17719582.html\">https:\/\/www.cnblogs.com\/sun-10387834\/p\/17719582.html<\/a><\/p>\n<p>SpringCloud Gateway\u53f2\u4e0a\u6700\u5168\uff1a<a href=\"https:\/\/www.cnblogs.com\/crazymakercircle\/p\/11704077.html\">https:\/\/www.cnblogs.com\/crazymakercircle\/p\/11704077.html<\/a><\/p>\n<p>\u6df1\u5165\u63a2\u7d22Spring Cloud Gateway\uff1a\u5fae\u670d\u52a1\u7f51\u5173\u7684\u6700\u4f73\u5b9e\u8df5\uff1a<a href=\"https:\/\/blog.csdn.net\/zhengzhaoyang122\/article\/details\/142744233\">https:\/\/blog.csdn.net\/zhengzhaoyang122\/article\/details\/142744233<\/a><\/p>\n<p>Spring Cloud Gateway API\u7f51\u5173\u7ec4\u4ef6\uff0c\u975e\u5e38\u8be6\u7ec6\uff1a<a href=\"https:\/\/c.biancheng.net\/springcloud\/gateway.html\">https:\/\/c.biancheng.net\/springcloud\/gateway.html<\/a><\/p>\n<p>spring cloud gateway \u4e4b\u9650\u6d41\u7bc7\uff1a<a href=\"https:\/\/www.cnblogs.com\/chengmr\/p\/13277824.html\">https:\/\/www.cnblogs.com\/chengmr\/p\/13277824.html<\/a><\/p>\n<p>spring cloud gateway\u7f51\u5173\u4f7f\u7528JMeter\u8fdb\u884c\u9650\u6d41\u6d4b\u8bd5\u4e0e\u7194\u65ad\uff1a<a href=\"https:\/\/cloud.tencent.com\/developer\/article\/2344888\">https:\/\/cloud.tencent.com\/developer\/article\/2344888<\/a><\/p>\n<p>\u6df1\u5165\u5b66\u4e60spring cloud gateway \u9650\u6d41\u7194\u65ad\uff1a<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/520347258\">https:\/\/zhuanlan.zhihu.com\/p\/520347258<\/a><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Spring Cloud(Kilburn 2022.0.2\u7248\u672c)\u7cfb\u5217\u6559\u7a0b(\u4e94) \u670d\u52a1\u7f51\u5173(SpringClou [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"class_list":["post-946","post","type-post","status-publish","format-standard","hentry","category-spring-cloudkilburn-2022-0-2"],"_links":{"self":[{"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/posts\/946","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=946"}],"version-history":[{"count":1,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/posts\/946\/revisions"}],"predecessor-version":[{"id":947,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=\/wp\/v2\/posts\/946\/revisions\/947"}],"wp:attachment":[{"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=946"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.huerpu.cc:7000\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}