欢迎访问Spring Cloud中国社区

我们致力于成为中国最专业的Spring Boot和Spring Cloud交流社区。推荐使用Github直接登录,欢迎加微信号Software_King进入社区微信交流群。若发现网站bug欢迎反馈!

Spring Cloud Gateway入门案例

xujin · 2月前 · 901 ·

摘要:本篇文章主要介绍了什么是Spring Cloud Gateway,并基于Spring Cloud Gateway的Finchley.M8版本编写一个Spring Cloud Gateway的入门案例,即基本代理的路由转发配置。

1.Spring Gateway概述

1.1 什么是Spring Cloud Gateway

Spring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技术开发的网关,Spring Cloud Gateway旨在为微服务架构提供一种简单而有效的统一的API路由管理方式。Spring Cloud Gateway作为Spring Cloud生态系中的网关,目标是替代Netflix ZUUL,其不仅提供统一的路由方式,并且基于Filter链的方式提供了网关基本的功能,例如:安全,监控/埋点,和限流等。

2. Spring Cloud Gateway入门案例

2.1 创建maven工程

配置Spring Cloud Gateway的相关Maven依赖

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <parent>
  4. <artifactId>ch18-1</artifactId>
  5. <groupId>cn.springcloud.book</groupId>
  6. <version>1.0-SNAPSHOT</version>
  7. </parent>
  8. <modelVersion>4.0.0</modelVersion>
  9. <artifactId>ch18-1-gateway</artifactId>
  10. <packaging>jar</packaging>
  11. <name>ch18-1-gateway</name>
  12. <url>http://springcloud.cn</url>
  13. <properties>
  14. <spring-cloud.version>Finchley.M8</spring-cloud.version>
  15. </properties>
  16. <dependencyManagement>
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.springframework.cloud</groupId>
  20. <artifactId>spring-cloud-dependencies</artifactId>
  21. <version>${spring-cloud.version}</version>
  22. <type>pom</type>
  23. <scope>import</scope>
  24. </dependency>
  25. </dependencies>
  26. </dependencyManagement>
  27. <dependencies>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-actuator</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.cloud</groupId>
  34. <artifactId>spring-cloud-starter-gateway</artifactId>
  35. </dependency>
  36. </dependencies>
  37. <build>
  38. <plugins>
  39. <plugin>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-maven-plugin</artifactId>
  42. </plugin>
  43. </plugins>
  44. </build>
  45. <repositories>
  46. <repository>
  47. <id>spring-snapshots</id>
  48. <name>Spring Snapshots</name>
  49. <url>https://repo.spring.io/snapshot</url>
  50. <snapshots>
  51. <enabled>true</enabled>
  52. </snapshots>
  53. </repository>
  54. <repository>
  55. <id>spring-milestones</id>
  56. <name>Spring Milestones</name>
  57. <url>https://repo.spring.io/milestone</url>
  58. <snapshots>
  59. <enabled>false</enabled>
  60. </snapshots>
  61. </repository>
  62. </repositories>
  63. <pluginRepositories>
  64. <pluginRepository>
  65. <id>spring-snapshots</id>
  66. <name>Spring Snapshots</name>
  67. <url>https://repo.spring.io/snapshot</url>
  68. <snapshots>
  69. <enabled>true</enabled>
  70. </snapshots>
  71. </pluginRepository>
  72. <pluginRepository>
  73. <id>spring-milestones</id>
  74. <name>Spring Milestones</name>
  75. <url>https://repo.spring.io/milestone</url>
  76. <snapshots>
  77. <enabled>false</enabled>
  78. </snapshots>
  79. </pluginRepository>
  80. </pluginRepositories>
  81. </project>

2.2 Spring Cloud Gateway主程序

SpringCloudGatewayApplication.java,代码如下所示:

  1. package cn.springcloud.book.gateway;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.gateway.route.RouteLocator;
  5. import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
  6. import org.springframework.context.annotation.Bean;
  7. @SpringBootApplication
  8. public class SpringCloudGatewayApplication {
  9. @Bean
  10. public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
  11. return builder.routes()
  12. //basic proxy
  13. .route(r -> r.path("/baidu")
  14. .uri("http://baidu.com:80/")
  15. ).build();
  16. }
  17. public static void main(String[] args) {
  18. SpringApplication.run(SpringCloudGatewayApplication.class, args);
  19. }
  20. }

2.3 编写application.yml文件

  1. server:
  2. port: 8080
  3. spring:
  4. application:
  5. name: spring-cloud-gateway
  6. spring:
  7. cloud:
  8. gateway:
  9. routes:
  10. - id: xujin_route
  11. uri: http://www.xujin.org:80/
  12. predicates:
  13. - Path=/xujin
  14. logging:
  15. level:
  16. org.springframework.cloud.gateway: TRACE
  17. org.springframework.http.server.reactive: DEBUG
  18. org.springframework.web.reactive: DEBUG
  19. reactor.ipc.netty: DEBUG

2.4 基本代理路由配置等同写法

Spring Cloud Gateway提供了两种配置路由规则的方法

  • 第一:通过@Bean自定义RouteLocator
  1. @Bean
  2. public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
  3. return builder.routes()
  4. //basic proxy
  5. .route(r -> r.path("/baidu")
  6. .uri("http://baidu.com:80/")
  7. ).build();
  8. }
  • 第二:通过属于文件或者yml文件配置
  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: xujin_route
  6. uri: http://www.xujin.org:80/
  7. predicates:
  8. - Path=/xujin

PS,以上两种方式等同。

2.5 错误的示范代码如下:

  1. @Bean
  2. public RouteLocator routingConfig() {
  3. return Routes.locator()
  4. .route("xujin_route")
  5. .uri("http://xujin.org")
  6. .predicate(host("**.xujin.org"))
  7. .build();
  8. }

温馨提示,上面这种写法是基于Spring Cloud Gateway FM4的版本,相关代码已废弃,目前Spring Cloud Gateway将会在FM9之后Realese。

2.6 运行测试

  1. 访问http://localhost:8080/baidu,路由转发到http://www.baidu.com
  2. 访问http://localhost:8080/xujin,路由转发到http://xujin.orgyml
  1. @Bean
  2. public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
  3. return builder.routes()
  4. //basic proxy
  5. .route(r -> r.path("/baidu")
  6. .uri("http://baidu.com:80/")
  7. ).build();
  8. }
  1. spring:
  2. cloud:
  3. gateway:
  4. routes:
  5. - id: xujin_route
  6. uri: http://www.xujin.org:80/
  7. predicates:
  8. - Path=/xujin