了解Gateway的配置才可以理解使用Gateway可以做什么事情,才能更好地应用在产品开发中。
Predicates主要起的作用是:配置路由匹配请求的规则
配置对于请求路径的匹配规则
- Path = /aa/**,/bb/**
{"name":"Path","args":{"pattern":"/aa/**","pattern1":"/bb/**"}}
配置对Cookie中值的匹配,第一个为key,第二个为value。下例匹配cookie设置chocolate:ch.p的请求
- Cookie = chocolate,ch.p
{"name":"Cookie","args":{"_genkey_0":"chocolate","_genkey_1":"ch.p"}}
匹配Http请求中设置的内容,http-header设置X-Request-Id:\d+可以匹配,第二个参数第二个参数是正则表达式
- Header = X-Request-Id,\d+
{"name":"Header","args":{"_genkey_0":"X-Request-Id","_genkey_1":"\d+"}}
匹配Http请求Host,匹配所有host为**.somehost.com的请求
- Host = **.somehost.com
{"name":"Host","args":{"_genkey_0":"**.somehost.com"}}
匹配Http请求头
- Method = GET
{"name":"Method","args":{"_genkey_0":"GET"}}
匹配Http请求中的查询参数,请求中携带param1=value的请求可以匹配
- Query = param1,value
{"name":"Query","args":{"_genkey_0":"param1","_genkey_1":"value"}}
匹配请求中的RemoteAddr
- RemoteAddr = 192.168.1.1/24
{"name":"RemoteAddr","args":{"_genkey_0":"192.168.1.1/24"}}
设置时间之后可以访问
- After = 2017-01-20T17:42:47.789-07:00[America/Denver]
{"name":"After","args":{"_genkey_0":"2017-01-20T17:42:47.789-07:00[America/Denver]"}}
设置时间之前可以访问
- Before = 2017-01-20T17:42:47.789-07:00[America/Denver]
{"name":"Before","args":{"_genkey_0":"2017-01-20T17:42:47.789-07:00[America/Denver]"}}
设置时间段内可以访问
- Between = 2017-01-20T17:42:47.789-07:00[America/Denver],2017-01-21T17:42:47.789-07:00[America/Denver]
{"name":"Between","args":{"_genkey_0":"2017-01-20T17:42:47.789-07:00[America/Denver]","_genkey_1":"2017-01-21T17:42:47.789-07:00[America/Denver]"}}
至少两组以上路由可以配置权重路由,配置后会根据权重随机访问几个路由
- Weight = service1,80
{"name":"Weight","args":{"_genkey_0":"service1","_genkey_1":"80"}}
- RewritePath = /path/(?>.*), /$\{segment}
{"name":"RewritePath","args":{"_genkey_0":"/foo/(?.*)" ,"_genkey_1":"/$\\{segment}"}}
- AddRequestHeader = X-Request-Foo,Bar
{"name":"AddRequestHeader","args":{"_genkey_0":"X-Request-Foo","_genkey_1":"Bar"}}
- AddRequestParameter = foo,bar
{"name":"AddRequestParameter","args":{"_genkey_0":"foo","_genkey_1":"bar"}}
- AddResponseHeader = X-Request-Foo,Bar
{"name":"AddResponseHeader","args":{"_genkey_0":"X-Request-Foo","_genkey_1":"Bar"}}
请求路径/hello, 将会被替换为 /mypath/hello
- PrefixPath = /mypath
{"name":"PrefixPath","args":{"_genkey_0":"/mypath"}}
请求/name/bar/foo,去除掉前面两个前缀之后,最后转发到目标服务的路径为/foo
- StripPrefix = 2
{"name":"StripPrefix","args":{"_genkey_0":"2"}}
- PreserveHostHeader
{"name":"PreserveHostHeader","args":{}}
- RedirectTo = 302,http://acme.org
{"name":"RedirectTo","args":{"_genkey_0":"302","_genkey_1":"http://acme.org"}}
- name: Hystrix
args:
# 断路后跳转地址
name: fallbackcmd
fallbackUri: forward:/incaseoffailureusethis
{"name":"Hystrix","args":{"name":"fallbackcmd","fallbackUri":"forward:/incaseoffailureusethis"}}
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 10
redis-rate-limiter.burstCapacity: 20
{"name":"RequestRateLimiter","args":{"redis-rate-limiter.replenishRate":"10","redis-rate-limiter.burstCapacity":"20"}}
- RemoveRequestHeader = X-Request-Foo
{"name":"RemoveRequestHeader","args":{"_genkey_0":"X-Request-Foo"}}
- RemoveResponseHeader = X-Request-Foo
{"name":"RemoveResponseHeader","args":{"_genkey_0":"X-Request-Foo"}}
将请求 /42?user=ford&password=omg!what&flag=true, 改为 /42?user=ford&password=***&flag=true
- RewriteResponseHeader = X-Response-Foo,password=[^&]+,password=***
{"name":"RewriteResponseHeader","args":{"_genkey_0":"X-Response-Foo","_genkey_1":"password=[^&]+","_genkey_2":"password=***"}}
请求/foo/bar,在接下来的处理中被改为/bar
- SetPath =/{segment}
{"name":"SetPath","args":{"_genkey_0":"/{segment}"}}
在接下来的处理中修改响应头X-Response-Foo为Bar
- SetResponseHeader =X-Request-Foo,Bar
{"name":"SetResponseHeader","args":{"_genkey_0":"X-Response-Foo","_genkey_1":"Bar"}}
- name: SetStatus
args:
status: 401
{"name":"SetStatus","args":{"_genkey_0":"302"}}
- name: RequestSize
args:
maxSize: 5000000
{"name":"RequestSize","args":{"_genkey_0":"5000000"}}
- name: Retry
args:
retries: 3
statuses: BAD_GATEWAY