{"id":4670,"date":"2021-12-19T17:44:14","date_gmt":"2021-12-19T09:44:14","guid":{"rendered":"http:\/\/123.57.164.21\/?p=4670"},"modified":"2021-12-19T17:44:14","modified_gmt":"2021-12-19T09:44:14","slug":"spring-cloud-gateway-%e5%88%9d%e4%bd%93%e9%aa%8c","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=4670","title":{"rendered":"Spring Cloud Gateway \u521d\u4f53\u9a8c"},"content":{"rendered":"\n<h5 class=\"wp-block-heading\">\u7b80\u4ecb<\/h5>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>Spring Cloud Gateway\u662fSpring Cloud\u5b98\u65b9\u63a8\u51fa\u7684\u7b2c\u4e8c\u4ee3\u7f51\u5173\u6846\u67b6\uff0c\u53d6\u4ee3Zuul\u7f51\u5173\u3002\u7f51\u5173\u4f5c\u4e3a\u6d41\u91cf\u7684\uff0c\u5728\u5fae\u670d\u52a1\u7cfb\u7edf\u4e2d\u6709\u7740\u975e\u5e38\u4f5c\u7528\uff0c\u7f51\u5173\u5e38\u89c1\u7684\u529f\u80fd\u6709\u8def\u7531\u8f6c\u53d1\u3001\u6743\u9650\u6821\u9a8c\u3001\u9650\u6d41\u63a7\u5236\u7b49\u4f5c\u7528\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u521b\u5efa\u5de5\u7a0b<\/h5>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u672c\u6848\u4f8b\u7684\u7684\u6e90\u7801\u4e0b\u8f7d\u4e8e<a href=\"https:\/\/github.com\/spring-guides\/gs-gateway.git\">\u5b98\u65b9\u6848\u4f8b<\/a>\u3002\u5de5\u7a0b\u4f7f\u7528\u7684Spring Boot\u7248\u672c\u4e3a2.0.5.RELEASE\uff0cSpring Cloud\u7248\u672c\u4e3aFinchley.SR1\u3002<\/p>\n\n\n\n<p>\u65b0\u5efa\u4e00\u4e2a\u5de5\u7a0b\uff0c\u53d6\u540d\u4e3asc-f-gateway-first-sight\u5728\u5de5\u7a0b\u7684pom\u6587\u4ef6\u5f15\u7528\u5de5\u7a0b\u6240\u9700\u7684\u4f9d\u8d56\uff0c\u5305\u62ecspring boot\u548cspring cloud\uff0c\u4ee5\u53cagateway\u7684\u8d77\u6b65\u4f9d\u8d56spring-cloud-starter-gateway\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> &lt;parent>\n        &lt;groupId>org.springframework.boot&lt;\/groupId>\n        &lt;artifactId>spring-boot-starter-parent&lt;\/artifactId>\n        &lt;version>2.0.5.RELEASE&lt;\/version>\n    &lt;\/parent>\n\n    &lt;dependencyManagement>\n        &lt;dependencies>\n            &lt;dependency>\n                &lt;groupId>org.springframework.cloud&lt;\/groupId>\n                &lt;artifactId>spring-cloud-dependencies&lt;\/artifactId>\n                &lt;version>Finchley.SR1&lt;\/version>\n                &lt;type>pom&lt;\/type>\n                &lt;scope>import&lt;\/scope>\n            &lt;\/dependency>\n        &lt;\/dependencies>\n    &lt;\/dependencyManagement>\n &lt;dependency>\n            &lt;groupId>org.springframework.cloud&lt;\/groupId>\n            &lt;artifactId>spring-cloud-starter-gateway&lt;\/artifactId>\n&lt;\/dependency><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u8def\u7531<\/h5>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5728spring cloud gateway\u4e2d\u4f7f\u7528RouteLocator\u7684Bean\u8fdb\u884c\u8def\u7531\u8f6c\u53d1\uff0c\u5c06\u8bf7\u6c42\u8fdb\u884c\u5904\u7406\uff0c\u6700\u540e\u8f6c\u53d1\u5230\u76ee\u6807\u7684\u4e0b\u6e38\u670d\u52a1\u3002\u5728\u672c\u6848\u4f8b\u4e2d\uff0c\u4f1a\u5c06\u8bf7\u6c42\u8f6c\u53d1\u5230http:\/\/httpbin.org:80\u8fd9\u4e2a\u5730\u5740\u4e0a\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@SpringBootApplication\n@RestController\npublic class Application {\n\n    public static void main(String[] args) {\n        SpringApplication.run(Application.class, args);\n    }\n    @Bean\n    public RouteLocator myRoutes(RouteLocatorBuilder builder) {\n       return builder.routes()\n        .route(p -> p\n            .path(\"\/get\")\n            .filters(f -> f.addRequestHeader(\"Hello\", \"World\"))\n            .uri(\"http:\/\/httpbin.org:80\"))\n        .build();\n    }\n    \n    }\n<\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684myRoutes\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528\u4e86\u4e00\u4e2aRouteLocatorBuilder\u7684bean\u53bb\u521b\u5efa\u8def\u7531\uff0c\u9664\u4e86\u521b\u5efa\u8def\u7531RouteLocatorBuilder\u53ef\u4ee5\u8ba9\u4f60\u6dfb\u52a0\u5404\u79cd<strong>predicates<\/strong>\u548c<strong>filters<\/strong>\uff0cpredicates\u65ad\u8a00\u7684\u610f\u601d\uff0c\u987e\u540d\u601d\u4e49\u5c31\u662f\u6839\u636e\u5177\u4f53\u7684\u8bf7\u6c42\u7684\u89c4\u5219\uff0c\u7531\u5177\u4f53\u7684route\u53bb\u5904\u7406\uff0cfilters\u662f\u5404\u79cd\u8fc7\u6ee4\u5668\uff0c\u7528\u6765\u5bf9\u8bf7\u6c42\u505a\u5404\u79cd\u5224\u65ad\u548c\u4fee\u6539\u3002<\/p>\n\n\n\n<p>\u4e0a\u9762\u521b\u5efa\u7684route\u53ef\u4ee5\u8ba9\u8bf7\u6c42\u201c\/get\u201d\u8bf7\u6c42\u90fd\u8f6c\u53d1\u5230\u201chttp:\/\/httpbin.org\/get\u201d\u3002\u5728route\u914d\u7f6e\u4e0a\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2afilter\uff0c\u8be5filter\u4f1a\u5c06\u8bf7\u6c42\u6dfb\u52a0\u4e00\u4e2aheader,key\u4e3ahello\uff0cvalue\u4e3aworld\u3002<\/p>\n\n\n\n<p>\u542f\u52a8springboot\u9879\u76ee\uff0c\u5728\u6d4f\u89c8\u5668\u4e0ahttp:\/\/localhost:8080\/get\uff0c\u6d4f\u89c8\u5668\u663e\u793a\u5982\u4e0b:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">{\n  \"args\": {}, \n  \"headers\": {\n    \"Accept\": \"text\/html,application\/xhtml+xml,application\/xml;q=0.9,image\/webp,image\/apng,*\/*;q=0.8\", \n    \"Accept-Encoding\": \"gzip, deflate, br\", \n    \"Accept-Language\": \"zh-CN,zh;q=0.9,en;q=0.8\", \n    \"Cache-Control\": \"max-age=0\", \n    \"Connection\": \"close\", \n    \"Cookie\": \"_ga=GA1.1.412536205.1526967566; JSESSIONID.667921df=node01oc1cdl4mcjdx1mku2ef1l440q1.node0; screenResolution=1920x1200\", \n    \"Forwarded\": \"proto=http;host=\\\"localhost:8080\\\";for=\\\"0:0:0:0:0:0:0:1:60036\\\"\", \n    \"Hello\": \"World\", \n    \"Host\": \"httpbin.org\", \n    \"Upgrade-Insecure-Requests\": \"1\", \n    \"User-Agent\": \"Mozilla\/5.0 (Windows NT 6.1; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/69.0.3497.100 Safari\/537.36\", \n    \"X-Forwarded-Host\": \"localhost:8080\"\n  }, \n  \"origin\": \"0:0:0:0:0:0:0:1, 210.22.21.66\", \n  \"url\": \"http:\/\/localhost:8080\/get\"\n}\n<\/pre>\n\n\n\n<p>\u53ef\u89c1\u5f53\u6211\u4eec\u5411gateway\u5de5\u7a0b\u8bf7\u6c42\u201c\/get\u201d,gateway\u4f1a\u5c06\u5de5\u7a0b\u7684\u8bf7\u6c42\u8f6c\u53d1\u5230\u201chttp:\/\/httpbin.org\/get\u201d\uff0c\u5e76\u4e14\u5728\u8f6c\u53d1\u4e4b\u524d\uff0c\u52a0\u4e0a\u4e00\u4e2afilter\uff0c\u8be5filter\u4f1a\u5c06\u8bf7\u6c42\u6dfb\u52a0\u4e00\u4e2aheader,key\u4e3ahello\uff0cvalue\u4e3aworld\u3002<\/p>\n\n\n\n<p>\u6ce8\u610fHTTPBin\u5c55\u793a\u4e86\u8bf7\u6c42\u7684header hello\u548c\u503cworld\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u4f7f\u7528Hystrix<\/h5>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5728spring cloud gateway\u4e2d\u53ef\u4ee5\u4f7f\u7528Hystrix\u3002Hystrix\u662f spring cloud\u4e2d\u4e00\u4e2a\u670d\u52a1\u7194\u65ad\u964d\u7ea7\u7684\u7ec4\u4ef6\uff0c\u5728\u5fae\u670d\u52a1\u7cfb\u7edf\u6709\u7740\u5341\u5206\u91cd\u8981\u7684\u4f5c\u7528\u3002 Hystrix\u662f spring cloud gateway\u4e2d\u662f\u4ee5filter\u7684\u5f62\u5f0f\u4f7f\u7528\u7684\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@Bean\n    public RouteLocator myRoutes(RouteLocatorBuilder builder) {\n        String httpUri = \"http:\/\/httpbin.org:80\";\n        return builder.routes()\n            .route(p -> p\n                .path(\"\/get\")\n                .filters(f -> f.addRequestHeader(\"Hello\", \"World\"))\n                .uri(httpUri))\n            .route(p -> p\n                .host(\"*.hystrix.com\")\n                .filters(f -> f\n                    .hystrix(config -> config\n                        .setName(\"mycmd\")\n                        .setFallbackUri(\"forward:\/fallback\")))\n                .uri(httpUri))\n            .build();\n    }\n<\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u53e6\u5916\u4e00\u4e2arouter\uff0c\u8be5router\u4f7f\u7528host\u53bb\u65ad\u8a00\u8bf7\u6c42\u662f\u5426\u8fdb\u5165\u8be5\u8def\u7531\uff0c\u5f53\u8bf7\u6c42\u7684host\u6709\u201c*.hystrix.com\u201d\uff0c\u90fd\u4f1a\u8fdb\u5165\u8be5router\uff0c\u8be5router\u4e2d\u6709\u4e00\u4e2ahystrix\u7684filter,\u8be5filter\u53ef\u4ee5\u914d\u7f6e\u540d\u79f0\u3001\u548c\u6307\u5411\u6027fallback\u7684\u903b\u8f91\u7684\u5730\u5740\uff0c\u6bd4\u5982\u672c\u6848\u4f8b\u4e2d\u91cd\u5b9a\u5411\u5230\u4e86\u201c\/fallback\u201d\u3002<\/p>\n\n\n\n<p>\u73b0\u5728\u5199\u7684\u4e00\u4e2a\u201c\/fallback\u201d\u7684l\u903b\u8f91\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@RequestMapping(\"\/fallback\")\n    public Mono&lt;String> fallback() {\n        return Mono.just(\"fallback\");\n    }\n<\/pre>\n\n\n\n<p>Mono\u662f\u4e00\u4e2aReactive stream\uff0c\u5bf9\u5916\u8f93\u51fa\u4e00\u4e2a\u201cfallback\u201d\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p>\u4f7f\u7528curl\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"> curl --dump-header - --header 'Host: www.hystrix.com' http:\/\/localhost:8080\/delay\/3\n<\/pre>\n\n\n\n<p>\u8fd4\u56de\u7684\u54cd\u5e94\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">fallback\n<\/pre>\n\n\n\n<p>\u53ef\u89c1\uff0c\u5e26hostwww.hystrix.com\u7684\u8bf7\u6c42\u6267\u884c\u4e86hystrix\u7684fallback\u7684\u903b\u8f91\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecb Spring Cloud Gateway\u662fSpring Cloud\u5b98\u65b9\u63a8\u51fa\u7684\u7b2c\u4e8c\u4ee3\u7f51\u5173\u6846\u67b6\uff0c\u53d6\u4ee3Zuul [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4670"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4670"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4670\/revisions"}],"predecessor-version":[{"id":4672,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/4670\/revisions\/4672"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}