Gateway - Gateway 网关
一、如何引入SpringCloud Gateway
1.1 Gateway简介
每一秒处理请求个数对比--Gateway vs Zuul 1.0 vs Linkerd
1.2 引入Gateway
To include Spring Cloud Gateway in your project use the starter with group org.springframework.cloud
and artifact id spring-cloud-starter-gateway
. See the Spring Cloud Project page for details on setting up your build system with the current Spring Cloud Release Train.
If you include the starter, but, for some reason, you do not want the gateway to be enabled, set spring.cloud.gateway.enabled=false
.
![]() |
Important |
---|---|
Spring Cloud Gateway is built upon Spring Boot 2.x, Spring WebFlux, and Project Reactor. As a consequence many of the familiar synchronous libraries (Spring Data and Spring Security, for example) and patterns you may not apply when using Spring Cloud Gateway. If you are unfamiliar with these projects we suggest you begin by reading their documentation to familiarize yourself with some of the new concepts before working with Spring Cloud Gateway. |
![]() |
Important |
---|---|
Spring Cloud Gateway requires the Netty runtime provided by Spring Boot and Spring Webflux. It does not work in a traditional Servlet Container or built as a WAR. |
二、Gateway术语概念 (路由,断言,过滤器)
核心流程:当请求到达网关Gateway,网关利用断言Predicate,判定这次请求是否符合某个路由规则Route(id + uri + predicates + filters),符合则根据该路由规则把请求路由到指定地方,期间需要经过一系列过滤器Filter进行过滤。
2.1 Predicate 断言
- Query 请求参数断言
共有两种参数模式
The Query Route Predicate Factory takes two parameters: a required param
and an optional regexp
.
application.yml.
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=baz
只包含param的参数时,只要request里有该param就行,至于param的值是什么无所谓。例如 127.0.0.1:80?baz=take , 127.0.0.1:80?baz=test
This route would match if the request contained a baz
query parameter.
application.yml.
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=foo, ba.
有两个参数时,就是对param及其值均有要求。其值可以用正则表达式来匹配
This route would match if the request contained a foo
query parameter whose value matched the ba.
regexp, so bar
and baz
would match.
- Path 请求路径断言
The Path Route Predicate Factory takes two parameter: a list of Spring PathMatcher
patterns and an optional flag to matchOptionalTrailingSeparator
.
application.yml.
spring:
cloud:
gateway:
routes:
- id: host_route
uri: https://example.org
predicates:
- Path=/foo/{segment},/bar/{segment}
This route would match if the request path was, for example: /foo/1
or /foo/bar
or /bar/baz
.
2.2 Gateway Filter 网关过滤器
- RewritePath 请求路径重写
会把127.0.0.1:80/api/test重写为127.0.0.1:80/renren-fast/test
- id: admin_route uri: lb://renren-fast predicates: - Path=/api/** filters: - RewritePath=/api/(?<segment>.*), /renren-fast/$\{segment}
2.3 Global Filter 全局过滤器
lb:// 负载均衡
If the url has a lb
scheme (ie lb://myservice
), it will use the Spring Cloud LoadBalancerClient
to resolve the name (myservice
in the previous example) to an actual host and port and replace the URI in the same attribute.
- id: admin_route uri: lb://renren-fast predicates: - Path=/api/** filters: - RewritePath=/api/(?<segment>.*), /renren-fast/$\{segment}
GateWay官方文档
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2020-08-26 Java基础 - String 的 replace 和 replaceAll 的区别?
2020-08-26 Java基础 - try 和 finally 里都有return,哪个为准?
2020-08-26 Java基础 - finalize()一定执行吗?GC执行finalize()流程
2020-08-26 Java基础 - class VS struct
2020-08-26 Java 基础 - hashCode()在什么场景下需要重写?