免费中文网站模板html免费发软文的网站
1、网关简介
网关作为流量的入口,常用的功能包括路由转发,权限校验,限流等。
2、Gateway简介
Spring Cloud Gateway 是Spring Cloud官方推出的第二代网关框架,定位于取代 Netflix Zuul。相比 Zuul 来说,Spring Cloud Gateway 提供更优秀的性能,更强大的有功能。
Spring Cloud Gateway 是由 WebFlux + Netty + Reactor 实现的响应式的 API 网关。
它不能在传统的 servlet 容器中工作,也不能构建成 war 包。
Spring Cloud Gateway 旨在为微服务架构提供一种简单且有效的 API 路由的管理方式,并基于 Filter 的方式提供网关的基本功能,例如说安全认证、监控、限流等等。
网文档:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gateway-starter
3、核心概念
-
路由(route)
路由是网关中最基础的部分,路由信息包括一个ID、一个目的URI、一组谓 词工厂、一组Filter组成。如果谓词为真,则说明请求的URL和配置的路由匹配。
-
谓词(predicates)
即java.util.function.Predicate , Spring Cloud Gateway使用Predicate实现路由的匹配条件。
-
过滤器(Filter)
SpringCloud Gateway中 的filter分为Gateway FilIer和Global Filter。Filter可以对请求和响应进行处理。
【路由就是转发规则,谓词就是是否走这个路径的条件,过滤器可以为路由添加业务逻辑,修改请求以及响应】
示例演示
spring:cloud:gateway:routes:#路由ID全局唯一- id: msb-order-route#目标微服务的请求地址和端口uri: http://localhost:8001predicates:- Path=/order/*filters:- AddRequestHeader=X-Request-Foo, Bar
这里就演示了请求 /order/*的路径就会路由到上边这个url上去
2、 工作原理
Spring Cloud Gateway 的工作原理跟 Zuul 的差不多,最大的区别就是 Gateway 的 Filter 只有 pre 和 post 两种。
客户端向 Spring Cloud Gateway 发出请求,如果请求与网关程序定义的路由匹配,则该请求就会被发送到网关 Web 处理程序,此时处理程序运行特定的请求过滤器链。
过滤器之间用虚线分开的原因是过滤器可能会在发送代理请求的前后执行逻辑。所有 pre 过滤器逻辑先执行,然后执行代理请求;代理请求完成后,执行 post 过滤器逻辑。
4、快速入门
3.1环境搭建
引入依赖
<!-- gateway网关--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency></dependencies><!-- nacos注册中心--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency>
父工程的pom
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency>
</dependencies>
spring:application:name: msb-gatewaycloud:nacos:discovery:server-addr: localhost:8848gateway:discovery:locator:#默认值是false,如果设为true开启通过微服务创建路由的功能,即可以通过微服务名访问服务#http://localhost:13001/msb-order/order/create#不建议打开,因为这样暴露了服务名称enabled: true#是否开启网关enabled: true
这里注意不要引入springmvc工程否则会报错如下:
我们工程中如下:
msb-gateway工程的pom
<dependencies><!-- gateway网关--><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><scope>test</scope> <!-- 特殊处理,不引入父工程依赖 --></dependency>
</dependencies>
3.2 测试
我们刚才说了我们不建议开启通过微服务创建路由的功能,这样就把微服务名称暴露了,如果设置位默认值在请求一下,是否能成功呢? 我们测试一下:
是失败的,刚才能成功主要是
http://localhost:13001/msb-order/order/create 可以进行转化:
http://localhost:13001/msb-order/order/create可以替换为http://localhost:8001/order/create
然而此时的请求是转化不了的
http://localhost:13001/order/create
这是我们就需要我们的gateway 中的 route& predicates 建立映射
二、路由谓词工厂(Route Predicate Factories)配置
1、路由配置的两种形式
1.1 路由到指定URL
-
通配
spring:cloud:gateway:routes:- id: {唯一标识}uri: http://localhost:8001
表示访问
GATEWAY_URL/**
会转发到http://localhost:8001/**
注:上面路由的配置必须和下面谓词(Predicate)配合使用才行
-
精确匹配
spring:cloud:gateway:routes:- id: {唯一标识}uri: http://localhost:8001/predicates:- Path=/order/*
表示访问
GATEWAY_URL/order/*
会转发到http://localhost:8001/order/*
1.2 路由到服务发现组件上的微服务
-
通配
spring:cloud:gateway:routes:- id: {唯一标识}uri: lb://msb-order
表示访问
GATEWAY_URL/**
会转发到msb-order
微服务的/**
注:上面路由的配置必须和下面谓词(Predicate)配合使用才行
-
精确匹配
spring:cloud:gateway:routes:- id: {唯一标识}uri: lb://msb-order/predicates:- Path=/order/*
表示访问
GATEWAY_URL/order/
会转发到msb-order
微服务的/order/
2、谓词工厂分类
官网:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gateway-request-predicates-factories
网关启动日志:
3、谓词介绍
3.1 After路由断言工厂
规则:
该断言工厂的参数是一个 UTC 格式的时间。其会将请求访问到 Gateway 的时间与该参数时间相比,若请求时间在参数时间之后,则匹配成功,断言为 true。
配置:
spring:cloud:gateway:routes:- id: after_routeuri: lb://msb-userpredicates:# 当且仅当请求时的时间After配置的时间时,才会转发到用户微服务# 目前配置不会进该路由配置,所以返回404# 将时间改成 < now的时间,则访问localhost:8040/** -> msb-user/**# eg. 访问http://localhost:8040/users/1 -> msb-user/users/1- After=2030-01-20T17:42:47.789-07:00[America/Denver]
技巧:时间可使用 System.out.println(ZonedDateTime.now());
打印,然后即可看到时区。例如:2019-08-10T16:50:42.579+08:00[Asia/Shanghai]
时间格式的相关逻辑:
- 默认时间格式:org.springframework.format.support.DefaultFormattingConversionService#addDefaultFormatters
- 时间格式注册:org.springframework.format.datetime.standard.DateTimeFormatterRegistrar#registerFormatters
3.2 Before路由断言工厂
规则:
该断言工厂的参数是一个 UTC 格式的时间。其会将请求访问到 Gateway 的时间与该参数时间相比,若请求时间在参数时间之前,则匹配成功,断言为 true。
配置:
spring:cloud:gateway:routes:- id: before_routeuri: lb://msb-userpredicates:# 当且仅当请求时的时间Before配置的时间时,才会转发到用户微服务# 目前配置不会进该路由配置,所以返回404# 将时间改成 > now的时间,则访问localhost:8040/** -> msb-user/**# eg. 访问http://localhost:8040/users/1 -> msb-user/users/1- Before=2018-01-20T17:42:47.789-