前言
Spring Cloud Gateway旨在提供一种简单而有效的方式来路由到api,并为它们提供横切关注点,如:安全性、监控/指标和弹性。
集成Sping Cloud Gateway需要Spring 5.0、Spring Boot2.0及以上版本。
特性
● 能够匹配任何请求属性的路由
● 断言和过滤特殊路由
● 断路器集成
● Spring Cloud DiscoveryClient集成
● 容易编写断言和过滤器
● 请求速率限制
● 地址重写
如何集成Spring Cloud Gateway?
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
如果集成了gateway但是不想开启,可使用配置将其关掉:spring.cloud.gateway.enabled=false;使用说明:
- Spring Cloud Gateway需要构建在Spring Boot2.x、Spring WebFlux、Project Reactor之上。
- Spring Cloud Gateway需要Spring Boot和Spring Webflux提供的Netty运行时。它不能在传统的Servlet容器中工作,也不能作为WAR构建。
术语
- Route:网关的基本组成部分,它由一个ID、一个目标URI、一个断言集合、一个过滤器集合定义,如果一个聚合断言为true则匹配路由。
- Predicate:一个Java8提供的新特性,它允许匹配任何来自HTTP的请求、比如请求头、请求参数。
- Filter:这些是使用特定工厂构造的GatewayFilter实例,通过filter,你可以修改发送到下游的请求或者响应信息。
工作原理

