maven上,只需引入以下依赖便可:java
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>
引入了依赖默认即开启gateway了,若是暂时不想使用这个功能,这能够配置spring.cloud.gateway.enabled=false
便可。web
Spring Cloud Gateway使用的是Spring Boot和Spring Webflux提供的Netty底层环境,不能和传统的Servlet容器一块儿使用,也不能打包成一个WAR包。正则表达式
当客户端发送请求到Spring Cloud Gateway,Gateway Handler Mapping会匹配Route映射分发到Gateway Web Handler。handler会将请求通过一系列的filter处理,代理请求前,会执行左右的"pre" filter逻辑,代理请求后,会执行全部"post" filter逻辑。spring
Spring Cloud Gateway是使用Spring WebFlux的HandlerMapping做为匹配路由底层实现,自己已自带不少Route Predicate Factories,分别匹配不一样的http请求属性,多个Route Predicate Factories也能够经过and
进行逻辑合并匹配。安全
After Route Predicate Factory使用的是时间做为匹配规则,只要当前时间大于设定时间,路由才会匹配请求。
application.yml:cookie
spring: cloud: gateway: routes: - id: after_route uri: http://www.google.com predicates: - After=2018-12-25T14:33:47.789+08:00
这个路由规则会在东8区的2018-12-25 14:33:47后,将请求都转跳到google。app
Before Route Predicate Factory也是使用时间做为匹配规则,只要当前时间小于设定时间,路由才会匹配请求。
application.yml:maven
spring: cloud: gateway: routes: - id: before_route uri: http://www.google.com predicates: - Before=2018-12-25T14:33:47.789+08:00
这个路由规则会在东8区的2018-12-25 14:33:47前,将请求都转跳到google。svg
Between Route Predicate Factory也是使用两个时间做为匹配规则,只要当前时间大于第一个设定时间,并小于第二个设定时间,路由才会匹配请求。post
application.yml:
spring: cloud: gateway: routes: - id: between_route uri: http://www.google.com predicates: - Between=2018-12-25T14:33:47.789+08:00, 2018-12-26T14:33:47.789+08:00
这个路由规则会在东8区的2018-12-25 14:33:47到2018-12-26 14:33:47之间,将请求都转跳到google。
Cookie Route Predicate Factory使用的是cookie名字和正则表达式的value做为两个输入参数,请求的cookie须要匹配cookie名和符合其中value的正则。
application.yml:
spring: cloud: gateway: routes: - id: cookie_route uri: http://www.google.com predicates: - Cookie=cookiename, cookievalue
路由匹配请求存在cookie名为cookiename,cookie内容匹配cookievalue的,将请求转发到google。
Header Route Predicate Factory,与Cookie Route Predicate Factory相似,也是两个参数,一个header的name,一个是正则匹配的value。
application.yml:
spring: cloud: gateway: routes: - id: header_route uri: http://www.google.com predicates: - Header=X-Request-Id, \d+
路由匹配存在名为X-Request-Id
,内容为数字的header的请求,将请求转发到google。
Host Route Predicate Factory使用的是host的列表做为参数,host使用Ant style匹配。
application.yml:
spring: cloud: gateway: routes: - id: host_route uri: http://www.google.com predicates: - Host=**.somehost.org,**.anotherhost.org
路由会匹配Host诸如:www.somehost.org
或 beta.somehost.org
或www.anotherhost.org
等请求。
Method Route Predicate Factory是经过HTTP的method来匹配路由。
application.yml:
spring: cloud: gateway: routes: - id: method_route uri: http://www.google.com predicates: - Method=GET
路由会匹配到全部GET方法的请求。
Path Route Predicate Factory使用的是path列表做为参数,使用Spring的PathMatcher
匹配path,能够设置可选变量。
application.yml:
spring: cloud: gateway: routes: - id: host_route uri: http://www.google.com predicates: - Path=/foo/{segment},/bar/{segment}
上面路由能够匹配诸如:/foo/1
或 /foo/bar
或 /bar/baz
等
其中的segment变量能够经过下面方式获取:
PathMatchInfo variables = exchange.getAttribute(URI_TEMPLATE_VARIABLES_ATTRIBUTE); Map<String, String> uriVariables = variables.getUriVariables(); String segment = uriVariables.get("segment");
在后续的GatewayFilter Factories就能够作对应的操做了。
Query Route Predicate Factory能够经过一个或两个参数来匹配路由,一个是查询的name,一个是查询的正则value。
application.yml:
spring: cloud: gateway: routes: - id: query_route uri: http://www.google.com predicates: - Query=baz
路由会匹配全部包含baz
查询参数的请求。
application.yml:
spring: cloud: gateway: routes: - id: query_route uri: http://www.google.com predicates: - Query=foo, ba.
路由会匹配全部包含baz
,而且baz
的内容为诸如:bar
或baz
等符合ba.
正则规则的请求。
RemoteAddr Route Predicate Factory经过无类别域间路由(IPv4 or IPv6)列表匹配路由。
application.yml:
spring: cloud: gateway: routes: - id: remoteaddr_route uri: http://www.google.com predicates: - RemoteAddr=192.168.1.1/24
上面路由就会匹配RemoteAddr诸如192.168.1.10
等请求。
RemoteAddr Route Predicate Factory默认状况下,使用的是请求的remote address。可是若是Spring Cloud Gateway是部署在其余的代理后面的,如Nginx,则Spring Cloud Gateway获取请求的remote address是其余代理的ip,而不是真实客户端的ip。
考虑到这种状况,你能够自定义获取remote address的处理器RemoteAddressResolver
。固然Spring Cloud Gateway也提供了基于X-Forwarded-For请求头的XForwardedRemoteAddressResolver
。
熟悉Http代理协议的,都知道X-Forwarded-For头信息作什么的,不熟悉的能够本身谷歌了解一下。
XForwardedRemoteAddressResolver
提供了两个静态方法获取它的实例:
XForwardedRemoteAddressResolver::trustAll
获得的RemoteAddressResolver
老是获取X-Forwarded-For的第一个ip地址做为remote address,这种方式就比较容易被假装的请求欺骗,模拟请求很容易经过设置初始的X-Forwarded-For
头信息,就能够欺骗到gateway。
XForwardedRemoteAddressResolver::maxTrustedIndex
获得的RemoteAddressResolver
则会在X-Forwarded-For
信息里面,从右到左选择信任最多maxTrustedIndex
个ip,由于X-Forwarded-For
是越往右是越接近gateway的代理机器ip,因此是越往右的ip,信任度是越高的。
那么若是前面只是挡了一层Nginx的话,若是只须要Nginx前面客户端的ip,则maxTrustedIndex
取1,就能够比较安全地获取真实客户端ip。
使用java的配置:
GatewayConfig.java:
RemoteAddressResolver resolver = XForwardedRemoteAddressResolver .maxTrustedIndex(1); ... .route("direct-route", r -> r.remoteAddr("10.1.1.1", "10.10.1.1/24") .uri("http://www.google.com") .route("proxied-route", r -> r.remoteAddr(resolver, "10.10.1.1", "10.10.1.1/24") .uri("http://www.google.com") )
这一章节讲的了几种Route Predicate Factory的使用及场景,下一章节讲GatewayFilter Factories的使用。
若是想查看其余spring cloud gateway的案例和使用,能够点击查看