{"id":1741,"date":"2023-03-26T21:15:49","date_gmt":"2023-03-26T13:15:49","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1741"},"modified":"2023-04-23T21:29:59","modified_gmt":"2023-04-23T13:29:59","slug":"spring-cloud-gateway-replace-zuul-as-api-gateway","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/26\/spring-cloud-gateway-replace-zuul-as-api-gateway\/","title":{"rendered":"Spring Cloud Gateway\u66ff\u4ee3Zuul\u4f5c\u4e3aAPI\u7f51\u5173"},"content":{"rendered":"<p>\u672c\u6587\u975e\u5e38\u7b80\u8981\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>Spring Cloud Gateway<\/code>\u4f5c\u4e3aAPI\u7f51\u5173\uff08\u4e0d\u662f\u4f7f\u7528<code>Zuul<\/code>\u4f5c\u4e3a\u7f51\u5173\uff09\uff0c\u5173\u4e8e<code>Spring Cloud Gateway<\/code>\u548c<code>Zuul<\/code>\u7684\u6027\u80fd\u6bd4\u8f83\u672c\u6587\u4e0d\u518d\u8d58\u8ff0\uff0c\u57fa\u672c\u53ef\u4ee5\u80af\u5b9a<code>Spring Cloud Finchley<\/code>\u7248\u672c\u7684Gateway\u6bd4<code>Zuul 1.x<\/code>\u7cfb\u5217\u7684\u6027\u80fd\u548c\u529f\u80fd\u6574\u4f53\u8981\u597d\u3002<\/p>\n<blockquote>\n<p>\u7279\u522b\u63d0\u9192\uff1a<code>Spring Cloud Finchley<\/code>\u7248\u672c\u4e2d\uff0c\u5373\u4f7f\u5f15\u5165\u4e86<code>spring-cloud-starter-netflix-zuul<\/code>\uff0c\u4e5f\u4e0d\u662f2.0\u7248\u672c\u7684<code>Zuul<\/code><\/p>\n<\/blockquote>\n<p><!-- more --><\/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-netflix-zuul&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h2>pom\u4f9d\u8d56<\/h2>\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;\n\n&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.cloud&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-cloud-starter-netflix-eureka-client&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n\n&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-actuator&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n\n&lt;!-- https:\/\/mvnrepository.com\/artifact\/org.springframework.cloud\/spring-cloud-starter-netflix-hystrix --&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.cloud&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-cloud-starter-netflix-hystrix&lt;\/artifactId&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<h2>\u9879\u76ee\u7ed3\u6784<\/h2>\n<ul>\n<li>\u7b2c\u4e00\u4e2a\u9879\u76ee\u662f<code>Eureka<\/code>\u6ce8\u518c\u4e2d\u5fc3\uff0c\u975e\u5e38\u7b80\u5355\uff0c\u57fa\u672c\u5c31\u4e00\u4e2a<code>Application<\/code>\u7c7b<\/li>\n<li>\u7b2c\u4e8c\u4e2a\u9879\u76ee\u662f<code>User service<\/code>\uff0c\u4e5f\u975e\u5e38\u7b80\u5355\u63d0\u4f9b\u4e24\u4e2a<code>rest api<\/code>\uff0c\u4e3a\u4e86\u7b80\u7565\u4e0d\u8fde\u63a5\u6570\u636e\uff0c\u76f4\u63a5\u5728\u5185\u5b58\u4e2d\u751f\u6210\u4e00\u7ec4\u6570\u636e\uff0c\u7aef\u53e3<code>6001<\/code><\/li>\n<li>\u7b2c\u4e09\u4e2a\u9879\u76ee\u5c31\u662f\u7f51\u5173\uff0c\u7aef\u53e3<code>8001<\/code><\/li>\n<\/ul>\n<p>\u76ee\u524d\u9879\u76ee\u4e2d\u96c6\u6210websocket\u670d\u52a1\u914d\u7f6e\uff0c\u672c\u6587\u6682\u4e0d\u4ecb\u7ecd\u53ef\u76f4\u63a5\u5ffd\u7565\u3002<\/p>\n<pre><code class=\"language-java\">@SpringCloudApplication\npublic class APIGatewayApplication  {\n    private static final Logger logger = LoggerFactory.getLogger(APIGatewayApplication.class);\n\n    @Bean\n    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {\n        return builder.routes()\n                .route(r -&gt; r.path(&quot;\/baidu&quot;)\n                    .uri(&quot;http:\/\/baidu.com:80\/&quot;)\n                )\n                .route(&quot;websocket_route&quot;, r -&gt; r.path(&quot;\/apitopic1\/**&quot;)\n                    .uri(&quot;ws:\/\/127.0.0.1:6605&quot;)\n                )\n                .route(r -&gt; r.path(&quot;\/userapi3\/**&quot;)\n                    .filters(f -&gt; f.addResponseHeader(&quot;X-AnotherHeader&quot;, &quot;testapi3&quot;))\n                    .uri(&quot;lb:\/\/user-service\/&quot;)\n                )\n                .build();\n    }\n\n    public static void main(String[] args) {\n        SpringApplication.run(APIGatewayApplication.class, args);\n        logger.info(&quot; Start APIGatewayApplication Done&quot;);\n    }\n}<\/code><\/pre>\n<p>\u7f51\u5173\u7684\u914d\u7f6e\u6587\u4ef6<code>application.yml<\/code><\/p>\n<pre><code class=\"language-yml\">server:\n  port: 8001\n\n#\u670d\u52a1\u540d\nspring:\n  application:\n    name: gateway-service\n  cloud:\n    gateway:\n      filter:\n        remove-non-proxy-headers:\n          headers:\n          - dummy\n      routes:\n        - id: apiuser\n          # \u91cd\u70b9\uff01\/info\u5fc5\u987b\u4f7f\u7528http\u8fdb\u884c\u8f6c\u53d1\uff0clb\u4ee3\u8868\u4ece\u6ce8\u518c\u4e2d\u5fc3\u83b7\u53d6\u670d\u52a1\n          uri: lb:\/\/user-service\n          predicates:\n          # \u91cd\u70b9\uff01\u8f6c\u53d1\u8be5\u8def\u5f84\uff01\/userapi\/**\n          - Path=\/userapi\/**\n          # http:\/\/localhost:8001\/userapi\/user\/users\/2 \u5fc5\u987b\u52a0\u4e0aStripPrefix=1\uff0c\u5426\u5219\u8bbf\u95ee\u670d\u52a1\u65f6\u4f1a\u5e26\u4e0auserapi\n          # \u800c\u4e0d\u662f\u6211\u4eec\u671f\u671b\u7684\u53bb\u6389userapi\uff0c\u53ea\u4fdd\u7559**\u90e8\u5206\n          filters:\n          - StripPrefix=1\n        - id: api2user\n          uri: lb:\/\/user-service\n          predicates:\n          - Path=\/userapi2\/**\n          filters:\n          - StripPrefix=1\n\neureka:\n  client:\n    serviceUrl:\n      defaultZone: http:\/\/localhost:7700\/eureka\/<\/code><\/pre>\n<p>\u914d\u7f6e\u4e86\u4e00\u4e2a\u8def\u7531<code>apiuser<\/code>\uff0c\u5f53\u8def\u5f84\u4e3a<code>- Path=\/userapi\/**<\/code>\uff0c\u5c31\u8f6c\u53d1\u5230\u670d\u52a1<code>lb:\/\/user-servic<\/code>\uff0c\u540c\u65f6\u628a\u8def\u5f84\u4e2d\u7684<code>userapi<\/code>\u8fd9\u90e8\u5206\u53bb\u6389<code>- StripPrefix=1<\/code><\/p>\n<h2>\u8fd0\u884c\u6d4b\u8bd5<\/h2>\n<p>\u76f4\u63a5\u8bbf\u95eeUser service: <a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/localhost:6001\/user\/users\/2\">http:\/\/localhost:6001\/user\/users\/2<\/a><\/p>\n<p>\u901a\u8fc7\u7f51\u5173\u8bbf\u95eeUser service: <a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/localhost:8001\/userapi\/user\/users\/2\">http:\/\/localhost:8001\/userapi\/user\/users\/2<\/a><\/p>\n<h2>\u53c2\u8003\u6587\u6863<\/h2>\n<ul>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/cloud.spring.io\/spring-cloud-static\/Finchley\/single\/spring-cloud.html#_spring_cloud_gateway\">http:\/\/cloud.spring.io\/spring-cloud-static\/Finchley\/single\/spring-cloud.html#_spring_cloud_gateway<\/a><\/li>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/spring-cloud-samples\/spring-cloud-gateway-sample\">https:\/\/github.com\/spring-cloud-samples\/spring-cloud-gateway-sample<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u975e\u5e38\u7b80\u8981\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Spring Cloud Gateway\u4f5c\u4e3aAPI\u7f51\u5173\uff08\u4e0d\u662f\u4f7f\u7528Zuul\u4f5c\u4e3a\u7f51\u5173\uff09\uff0c\u5173 [&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,250],"class_list":["post-1741","post","type-post","status-publish","format-standard","hentry","category-spring-cloud","tag-spring-cloud-gateway","tag-zuul"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1741","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=1741"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1741\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}