目录
简介
Spring Cloud GateWay是Spring Cloud的⼀个全新项⽬,⽬标是取代Netflix Zuul,它基于Spring5.0+SpringBoot2.0+WebFlux(基于⾼性能的Reactor模式响应式通信框架Netty,异步⾮阻塞模型)等技术开发,性能⾼于Zuul,官⽅测试,GateWay是Zuul的1.6倍,旨在为微服务架构提供⼀种简单有效的统⼀的API路由管理⽅式。
Spring Cloud GateWay不仅提供统⼀的路由⽅式(反向代理)并且基于 Filter(定义过滤器对请求过滤,完成⼀些功能) 链的⽅式提供了⽹关基本的功能,例如:鉴权、流量控制、熔断、路径重写、⽇志监控等。
官网文档地址
工作原理
客户端向 Spring Cloud Gateway 发出请求。如果网关处理程序映射确定请求与路由匹配,则将其发送到网关 Web 处理程序。此处理程序通过特定于请求的过滤器链运行请求。过滤器用虚线划分的原因是过滤器可以在发送代理请求之前和之后运行逻辑。执行所有“预”过滤器逻辑。然后发出代理请求。发出代理请求后,将运行“发布”过滤器逻辑
上面这段话来自官网的翻译:我的理解是请求打进来之后先经过配置好的过滤器过滤走一遍然后匹配断言配置的路径符合的话就跳转到配置好的url
常见配置
spring:
cloud:
gateway:
globalcors: ##跨域相关的主力
add-to-simple-url-handler-mapping: true # 解决options请求被拦截问题
corsConfigurations:
'[/**]': # 拦截所有请求
allowCredentials: false # 是否允许携带cookie
allowedHeaders: "*" # 允许哪些header
allowedOrigins: "*" # 允许哪些网站的跨域请求 * 全部
allowedMethods: # 允许的跨域ajax的请求方式
- GET
- POST
- DELETE
- PUT
- OPTION
httpclient: ##全局的http超时配置 也可以配置单个路由
connect-timeout: 10000 #默认连接超时45秒
response-timeout: 5s ##响应超时时间
routes:
- id: {唯一不重复就行}
uri: {匹配后需要跳转的路径}
predicates: {断言匹配}
filters: {过滤器}
routes配置详解
id
唯一不重复就可以
uri
匹配成功后跳转的路径
写法1 : uri: https://example.org 直接跳转路径
写法2 : uri: lb://{注册中心实例} 结合微服务使用
predicates 断言器
结合官网第五章的例子 总共11种
时间相关
判断请求日期是否晚于指定时间
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
判断请求日期是否早于指定时间
spring:
cloud:
gateway:
routes:
- id: before_route
uri: https://example.org
predicates:
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
判断请求日期是否在指定时间段内
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]
Http相关
Cookie
配置对Cookie中值的匹配,第一个为key,第二个为value。下例匹配cookie设置chocolate:ch.p的请求
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: https://example.org
predicates:
- Cookie=chocolate, ch.p
Header
匹配Http请求中设置的内容,http-header设置X-Request-Id:\d+可以匹配,第二个参数是正则表达式, 第一个是key 然后value
spring:
cloud:
gateway:
routes:
- id: header_route
uri: https://example.org
predicates:
- Header=X-Request-Id, \d+
Host
匹配Http请求Host,匹配所有host为**.somehost.org,**.anotherhost.org的请求
spring:
cloud:
gateway:
routes:
- id: host_route
uri: https://example.org
predicates:
- Host=**.somehost.org,**.anotherhost.org
Method
匹配Http请求方式 get post
spring:
cloud:
gateway:
routes:
- id: method_route
uri: https://example.org
predicates:
- Method=GET,POST
Path
配置对于请求路径的匹配规则
spring:
cloud:
gateway:
routes:
- id: path_route
uri: https://example.org
predicates:
- Path=/red/{segment},/blue/{segment}
Query
匹配Http请求中的查询参数,如果请求包含green
查询参数,则前面的路由匹配。
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=green
如果请求包含red
其值与正则gree.
表达式匹配的查询参数,则前面的路由匹配,因此green
并且greet
会匹配
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=red, gree.
RemoteAddr
匹配请求中的RemoteAddr
匹配最后一位 1-24的ip 例如192.168.1.10 就会被匹配到
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: https://example.org
predicates:
- RemoteAddr=192.168.1.1/24
权重相关
Weight
该路由会将约 80% 的流量转发到weighthigh.org,将约 20% 的流量转发到weightlow.org
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
filters 过滤器 对应官网 第六章和第七章
如上图如果按照生命周期来说,那么过滤器有“pre”和“post”两种方式的filter,客户端的请求先经过“pre”类型的filter,然后将请求转发到具体的业务服务,比如上图中的user-service,收到业务服务的响应之后,再经过“post”类型的filter处理,最后返回响应到客户端。
这边按照官网文档按照特定和全局区分
特定的过滤器(更全的资料建议直接看官方文档 第六章)
这边只讲解常用功能的,详细的大家可以去官方文档看
pre:pre开头的都是预处理的过滤器
用户每次请求路径的时候,我们可以给真实请求加一个统一前缀,例如用户请求http://localhost:8080
的时候我们让它请求真实地址http://localhost:8080/mypath
- PrefixPath=/mypath
路径前缀删除
请求/name/bar/foo,去除掉前面两个前缀之后,最后转发到目标服务的路径为/foo
- StripPrefix = 2
post:非pre开头的
过滤完之后给请求的header加上参数
- AddRequestHeader=X-Request-red, blue
过滤完之后给请求加上参数,参数放在query里
- AddRequestParameter=foo, bar-{segment}
给响应的header加上参数
AddResponseHeader=X-Response-Red, Blue
断路器 CircuitBreaker
filters:
- name: CircuitBreaker
args:
name: myCircuitBreaker
fallbackUri: forward:/inCaseOfFailureUseThis
- RewritePath=/consumingServiceEndpoint, /backingServiceEndpoint
全局过滤器(更全的资料建议直接看官方文档 第七章)
websocker过滤器
spring:
cloud:
gateway:
routes:
# SockJS route
- id: websocket_sockjs_route
uri: http://localhost:3001
predicates:
- Path=/websocket/info/**
# Normal Websocket route
- id: websocket_route
uri: ws://localhost:3001
predicates:
- Path=/websocket/**