Spring-cloud-gateway 软件开发定制路由配置方式及匹配规则
文章目录
1、路由配置⽅式
路由是⽹软件开发定制关配置的基本组成模块,和Zuul软件开发定制的路由配置模块类似。⼀个Route模块由⼀个 ID,⼀个⽬标 URI,⼀组断⾔和⼀软件开发定制组过滤器定义。如果断⾔为真,软件开发定制则路由匹配,⽬标URI会被访问。
- 1
1.1 软件开发定制基础路由配置⽅式
软件开发定制如果请求的⽬标地址,是单个的URI资源路径,配置⽂软件开发定制件实例如下:
spring: application: name: api-gateway cloud: gateway: routes: - id: service1 uri: https://blog.csdn.net predicates: - Path=/csdn
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
软件开发定制各字段含义如下。
-
id:我们⾃软件开发定制定义的路由 ID,保持唯⼀
-
uri:⽬软件开发定制标服务地址
-
predicates:路由条件, 接受⼀个输⼊参数,返回⼀软件开发定制个布尔值结果。该接⼝软件开发定制包含多种默
认⽅法来将 Predicate 软件开发定制组合成其他复杂的逻辑(⽐如:与,或,⾮)。
上⾯这段配置的意思是,配置了⼀个 id 为 url-proxy-1的URI代理规则,路由的规则为,当访问地址
http://localhost:8080/csdn/1.jsp时,会路由到上游地址https://blog.csdn.net/1.jsp。
1.2 基于代码的路由配置⽅式
转发功能同样可以通过代码来实现,我们可以在启动类 GateWayApplication 中添加⽅法 customRouteLocator() 来定制转发规则。
@SpringBootApplication@EnableDiscoveryClientpublic 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("https://blog.csdn.net")).build(); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
1.3 和注册中⼼相结合的路由配置⽅式
在uri的schema协议部分为⾃定义的lb:类型,表示从微服务注册中⼼(如Eureka)订阅服 务,并且通过负载均衡进⾏服务的路由。代码如下。
server: port: 9005spring: application: name: api-gateway cloud: gateway: routes: - id: service1 uri: https://blog.csdn.net predicates: - Path=/csdn - id: service2 # uri: http://127.0.0.1:9001 uri: lb://cloud-payment-service predicates: - Path=/payment/**eureka: client: service-url: defaultZone: http://127.0.0.1:9004/eureka
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
注册中⼼相结合的路由配置⽅式,与单个URI的路由配置,区别其实很⼩,仅仅在于URI的 schema协议不同。单个URI的地址的schema协议,⼀般为http或者https协议。启动多个⽀付微 服务,会发现端⼝9000,9001轮流出现。
2、路由匹配规则
Spring Cloud Gateway的主要功能之⼀是转发请求,转发规则的定义主要包含三个部分,如表所示。
Route(路由) | 路由是⽹关的基本单元,由ID、URI、⼀组Predicate、⼀组Filter组成,根据Predicate进⾏匹配转发。 |
Predicate(谓语、断⾔) | 路由转发的判断条件,⽬前SpringCloud Gateway⽀持多种⽅式,常⻅如:Path、Query、Method、Header等,写法必须遵循 key=vlue的形式 |
Filter(过滤器) | 过滤器是路由转发请求时所经过的过滤逻辑,可⽤于修改请求、响应内容 |
2.1Predicate 断⾔条件
说⽩了 Predicate 就是为了实现⼀组匹配规则,⽅便让请求过来找到对应的 Route 进⾏处理,接下来我们接下 Spring Cloud GateWay 内置⼏种 Predicate 的使⽤。转发规则(predicates),假设 转发uri都设定为http://localhost:9001,常⻅Predicate,如表所示
规则 | 实例 | 说明 |
---|---|---|
Path | - Path=/gate/,/rule/ | 当请求的路径为gate、rule开头的时,转发到 http://localhost:9001服务器上 |
Before | - Before=2017-01-20T17:42:47.789-07:00[America/Denver] | 在某个时间之前的请求才会被转发到 http://localhost:9001 服务器上 |
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的时候直接转发到 http://localhost:9001服务器上 |
Method | - Method=GET | 只有GET⽅法才会匹配转发请求,还可以限定POST、PUT等 |
2.1.1 通过请求参数匹配
Query Route Predicate ⽀持传⼊两个参数,⼀个是属性名⼀个为属性值,属性值可以是正则表达 式
spring: cloud: gateway: routes: - id: service3 uri: https://www.baidu.com order: 0 predicates: - Query=smile
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
这样配置,只要请求中包含 smile 属性的参数即可匹配路由。使⽤ curl 测试,命令⾏输⼊:curl localhost:9005?smile=x&id=2,经过测试发现只要请求汇总带有 smile 参数即会匹配路由,不带 smile 参数则不会匹配。 还可以将 Query 的值以键值对的⽅式进⾏配置,这样在请求过来时会对属性值和正则进⾏匹配,匹配上才会⾛路由。
spring: cloud: gateway: routes: - id: service3 uri: https://www.baidu.com order: 0 predicates: - Query=keep, pu.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
这样只要当请求中包含 keep 属性并且参数值是以 pu 开头的⻓度为三位的字符串才会进⾏匹配和路由。 使⽤ curl 测试,命令⾏输⼊:curl localhost:8080?keep=pub,测试可以返回⻚⾯代码,将 keep 的属 性值改为 pubx 再次访问就会报 404,证明路由需要匹配正则表达式才会进⾏路由。
2.1.2 通过匹配
Header Route Predicate 和 Query Route Predicate ⼀样,也是接收 2 个参数,⼀个 header 中属性名称和⼀个正则表达式,这个属性值和正则表达式匹配则执⾏。
spring: cloud: gateway: routes: - id: service4 uri: https://www.baidu.com order: 0 predicates: - Header=X-Request-Id, \d+
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
使⽤ curl 测试,命令⾏输⼊:curl http://localhost:9005 -H “X-Request-Id:88”,则返回⻚⾯代 码证明匹配成功。将参数-H "X-Request-Id:88"改为-H “X-Request-Id:spring”,再次执⾏时返回 404证明没有匹配。
2.1.3 通过Cookie匹配
Cookie Route Predicate 可以接收两个参数,⼀个是 Cookie name ,⼀个是正则表达式,路由规则会通过获取对应的 Cookie name 值和正则表达式去匹配,如果匹配上就会执⾏路由,如果没有匹配 上则不执⾏。
spring: cloud: gateway: routes: - id: service5 uri: https://www.baidu.com predicates: - Cookie=sessionId, test
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
使⽤ curl 测试,命令⾏输⼊,curl http://localhost:9005 --cookie “sessionId=test”,则会返回⻚⾯代码,如果去掉–cookie “sessionId=test”,后台汇报 404 错误。
2.1.4 通过Host匹配
Host Route Predicate 接收⼀组参数,⼀组匹配的域名列表,这个模板是⼀个 ant 分隔的模板, ⽤.号作为分隔符。它通过参数中的主机地址作为匹配规则。
spring: cloud: gateway: routes: - id: service6 uri: https://www.baidu.com predicates: - Host=**.baidu.com
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
使⽤ curl 测试,命令⾏输⼊,curl http://localhost:9005 -H "Host: www.baidu.com"或者curl http://localhost:8080 -H “Host: md.baidu.com”,经测试以上两种 host 均可匹配到 host_route 路由,去掉 host 参数则会报 404 错误。
2.1.5 通过请求⽅式匹配
spring: cloud: gateway: routes: - id: service7 uri: https://www.baidu.com predicates: - Method=PUT
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
使⽤ curl 测试,命令⾏输⼊,curl -X PUT http://localhost:9005,测试返回⻚⾯代码,证明匹配 到路由,以其他⽅式,返回 404 没有找到,证明没有匹配上路由
2.1.6 通过请求路径匹配
Path RoutePredicate 接收⼀个匹配路径的参数来判断是否路由。
spring: cloud: gateway: routes: - id: service8 uri: https://www.baidu.com predicates: - Path=/payment/{segment}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
如果请求路径符合要求,则此路由将匹配, curl 测试,命令⾏输⼊,curl http://localhost:9005/payment/1, 可以正常获取到⻚⾯返回值,curl http://localhost:9005/payment2/1,报404,证明路由是通过指定路由来匹配
2.1.7 组合匹配
spring: cloud: gateway: routes: - id: service9 uri: http://127.0.0.1:9001 predicates: - Host=**.foo.org - Path=/headers - Method=GET - Header=X-Request-Id, \d+ - Query=foo, ba. - Query=baz - Cookie=chocolate, ch.p
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
各种 Predicates 同时存在于同⼀个路由时,请求必须同时满⾜所有的条件才被这个路由匹配。 ⼀个请求满⾜多个路由的断⾔条件时,请求只会被⾸个成功匹配的路由转发
2.2 过滤器规则
列举⼏个过滤器,如表所示。
过滤规则 | 实例 | 说明 |
---|---|---|
PrefixPath | - PrefixPath=/app | 在请求路径前加上app |
RewritePath | - RewritePath=/test, /app/test | 访问localhost:9022/test,请求会转发到 localhost:8001/app/test |
SetPath | SetPath=/app/{path} | 通过模板设置路径,转发的规则时会在路径前增加 app,{path}表示原请求路径 |
RedirectTo | 重定向 | |
RemoveRequestHea | 去掉某个请求头信息\1. PrefixPath |
2.2.1 PrefixPath
对所有的请求路径添加前缀
spring: cloud: gateway: routes: - id: service10 uri: http://127.0.0.1:9001 predicates: - Path=/payment/{segment} filters: - PrefixPath=/payment
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
访问/123请求被发送到http://127.0.0.1:9001/payment/123。
2.2.2 StripPrefix
跳过指定的路径
spring: cloud: gateway: routes: - id: service11 uri: http://127.0.0.1:9001 predicates: - Path=/payment/{segment} filters: - StripPrefix=1 - PrefixPath=/payment
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
此时访问http://localhost:9005/api/123,⾸先StripPrefix过滤器去掉⼀个/api,然后PrefixPath 过滤器加上⼀个/payment,能够正确访问到微服务。
2.2.3 RewritePath
spring: cloud: gateway: routes: - id: service12 uri: http://127.0.0.1:9001 predicates: - Path=/payment/{segment} filters: - RewritePath=/api/(?<segment>.*), /$\{segment}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
请求http://localhost:9005/api/payment/123路径,RewritePath过滤器将路径重写为 http://localhost:9005/payment/123,能够正确访问微服务。
2.2.4 SetPath
SetPath和Rewrite类似,代码如下
spring: cloud: gateway: routes: - id: service13 uri: http://127.0.0.1:9001 predicates: - Path=/payment/{segment} filters: - SetPath=/payment/{segment}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
请求http://localhost:9005/api/payment/123路径,SetPath过滤器将路径设置为 http://localhost:9005/payment/123,能够正确访问微服务。
2.2.5 RemoveRequestHeader
去掉某个请求头信息。
spring: cloud: gateway: routes: - id: removerequestheader_route uri: https://example.org filters: - RemoveRequestHeader=X-Request-Foo 12345678
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
去掉请求头X-Request-Foo
2.2.6 RemoveResponseHeader
去掉某个回执头信息
spring: cloud: gateway: routes: - id: removerequestparameter_route uri: https://example.org filters: - RemoveRequestParameter=red
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2.2.7 SetRequestHeader
设置请求头信息
spring: cloud: gateway: routes: - id: setrequestheader_route uri: https://example.org filters: - SetRequestHeader=X-Request-Red, Blue
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2.2.8 default-filters
对所有的请求添加过滤器
spring: cloud: gateway: routes: - id: service14 uri: http://127.0.0.1:9001 predicates: - Path=/9001/{segment} - id: service15 uri: http://127.0.0.1:9000 predicates: - Path=/9000/{segment} default-filters: - StripPrefix=1 - PrefixPath=/payment
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
对所有的请求添加过滤器
spring: cloud: gateway: routes: - id: service14 uri: http://127.0.0.1:9001 predicates: - Path=/9001/{segment} - id: service15 uri: http://127.0.0.1:9000 predicates: - Path=/9000/{segment} default-filters: - StripPrefix=1 - PrefixPath=/payment
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15