{"id":1946,"date":"2023-04-01T10:09:25","date_gmt":"2023-04-01T02:09:25","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1946"},"modified":"2023-04-22T08:41:42","modified_gmt":"2023-04-22T00:41:42","slug":"spring-cloud-gateway-access","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/04\/01\/spring-cloud-gateway-access\/","title":{"rendered":"Spring Cloud Gateway\u63a5\u5165\u8bb0\u5f55"},"content":{"rendered":"<h2>\u4f9d\u8d56\u6ce8\u610f<\/h2>\n<p>Spring Cloud Gateway\u662f\u4f7f\u7528<code>netty+webflux<\/code>\u5b9e\u73b0\u56e0\u6b64\u4e0d\u9700\u8981\u518d\u5f15\u5165<code>spring-boot-starter-web<\/code>\u5305\uff0c\u8fd9\u91cc\u6211\u4eec\u5f15\u5165Eureka\uff0c\u662f\u4e3a\u4e86\u76f4\u63a5\u901a\u8fc7Eureka \u83b7\u53d6\u6ce8\u518c\u670d\u52a1\u5e76\u53d1\u9001\u8bf7\u6c42\u3002<\/p>\n<blockquote>\n<p>Spring Cloud Gateway\u5207\u8bb0\u4e0d\u53ef\u6dfb\u52a0<code>spring-boot-starter<\/code>\u548c<code>spring-boot-starter-web<\/code>\u4f9d\u8d56\uff0c\u5426\u5219\u62a5404\u9519\u8bef<\/p>\n<\/blockquote>\n<p><!-- more --><\/p>\n<pre><code class=\"language-xml\">&lt;!--    &lt;dependency&gt;--&gt;\n&lt;!--        &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;--&gt;\n&lt;!--        &lt;artifactId&gt;spring-boot-starter&lt;\/artifactId&gt;--&gt;\n&lt;!--    &lt;\/dependency&gt;--&gt;<\/code><\/pre>\n<h2>\u57fa\u672c\u4f9d\u8d56<\/h2>\n<pre><code class=\"language-xml\">&lt;dependencies&gt;\n&lt;!--    &lt;dependency&gt;--&gt;\n&lt;!--        &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;--&gt;\n&lt;!--        &lt;artifactId&gt;spring-boot-starter&lt;\/artifactId&gt;--&gt;\n&lt;!--    &lt;\/dependency&gt;--&gt;\n    &lt;!-- https:\/\/mvnrepository.com\/artifact\/org.springframework.cloud\/spring-cloud-starter-gateway --&gt;\n    &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;version&gt;2.1.1.RELEASE&lt;\/version&gt;\n    &lt;\/dependency&gt;\n&lt;\/dependencies&gt;\n\n&lt;dependencyManagement&gt;\n    &lt;dependencies&gt;\n        &lt;!--https:\/\/mvnrepository.com\/artifact\/org.springframework.cloud\/spring-cloud-dependencies--&gt;\n        &lt;dependency&gt;\n            &lt;groupId&gt;org.springframework.cloud&lt;\/groupId&gt;\n            &lt;artifactId&gt;spring-cloud-dependencies&lt;\/artifactId&gt;\n            &lt;version&gt;Greenwich.SR1&lt;\/version&gt;\n            &lt;type&gt;pom&lt;\/type&gt;\n            &lt;scope&gt;import&lt;\/scope&gt;\n        &lt;\/dependency&gt;\n    &lt;\/dependencies&gt;\n&lt;\/dependencyManagement&gt;<\/code><\/pre>\n<h2>\u5f00\u542f\u8c03\u8bd5\u65e5\u5fd7<\/h2>\n<pre><code class=\"language-yml\">logging:\n  level:\n    org.springframework.cloud.gateway: debug<\/code><\/pre>\n<h2>\u57fa\u672c\u914d\u7f6e\uff08Java\uff09<\/h2>\n<pre><code class=\"language-java\">@Configuration\npublic class RouteLocatorConfig {\n\n    @Bean\n    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {\n        return builder.routes()\n\/\/                .route(&quot;path_route&quot;, r -&gt; r.path(&quot;\/about&quot;)  \/\/http:\/\/127.0.0.1:9000\/about -&gt; http:\/\/www.appblog.cn\/about\n\/\/                        .uri(&quot;http:\/\/www.appblog.cn&quot;))\n                .route(&quot;path_route&quot;, r -&gt; r.path(&quot;\/about&quot;)  \/\/http:\/\/127.0.0.1:9000\/about -&gt; http:\/\/www.appblog.cn\n                        .filters(f -&gt; f.stripPrefix(1))\n                        .uri(&quot;http:\/\/www.appblog.cn&quot;))\n                .route(r -&gt; r.path(&quot;\/test\/**&quot;)  \/\/http:\/\/127.0.0.1:9000\/test\/hello -&gt; http:\/\/127.0.0.1:9001\/hello\n                        .filters(f -&gt; f.stripPrefix(1))\n                        .uri(&quot;http:\/\/127.0.0.1:9001&quot;)\n                )\n                .route(&quot;service_route&quot;, r -&gt; r.path(&quot;\/consumer\/**&quot;)\n                        .filters(f -&gt; f.stripPrefix(1)\n                                .addResponseHeader(&quot;X-AnotherHeader&quot;, &quot;test&quot;))\n                        .uri(&quot;lb:\/\/spring-cloud-consumer&quot;)\n                )\n                .route(r -&gt; r.path(&quot;\/test\/rateLimit&quot;)\n                        .filters(f -&gt; f.rewritePath(&quot;test&quot;, &quot;hello&quot;)\n                                .filter(new GatewayRateLimitByIpFilter(10, 1, Duration.ofSeconds(1))))\n                        .uri(&quot;http:\/\/127.0.0.1:9001&quot;)\n                )\n                .build();\n    }\n}<\/code><\/pre>\n<h2>\u57fa\u672c\u914d\u7f6e\uff08yml\uff09<\/h2>\n<pre><code class=\"language-yml\">spring:\n  cloud:\n    gateway:\n      discovery:\n        locator:\n          enabled: true\n          lower-case-service-id: true\n      routes:\n      # http:\/\/127.0.0.1:9000\/about -&gt; http:\/\/www.appblog.cn\n      - id: appblog\n        uri: http:\/\/www.appblog.cn\n        predicates:\n          - Path=\/about\n        filters:\n          - StripPrefix=1\n      # http:\/\/127.0.0.1:9000\/consumer\/hello -&gt; http:\/\/spring-cloud-consumer\/hello\n      - id: consumer-service\n        uri: lb:\/\/spring-cloud-consumer\n        predicates:\n          - Path=\/consumer\/**\n        filters:\n          - StripPrefix=1\n      - id: producer-service\n        uri: lb:\/\/spring-cloud-producer\n        predicates:\n          - Path=\/producer\/**\n        filters:\n          - StripPrefix=1<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f9d\u8d56\u6ce8\u610f Spring Cloud Gateway\u662f\u4f7f\u7528netty+webflux\u5b9e\u73b0\u56e0\u6b64\u4e0d\u9700\u8981\u518d\u5f15\u5165spri [&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-1946","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\/1946","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=1946"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1946\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}