{"id":10008266,"date":"2023-09-11T16:15:33","date_gmt":"2023-09-11T08:15:33","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008266"},"modified":"2023-09-11T16:15:33","modified_gmt":"2023-09-11T08:15:33","slug":"spring-cloud-gateway%ef%bc%9a%e6%96%b0%e4%b8%80%e4%bb%a3%e5%be%ae%e6%9c%8d%e5%8a%a1-api-%e7%bd%91%e5%85%b3%ef%bc%8c%e7%94%a8%e8%b5%b7%e6%9d%a5%e7%9c%9f%e4%bc%98%e9%9b%85%ef%bc%81","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008266","title":{"rendered":"Spring Cloud Gateway\uff1a\u65b0\u4e00\u4ee3\u5fae\u670d\u52a1 API \u7f51\u5173\uff0c\u7528\u8d77\u6765\u771f\u4f18\u96c5\uff01"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" width=\"690\" height=\"295\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9DlE\" alt=\"\" title=\"\"><\/p>\n<h2><strong>1.\u7f51\u5173\u4ecb\u7ecd<\/strong><\/h2>\n<p>\u5982\u679c\u6ca1\u6709\u7f51\u5173\uff0c\u96be\u9053\u4e0d\u884c\u5417\uff1f\u529f\u80fd\u4e0a\u662f\u53ef\u4ee5\u7684\uff0c\u6211\u4eec\u76f4\u63a5\u8c03\u7528\u63d0\u4f9b\u7684\u63a5\u53e3\u5c31\u53ef\u4ee5\u4e86\u3002\u90a3\u4e3a\u4ec0\u4e48\u8fd8\u9700\u8981\u7f51\u5173\uff1f<\/p>\n<p>\u56e0\u4e3a\u7f51\u5173\u7684\u4f5c\u7528\u4e0d\u4ec5\u4ec5\u662f\u8f6c\u53d1\u8bf7\u6c42\u800c\u5df2\u3002\u6211\u4eec\u53ef\u4ee5\u8bd5\u60f3\u4e00\u4e0b\uff0c\u5982\u679c\u9700\u8981\u505a\u4e00\u4e2a\u8bf7\u6c42\u8ba4\u8bc1\u529f\u80fd\uff0c\u6211\u4eec\u53ef\u4ee5\u63a5\u5165\u5230 API \u670d\u52a1\u4e2d\u3002\u4f46\u662f\u5018\u82e5\u540e\u7eed\u53c8\u6709\u670d\u52a1\u9700\u8981\u63a5\u5165\uff0c\u6211\u4eec\u53c8\u9700\u8981\u91cd\u590d\u63a5\u5165\u3002\u8fd9\u6837\u6211\u4eec\u4e0d\u4ec5\u4ee3\u7801\u8981\u91cd\u590d\u7f16\u5199\uff0c\u800c\u4e14\u540e\u671f\u4e5f\u4e0d\u5229\u4e8e\u7ef4\u62a4\u3002<\/p>\n<p>\u7531\u4e8e\u63a5\u5165\u7f51\u5173\u540e\uff0c\u7f51\u5173\u5c06\u8f6c\u53d1\u8bf7\u6c42\u3002\u6240\u4ee5\u5728\u8fd9\u4e00\u5c42\u505a\u8bf7\u6c42\u8ba4\u8bc1\uff0c\u5929\u7136\u5408\u9002\u3002\u8fd9\u6837\u8fd9\u9700\u8981\u7f16\u5199\u4e00\u6b21\u4ee3\u7801\uff0c\u5728\u8fd9\u4e00\u5c42\u8fc7\u6ee4\u5b8c\u6bd5\uff0c\u518d\u8f6c\u53d1\u7ed9\u4e0b\u9762\u7684 API\u3002<\/p>\n<p>\u6240\u4ee5 API \u7f51\u5173\u7684\u901a\u5e38\u4f5c\u7528\u662f\u5b8c\u6210\u4e00\u4e9b\u901a\u7528\u7684\u529f\u80fd\uff0c\u5982\u8bf7\u6c42\u8ba4\u8bc1\uff0c\u8bf7\u6c42\u8bb0\u5f55\uff0c\u8bf7\u6c42\u9650\u6d41\uff0c\u9ed1\u767d\u540d\u5355\u5224\u65ad\u7b49\u3002<\/p>\n<p>API\u7f51\u5173\u662f\u4e00\u4e2a\u670d\u52a1\u5668\uff0c\u662f\u7cfb\u7edf\u7684\u552f\u4e00\u5165\u53e3\u3002<\/p>\n<p>API\u7f51\u5173\u65b9\u5f0f\u7684\u6838\u5fc3\u8981\u70b9\u662f\uff0c\u6240\u6709\u7684\u5ba2\u6237\u7aef\u548c\u6d88\u8d39\u7aef\u90fd\u901a\u8fc7\u7edf\u4e00\u7684\u7f51\u5173\u63a5\u5165\u5fae\u670d\u52a1\uff0c\u5728\u7f51\u5173\u5c42\u5904\u7406\u6240\u6709\u7684\u975e\u4e1a\u52a1\u529f\u80fd\u3002\u901a\u5e38\uff0c\u7f51\u5173\u63d0\u4f9bREST\/HTTP\u7684\u8bbf\u95eeAPI\u3002<\/p>\n<h2><strong>2.Spring Cloud Gateway\u4ecb\u7ecd<\/strong><\/h2>\n<p>Spring Cloud Gateway\u662fSpring Cloud\u7684\u65b0\u4e00\u4ee3API\u7f51\u5173\uff0c\u57fa\u4e8eWebFlux\u6846\u67b6\u5b9e\u73b0\uff0c\u5b83\u65e8\u5728\u4e3a\u5fae\u670d\u52a1\u67b6\u6784\u63d0\u4f9b\u4e00\u79cd\u7b80\u5355\u800c\u6709\u6548\u7684\u7edf\u4e00\u7684API\u8def\u7531\u7ba1\u7406\u65b9\u5f0f\u3002<\/p>\n<p>Spring Cloud Gateway\u4f5c\u4e3aSpring Cloud\u751f\u6001\u7cfb\u7edf\u4e2d\u7684\u7f51\u5173\uff0c\u76ee\u6807\u662f\u66ff\u4ee3Netflix ZUUL\uff0c\u5177\u6709\u66f4\u597d\u7684\u6027\u80fd\u3001\u66f4\u5f3a\u7684\u6269\u5c55\u6027\u3001\u4ee5\u53ca\u66f4\u4e30\u5bcc\u7684\u529f\u80fd\u7279\u6027\uff0c\u5176\u4e0d\u4ec5\u63d0\u4f9b\u7edf\u4e00\u7684\u8def\u7531\u65b9\u5f0f\uff0c\u5e76\u4e14\u57fa\u4e8eFilter\u94fe\u7684\u65b9\u5f0f\u63d0\u4f9b\u4e86\u7f51\u5173\u57fa\u672c\u7684\u529f\u80fd\uff0c\u4f8b\u5982:\u5b89\u5168\uff0c\u76d1\u63a7\/\u57cb\u70b9\uff0c\u9650\u6d41\u7b49\u3002<\/p>\n<h2><strong>3.Spring Cloud Gateway\u7684\u7279\u6027<\/strong><\/h2>\n<ul>\n<li>\u57fa\u4e8eSpring Framework 5\uff0c Project Reactor\u548cSpring Boot 2.0<\/li>\n<li>\u52a8\u6001\u8def\u7531\uff1a\u80fd\u591f\u5339\u914d\u4efb\u4f55\u8bf7\u6c42\u5c5e\u6027<\/li>\n<li>\u53ef\u4ee5\u5bf9\u8def\u7531\u6307\u5b9a Predicate \u548c Filter<\/li>\n<li>\u96c6\u6210Hystrix\u65ad\u8def\u5668<\/li>\n<li>\u96c6\u6210Spring Cloud DiscoveryClient \u670d\u52a1\u53d1\u73b0\u529f\u80fd<\/li>\n<li>\u6613\u4e8e\u7f16\u5199\u7684Predicate\u548cFilter<\/li>\n<li>\u8bf7\u6c42\u9650\u6d41<\/li>\n<li>\u652f\u6301\u8def\u5f84\u91cd\u5199<\/li>\n<\/ul>\n<h2><strong>4.Spring Cloud Gateway\u7684\u4e09\u5927\u6838\u5fc3\u6982\u5ff5<\/strong><\/h2>\n<p><strong>\u8def\u7531(Route)\uff1a<\/strong> \u8def\u7531\u662f\u7f51\u5173\u6700\u57fa\u7840\u7684\u90e8\u5206\uff0c\u8def\u7531\u4fe1\u606f\u7531\u4e00\u4e2aID\uff0c\u4e00\u4e2a\u76ee\u6807URI\uff0c\u4e00\u7ec4\u65ad\u8a00\u548c\u8fc7\u6ee4\u5668\u7ec4\u6210\u3002\u8def\u7531\u65ad\u8a00Predicate\u7528\u4e8e\u5339\u914d\u8bf7\u6c42\uff0c\u8fc7\u6ee4\u5668Filter\u7528\u4e8e\u4fee\u6539\u8bf7\u6c42\u548c\u54cd\u5e94\u3002\u5982\u679c\u65ad\u8a00\u4e3atrue\uff0c\u5219\u8bf4\u660e\u8bf7\u6c42URI\u548c\u914d\u7f6e\u5339\u914d\uff0c\u5219\u6267\u884c\u8def\u7531\u3002<\/p>\n<pre><code>spring:\n  cloud:\n    gateway:\n      # \u5b9a\u4e49\u591a\u4e2a\u8def\u7531\n      routes:\n      # \u4e00\u4e2a\u8def\u7531route\u7684id\n      - id: path_route\n        # \u8be5\u8def\u7531\u8f6c\u53d1\u7684\u76ee\u6807URI\n        uri: https:\/\/example.org\n        # \u8def\u7531\u6761\u4ef6\u96c6\u5408\n        predicates:\n        - Path=\/test\/**\n        # \u8fc7\u6ee4\u5668\u96c6\u5408\n        filters:\n        - AddRequestHeader=X-Request-Id, 1024\n        - AddRequestParameter=color, red<\/code><\/pre>\n<p><strong>\u65ad\u8a00(Predicate)\uff1a<\/strong> \u53c2\u8003Java8\u4e2d\u7684\u65ad\u8a00Predicate\uff0c\u7528\u4e8e\u5b9e\u73b0\u8bf7\u6c42\u5339\u914d\u903b\u8f91\uff0c\u4f8b\u5982\u5339\u914d\u8def\u5f84\u3001\u8bf7\u6c42\u5934\u3001\u8bf7\u6c42\u53c2\u6570\u7b49\u3002\u8bf7\u6c42\u4e0e\u65ad\u8a00\u5339\u914d\u5219\u6267\u884c\u8be5\u8def\u7531\u3002<\/p>\n<p><strong>\u8fc7\u6ee4\u5668(Filter)\uff1a<\/strong> \u6307\u7684\u662fSpring\u6846\u67b6\u4e2d<code>GatewayFilter<\/code>\u7684\u5b9e\u4f8b\uff0c\u4f7f\u7528\u8fc7\u6ee4\u5668\uff0c\u53ef\u4ee5\u5728\u8bf7\u6c42\u88ab\u8def\u7531\u524d\u540e\u5bf9\u8bf7\u6c42\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<h2><strong>5.Gateway\u5de5\u4f5c\u6d41\u7a0b<\/strong><\/h2>\n<p>\u5ba2\u6237\u7aef\u5411<code>Spring Cloud Gateway<\/code>\u53d1\u51fa\u8bf7\u6c42\uff0c\u7136\u540e\u5728<code>Gateway Handler Mapping<\/code>\u4e2d\u627e\u5230\u4e0e\u8bf7\u6c42\u76f8\u5339\u914d\u7684\u8def\u7531\uff0c\u5c06\u5176\u53d1\u9001\u5230<code>Gateway Web Handler<\/code>\u3002Handler\u518d\u901a\u8fc7\u6307\u5b9a\u7684\u8fc7\u6ee4\u5668\u94fe\u6765\u5bf9\u8bf7\u6c42\u8fdb\u884c\u8fc7\u6ee4\u5904\u7406\uff0c\u6700\u540e\u53d1\u9001\u5230\u6211\u4eec\u5b9e\u9645\u7684\u670d\u52a1\u6267\u884c\u4e1a\u52a1\u903b\u8f91\uff0c\u7136\u540e\u8fd4\u56de\u3002<\/p>\n<p><img decoding=\"async\" width=\"411\" height=\"547\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc9DlF\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u8fc7\u6ee4\u5668\u94fe\u88ab\u865a\u7ebf\u5206\u9694\uff0c\u662f\u56e0\u4e3a\u8fc7\u6ee4\u5668\u65e2\u53ef\u4ee5\u5728\u8f6c\u53d1\u8bf7\u6c42\u524d\u62e6\u622a\u8bf7\u6c42\uff0c\u4e5f\u53ef\u4ee5\u5728\u8bf7\u6c42\u5904\u7406\u4e4b\u540e\u5bf9\u54cd\u5e94\u8fdb\u884c\u62e6\u622a\u5904\u7406\u3002<\/p>\n<p>\u63a8\u8350\u4e00\u4e2a\u5f00\u6e90\u514d\u8d39\u7684 Spring Boot \u5b9e\u6218\u9879\u76ee\uff1a<\/p>\n<blockquote><p><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=Nt3XcSVFVYK18OkVi%2FwWsQ%3D%3D.NuZkOoGgkalVWrrL2Tna2yGDTQ5zYvRxiNKG9f9TK9SkfA1HZSWgJIDjaIBO9pe8%2BCgEjG8C3So%2BAcCnECU%2BaQ%3D%3D\" rel=\"noopener noreferrer\">https:\/\/github.com\/javastacks\/spring-boot-best-practice<\/a><\/p><\/blockquote>\n<h2><strong>6.Gateway\u6838\u5fc3\u914d\u7f6e<\/strong><\/h2>\n<p>\u4f9d\u8d56<\/p>\n<pre><code>&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>\u542f\u52a8\u7c7b<\/p>\n<pre><code>@SpringBootApplication\n@EnableEurekaClient\npublic class GatewayApplication {\n\n public static void main(String[] args) {\n  SpringApplication.run(GatewayApplication.class, args);\n }\n\n}<\/code><\/pre>\n<p>application.yml<\/p>\n<pre><code>spring: \n  application:\n    name: cloud-gateway \n  cloud:\n    gateway:\n      routes:\n      # \u8def\u7531\u7684ID\uff0c\u6ca1\u6709\u56fa\u5b9a\u89c4\u5219\u4f46\u8981\u6c42\u552f\u4e00\uff0c\u5efa\u8bae\u914d\u5408\u670d\u52a1\u540d\n      - id: config_route\n       # \u5339\u914d\u540e\u63d0\u4f9b\u670d\u52a1\u7684\u8def\u7531\u5730\u5740\n        uri: http:\/\/ityouknow.com\n        # \u65ad\u8a00\uff0c\u8def\u5f84\u76f8\u5339\u914d\u7684\u6761\u4ef6\n        predicates:\n        - Path=\/routeconfig\/rest\/**\n      - id: header_route\n        uri: http:\/\/ityouknow.com\n        predicates:\n        - Header=X-Request-Id, \/d+<\/code><\/pre>\n<h2><strong>7.\u52a8\u6001\u8def\u7531<\/strong><\/h2>\n<p>\u7f51\u5173\u63a5\u6536\u5916\u90e8\u8bf7\u6c42\uff0c\u6309\u7167\u4e00\u5b9a\u7684\u89c4\u5219\uff0c\u5c06\u8bf7\u6c42\u8f6c\u53d1\u7ed9\u5176\u4ed6\u670d\u52a1\u6216\u8005\u5e94\u7528\u3002\u5982\u679c\u7ad9\u5728\u670d\u52a1\u8c03\u7528\u7684\u89d2\u5ea6\uff0c\u7f51\u5173\u5c31\u626e\u6f14\u7740\u670d\u52a1\u6d88\u8d39\u8005\u7684\u89d2\u8272\uff0c\u6b64\u65f6\uff0c\u5982\u679c\u518d\u6765\u770b\u770b\u670d\u52a1\u8c03\u7528\u7684\u76ee\u6807URI\u914d\u7f6e\uff0c\u5c31\u4f1a\u5f88\u81ea\u7136\u7684\u53d1\u73b0\u4e00\u4e2a\u95ee\u9898\uff0c\u670d\u52a1\u63d0\u4f9b\u8005\u8c03\u7528\u7684\u5730\u5740\u662f\u5199\u6b7b\u7684\uff0c\u5373\u7f51\u5173\u6ca1\u6709\u52a8\u6001\u7684\u53d1\u73b0\u670d\u52a1\uff0c\u8fd9\u5c31\u6d89\u53ca\u5230\u4e86\u670d\u52a1\u7684\u81ea\u52a8\u53d1\u73b0\u95ee\u9898\uff0c\u4ee5\u53ca\u53d1\u73b0\u670d\u52a1\u540e\uff0c\u6240\u6d89\u53ca\u5230\u7684\u670d\u52a1\u8c03\u7528\u7684\u8d1f\u8f7d\u5747\u8861\u7684\u95ee\u9898\u3002<\/p>\n<p>\u53ef\u4ee5\u901a\u8fc7Nacos\u6216\u8005Eureka\u6ce8\u518c\u4e2d\u5fc3\u52a8\u6001\u53d1\u73b0\u670d\u52a1\uff0c\u901a\u8fc7Ribbon\u8fdb\u884c\u670d\u52a1\u8c03\u7528\u7684\u8d1f\u8f7d\u5747\u8861\u3002\u540c\u6837\uff0cGateway\u4e5f\u53ef\u4ee5\u6574\u5408Nacos\u6216\u8005Eureka\uff0cRibbon\u4ece\u800c\u5b9e\u73b0\u52a8\u6001\u8def\u7531\u7684\u529f\u80fd\u3002<\/p>\n<p>\u60f3\u8981\u4f7f\u7528\u52a8\u6001\u8def\u7531\u7684\u529f\u80fd\uff0c\u9996\u5148\u8981\u6574\u5408\u6ce8\u518c\u4e2d\u5fc3\uff0c\u8fd9\u91cc\u4ee5Nacos\u4e3a\u4f8b<\/p>\n<pre><code>&lt;!--SpringCloud ailibaba nacos --&gt;\n&lt;dependency&gt;\n &lt;groupId&gt;com.alibaba.cloud&lt;\/groupId&gt;\n &lt;artifactId&gt;spring-cloud-starter-alibaba-nacos-discovery&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\nspring:\n  application:\n    name: cloud-gateway\n  cloud:\n    nacos:\n      discovery:\n        server-addr: localhost:8848\n    gateway:\n      routes:\n        #\u8def\u7531\u7684ID\uff0c\u6ca1\u6709\u56fa\u5b9a\u89c4\u5219\u4f46\u8981\u6c42\u552f\u4e00\uff0c\u5efa\u8bae\u914d\u5408\u670d\u52a1\u540d\n        - id: config_route\n  #\u5339\u914d\u540e\u63d0\u4f9b\u670d\u52a1\u7684\u8def\u7531\u5730\u5740, \u8fd9\u91cclb\u4e4b\u540e\uff0c\u8ddf\u7684\u662f\u8981\u8c03\u7528\u7684\u670d\u52a1\u540d\u79f0\n          uri: lb:\/\/nacos-provider-8002\n  # \u65ad\u8a00\uff0c\u8def\u5f84\u76f8\u5339\u914d\u7684\u6761\u4ef6\n          predicates:\n            - Path=\/routeconfig\/rest\/**<\/code><\/pre>\n<p>\u6b64\u65f6\uff0c\u5f53id\u4e3a<code>config_route<\/code>\u7684\u8def\u7531\u89c4\u5219\u5339\u914d\u67d0\u4e2a\u8bf7\u6c42\u540e\uff0c\u5728\u8c03\u7528\u8be5\u8bf7\u6c42\u5bf9\u5e94\u7684\u670d\u52a1\u65f6\uff0c\u5c31\u4f1a\u4ecenacos\u6ce8\u518c\u4e2d\u5fc3\u81ea\u52a8\u53d1\u73b0\u670d\u52a1\uff0c\u5e76\u5728\u670d\u52a1\u8c03\u7528\u7684\u65f6\u5019\u5b9e\u73b0\u8d1f\u8f7d\u5747\u8861\u3002<\/p>\n<h2><strong>8.Predicate<\/strong><\/h2>\n<p>\u5728Gateway\u4e2d\uff0c\u6709\u4e00\u4e9b\u7684\u5185\u7f6e<code>Predicate Factory<\/code>\uff0c\u6709\u4e86\u8fd9\u4e9b<code>Pridicate Factory<\/code>\uff0c\u5728\u8fd0\u884c\u65f6\uff0cGateway\u4f1a \u81ea\u52a8\u6839\u636e\u9700\u8981\u521b\u5efa\u5176\u5bf9\u5e94\u7684Pridicate\u5bf9\u8c61\u6d4b\u8bd5\u8def\u7531\u6761\u4ef6\u3002<\/p>\n<p><strong>Path \u8def\u7531\u65ad\u8a00 Factory\uff1a<\/strong> \u6839\u636e\u8bf7\u6c42\u8def\u5f84\u5339\u914d\u7684\u8def\u7531\u6761\u4ef6\u5de5\u5382<\/p>\n<pre><code>spring:\n  cloud:\n    gateway:\n      routes:\n      - id: path_route\n        uri: https:\/\/example.org\n        predicates:\n        # \u5982\u679c\u53ef\u4ee5\u5339\u914d\u7684PathPattern\u6709\u591a\u4e2a\uff0c\u5219\u6bcf\u4e2a\u8def\u5f84\u6a21\u5f0f\u4ee5\uff0c\u5206\u5f00\n        - Path=\/red\/{segment},\/blue\/{segment}<\/code><\/pre>\n<p><strong>After \u8def\u7531\u65ad\u8a00 Factory\uff1a<\/strong> \u5728\u6307\u5b9a\u65e5\u671f\u65f6\u95f4\u4e4b\u540e\u53d1\u751f\u7684\u8bf7\u6c42\u90fd\u5c06\u88ab\u5339\u914d<\/p>\n<pre><code>spring:\n  cloud:\n    gateway:\n      routes:\n      - id: after_route\n        uri: https:\/\/example.org\n        predicates:\n        - After=2017-01-20T17:42:47.789-07:00[America\/Denver]<\/code><\/pre>\n<p><strong>Cookie \u8def\u7531\u65ad\u8a00 Factory\uff1a<\/strong> Cookie \u8def\u7531\u65ad\u8a00 Factory\u6709\u4e24\u4e2a\u53c2\u6570\uff0ccookie\u540d\u79f0\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u8bf7\u6c42\u5305\u542b\u6b64cookie\u540d\u79f0\u4e14\u6b63\u5219\u8868\u8fbe\u5f0f\u4e3a\u771f\u7684\u5c06\u4f1a\u88ab\u5339\u914d\u3002<\/p>\n<pre><code>spring:\n  cloud:\n    gateway:\n      routes:\n      - id: cookie_route\n        uri: https:\/\/example.org\n        predicates:\n        - Cookie=chocolate, ch.p<\/code><\/pre>\n<p><strong>Header \u8def\u7531\u65ad\u8a00 Factory\uff1a<\/strong> Header \u8def\u7531\u65ad\u8a00 Factory\u6709\u4e24\u4e2a\u53c2\u6570\uff0cheader\u540d\u79f0\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u8bf7\u6c42\u5305\u542b\u6b64header\u540d\u79f0\u4e14\u6b63\u5219\u8868\u8fbe\u5f0f\u4e3a\u771f\u7684\u5c06\u4f1a\u88ab\u5339\u914d\u3002<\/p>\n<pre><code>spring:\n  cloud:\n    gateway:\n      routes:\n      - id: header_route\n        uri: https:\/\/example.org\n        predicates:\n        - Header=X-Request-Id, \/d+<\/code><\/pre>\n<p><strong>Host \u8def\u7531\u65ad\u8a00 Factory\uff1a<\/strong> Host \u8def\u7531\u65ad\u8a00 Factory\u5305\u62ec\u4e00\u4e2a\u53c2\u6570\uff1ahost name\u5217\u8868\u3002\u4f7f\u7528Ant\u8def\u5f84\u5339\u914d\u89c4\u5219\uff0c . \u4f5c\u4e3a\u5206\u9694\u7b26\u3002<\/p>\n<pre><code>spring:\n  cloud:\n    gateway:\n      routes:\n      - id: host_route\n        uri: https:\/\/example.org\n        predicates:\n        - Host=**.somehost.org,**.anotherhost.org<\/code><\/pre>\n<p><strong>Method \u8def\u7531\u65ad\u8a00 Factory\uff1a<\/strong> Method \u8def\u7531\u65ad\u8a00 Factory\u53ea\u5305\u542b\u4e00\u4e2a\u53c2\u6570\uff1a\u9700\u8981\u5339\u914d\u7684HTTP\u8bf7\u6c42\u65b9\u5f0f<\/p>\n<pre><code>spring:\n  cloud:\n    gateway:\n      routes:\n      - id: method_route\n        uri: https:\/\/example.org\n        predicates:\n        - Method=GET<\/code><\/pre>\n<h4>\u81ea\u5b9a\u4e49Predicate<\/h4>\n<p>\u53ef\u4ee5\u81ea\u5b9a\u4e49Predicate\u6765\u5b9e\u73b0\u590d\u6742\u7684\u8def\u7531\u5339\u914d\u89c4\u5219:<\/p>\n<pre><code>\/\/ \u5b9e\u73b0\u81ea\u5b9a\u4e49 Predicate \u5de5\u5382\n\/\/ \u901a\u8fc7HostRoutePredicateFactory\u521b\u5efaPredicate\u8fdb\u884c\u8def\u7531\u5224\u65ad\n@Component\npublic class MyHostRoutePredicateFactory extends AbstractRoutePredicateFactory&lt;MyHostRoutePredicateFactory.Config&gt; {\n\n  public MyHostRoutePredicateFactory() {\n      \/\/ Config \u7c7b\u4f5c\u4e3a Predicate \u7684\u914d\u7f6e\u53c2\u6570\u7c7b\n      super(Config.class);\n  }\n\n  public static class Config {\n      \/\/ \u8def\u7531\u5339\u914d\u89c4\u5219\n      private String hostName;\n\n      public String getHostName() {\n        return hostName;\n      }\n\n      public void setHostName(String hostName) {\n        this.hostName = hostName; \n      }\n  }\n\n  \/\/ \u751f\u6210\u4e00\u4e2a Predicate \u5b9e\u4f8b\n  @Override\n  public Predicate&lt;ServerWebExchange&gt; apply(Config config) {\n      \/\/ \u5b9e\u73b0\u5339\u914d\u903b\u8f91\n      return exchange -&gt; {\n          \/\/ \u6839\u636econfig\u5b9e\u73b0\u5339\u914d\u5224\u65ad\n          \n          String host = exchange.getRequest().getURI().getHost();\n          \/\/ \u5339\u914d\u914d\u7f6e\u4e2d\u7684\u57df\u540d\n          return host.equals(config.getHostName());\n      };\n  } \n\n}\n\n\/\/ \u4f7f\u7528\nRouteLocator locator = new RouteLocatorBuilder(router)\n  .routes()\n  .route(&quot;test_route&quot;, r -&gt; r.path(&quot;\/test&quot;)\n  .filters(f -&gt; f.filter(new MyHostRoutePredicateFactory.Config(&quot;www.test.com&quot;)))\n  .uri(&quot;http:\/\/localhost:8080&quot;))\n  .build();<\/code><\/pre>\n<h2><strong>9.\u81ea\u5b9a\u4e49Filter<\/strong><\/h2>\n<p>\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0GatewayFilter\u548cOrdered\u63a5\u53e3\u81ea\u5b9a\u4e49Filter\u6765\u5b9e\u73b0\u8bf7\u6c42\u5904\u7406\u903b\u8f91:<\/p>\n<pre><code>@Component\npublic class TokenFilter implements GatewayFilter, Ordered {\n\n   @Override\n   public Mono&lt;Void&gt; filter(ServerWebExchange exchange, GatewayFilterChain chain) {\n      \/\/\u8bf7\u6c42\u5904\u7406\u903b\u8f91\n      log.info(&quot;\u8bf7\u6c42\u8def\u5f84:&quot;+ exchange.getRequest().getPath());\n      \n      ServerHttpRequest request = exchange.getRequest();\n   MultiValueMap&lt;String, HttpCookie&gt; cookies = request.getCookies();\n   List&lt;HttpCookie&gt; tokens = cookies.get(&quot;access_token&quot;);\n   if (tokens == null || tokens.size() == 0) {\n      throw new RuntimeException(&quot;\u5c11\u4e86cookie\uff01&quot;);\n   }\n      \n      return chain.filter(exchange);\n   }\n\n   @Override\n   public int getOrder() {\n      return 0; \n   }\n\n}<\/code><\/pre>\n<h2><strong>10.\u9ed8\u8ba4\u8fc7\u6ee4\u5668<\/strong><\/h2>\n<p>Spring Cloud Gateway\u5185\u7f6e\u4e86\u591a\u79cd\u8fc7\u6ee4\u5668,\u4f8b\u5982:<\/p>\n<ul>\n<li><code>AddRequestHeader GatewayFilter<\/code>:\u5728\u8bf7\u6c42\u5934\u4e2d\u6dfb\u52a0\u53c2\u6570<\/li>\n<li><code>PrefixPath GatewayFilter<\/code>:\u8bf7\u6c42\u8def\u5f84\u524d\u7f00<\/li>\n<li><code>Hystrix GatewayFilter<\/code>: \u65ad\u8def\u5668<\/li>\n<li><code>RateLimit GatewayFilter<\/code>: \u9650\u6d41<\/li>\n<li><code>Retry GatewayFilter<\/code>: \u91cd\u8bd5<\/li>\n<\/ul>\n<blockquote><p>\u6765\u6e90\uff1ablog.csdn.net\/Cristiano272\/article\/details\/131814765<\/p><\/blockquote>\n<p><strong>\u8fd1\u671f\u70ed\u6587\u63a8\u8350\uff1a<\/strong><\/p>\n<p>1.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=eZEqmLhl1RlQzxWvPYJ33g%3D%3D.xwClDetWMFy6CYsalsd%2FewwMm%2FjcF%2Bj%2BXLUUGJAIvIM%3D\" rel=\"noopener noreferrer\">1,000+ \u9053 Java\u9762\u8bd5\u9898\u53ca\u7b54\u6848\u6574\u7406(2022\u6700\u65b0\u7248)<\/a><\/p>\n<p>2.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=vNQrc6V5xkOgNOb8W7BL8w%3D%3D.y00SyvL2WibNa8LPGirULgh2NegLWod6GGbc2leuP4YR%2BRAj%2FsgdAJ6RvCoNBNCqvh5xr6w8Mcfxh9qQS%2F0cEg%3D%3D\" rel=\"noopener noreferrer\">\u52b2\u7206\uff01Java \u534f\u7a0b\u8981\u6765\u4e86\u3002\u3002\u3002<\/a><\/p>\n<p>3.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=GuCN1l2VMxE1NwZ5JMkoKw%3D%3D.F0DIbZLPhj84s87AIdC7GGkyKrdB%2BVXw1wuwfXYYx7OsAAL%2FMFNKQDP%2Funrfc1Ea\" rel=\"noopener noreferrer\">Spring Boot 2.x \u6559\u7a0b\uff0c\u592a\u5168\u4e86\uff01<\/a><\/p>\n<p>4.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=tPJUK57TSlsNMTpot0sHlA%3D%3D.dlnR6ofG76eeegIi8pXplvOMsVG6Niv32gmMFHMW%2BOnv7We02pv%2FwS4%2F75I4odPEXYE0ohCHzhHIYVMI5ctjmA%3D%3D\" rel=\"noopener noreferrer\">\u522b\u518d\u5199\u6ee1\u5c4f\u7684\u7206\u7206\u7206\u70b8\u7c7b\u4e86\uff0c\u8bd5\u8bd5\u88c5\u9970\u5668\u6a21\u5f0f\uff0c\u8fd9\u624d\u662f\u4f18\u96c5\u7684\u65b9\u5f0f\uff01\uff01<\/a><\/p>\n<p>5.<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=L5nLP7NbcZ5jxNtKkw%2BqyQ%3D%3D.853b6O%2BY2w41TWaMr0nZGMOtNHvx3HQ3xjTNiaRcTWTSd7ELof%2BE3n%2Fo%2Bun5h%2Fl07RIvi1wT9cLODszXZktygZsQyMM74cNOcV3kZsUAZ1s%3D\" rel=\"noopener noreferrer\">\u300aJava\u5f00\u53d1\u624b\u518c\uff08\u5d69\u5c71\u7248\uff09\u300b\u6700\u65b0\u53d1\u5e03\uff0c\u901f\u901f\u4e0b\u8f7d\uff01<\/a><\/p>\n<p>\u89c9\u5f97\u4e0d\u9519\uff0c\u522b\u5fd8\u4e86\u968f\u624b\u70b9\u8d5e+\u8f6c\u53d1\u54e6\uff01<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044202870\">https:\/\/segmentfault.com\/a\/1190000044202870<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1.\u7f51\u5173\u4ecb\u7ecd \u5982\u679c\u6ca1\u6709\u7f51\u5173\uff0c\u96be\u9053\u4e0d\u884c\u5417\uff1f\u529f\u80fd\u4e0a\u662f\u53ef\u4ee5\u7684\uff0c\u6211\u4eec\u76f4\u63a5\u8c03\u7528\u63d0\u4f9b\u7684\u63a5\u53e3\u5c31\u53ef\u4ee5\u4e86\u3002\u90a3\u4e3a\u4ec0\u4e48\u8fd8\u9700\u8981\u7f51\u5173\uff1f  [&hellip;]<\/p>\n","protected":false},"author":1,"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-10008266","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\/10008266","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008266"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008266\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}