https://github.com/zq2599/blog_demos
内容:所有原创文章分类汇总及配套源码,涉及Java、Docker、Kubernetes、DevOPS等;
After表示路由在指定时间之后才生效
配置文件,注意时间字符串的格式,+08:00表示东八区:
spring: cloud: gateway: routes: - id: after_route uri: http://127.0.0.1:8082 predicates: - After=2021-08-16T07:36:00.000+08:00[Asia/Shanghai]
[ { "id": "after_route", "uri": "http://127.0.0.1:8082", "predicates":[ { "name": "After", "args": { "datetime": "2021-08-16T07:36:00.000+08:00[Asia/Shanghai]" } } ] } ]
Before表示路由在指定时间之前才生效
配置文件:
spring: cloud: gateway: routes: - id: before_route uri: http://127.0.0.1:8082 predicates: - Before=2021-08-16T07:36:00.000+08:00[Asia/Shanghai]
[ { "id": "before_route", "uri": "http://127.0.0.1:8082", "predicates":[ { "name": "Before", "args": { "datetime": "2021-08-16T07:36:00.000+08:00[Asia/Shanghai]" } } ] } ]
Between表示路由在指定时间段之内才生效,既然是时间段就是两个参数,注意它们的写法
配置文件:
spring: application: name: hello-gateway cloud: gateway: routes: - id: between_route uri: http://127.0.0.1:8082 predicates: - Between=2021-08-16T07:36:00.000+08:00[Asia/Shanghai], 2021-08-16T08:15:00.000+08:00[Asia/Shanghai]
[ { "id": "path_route_addr", "uri": "http://127.0.0.1:8082", "predicates":[ { "name": "Between", "args": { "datetime1": "2021-08-16T07:36:00.000+08:00[Asia/Shanghai]", "datetime2": "2021-08-16T08:18:00.000+08:00[Asia/Shanghai]" } } ] } ]
Cookie表示cookie存在指定名称,并且对应的值符合指定正则表达式,才算匹配成功
配置文件:
spring: cloud: gateway: routes: - id: cookie_route uri: https://example.org predicates: - Cookie=chocolate, ch.p
[ { "id": "cookie_route", "uri": "http://127.0.0.1:8082", "predicates":[ { "name": "Cookie", "args": { "name": "chocolate", "regexp": "ch.p" } } ] } ]
Header表示header存在指定名称,并且对应的值符合指定正则表达式,才算匹配成功
下面的例子要求header中必须存在X-Request-Id,并且值一定要是数字
配置文件:
spring: cloud: gateway: routes: - id: header_route uri: https://example.org predicates: - Header=X-Request-Id, \d+
[ { "id": "header_route", "uri": "http://127.0.0.1:8082", "predicates":[ { "name": "Header", "args": { "header": "X-Request-Id", "regexp": "\\d+" } } ] } ]
Host表示请求的host要和指定的字符串匹配,并且对应的值符合指定正则表达式,才算匹配成功,可以同时指定多个host匹配表达式,下面的例子给了两个,其中第一个指定了端口:
配置文件:
spring: cloud: gateway: routes: - id: host_route uri: http://127.0.0.1:8082 predicates: - Host=test.com:8081,**.anotherhost.org
[ { "id": "header_route", "uri": "http://127.0.0.1:8082", "predicates":[ { "name": "Host", "args": { "regex": "test.com:8086" } } ] } ]
Method非常好理解,匹配指定的方法类型(可以有多个)
配置文件:
spring: cloud: gateway: routes: - id: method_route uri: http://127.0.0.1:8082 predicates: - Method=GET,POST
[ { "id": "method_route", "uri": "http://127.0.0.1:8082", "predicates":[ { "name": "Method", "args": { "methods": "GET" } } ] } ]
Path很常用,匹配指定的方法类型(可以有多个)
配置文件,注意{segment},表示该位置的真实值可以被提取出来,在filter中可以使用,这在后续的filter文章中会有说明:
spring: cloud: gateway: routes: - id: path_route uri: http://127.0.0.1:8082 predicates: - Path=/hello/{segment},/lbtest/{segment}
[ { "id": "path_route", "uri": "http://127.0.0.1:8082", "predicates":[ { "name": "Path", "args": { "pattern": "/hello/{segment}" } } ] } ]
Query匹配的是请求中是否带有指定的参数,也能要求该参数等于指定的值(正则表达式)才被匹配上
配置文件,只要带有名为name的请求参数就被匹配:
spring: cloud: gateway: routes: - id: query_route uri: http://127.0.0.1:8082 predicates: - Query=name
spring: cloud: gateway: routes: - id: query_route uri: http://127.0.0.1:8082 predicates: - Query=name,aaa.
[ { "id": "query_route", "uri": "http://127.0.0.1:8082", "predicates":[ { "name": "Query", "args": { "param": "name", "regexp": "aaa." } } ] } ]
RemoteAddr很好理解,匹配的指定来源的请求
配置文件:
spring: cloud: gateway: routes: - id: remoteaddr_route uri: http://127.0.0.1:8082 predicates: - RemoteAddr=192.168.50.134/24
[ { "id": "remoteaddr_route", "uri": "http://127.0.0.1:8082", "predicates":[ { "name": "RemoteAddr", "args": { "sources": "192.168.50.134/24" } } ] } ]
Weight顾名思义,按照权重将请求分发到不同位置
配置文件:
spring: cloud: gateway: routes: - id: weight_high uri: http://192.168.50.134:8082 predicates: - Weight=group1, 8 - id: weight_low uri: http://192.168.50.135:8082 predicates: - Weight=group1, 2
我是欣宸,期待与您一同畅游Java世界…
https://github.com/zq2599/blog_demos