摘要:无。
环境
Windows 10 企业版 LTSC 21H2
Java 1.8
Maven 3.6.3
Spring 5.3.31
Spring Boot 2.7.18
Spring Cloud 2021.0.5
Spring Cloud Gateway 3.1.4
1 入门案例
1.1 项目结构
创建父项目和三个模块:
code1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| demo-gateway/ ├── demo-gateway-business-a/ │ ├── src/ │ │ ├── main/ │ │ │ ├── java/ │ │ │ │ └── com/ │ │ │ │ └── example/ │ │ │ │ ├── controller/ │ │ │ │ │ └── BusinessAController.java │ │ │ │ └── BusinessAApplication.java │ │ │ └── resources/ │ │ │ └── application.yml │ │ └── test/ │ └── pom.xml ├── demo-gateway-business-b/ │ ├── src/ │ │ ├── main/ │ │ │ ├── java/ │ │ │ │ └── com/ │ │ │ │ └── example/ │ │ │ │ ├── controller/ │ │ │ │ │ └── BusinessBController.java │ │ │ │ └── BusinessBApplication.java │ │ │ └── resources/ │ │ │ └── application.yml │ │ └── test/ │ └── pom.xml ├── demo-gateway-server/ │ ├── src/ │ │ ├── main/ │ │ │ ├── java/ │ │ │ │ └── com/ │ │ │ │ └── example/ │ │ │ │ └── GatewayApplication.java │ │ │ └── resources/ │ │ │ └── application.yml │ │ └── test/ │ └── pom.xml └── pom.xml
|
1.2 父项目
创建pom.xml文件:
pom.xml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId> <artifactId>demo-gateway</artifactId> <version>1.0.0-SNAPSHOT</version> <packaging>pom</packaging>
<modules> <module>demo-gateway-business-a</module> <module>demo-gateway-business-b</module> <module>demo-gateway-server</module> </modules>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <spring-boot.version>2.7.18</spring-boot.version> <spring-cloud.version>2021.0.5</spring-cloud.version> </properties>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
|
1.3 业务A
1.3.1 配置依赖
创建pom.xml文件:
pom.xml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<parent> <groupId>com.example</groupId> <artifactId>demo-gateway</artifactId> <version>1.0.0-SNAPSHOT</version> </parent>
<artifactId>demo-gateway-business-a</artifactId>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
|
1.3.2 配置文件
创建application.yml文件:
application.yml1 2 3 4 5 6
| server: port: 8011
spring: application: name: demo-gateway-business-a
|
1.3.3 提供服务
创建控制器:
java1 2 3 4 5 6 7
| @RestController public class BusinessAController { @GetMapping("/a/hello") public String hello() { return "Hello from business A"; } }
|
1.3.4 启动类
创建启动类:
java1 2 3 4 5 6
| @SpringBootApplication public class BusinessAApplication { public static void main(String[] args) { SpringApplication.run(BusinessAApplication.class, args); } }
|
1.4 业务B
1.4.1 配置依赖
创建pom.xml文件:
pom.xml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<parent> <groupId>com.example</groupId> <artifactId>demo-gateway</artifactId> <version>1.0.0-SNAPSHOT</version> </parent>
<artifactId>demo-gateway-business-b</artifactId>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
|
1.4.2 配置文件
创建application.yml文件:
application.yml1 2 3 4 5 6
| server: port: 8021
spring: application: name: demo-gateway-business-b
|
1.4.3 提供服务
创建控制器:
java1 2 3 4 5 6 7
| @RestController public class BusinessBController { @GetMapping("/b/hello") public String hello() { return "Hello from business B"; } }
|
1.4.4 启动类
创建启动类:
java1 2 3 4 5 6
| @SpringBootApplication public class BusinessBApplication { public static void main(String[] args) { SpringApplication.run(BusinessBApplication.class, args); } }
|
1.5 网关服务
1.5.1 配置依赖
创建pom.xml文件:
pom.xml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<parent> <groupId>com.example</groupId> <artifactId>demo-gateway</artifactId> <version>1.0.0-SNAPSHOT</version> </parent>
<artifactId>demo-gateway-server</artifactId>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> </dependencies> </project>
|
1.5.2 配置文件
创建application.yml文件:
application.yml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| server: port: 8080
spring: application: name: demo-gateway-server cloud: gateway: routes: - id: business-a-route uri: http://127.0.0.1:8011 predicates: - Path=/a/** - id: business-b-route uri: http://127.0.0.1:8021 predicates: - Path=/b/**
|
1.5.3 启动类
创建启动类:
java1 2 3 4 5 6
| @SpringBootApplication public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } }
|
2 路由配置
2.1 配置方式
2.1.1 配置文件
配置文件是最常用的方式,适合路由规则相对固定的场景:
application.yml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| spring: cloud: gateway: routes: - id: business-a-route uri: http://127.0.0.1:8011 predicates: - Path=/api/a/** filters: - StripPrefix=1 - id: business-b-route uri: http://127.0.0.1:8021 predicates: - Path=/api/b/** filters: - StripPrefix=1
|
2.1.2 编程方式
对于需要用代码动态构建路由的场景,可以注册RouteLocator类型的Bean对象以编程方式声明路由:
java1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @Configuration public class CustomConfig { @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("business-a-route", r -> r .path("/api/a/**") .filters(f -> f.stripPrefix(1)) .uri("http://127.0.0.1:8011")) .route("business-b-route", r -> r .path("/api/b/**") .filters(f -> f.stripPrefix(1)) .uri("http://127.0.0.1:8021")) .build(); } }
|
2.2 负载均衡
在生产环境中,下游服务通常注册在注册中心里,并且实例地址会动态变化。
此时可以把路由的目标URI写成lb://服务名的形式,使用lb前缀表示启用负载均衡,网关会结合负载均衡组件从注册中心获取服务实例并转发。
需要注意的是,使用这种方式需要引入服务发现组件和负载均衡组件。
条