一、什么是Spring Cloud Gateway
Spring Cloud Gateway是基于Spring生态系统API网关实现,Spring Cloud Gateway旨在提供一种简单而有效的方法,路由到下游API,并为它们提供微服务横向切割后需要关注的方面,如:安全、监控/指标和弹性等。详细内容请参考官方文档
二、相关术语解释
- 路由(Route):网关的基本构件。它由一个ID、一个目标URI、一组断言和一组过滤器组成。如果聚合断言为真,则匹配路由。
- 断言(Predicate):Java8断言函数。输入类型是Spring Framework serverwebeexchange。这允许您匹配HTTP请求中的任何内容,比如请求头或请求参数。
- 过滤器(Filter):网关涉及到的过滤器种类繁多,他们都是通过特定工厂构造的GatewayFilter实例。您可以通过特定的过滤器,在发送下游请求之前或之后修改请求和响应相关信息。
三、工作原理
客户端向网关发送请求,如果网关处理器映射器确定了一个请求与路由匹配,它将被发送到网关web处理器,web处理器将通过特定于该请求的过滤器链,将请求分发到实际的下游服务执行业务逻辑。如上图中,过滤器之所以被虚线分隔,是因为过滤器可以在发送代理请求之前(虚线左)和之后(虚线右)处理相关业务逻辑,然后返回。所有的前置过滤逻辑执行完毕后,发出代理请求,再去执行后置过滤逻辑。另外需要注意,在没有端口的路由中定义的uri,http和https协议下,uri的缺省端口值分别为80和443。
四、依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
五、配置断言工厂
- 快捷配置语法(通过断言工厂名称识别断言,等号后面是以逗号分隔的kv对,跟下面一回事)
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
# Cookie断言工厂=key,value
- Cookie=mycookie,mycookievalue
- 展开配置语法(predicates的name属性是断言工厂名称,args属性配置断言和过滤的kv对,跟上面一回事)
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
# Cookie断言工厂
- name: Cookie
# 参数
args:
# key
name: mycookie
# value
regexp: mycookievalue
1、路由后置断言工厂(The After Route Predicate Factory)
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
说明:这个断言匹配美国丹佛时间2017年1月20日17:42以后的请求
2、路由前置断言工厂(The Before Route Predicate Factory)
spring:
cloud:
gateway:
routes:
- id: before_route
uri: https://example.org
predicates:
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
说明:这个断言匹配美国丹佛时间2017年1月20日17:42以前的请求
3、路由范围断言工厂(The Between Route Predicate Factory)
spring:
cloud:
gateway:
routes:
- id: between_route
uri: https://example.org
predicates:
- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2017-01-21T17:42:47.789-07:00[America/Denver]
说明:这个断言匹配美国丹佛时间2017年1月20日17:42到2017年1月21日17:42之间的请求
4、路由Cookie断言工厂(The Cookie Route Predicate Factory)
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: https://example.org
predicates:
- Cookie=chocolate, ch.p
说明:这个断言匹配请求中存在名为chocolate的Cookie,且它的值匹配ch.p这个正则表达式的请求
5、路由Header断言工厂(The Header Route Predicate Factory)
spring:
cloud:
gateway:
routes:
- id: header_route
uri: https://example.org
predicates:
- Header=X-Request-Id, \d+
说明:这个断言匹配请求中包含一个名为X-Request-Id的请求头,它的值匹配\d+正则表达式(也就是说,它有一个或多个数字的值)的请求
6、路由Host断言工厂(The Host Route Predicate Factory)
spring:
cloud:
gateway:
routes:
- id: host_route
uri: https://example.org
predicates:
- Host=**.somehost.org,**.anotherhost.org
说明:支持URI模板变量(例如{sub}.myhost.org)如www.somehost.org、beta.somehost.org、www.anotherhost.org这些路由都可以匹配。本例中,这个断言提取了URI模板变量sub,作为名称和值的Map映射,并将该Map映射放在ServerWebExchange.getAttributes()中,并使用ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE中定义的值作为key
7、路由请求方式断言工厂(The Method Route Predicate Factory)
spring:
cloud:
gateway:
routes:
- id: method_route
uri: https://example.org
predicates:
- Method=GET,POST
说明:这个断言匹配请求方式为Get或Post的请求
8、路由路径断言工厂(The Path Route Predicate Factory)
spring:
cloud:
gateway:
routes:
- id: path_route
uri: https://example.org
predicates:
- Path=/red/{segment},/blue/{segment}
说明:如/red/1、/red/blue、/blue/green这些路由都可以匹配。这个端元提取URI模板变量(如前面例子中定义的segment)作为名称和值的映射,并将其放在ServerWebExchange.getAttributes()中,并使用ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE中定义的值作为key
9、路由查询参数断言工厂(The Query Route Predicate Factory)
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=green
说明:这个断言匹配请求路由中包含查询参数green的请求
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=red, gree.
说明:这个断言匹配请求路由中包含查询参数red,并且查询参数red的值与gree.这个正则表达式匹配的请求
10、路由远程地址断言工厂(The RemoteAddr Route Predicate Factory)
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: https://example.org
predicates:
- RemoteAddr=192.168.1.1/24
说明:例如192.168.0.1/16中,192.168.0.1是IP地址16是子网掩码,因此上例这个断言即可以匹配192.168.1.10来源的请求
11、路由权重断言工厂(The Weight Route Predicate Factory)
spring:
cloud:
gateway:
routes:
- id: weight_high
uri: https://weighthigh.org
predicates:
- Weight=group1, 8
- id: weight_low
uri: https://weightlow.org
predicates:
- Weight=group1, 2
说明:这个断言将会把请求80%的流量转发到https://weighthigh.org,20%的流量转发到https://weightlow.org
六、配置过滤器工厂(常用)
网关路由过滤器允许以某种方式修改传入的HTTP请求或传出的HTTP响应。路由过滤器的作用域是特定的路由。以下详细说明各种过滤器的配置方式。
1、请求头过滤器工厂(The AddRequestHeader GatewayFilter Factory)
- 不使用路径变量
spring:
cloud:
gateway:
routes:
- id: add_request_header_route
uri: https://example.org
filters:
- AddRequestHeader=X-Request-red, blue
- 使用路径变量
spring:
cloud:
gateway:
routes:
- id: add_request_header_route
uri: https://example.org
predicates:
- Path=/red/{segment}
filters:
- AddRequestHeader=X-Request-Red, Blue-{segment}
说明:将X-Request-red:Blue-{segment}头,添加到所有匹配请求的下游请求头中
2、响应头过滤器工厂(The AddResponseHeader GatewayFilter Factory)
- 不使用路径变量
spring:
cloud:
gateway:
routes:
- id: add_response_header_route
uri: https://example.org
filters:
- AddResponseHeader=X-Response-Red, Blue
- 使用路径变量
spring:
cloud:
gateway:
routes:
- id: add_response_header_route
uri: https://example.org
predicates:
- Host: {segment}.myhost.org
filters:
- AddResponseHeader=foo, bar-{segment}
说明:将X-Response-Foo:bar-{segment}头,添加到所有匹配请求的下游响应头中
3、请求参数过滤器工厂(The AddRequestParameter GatewayFilter Factory)
- 不使用路径变量
spring:
cloud:
gateway:
routes:
- id: add_request_parameter_route
uri: https://example.org
filters:
- AddRequestParameter=red, blue
- 使用路径变量
pring:
cloud:
gateway:
routes:
- id: add_request_parameter_route
uri: https://example.org
predicates:
- Host: {segment}.myhost.org
filters:
- AddRequestParameter=foo, bar-{segment}
说明:将为所有匹配的请求向下游请求的查询字符串添加qs:foo=bar-{segment}
4、路径重写过滤器工厂(The RewritePath GatewayFilter Factory)
spring:
cloud:
gateway:
routes:
- id: rewritepath_route
uri: https://example.org
predicates:
- Path=/red/**
filters:
- RewritePath=/red(?<segment>/?.*), $\{segment}
说明:对于/red/blue这样的请求路径,在发送下游请求之前,会将路径重写为/blue。另外,由于YAML规范,$
应该替换为$\
。