4 网关
网关
一、简介
当用户想调用项目A和项目B的接口时,用户需要多次请求不同的服务,增加了用户端的复杂性,并且用户在调用的时候,每个项目都需要做相关的鉴权和跨域处理,增加了每个项目的开发难度,并且随着项目的迭代,使得该系统难以重构。
而引入网关,可以统一去处理一些操作、处理相关的问题,比如:统一鉴权、跨域、统一日志等操作。
二、网关的基本作用
以下都是以spring cloud gateway为例进行说明
2.1 路由
做路由转发,比如由接口A和接口B,网关会记录这些信息,根据用户访问的地址和参数,请求到对应的接口(服务器/集群),比如:
- /a => 接口A
- /b => 接口B
https://docs.spring.io/spring-cloud-gateway/docs/3.1.7/reference/html/#gateway-request-predicates-factories
2.2 负载均衡
/c => 集群A(随机转发到集群中的某一个机器)
2.3 统一跨域处理
网关统一做请求跨域,而不需要在每个服务中单独处理
https://docs.spring.io/spring-cloud-gateway/docs/3.1.7/reference/html/#cors-configuration
2.4 发布控制
也叫灰度发布常见于在上线新版本的接口时,为了测试新接口的稳定性,先给接口分配20%的流量,老接口80%,在更加情况慢慢调整,直到新接口100%时将老接口替换掉。
https://docs.spring.io/spring-cloud-gateway/docs/3.1.7/reference/html/#the-weight-route-predicate-factory (权重)
2.5 流量染色
给请求添加标识,一般是在请求头中加入特定的标识,这样在调用后续的服务时,携带该标识,相关的服务可以根据标识做拦截,提高安全性。
https://docs.spring.io/spring-cloud-gateway/docs/3.1.7/reference/html/#the-addrequestheader-gatewayfilter-factory (添加特定的请求头等信息)
https://docs.spring.io/spring-cloud-gateway/docs/3.1.7/reference/html/#default-filters (所有请求都添加)
2.6 统一接口保护
- 限制请求
https://docs.spring.io/spring-cloud-gateway/docs/3.1.7/reference/html/#requestheadersize-gatewayfilter-factory - 信息脱敏:去除不方便访问给客户端的敏感数据
https://docs.spring.io/spring-cloud-gateway/docs/3.1.7/reference/html/#the-removerequestheader-gatewayfilter-factory - 降级(熔断)
https://docs.spring.io/spring-cloud-gateway/docs/3.1.7/reference/html/#fallback-headers - 限流
https://docs.spring.io/spring-cloud-gateway/docs/3.1.7/reference/html/#the-requestratelimiter-gatewayfilter-factory - 超时时间
https://docs.spring.io/spring-cloud-gateway/docs/3.1.7/reference/html/#http-timeouts-configuration - 重试(业务保护)
https://docs.spring.io/spring-cloud-gateway/docs/3.1.7/reference/html/#the-retry-gatewayfilter-factory
2.7 统一业务处理
把一些项目中都需要做到的通用逻辑放到网关统一处理
2.8 统一鉴权
判断用户是否有权限进行操作,无论访问什么接口,都需要统一去判断权限,而不用重复书写
2.9 访问控制
添加黑白名单,比如限制DDOS IP
2.10 统一日志
统一处理请求和响应的信息记录
2.11 统一文档
将各个服务的相关文档进行汇聚,在一个统一的页面上进行查看
使用:https://doc.xiaominfo.com/docs/middleware-sources/aggregation-introduction
三、网关的分类与实现
网关分类:
- 全局网关(接入层网关):作用是负载均衡、请求日志等,不和业务逻辑绑定,是最外层的。
- 业务网关(微服务网关):作用是做一些业务逻辑,根据请求转发到不同的业务/项目/接口/服务
网关实现: - 全局网关:Nginx、Kong(下载:https://github.com/Kong/kong 文档:https://blog.csdn.net/qq_21040559/article/details/122961395)
四、spring cloud gateway
官网文档:https://docs.spring.io/spring-cloud-gateway/docs/3.1.7/reference/html/#gateway-starter
4.1 核心概念
- 路由:根据什么条件,指定请求到哪里
- 断言:一组规则、条件,用来确定如何转发路由
- 过滤器:对请求进行一系列的处理,比如添加请求头、添加请求参数
工作原理:
请求流程:
- 客户端发起请求
- Handler Mapping根据断言,将请求转发到对应的路由
- Web Handler处理请求(经过一层层的过滤器)
- 实际调用服务
4.2 调用方式
4.2.1 配置式
优点:方便、规范
spring:
cloud:
gateway:
routes:
- id: after_route
uri: http://yupi.icu
predicates:
- Path=/zpyl
4.2.2 编程式
优点:更加的灵活,但是相对比较麻烦
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("toZpyl", r -> r.path("/yupi")
.uri("http://yupi.icu"))
.build();
}
4.2.3 配置路由日志
当路由过多时,不确定跳转到哪个路由是,可以开启日志记录
logging:
level:
org:
springframework:
cloud:
gateway: trace
4.3 断言
- After与Before
After:在XX时间之后 Before:在XX时间之前
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=2023-01-01T17:42:47.789-07:00[America/Denver]
- Before=2023-03-01T17:42:47.789-07:00[America/Denver]
- between
在XX时间之间
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- Between=2023-01-01T17:42:47.789-07:00[America/Denver],2023-03-01T17:42:47.789-07:00[America/Denver]
- 请求类别
spring:
cloud:
gateway:
routes:
- id: method_route
uri: https://example.org
predicates:
- Method=GET,POST
- 请求头(cookie)
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: https://example.org
predicates:
- Cookie=mycookie, mycookievalue
其中在cookie中包含mycookie值为mycookievalue,也可以写成以下形式
predicates:
- name: Cookie
args:
name: mycookie
regexp: mycookievalue
5.查询参数
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=green
客户端地址、权重等其他的请查看官方文档即可
https://docs.spring.io/spring-cloud-gateway/docs/3.1.7/reference/html/#gateway-request-predicates-factories
4.4 过滤器
- 添加请求头
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_parameter_route
uri: https://example.org
filters:
- AddRequestParameter=red, blue
- 添加响应头
spring:
cloud:
gateway:
routes:
- id: add_response_header_route
uri: https://example.org
filters:
- AddResponseHeader=X-Response-Red, Blue
…其他的请查看官方文档
https://docs.spring.io/spring-cloud-gateway/docs/3.1.7/reference/html/#gatewayfilter-factories
原文地址:https://blog.csdn.net/qq_40983975/article/details/140364682
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!