- 客户端首先请求到Spring Cloud Gateway.
- 如果网关处理程序映射确认这个请求能匹配到一个路由,则将该请求发送到Gateway Web Handler.
- 这个处理器会通过一个特定的请求过滤链来运行本次请求.
- 该过滤器链会在请求执行前后对请求或响应结果进行处理.
配置路由断言工厂和网管过滤器工厂
Spring Cloud Gateway提供两种配置断言和过滤器方式:快捷配置和完全展开参数配置。大多数情况下会使用快捷方式。
快捷配置
可通过过滤器名称识别快捷配置,后面跟着=号,参数如果有多个,使用逗号隔开(,)。
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- Cookie=mycookie,mycookievalue
其中Cookie为路由,mycookie为要匹配的cookie名称,mycookievalue为需要匹配的值。
完全展开参数
完全展开的参数看起来更像是带有key/value的标准yaml配置。一般会有一个name的键和一个args的键,其中name代表当前路由名称,而arg键主要用于配置断言或过滤器的键值对的映射。
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- name: Cookie
args:
name: mycookie
regexp: mycookievalue
路由断言工厂
Spring Cloud Gateway做为Spring WebFlux HandlerMapping基础设施的一部分,内置了许多断言工厂。所有断言都匹配不同HTTP请求的不同属性,可以使用逻辑或者语句组合多个断言工厂。
后置路由断言工厂
后置断言工厂接收一个日期参数(java ZonedDateTime),这个断言匹配在指定日期时间之后发生的请求。
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
前置路由断言工厂
前置断言工厂接收一个日期参数(java ZonedDateTime),这个断言匹配在指定日期时间之前发生的请求。
spring:
cloud:
gateway:
routes:
- id: before_route
uri: https://example.org
predicates:
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
区间路由断言工厂
区间路由断言工厂接收两个参数,datetime1和datetime2,都是java ZonedDateTime对象。这个断言匹配在datetime1和datetime2之间发生的请求。其中,datetime2必须在datetime1之后。
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]
Cookie路由断言工厂
Cookie路由断言工厂接收两个参数,cookie名称和正则表达式。这个断言匹配具有指定名称的cookie,且cookie的值能满足正则表达式的匹配条件。
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: https://example.org
predicates:
- Cookie=chocolate, ch.p
该示例路由匹配cookie名称为chocolate,cookie值为ch.p的请求。
请求头路由断言工厂
该断言工厂接收两个参数,header和一个正则表达式。这个断言匹配具有指定名称的header,且header的值满足匹配表示式的请求。
spring:
cloud:
gateway:
routes:
- id: header_route
uri: https://example.org
predicates:
- Header=X-Request-Id, \d+
主机路由断言工厂
该断言工厂接收一个参数:主机名模式列表,该模式是ant风格的模式 , 作为分隔符。这个断言匹配能按照主机头匹配模式匹配到的请求。
spring:
cloud:
gateway:
routes:
- id: host_route
uri: https://example.org
predicates:
- Host=**.somehost.org,**.anotherhost.org
如果请求的主机头为www.somehost.org or beta.somehost.org or www.anotherhost.org,则此路由匹配。
方法路由断言工厂
该断言工厂接收一个方法参数,它是一个或多个参数:要匹配的HTTP方法。
spring:
cloud:
gateway:
routes:
- id: method_route
uri: https://example.org
predicates:
- Method=GET,POST
该断言匹配一个GET或POST请求。
路径路由断言工厂
该断言工厂接收两个参数:Spring PathMatcher patterns列表和一个可选标志matchTrailingSlash(默认为true)。
spring:
cloud:
gateway:
routes:
- id: path_route
uri: https://example.org
predicates:
- Path=/red/{segment},/blue/{segment}
如果请求路径为/red/1 or /red/1/ or /red/blue or /blue/green则此路由匹配。如果matchTrailingSlash参数设置为false则路由不匹配,因为关闭该参数以为不匹配后续/内容。
查询路由断言工厂
该断言工厂接受两个参数:一个必须参数param和一个可选参数regex。
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.
该示例中,如果请求参数中key包含red,value包含green或greet则该请求将会被匹配。
远程地址路由断言工厂
这个RemoteAddr路由断言工厂接收一个列表,例如192.168.0.1/16(其中192.168.0.1是IP地址,16是子网掩码)。
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: https://example.org
predicates:
- RemoteAddr=192.168.1.1/24
如果发送请求的远程地址为192.168.1.10,则该路由将会被匹配。
网关拦截器工厂
AddRequestHeader GatewayFilter工厂
AddRequestHander网关拦截器工厂接收一个参数name和value参数。
spring:
cloud:
gateway:
routes:
- id: add_request_header_route
uri: https://example.org
filters:
- AddRequestHeader=X-Request-red, blue
这个列表将会添加一个X-Request-red:blue请求头为所有拦截到的请求。
AddRequestParameter网关过滤器工厂
AddRequestParameter网关过滤器工厂接收一个name和value参数。
spring:
cloud:
gateway:
routes:
- id: add_request_parameter_route
uri: https://example.org
filters:
- AddRequestParameter=red, blue
该示例中将会添加red=blue参数到下游请求的查询字符串中。
AddResponseHeader网关过滤器工厂
AddResponseHeader网关过滤器工厂接收一个name和一个value参数。
spring:
cloud:
gateway:
routes:
- id: add_response_header_route
uri: https://example.org
filters:
- AddResponseHeader=X-Response-Red, Blue
该过滤器将会为所有匹配到的下游响应结果添加X-Response-Foo:Bar响应头;同样,该过滤器也支持路由参数,对请求及响应进行处理:
spring:
cloud:
gateway:
routes:
- id: add_response_header_route
uri: https://example.org
predicates:
- Host: {segment}.myhost.org
filters:
- AddResponseHeader=foo, bar-{segment}
DedupeResponseHeader网关过滤器工厂
DedupeResponseHeader 网关过滤器工厂接收一个name参数和一个可选的strategy参数。name能够包含一个用逗号分割的header name列表
spring:
cloud:
gateway:
routes:
- id: dedupe_response_header_route
uri: https://example.org
filters:
- DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin
Spring Cloud 断路器网关过滤器工厂
Spring Cloud断路器网关过滤器工厂用于将Spring Cloud 断路器API包装在网关路由中。
spring:
cloud:
gateway:
routes:
- id: circuitbreaker_route
uri: https://example.org
filters:
- CircuitBreaker=myCircuitBreaker
FallBackHeaders网关过滤器工厂
FallBackHeaders工厂允许在转发到外部应用程序中的fallbackUri的请求头中添加Spring Cloud断路器执行异常细节。
spring:
cloud:
gateway:
routes:
- id: ingredients
uri: lb://ingredients
predicates:
- Path=//ingredients/**
filters:
- name: CircuitBreaker
args:
name: fetchIngredients
fallbackUri: forward:/fallback
- id: ingredients-fallback
uri: http://localhost:9994
predicates:
- Path=/fallback
filters:
- name: FallbackHeaders
args:
executionExceptionTypeHeaderName: Test-Header
6910




被折叠的 条评论
为什么被折叠?



