内容来源:
如何使用 SpringCloud Gateway-简单实战
引入依赖包
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
写配置文件
server:
port: 8088 # 指定端口启动
spring:
application:
name: test-gateway
cloud:
gateway:
enabled: true # 默认配置是 true ,不需要网关则配置 false
routes:
- id: test-gateway # 指定系统id
uri: http://localhost:8080 # 目标URI
predicates:
- Path=/redis/** # path路由规则
filters:
- StripPrefix=1 #前缀匹配;比如 http://localhost:8088/redis/gateway 经过网关,进入 8080 服务的时候,会把 redis 替换掉
然后启动此项目
启动模拟服务
随便再另起一个项目,然后启动,写一个接口,也可以随便打印信息。主要目的是模拟通过 Spring Cloud GateWay 能够访问其他内容。比如这个服务的一个接口地址是:/gateway
那么我在浏览器访问:localhost:8088/redis/gateway 即可通过网关访问到此服务
基本词汇
- routes:网关的基本构建块,由 ID/目标URI/predicate/过滤器 组合定义。如果 predicate 为真,则匹配路由
- predicate:这是 Java8 的函数谓词,可以匹配来自 HTTP 请求的任何内容
- filter:设置一些过滤条件
两种配置方式
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- Cookie=mycookie,mycookievalue
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- name: Cookie
args:
name: mycookie
regexp: mycookievalue
路由规则
After
一个参数:一个日期时间(它是一个 Java ZoneDateTime)。匹配在指定日期时间之后发生的请求
spring:
application:
name: test-gateway
cloud:
gateway:
enabled: true # 默认配置是 true ,不需要网关则配置 false
routes:
- id: test-gateway # 指定系统id
uri: http://localhost:8080 # 系统访问地址
predicates:
- After=2022-09-25T16:15:00.000+08:00[Asia/Shanghai]
如果只是配置这个,那么请求路径则必须和服务路径完全一致,没有前缀替换
Before
spring:
application:
name: test-gateway
cloud:
gateway:
enabled: true # 默认配置是 true ,不需要网关则配置 false
routes:
- id: test-gateway # 指定系统id
uri: http://localhost:8080 # 系统访问地址
predicates:
- Before=2022-09-25T16:40:00.000+08:00[Asia/Shanghai]
在这个时间之前才能访问
Between
spring:
application:
name: test-gateway
cloud:
gateway:
enabled: true # 默认配置是 true ,不需要网关则配置 false
routes:
- id: test-gateway # 指定系统id
uri: http://localhost:8080 # 系统访问地址
predicates:
- Before=2022-09-25T16:40:00.000+08:00[Asia/Shanghai], 2022-09-25T16:50:00.000+08:00[Asia/Shanghai]
在这个时间之内才能访问
Cooike
spring:
application:
name: test-gateway
cloud:
gateway:
enabled: true # 默认配置是 true ,不需要网关则配置 false
routes:
- id: test-gateway # 指定系统id
uri: http://localhost:8080 # 系统访问地址
predicates:
- Cooike=loginname,ncharming
测试在 postman 里面测试,在 header 里面加上即可访问;或者 curl 测试也行
Header
spring:
application:
name: test-gateway
cloud:
gateway:
enabled: true # 默认配置是 true ,不需要网关则配置 false
routes:
- id: test-gateway # 指定系统id
uri: http://localhost:8080 # 系统访问地址
predicates:
- Header=x-header,ncharming
请求的时候 Header 里面必须有 x-header=ncharming;
或者 - Header=x-header,\d+ 表示一位或多位数字也行
Host
spring:
application:
name: test-gateway
cloud:
gateway:
enabled: true # 默认配置是 true ,不需要网关则配置 false
routes:
- id: test-gateway # 指定系统id
uri: http://localhost:8080 # 系统访问地址
predicates:
- Host=**.somehost.org,**.anotherhost.org
匹配主机名的列表,类似于白名单
Method
spring:
application:
name: test-gateway
cloud:
gateway:
enabled: true # 默认配置是 true ,不需要网关则配置 false
routes:
- id: test-gateway # 指定系统id
uri: http://localhost:8080 # 系统访问地址
predicates:
- Method=GET,POST
指定请求方式,只能是GET,POST
Path
spring:
application:
name: test-gateway
cloud:
gateway:
enabled: true # 默认配置是 true ,不需要网关则配置 false
routes:
- id: test-gateway # 指定系统id
uri: http://localhost:8080 # 系统访问地址
predicates:
- Path=/redis/**
filters:
- StripPrefix=1
匹配前缀是 redis ,并且会替换这个前缀(就是开头实战的配置)
Query
spring:
application:
name: test-gateway
cloud:
gateway:
enabled: true # 默认配置是 true ,不需要网关则配置 false
routes:
- id: test-gateway # 指定系统id
uri: http://localhost:8080 # 系统访问地址
predicates:
- Query=green
请求参数包含green「这里配置只适用于 get 请求」
RemoteAddr
spring:
application:
name: test-gateway
cloud:
gateway:
enabled: true # 默认配置是 true ,不需要网关则配置 false
routes:
- id: test-gateway # 指定系统id
uri: http://localhost:8080 # 系统访问地址
predicates:
- RemoteAddr=192.168.1.1/24
请求的远程地址匹配
Weight
spring:
application:
name: test-gateway
cloud:
gateway:
routes:
- id: test-system-a
uri: http://localhost:8080/
predicates:
- Weight=group1, 8
- id: test-system-b
uri: http://localhost:8080/
predicates:
- Weight=group1, 2
类似 nginx 转发权重;一般一个服务都是提供多个实例
Spring Cloud GateWay 上篇结束
不要怕,不要急,不要悔,不要死
\