Spring Cloud Gateway如何实现微服务架构下的高效流量管理?
- 内容介绍
- 文章标签
- 相关推荐
本文共计3880个文字,预计阅读时间需要16分钟。
Spring Cloud Gateway 是基于 Spring Cloud 生态圈开发的新一代 API 网关产品,采用 NIO 异步处理,摒弃了 Zuul 基于 Servlet 同步通信的设计。
Spring Cloud GateWay
Spring 自己开发的新一代API网关产品,基于NIO异步处理,摒弃了Zuul基于Servlet同步通信的设计。
Spring Cloud Gateway 作为 Spring Cloud 生态系统中的网关,目标是替代 Netflix Zuul,其不仅提供统一的路由方式,并且基于 Filter 链的方式提供了网关基本的功能,例如:安全,监控/指标,和限流。
关键特征:
- 1、基于JDK8+开发。
- 2、Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的 API 路由管理方式。
- 3、支持动态路由,能够匹配任何请求属性上的路由。
- 4、支持基于HTTP请求的路由匹配(Path、Method、Header、Host等)。
- 5、过滤器可以修改HTTP请求和HTTP响应。
在性能方面,根据官方提供的基准测试, Spring Cloud Gateway 的 RPS(每秒请求数)是Zuul 的 1.6 倍。
Spring Cloud Gateway十分优秀,Spring Cloud Alibaba也默认选用该组件作为网关产品。
相关概念:
- Route(路由):这是网关的基本构建块。它由一个 ID,一个目标 URI,一组断言和一组过滤器定义。如果断言为真,则路由匹配。
- Predicate(断言):这是一个 Java 8 的 Predicate。输入类型是一个 ServerWebExchange。我们可以使用它来匹配来自 HTTP 请求的任何内容,例如 headers 或参数。
- Filter(过滤器):这是org.springframework.cloud.gateway.filter.GatewayFilter的实例,我们可以使用它修改请求和响应。
工作流程:
客户端向 Spring Cloud Gateway 发出请求。如果 Gateway Handler Mapping 中找到与请求相匹配的路由,将其发送到 Gateway Web Handler。Handler 再通过指定的过滤器链来将请求发送到我们实际的服务执行业务逻辑,然后返回。 过滤器之间用虚线分开是因为过滤器可能会在发送代理请求之前(“pre”)或之后(“post”)执行业务逻辑。
Spring Cloud Gateway 的特征:
- 基于 Spring Framework 5,Project Reactor 和 Spring Boot 2.0
- 动态路由
- Predicates 和 Filters 作用于特定路由
- 集成 Hystrix 断路器
- 集成 Spring Cloud DiscoveryClient
- 易于编写的 Predicates 和 Filters
- 限流
- 路径重写
Spring Cloud GateWay 快速上手
Spring Cloud Gateway 网关路由有两种配置方式:
- 在配置文件 yml 中配置
- 通过@Bean自定义 RouteLocator,在启动主类 Application 中配置
这两种方式是等价的,建议使用 yml 方式进配置。
引入依赖:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.12.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <!--整合Spring Cloud--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR12</version> <type>pom</type> <scope>import</scope> </dependency> <!--整合Spring Cloud Alibaba--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.6.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>Spring Cloud Gateway 是使用 netty+webflux 实现因此不需要再引入 web 模块。
gateWay的主要功能之一是转发请求,转发规则的定义主要包含三个部分:
- Route(路由):路由是网关的基本单元,由ID、URI、一组Predicate、一组Filter组成,根据Predicate进行匹配转发。
- Predicate(谓语、断言):路由转发的判断条件,目前SpringCloud Gateway支持多种方式,常见如:Path、Query、Method、Header等,写法必须遵循 key=vlue的形式。
- Filter(过滤器):过滤器是路由转发请求时所经过的过滤逻辑,可用于修改请求、响应内容。
注意:其中Route和Predicate必须同时申明
路由配置方式
基础URI路由配置方式
- 如果请求的目标地址,是单个的URI资源路径,配置文件示例如下:
基于代码的路由配置方式
转发功能同样可以通过代码来实现,我们可以在启动类 GateWayApplication 中添加方法 customRouteLocator() 来定制转发规则。
@SpringBootApplication public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("path_route", r -> r.path("/csdn") .uri("blog.csdn.net")) .build(); } }上面配置了一个 id 为 path_route 的路由,当访问地址localhost:8080/about时会自动转发到地址:blog.csdn.net/csdn和上面的转发效果一样,只是这里转发的是以项目地址/csdn格式的请求地址。
和注册中心相结合的路由配置方式
在uri的schema协议部分为自定义的lb:类型,表示从微服务注册中心(如Eureka)订阅服务,并且进行服务的路由。
server: port: 8084 spring: cloud: gateway: routes: -id: seckill-provider-route uri: lb://seckill-provider predicates: - Path=/seckill-provider/** -id: message-provider-route uri: lb://message-provider predicates: -Path=/message-provider/** application: name: cloud-gateway eureka: instance: prefer-ip-address: true client: service-url: defaultZone: localhost:8888/eureka/注册中心相结合的路由配置方式,与单个URI的路由配置,区别其实很小,仅仅在于URI的schema协议不同。单个URI的地址的schema协议,一般为localhost:9023*** 规则 实例 说明
Path - Path=/gate/,/rule/ 当请求的路径为gate、rule开头的时,转发到localhost:9023服务器上 Before - Before=2017-01-20T17:42:47.789-07:00[America/Denver] 在某个时间之前的请求才会被转发到 localhost:9023服务器上 After - After=2017-01-20T17:42:47.789-07:00[America/Denver] 在某个时间之后的请求才会被转发 Between - Between=2017-01-20T17:42:47.789-07:00[America/Denver],2017-01-21T17:42:47.789-07:00[America/Denver] 在某个时间段之间的才会被转发 Cookie - Cookie=chocolate, ch.p 名为chocolate的表单或者满足正则ch.p的表单才会被匹配到进行请求转发 Header - Header=X-Request-Id, \d+ 携带参数X-Request-Id或者满足\d+的请求头才会匹配 Host - Host=www.hd123.com 当主机名为www.hd123.com的时候直接转发到localhost:9023服务器上 Method - Method=GET 只有GET方法才会匹配转发请求,还可以限定POST、PUT等请求方式通过请求参数匹配
- Query Route Predicate 支持传入两个参数,一个是属性名一个为属性值,属性值可以是正则表达式。
只要请求中包含 smile 属性的参数即可匹配路由,不带 smile 参数则不会匹配。
curl localhost:8080?smile=x&id=2- 还可以将 Query 的值以键值对的方式进行配置,这样在请求过来时会对属性值和正则进行匹配,匹配上才会走路由。
这样只要当请求中包含 keep 属性并且参数值是以 pu 开头的长度为三位的字符串才会进行匹配和路由。
curl localhost:8080?keep=pub通过 Header 属性匹配
Header Route Predicate 和 Cookie Route Predicate 一样,也是接收 2 个参数,一个 header 中属性名称和一个正则表达式,这个属性值和正则表达式匹配则执行。
server: port: 8080 spring: application: name: api-gateway cloud: gateway: routes: - id: gateway-service uri: www.baidu.com order: 0 predicates: - Header=X-Request-Id, \d+使用 curl 测试,命令行输入:
curl localhost:8080 -H "X-Request-Id:88"则返回页面代码证明匹配成功。将参数-H "X-Request-Id:88"改为-H "X-Request-Id:spring"再次执行时返回404证明没有匹配。
通过 Cookie 匹配
Cookie Route Predicate 可以接收两个参数,一个是 Cookie name ,一个是正则表达式,路由规则会通过获取对应的 Cookie name 值和正则表达式去匹配,如果匹配上就会执行路由,如果没有匹配上则不执行。
server: port: 8080 spring: application: name: api-gateway cloud: gateway: routes: - id: gateway-service uri: www.baidu.com order: 0 predicates: - Cookie=sessionId, test使用 curl 测试,命令行输入:
curl localhost:8080 --cookie "sessionId=test"则会返回页面代码,如果去掉--cookie "sessionId=test",后台汇报 404 错误。
通过 Host 匹配
Host Route Predicate 接收一组参数,一组匹配的域名列表,这个模板是一个 ant 分隔的模板,用.号作为分隔符。它通过参数中的主机地址作为匹配规则。
server: port: 8080 spring: application: name: api-gateway cloud: gateway: routes: - id: gateway-service uri: www.baidu.com order: 0 predicates: - Host=**.baidu.com使用 curl 测试,命令行输入:
curl localhost:8080 -H "Host: www.baidu.com" curl localhost:8080 -H "Host: md.baidu.com"经测试以上两种 host 均可匹配到 host_route 路由,去掉 host 参数则会报 404 错误。
通过请求方式匹配
可以通过是 POST、GET、PUT、DELETE 等不同的请求方式来进行路由。
server: port: 8080 spring: application: name: api-gateway cloud: gateway: routes: - id: gateway-service uri: www.baidu.com order: 0 predicates: - Method=GET使用 curl 测试,命令行输入:
# curl 默认是以 GET 的方式去请求 curl localhost:8080测试返回页面代码,证明匹配到路由,我们再以 POST 的方式请求测试。
# curl 默认是以 GET 的方式去请求 curl -X POST localhost:8080返回 404 没有找到,证明没有匹配上路由。
通过请求路径匹配
Path Route Predicate 接收一个匹配路径的参数来判断是否走路由。
server: port: 8080 spring: application: name: api-gateway cloud: gateway: routes: - id: gateway-service uri: ityouknow.com order: 0 predicates: - Path=/foo/{segment}如果请求路径符合要求,则此路由将匹配,例如:/foo/1 或者 /foo/bar。
使用 curl 测试,命令行输入:
curl localhost:8080/foo/1 curl localhost:8080/foo/xx curl localhost:8080/boo/xx经过测试第一和第二条命令可以正常获取到页面返回值,最后一个命令报404,证明路由是通过指定路由来匹配。
通过请求 ip 地址进行匹配
Predicate 也支持通过设置某个 ip 区间号段的请求才会路由,RemoteAddr Route Predicate 接受 cidr 符号(IPv4 或 IPv6 )字符串的列表(最小大小为1),例如 192.168.0.1/16 (其中 192.168.0.1 是 IP 地址,16 是子网掩码)。
server: port: 8080 spring: application: name: api-gateway cloud: gateway: routes: - id: gateway-service uri: www.baidu.com order: 0 predicates: - RemoteAddr=192.168.1.1/24可以将此地址设置为本机的 ip 地址进行测试。
curl localhost:8080如果请求的远程地址是 192.168.1.10,则此路由将匹配。
组合使用
server: port: 8080 spring: application: name: api-gateway cloud: gateway: routes: - id: gateway-service uri: www.baidu.com order: 0 predicates: - Host=**.foo.org - Path=/headers - Method=GET - Header=X-Request-Id, \d+ - Query=foo, ba. - Query=baz - Cookie=chocolate, ch.p各种 Predicates 同时存在于同一个路由时,请求必须同时满足所有的条件才被这个路由匹配。
一个请求满足多个路由的断言条件时,请求只会被首个成功匹配的路由转发。
过滤器规则(Filter)
过滤规则 实例 说明 PrefixPath - PrefixPath=/app 在请求路径前加上app RewritePath - RewritePath=/test, /app/test 访问localhost:9022/test,请求会转发到localhost:8001/app/test SetPath SetPath=/app/{path} 通过模板设置路径,转发的规则时会在路径前增加app,{path}表示原请求路径 RedirectTo 重定向 RemoveRequestHeader 去掉某个请求头信息注意:当配置多个filter时,优先定义的会被调用,剩余的filter将不会生效
PrefixPath
- 对所有的请求路径添加前缀:
访问/hello的请求被发送到example.org/mypath/hello。
RedirectTo
- 重定向,配置包含重定向的返回码和地址:
RemoveRequestHeader
- 去掉某个请求头信息:
去掉请求头信息 X-Request-Foo
RemoveResponseHeader
- 去掉某个回执头信息:
RemoveRequestParameter
- 去掉某个请求参数信息:
RewritePath
- 改写路径:
/where/... 改成 test/...
使用代码改下路径
RouteLocatorBuilder.Builder builder = routeLocatorBuilder.routes(); builder.route("path_rote_at_guigu", r -> r.path("/guonei") .uri("news.baidu.com/guonei")) .route("csdn_route", r -> r.path("/csdn") .uri("blog.csdn.net")) .route("blog3_rewrite_filter", r -> r.path("/blog3/**") .filters(f -> f.rewritePath("/blog3/(?<segment>.*)", "/$\\{segment}")) .uri("blog.csdn.net")) .route("rewritepath_route", r -> r.path("/baidu/**") .filters(f -> f.rewritePath("/baidu/(?<segment>.*)", "/$\\{segment}")) .uri("www.baidu.com")) .build();SetPath
- 设置请求路径,与RewritePath类似。
如/red/blue的请求被转发到/blue。
SetRequestHeader
- 设置请求头信息。
SetStatus
- 设置回执状态码。
StripPrefix
- 跳过指定路径。
请求/name/blue/red会转发到/red。
RequestSize
- 请求大小。
超过5M的请求会返回413错误。
Default-filters
- 对所有请求添加过滤器。
参考: www.ityouknow.com/springcloud/2018/12/12/spring-cloud-gateway-start.html
www.likecs.com/show-50293.html
zhuanlan.zhihu.com/p/299608850?utm_source=wechat_session
juejin.cn/post/6844903965352525838
blog.csdn.net/weixin_38361347/article/details/114108368
www.zyiz.net/tech/detail-98256.html
www.cnblogs.com/crazymakercircle/p/11704077.html
blog.csdn.net/forezp/article/details/85057268
本文共计3880个文字,预计阅读时间需要16分钟。
Spring Cloud Gateway 是基于 Spring Cloud 生态圈开发的新一代 API 网关产品,采用 NIO 异步处理,摒弃了 Zuul 基于 Servlet 同步通信的设计。
Spring Cloud GateWay
Spring 自己开发的新一代API网关产品,基于NIO异步处理,摒弃了Zuul基于Servlet同步通信的设计。
Spring Cloud Gateway 作为 Spring Cloud 生态系统中的网关,目标是替代 Netflix Zuul,其不仅提供统一的路由方式,并且基于 Filter 链的方式提供了网关基本的功能,例如:安全,监控/指标,和限流。
关键特征:
- 1、基于JDK8+开发。
- 2、Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的 API 路由管理方式。
- 3、支持动态路由,能够匹配任何请求属性上的路由。
- 4、支持基于HTTP请求的路由匹配(Path、Method、Header、Host等)。
- 5、过滤器可以修改HTTP请求和HTTP响应。
在性能方面,根据官方提供的基准测试, Spring Cloud Gateway 的 RPS(每秒请求数)是Zuul 的 1.6 倍。
Spring Cloud Gateway十分优秀,Spring Cloud Alibaba也默认选用该组件作为网关产品。
相关概念:
- Route(路由):这是网关的基本构建块。它由一个 ID,一个目标 URI,一组断言和一组过滤器定义。如果断言为真,则路由匹配。
- Predicate(断言):这是一个 Java 8 的 Predicate。输入类型是一个 ServerWebExchange。我们可以使用它来匹配来自 HTTP 请求的任何内容,例如 headers 或参数。
- Filter(过滤器):这是org.springframework.cloud.gateway.filter.GatewayFilter的实例,我们可以使用它修改请求和响应。
工作流程:
客户端向 Spring Cloud Gateway 发出请求。如果 Gateway Handler Mapping 中找到与请求相匹配的路由,将其发送到 Gateway Web Handler。Handler 再通过指定的过滤器链来将请求发送到我们实际的服务执行业务逻辑,然后返回。 过滤器之间用虚线分开是因为过滤器可能会在发送代理请求之前(“pre”)或之后(“post”)执行业务逻辑。
Spring Cloud Gateway 的特征:
- 基于 Spring Framework 5,Project Reactor 和 Spring Boot 2.0
- 动态路由
- Predicates 和 Filters 作用于特定路由
- 集成 Hystrix 断路器
- 集成 Spring Cloud DiscoveryClient
- 易于编写的 Predicates 和 Filters
- 限流
- 路径重写
Spring Cloud GateWay 快速上手
Spring Cloud Gateway 网关路由有两种配置方式:
- 在配置文件 yml 中配置
- 通过@Bean自定义 RouteLocator,在启动主类 Application 中配置
这两种方式是等价的,建议使用 yml 方式进配置。
引入依赖:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.12.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <!--整合Spring Cloud--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR12</version> <type>pom</type> <scope>import</scope> </dependency> <!--整合Spring Cloud Alibaba--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.6.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>Spring Cloud Gateway 是使用 netty+webflux 实现因此不需要再引入 web 模块。
gateWay的主要功能之一是转发请求,转发规则的定义主要包含三个部分:
- Route(路由):路由是网关的基本单元,由ID、URI、一组Predicate、一组Filter组成,根据Predicate进行匹配转发。
- Predicate(谓语、断言):路由转发的判断条件,目前SpringCloud Gateway支持多种方式,常见如:Path、Query、Method、Header等,写法必须遵循 key=vlue的形式。
- Filter(过滤器):过滤器是路由转发请求时所经过的过滤逻辑,可用于修改请求、响应内容。
注意:其中Route和Predicate必须同时申明
路由配置方式
基础URI路由配置方式
- 如果请求的目标地址,是单个的URI资源路径,配置文件示例如下:
基于代码的路由配置方式
转发功能同样可以通过代码来实现,我们可以在启动类 GateWayApplication 中添加方法 customRouteLocator() 来定制转发规则。
@SpringBootApplication public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("path_route", r -> r.path("/csdn") .uri("blog.csdn.net")) .build(); } }上面配置了一个 id 为 path_route 的路由,当访问地址localhost:8080/about时会自动转发到地址:blog.csdn.net/csdn和上面的转发效果一样,只是这里转发的是以项目地址/csdn格式的请求地址。
和注册中心相结合的路由配置方式
在uri的schema协议部分为自定义的lb:类型,表示从微服务注册中心(如Eureka)订阅服务,并且进行服务的路由。
server: port: 8084 spring: cloud: gateway: routes: -id: seckill-provider-route uri: lb://seckill-provider predicates: - Path=/seckill-provider/** -id: message-provider-route uri: lb://message-provider predicates: -Path=/message-provider/** application: name: cloud-gateway eureka: instance: prefer-ip-address: true client: service-url: defaultZone: localhost:8888/eureka/注册中心相结合的路由配置方式,与单个URI的路由配置,区别其实很小,仅仅在于URI的schema协议不同。单个URI的地址的schema协议,一般为localhost:9023*** 规则 实例 说明
Path - Path=/gate/,/rule/ 当请求的路径为gate、rule开头的时,转发到localhost:9023服务器上 Before - Before=2017-01-20T17:42:47.789-07:00[America/Denver] 在某个时间之前的请求才会被转发到 localhost:9023服务器上 After - After=2017-01-20T17:42:47.789-07:00[America/Denver] 在某个时间之后的请求才会被转发 Between - Between=2017-01-20T17:42:47.789-07:00[America/Denver],2017-01-21T17:42:47.789-07:00[America/Denver] 在某个时间段之间的才会被转发 Cookie - Cookie=chocolate, ch.p 名为chocolate的表单或者满足正则ch.p的表单才会被匹配到进行请求转发 Header - Header=X-Request-Id, \d+ 携带参数X-Request-Id或者满足\d+的请求头才会匹配 Host - Host=www.hd123.com 当主机名为www.hd123.com的时候直接转发到localhost:9023服务器上 Method - Method=GET 只有GET方法才会匹配转发请求,还可以限定POST、PUT等请求方式通过请求参数匹配
- Query Route Predicate 支持传入两个参数,一个是属性名一个为属性值,属性值可以是正则表达式。
只要请求中包含 smile 属性的参数即可匹配路由,不带 smile 参数则不会匹配。
curl localhost:8080?smile=x&id=2- 还可以将 Query 的值以键值对的方式进行配置,这样在请求过来时会对属性值和正则进行匹配,匹配上才会走路由。
这样只要当请求中包含 keep 属性并且参数值是以 pu 开头的长度为三位的字符串才会进行匹配和路由。
curl localhost:8080?keep=pub通过 Header 属性匹配
Header Route Predicate 和 Cookie Route Predicate 一样,也是接收 2 个参数,一个 header 中属性名称和一个正则表达式,这个属性值和正则表达式匹配则执行。
server: port: 8080 spring: application: name: api-gateway cloud: gateway: routes: - id: gateway-service uri: www.baidu.com order: 0 predicates: - Header=X-Request-Id, \d+使用 curl 测试,命令行输入:
curl localhost:8080 -H "X-Request-Id:88"则返回页面代码证明匹配成功。将参数-H "X-Request-Id:88"改为-H "X-Request-Id:spring"再次执行时返回404证明没有匹配。
通过 Cookie 匹配
Cookie Route Predicate 可以接收两个参数,一个是 Cookie name ,一个是正则表达式,路由规则会通过获取对应的 Cookie name 值和正则表达式去匹配,如果匹配上就会执行路由,如果没有匹配上则不执行。
server: port: 8080 spring: application: name: api-gateway cloud: gateway: routes: - id: gateway-service uri: www.baidu.com order: 0 predicates: - Cookie=sessionId, test使用 curl 测试,命令行输入:
curl localhost:8080 --cookie "sessionId=test"则会返回页面代码,如果去掉--cookie "sessionId=test",后台汇报 404 错误。
通过 Host 匹配
Host Route Predicate 接收一组参数,一组匹配的域名列表,这个模板是一个 ant 分隔的模板,用.号作为分隔符。它通过参数中的主机地址作为匹配规则。
server: port: 8080 spring: application: name: api-gateway cloud: gateway: routes: - id: gateway-service uri: www.baidu.com order: 0 predicates: - Host=**.baidu.com使用 curl 测试,命令行输入:
curl localhost:8080 -H "Host: www.baidu.com" curl localhost:8080 -H "Host: md.baidu.com"经测试以上两种 host 均可匹配到 host_route 路由,去掉 host 参数则会报 404 错误。
通过请求方式匹配
可以通过是 POST、GET、PUT、DELETE 等不同的请求方式来进行路由。
server: port: 8080 spring: application: name: api-gateway cloud: gateway: routes: - id: gateway-service uri: www.baidu.com order: 0 predicates: - Method=GET使用 curl 测试,命令行输入:
# curl 默认是以 GET 的方式去请求 curl localhost:8080测试返回页面代码,证明匹配到路由,我们再以 POST 的方式请求测试。
# curl 默认是以 GET 的方式去请求 curl -X POST localhost:8080返回 404 没有找到,证明没有匹配上路由。
通过请求路径匹配
Path Route Predicate 接收一个匹配路径的参数来判断是否走路由。
server: port: 8080 spring: application: name: api-gateway cloud: gateway: routes: - id: gateway-service uri: ityouknow.com order: 0 predicates: - Path=/foo/{segment}如果请求路径符合要求,则此路由将匹配,例如:/foo/1 或者 /foo/bar。
使用 curl 测试,命令行输入:
curl localhost:8080/foo/1 curl localhost:8080/foo/xx curl localhost:8080/boo/xx经过测试第一和第二条命令可以正常获取到页面返回值,最后一个命令报404,证明路由是通过指定路由来匹配。
通过请求 ip 地址进行匹配
Predicate 也支持通过设置某个 ip 区间号段的请求才会路由,RemoteAddr Route Predicate 接受 cidr 符号(IPv4 或 IPv6 )字符串的列表(最小大小为1),例如 192.168.0.1/16 (其中 192.168.0.1 是 IP 地址,16 是子网掩码)。
server: port: 8080 spring: application: name: api-gateway cloud: gateway: routes: - id: gateway-service uri: www.baidu.com order: 0 predicates: - RemoteAddr=192.168.1.1/24可以将此地址设置为本机的 ip 地址进行测试。
curl localhost:8080如果请求的远程地址是 192.168.1.10,则此路由将匹配。
组合使用
server: port: 8080 spring: application: name: api-gateway cloud: gateway: routes: - id: gateway-service uri: www.baidu.com order: 0 predicates: - Host=**.foo.org - Path=/headers - Method=GET - Header=X-Request-Id, \d+ - Query=foo, ba. - Query=baz - Cookie=chocolate, ch.p各种 Predicates 同时存在于同一个路由时,请求必须同时满足所有的条件才被这个路由匹配。
一个请求满足多个路由的断言条件时,请求只会被首个成功匹配的路由转发。
过滤器规则(Filter)
过滤规则 实例 说明 PrefixPath - PrefixPath=/app 在请求路径前加上app RewritePath - RewritePath=/test, /app/test 访问localhost:9022/test,请求会转发到localhost:8001/app/test SetPath SetPath=/app/{path} 通过模板设置路径,转发的规则时会在路径前增加app,{path}表示原请求路径 RedirectTo 重定向 RemoveRequestHeader 去掉某个请求头信息注意:当配置多个filter时,优先定义的会被调用,剩余的filter将不会生效
PrefixPath
- 对所有的请求路径添加前缀:
访问/hello的请求被发送到example.org/mypath/hello。
RedirectTo
- 重定向,配置包含重定向的返回码和地址:
RemoveRequestHeader
- 去掉某个请求头信息:
去掉请求头信息 X-Request-Foo
RemoveResponseHeader
- 去掉某个回执头信息:
RemoveRequestParameter
- 去掉某个请求参数信息:
RewritePath
- 改写路径:
/where/... 改成 test/...
使用代码改下路径
RouteLocatorBuilder.Builder builder = routeLocatorBuilder.routes(); builder.route("path_rote_at_guigu", r -> r.path("/guonei") .uri("news.baidu.com/guonei")) .route("csdn_route", r -> r.path("/csdn") .uri("blog.csdn.net")) .route("blog3_rewrite_filter", r -> r.path("/blog3/**") .filters(f -> f.rewritePath("/blog3/(?<segment>.*)", "/$\\{segment}")) .uri("blog.csdn.net")) .route("rewritepath_route", r -> r.path("/baidu/**") .filters(f -> f.rewritePath("/baidu/(?<segment>.*)", "/$\\{segment}")) .uri("www.baidu.com")) .build();SetPath
- 设置请求路径,与RewritePath类似。
如/red/blue的请求被转发到/blue。
SetRequestHeader
- 设置请求头信息。
SetStatus
- 设置回执状态码。
StripPrefix
- 跳过指定路径。
请求/name/blue/red会转发到/red。
RequestSize
- 请求大小。
超过5M的请求会返回413错误。
Default-filters
- 对所有请求添加过滤器。
参考: www.ityouknow.com/springcloud/2018/12/12/spring-cloud-gateway-start.html
www.likecs.com/show-50293.html
zhuanlan.zhihu.com/p/299608850?utm_source=wechat_session
juejin.cn/post/6844903965352525838
blog.csdn.net/weixin_38361347/article/details/114108368
www.zyiz.net/tech/detail-98256.html
www.cnblogs.com/crazymakercircle/p/11704077.html
blog.csdn.net/forezp/article/details/85057268

