SpringCloud Gateway是Spring Cloud的一个子项目,该项目是基于Spring5.0、SpringBoot2.0和Project Reactor等技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的API路由管理。spring
Spring Cloud Gateway具备以下特性:api
基于Spring Frameworke 五、Project Reactor和Spring Boot2.0构建cookie
可以匹配任何请求属性上的路由;架构
能够对路由指定Predicate和Filter;app
集成了Hystrix的断路器功能;ide
集成了Spring Cloud服务发现功能;微服务
易于编写Predicate和Filter;学习
请求限流功能;ui
支持路径重写;url
简答介绍一些上边的几个术语:
Filter:和zuul的过滤器在概念上相似,可使用它拦截和修改请求,而且对上游的响应进行二次处理。过滤器为org.springframework.cloud.gateway.filter.GatewayFilter类的实例。
Route:网关配置的基本组成模块,和Zuul的路由配置模块相似。一个Route模块由一个ID,一个目标URI,一组断言和一组过滤器定义。若是断言为真,则路由匹配,目标的URI会被访问。
Predicate:这是一个Java8的Predicate,可使用它来匹配来自HTTP请求的任何内容,例如headers或参数。断言的输入类型是一个ServerWebExchange。
须要在pom.xml中添加的依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId></dependency>
方式一: 使用yml配置
server: port: 8080spring: application: name: api-gateway cloud: gateway: routes: -id: api-gateway#路由的id uri: ${service-url.user-service}/user/{id}#匹配后路由的地址 predicates:#断言,路径相匹配的进行路由 -Path=/user/{id}
方式二: 使用Java Bean配置
@configurationpublic class GatewayConfig{ @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder){ return builder.routes() .route("path_route2", r -> r.path("/user/getByUsername") .uri("http://localhost:8201/user/getByUsername")) .build(); }}
客户端向Spring Cloud Gateway发出请求。若是网关处理程序映射肯定请求与路由匹配,则将其发送到网关Web处理程序。该处理程序经过特定于请求的过滤器链来运行请求。筛选器由虚线分隔的缘由是,筛选器能够在发送代理请求以前和以后运行逻辑。全部“前置”过滤器逻辑均被执行。而后发出代理请求。发出代理请求后,将运行“后”过滤器逻辑。
Spring Cloud Gateway将路由做为Spring WebFlux HandlerMapping基础架构的一部分进行匹配,Spring Cloud Gateway有不少内置的Route Predicate工厂,全部这些Predicate都与http请求的不一样属性匹配。多个Route Predicate工厂能够组合使用。
The After Route Predicate Factory
After Route Predicate有一个参数,一个datetime,该Route匹配在指定日期时间以后发生的请求。
spring: cloud: gateway: routes: - id: after_route uri: https://example.org predicates: - After=2020-03-20T15:00:00+08:00[Asia/Shanghai]
The Before Route Predicate Factory
匹配指定时间以前的请求
spring: cloud: gateway: routes: - id: before_route uri: https://example.org predicates: - Before=2020-03-20T15:00:00+08:00[Asia/Shanghai]
The Between Route Predicate Factory
匹配指定时间区间内的请求
spring: cloud: gateway: routes: - id: between_route uri: ${service-url.user-service} predicates: - Between=2020-03-20T15:00:00+08:00[Asia/Shanghai], 2020-03-20T16:00:00+08:00[Asia/Shanghai]
The Cookie Route Predicate Factory
匹配带有指定cookie的请求
spring: cloud: gateway: routes: - id: cookie_route uri: ${service-url.user-service} predicates: - Cookie=username,guli #只有cookie为username=guli的请求能够匹配
The Header Route Predicate Factory
匹配带有指定请求头的请求
spring: cloud: gateway: routes: - id: header_route uri: ${service-url.user-service} predicates: - Header=X-Request-Id, \d+
The Host Route Predicate Factory
匹配带有指定Host的请求
spring: cloud: gateway: routes: - id: host_route uri: ${service-url.user-service} predicates: - Host=**.guli.com #只有请求头带有Host:www.guli.com的请求能够匹配该路由
The Method Route Predicate Factory
匹配指定方法的请求
spring: cloud: gateway: routes: - id: method_route uri: ${service-url.user-service} predicates: - Method=GET,POST #该路由匹配GET、POST请求
The Path Route Predicate Factory
匹配指定路径的请求
spring: cloud: gateway: routes: - id: method_route uri: ${service-url.user-service} predicates: - Path=/user/{id} #只能匹配/user/id路径的请求
The Query Route Predicate Factory
匹配带有指定查询参数的请求
spring: cloud: gateway: routes: - id: query_route uri: ${service-url.user-service}/user/getByUsername predicates: - Query=username #只有发送带有username查询参数的请求能够匹配
The RemoteAddr Route Predicate Factory
匹配指定远程地址发送的请求
spring: cloud: gateway: routes: - id: remoteaddr_route uri: ${service-url.user-service} predicates: - RemoteAddr=192.168.1.1/24 #这个远程地址根据实际状况填写
The Weight Route Predicate Factory
使用权重来路由相应的请求
spring: cloud: gateway: routes: - id: weight_high uri: http://localhost:8001 predicates: - Weight=group1, 8 #80%的请求路由到localhost:8001 - id: weight_low uri: http://localhost:8002 predicates: - Weight=group1, 2 #20%的请求路由到localhost:8002
路由过滤器容许以某种方式修改传入的HTTP请求或传出的HTTP响应。路由过滤器适用于特定路由。Spring Cloud Gateway包括许多内置的GatewayFilter工厂。下面介绍几种经常使用的路由过滤器的用法:
The AddRequestHander GatewayFilter Factory
spring: cloud: gateway: routes: - id: add_request_header_route uri: https:localhost:8001 predicates: - Path=/red/{segment} filters: - AddRequestHeader=X-Request-Red, Blue-{segment}
The AddRequestParameter GatewayFilter Factory
这是给请求添加参数的过滤器
spring: cloud: gateway: routes: - id: add_request_parameter_route uri: http://localhost:8001 filters: - AddRequestParameter=username, guli predicates: - Method=GET
这个配置就是给Get请求添加username=guli的请求参数,发送http://localhost:8001/user/getByUsername
,就至关于发送了http://localhost:8001/user/getByUsername?username=guli
The Hystrix GatewayFilter Factory
Hystrix过滤器容许将断路器功能添加在网关路由中,使服务免于受到级联故障的影响,而且提供服务降级功能。
须要在项目中引入Hystrix的相关依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency>
服务降级的fallback:
@RestControllerpublic class fallBack(){ @GetMapping("/fallback") public Object fallback(){ Map<String,Object> result = new HashMap<>(); result.put("data",null); result.put("message","Request fallback!"); result.put("code",500); return result; }}
添加相关配置:
spring: cloud: gateway: routes: - id: hystrix_route uri: http://localhost:8001 predicates: - Method=GET filters: - name: Hystrix args: name: fallbackcmd fallbackUri: forward:/fallback
The stripPrefix Gateway Factory
对指定数量的路由前缀进行去除的过滤器、
spring: cloud: gateway: routes: - id: nameRoot uri: https://localhost:8001 predicates: - Path=/user-service/** filters: - StripPrefix=2 #对/user-service开头的请求的路径去除两位
The PrefixPath GatewayFilter Factory
对原有路径进行增长操做
spring: cloud: gateway: routes: - id: prefixpath_route uri: https://localhost:8001 filters: - PrefixPath=/mypath #对全部请求加上/mypath的路径前缀
The Retry GatewayFilter Factory
根据路由请求返回的HTTP状态码来肯定是否进行重试。
spring: cloud: gateway: routes: - id: retry_test uri: http://localhost:8001 predicates: - Method=GET filters: - name: Retry args: retries: 3 #须要进行重试的次数 statuses: BAD_GATEWAY #返回哪一个状态码时进行重试 backoff: firstBackoff: 10ms maxBackoff: 50ms factor: 2 basedOnPreviousValue: false
其余还有不少过滤器官网都有介绍,能够学习