gateway-predicate模块的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cntaiping.tpa</groupId>
<artifactId>gateway-predicate</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>gateway-predicate</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.cntaiping.tpa</groupId>
<artifactId>gateway</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
</project>
After Route Predicate Factory,可配置一个UTC时间格式的时间参数,当请求进来的当前时间在路由断言工厂之后会成功匹配,才交给 router去处理。否则则报错,不通过路由。
server:
port: 7012
spring:
profiles:
active: query_route
---
spring:
cloud:
gateway:
routes:
- id: after_route
uri: http://httpbin.org:80/get
predicates:
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
# - After=2018-04-1T17:42:47.789-07:00[Asia/Shanghai]
profiles: after_route
package com.cntaiping.tpa.gatewaypredicate;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GatewayPredicateApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayPredicateApplication.class, args);
}
}
配置spring.profiles.active:after_route指定了程序的spring的启动文件为after_route文件。
- After=2020-01-20T17:42:47.789-07:00[America/Denver]
Between路由断言工厂接受两个参数,datetime1和datetime2。该谓词匹配发生在datetime1与datetime2之间请求。
---
spring:
cloud:
gateway:
routes:
- id: between_route
uri: http://httpbin.org/get
predicates:
- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2019-01-21T17:42:47.789-07:00[America/Denver]
profiles: between_route
- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2019-01-21T17:42:47.789-07:00[America/Denver]
- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2020-01-21T17:42:47.789-07:00[America/Denver]
Header由断言工厂接受两个参数,头名称和正则表达式。这个谓词与给定名称的头匹配,该值与正则表达式匹配。 Header Route Predicate Factory需要2个参数,一个是header名,另外一个header值,该值可以是一个正则表达式。当此断言匹配了请求的header名和值时,断言通过,进入到router的规则中去。
---
spring:
cloud:
gateway:
routes:
- id: header_route
uri: http://httpbin.org/get
predicates:
- Header=X-Request-Id, \d+
profiles: header_route
当请求的Header中有X-Request-Id的header名,且header值为数字时,请求会被路由到配置的 uri.
curl -H 'X-Request-Id:1' localhost:7012
如果在请求中没有带上X-Request-Id的header名,并且值不为数字时,请求就会报404,路由没有被正确转发。
Cookie路由断言工厂接受两个参数:一个时cookie名字,另一个时值,可以为正则表达式。它用于匹配请求中,带有该名称的cookie和cookie匹配正则表达式的请求。
---
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: http://httpbin.org:80/get
predicates:
- Cookie=flag, ch.p
profiles: cookie_route
curl -H Cookie:flag=chip localhost:7012
Host路由断言工厂接受一个参数:需要一个参数即hostname。它可以使用. * 等去匹配host。这个参数会匹配请求头中的host的值,一致,则请求正确转发。
---
spring:
cloud:
gateway:
routes:
- id: host_route
uri: http://httpbin.org:80/get
predicates:
- Host=**.hadron.cn
profiles: host_route
请求头中含有Host为hadron.cn的请求将会被路由转发转发到配置的uri。
curl -H Host:www.hadron.cn localhost:7012
Method路由断言工厂接受一个参数:HTTP方法来匹配。
---
spring:
cloud:
gateway:
routes:
- id: method_route
uri: http://httpbin.org:80/post
predicates:
- Method=POST
profiles: method_route
curl -XPOST localhost:7012
curl localhost:7012
Path路由断言工厂接受一个参数:采用Spring PathMatcher 模式。
---
spring:
cloud:
gateway:
routes:
- id: path_route
uri: http://httpbin.org
predicates:
- Path=/foo/{segment}
profiles: path_route
请求路径满足/foo/{segment}的请求将会匹配并被路由,比如/foo/1 、/foo/bar的请求,将会命中匹配,并成功转发。
请注意,curl http://localhost:7012/foo/1
已经请求成功(结果含有HTML标签),返回结果是服务器没有这个值。而curl http://localhost:7012/fee/1
请求拒绝。
Query路由断言工厂接受两个参数:一个必需的参数(param)和一个可选的表达式(regexp)。
---
spring:
cloud:
gateway:
routes:
- id: query_route
uri: http://httpbin.org:80/get
predicates:
- Query=flag
profiles: query_route
这个路由将匹配如果请求包含一个flag查询参数。
RemoteAddr路由断言工厂支持通过设置某个 ip 区间号段的请求才会路由,接受 cidr 符号(IPv4 或 IPv6 )字符串的列表(最小大小 1)
---
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: http://httpbin.org:80/get
predicates:
- RemoteAddr=10.17.36.1/24
profiles: remoteaddr_route
这个路由将匹配请求的远程地址为10.17.36网段,例如,我的本地机地址是10.17.36.27。
---
spring:
cloud:
gateway:
routes:
- id: group_route
uri: http://httpbin.org
predicates:
- Host=**.httpbin.com
- Header=X-Request-Id, \d+
- Cookie=name, chengyuqiang
- After=2019-06-20T00:00:00+08:00[Asia/Shanghai]
profiles: group_route
curl localhost:7012 -H "X-Request-Id:520" -H "Host:www.httpbin.com" --cookie "name=chengyuqiang"
server:
port: 7012
---
spring:
cloud:
gateway:
routes:
- id: after_route
uri: http://httpbin.org/get
predicates:
- After=2020-01-20T17:42:47.789-07:00[America/Denver]
#- After=2018-04-1T17:42:47.789-07:00[Asia/Shanghai]
profiles: after_route
---
spring:
cloud:
gateway:
routes:
- id: between_route
uri: http://httpbin.org/get
predicates:
- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2020-01-21T17:42:47.789-07:00[America/Denver]
profiles: between_route
---
spring:
cloud:
gateway:
routes:
- id: header_route
uri: http://httpbin.org/get
predicates:
- Header=X-Request-Id, \d+
profiles: header_route
---
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: http://httpbin.org:80/get
predicates:
- Cookie=flag, ch.p
profiles: cookie_route
---
spring:
cloud:
gateway:
routes:
- id: host_route
uri: http://httpbin.org:80/get
predicates:
- Host=**.hadron.cn
profiles: host_route
---
spring:
cloud:
gateway:
routes:
- id: method_route
uri: http://httpbin.org:80/get
predicates:
- Method=get
profiles: method_route
---
spring:
cloud:
gateway:
routes:
- id: path_route
uri: http://httpbin.org
predicates:
- Path=/foo/{segment}
profiles: path_route
---
spring:
cloud:
gateway:
routes:
- id: query_route
uri: http://httpbin.org:80/get
predicates:
- Query=flag
profiles: query_route
---
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: http://httpbin.org:80/get
predicates:
- RemoteAddr=10.17.36.1/24
profiles: remoteaddr_route
---
spring:
cloud:
gateway:
routes:
- id: group_route
uri: http://httpbin.org
predicates:
- Host=**.httpbin.com
- Header=X-Request-Id, \d+
- Cookie=name, chengyuqiang
- After=2019-06-20T00:00:00+08:00[Asia/Shanghai]
profiles: group_route