Spring Cloud Gateway入门案例

摘要:本篇文章主要介绍了什么是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
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<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">
<parent>
<artifactId>ch18-1</artifactId>
<groupId>cn.springcloud.book</groupId>
<version>1.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>ch18-1-gateway</artifactId>
<packaging>jar</packaging>

<name>ch18-1-gateway</name>
<url>http://springcloud.cn</url>

<properties>
<spring-cloud.version>Finchley.M8</spring-cloud.version>
</properties>

<dependencyManagement>
<dependencies>
<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>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>


</project>

2.2 Spring Cloud Gateway主程序

SpringCloudGatewayApplication.java,代码如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package cn.springcloud.book.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringCloudGatewayApplication {


@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
//basic proxy
.route(r -> r.path("/baidu")
.uri("http://baidu.com:80/")
).build();
}

public static void main(String[] args) {
SpringApplication.run(SpringCloudGatewayApplication.class, args);
}
}

2.3 编写application.yml文件

1
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: spring-cloud-gateway

spring:
cloud:
gateway:
routes:
- id: xujin_route
uri: http://www.xujin.org:80/
predicates:
- Path=/xujin

logging:
level:
org.springframework.cloud.gateway: TRACE
org.springframework.http.server.reactive: DEBUG
org.springframework.web.reactive: DEBUG
reactor.ipc.netty: DEBUG

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

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

  • 第一:通过@Bean自定义RouteLocator
    1
    2
    3
    4
    5
    6
    7
    8
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
    return builder.routes()
    //basic proxy
    .route(r -> r.path("/baidu")
    .uri("http://baidu.com:80/")
    ).build();
    }

  • 第二:通过属于文件或者yml文件配置
1
2
3
4
5
6
7
8
spring:
cloud:
gateway:
routes:
- id: xujin_route
uri: http://www.xujin.org:80/
predicates:
- Path=/xujin

PS,以上两种方式等同。

2.5 错误的示范代码如下:

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

温馨提示,上面这种写法是基于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
2
3
4
5
6
7
8
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
//basic proxy
.route(r -> r.path("/baidu")
.uri("http://baidu.com:80/")
).build();
}
1
2
3
4
5
6
7
8
spring:
cloud:
gateway:
routes:
- id: xujin_route
uri: http://www.xujin.org:80/
predicates:
- Path=/xujin
感谢您的阅读,本文由 许进沉思录-专注于互联网与中间件基础架构技术研究 版权所有。如若转载,请注明出处:许进沉思录-专注于互联网与中间件基础架构技术研究(http://xujin.org/sc/gw/gw-01/
Mysql批量执行更新shell脚本
Spring Cloud Gateway揭秘之处理请求流程