Spring Cloud Gateway网关
Gatewaty是在Spring生态系统之上构建的API网关服务,基于Spring5,Spring Boot2和Project Reactor等技术。Gateway旨在提供一种简单而有效的方式来对API进行路由,以及提供一些强大的过滤器功能。例如:熔断、限流、重试等。
1. Spring Cloud Gateway具有如下特性
- 基于Spring Framework5,Project Reactor和Spring Boot2.0进行构建;
- 动态路由:能够匹配任何请求属性;
- 可以对路由指定Predicate(断言)和Filter(过滤器);
- 集成Hystrix的断路器功能;
- 集成Spring Cloud服务发现功能;
- 易于编写的Predicate(断言)和Filter(过滤器);
- 请求限流功能;
- 支持路径重写。
2. 相关概念
- Route(路由):路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由;
- Predicatae(断言):指的是Java8Function Predicate。SpringCloud断言的输入类型是Spring框架中的ServerWebExchange(服务网络交换器,存放着重要的请求-响应属性、请求实例和响应实例等等)。这使开发人员可以匹配HTTP请求中的内容,例如请求头或请求参数。如果请求与断言相匹配,则进行路由;
- Filter(过滤器):指的是Spring框架中Gateway Filter的实例,使用过滤器,可以在请求被路由前后对请求进行修改。
3. 路由配置
3.1 环境搭建
-
导入依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency>
-
编写配置
server: port: 9000 spring: application: name: lkj-gateway cloud: gateway: routes: - id: path_route #路由id uri: lkj-eureka-client/user/find predicates: # 断言,路径相匹配的进行路由 - path=/user eureka: client: service-url: defaultZone: http://lkj:123@localhost:8761/eureka
-
使用地址:http://localhost:9000/user/find进行测试,发现该请求被路由到了user-service的该路径上:http://localhost:8081/user/find
3.2 路由规则
Spring Cloud Gateway包括许多内置的Route Predicate工厂。所有这些Predicate都与HTTP请求的不同属性匹配。多个Route Predicate工厂可以进行组合,下面我们来介绍以下常用的Route Predicate。
-
After Route Predicate 在指定时间之后的请求会匹配该路由。
spring: application: name: lkj-gateway cloud: gateway: routes: - id: path_route #路由id uri: lkj-eureka-client/user/find predicates: # 断言,路径相匹配的进行路由 - After=2020-09-20T10:30:00[Asia/Shanghai]
3.3 动态路由
使用Zuul作为网关结合注册中心进行使用时,默认情况下Zuul会根据注册中心注册的服务列表,一服务名为路径创建动态路由,Gateway同样页实现了该功能。下面我们演示下Gateway结合注册中心如何使用默认的动态路由和过滤器。
-
配置yml文件
# 方式一 spring: application: name: lkj-gateway cloud: gateway: routes: #配置路由:路由ID,路由到微服务的uri,断言(判断条件) - id: path_route # 服务的ID,uri的协议为lb,这样才能启用Gateway的负载均衡功能 uri: lb://lkj-eureka-client predicates: # 断言,路径相匹配的进行路由 - path=/user # 方式二 spring: application: name: lkj-gateway cloud: gateway: discovery: locator: enable: true # 开启从注册中心动态创建路由的功能 lower-case-service-id: true # 使用小写服务名,默认是大写
4. Route Filter过滤器
路由过滤器可用于修改进入的HTTP请求和返回的HTTP响应,路由过滤器只能指定路由进行使用。Spring Cloud Gateway内置了多种路由过滤器,他们都由GatewayFilter的工厂类来产生,下面我们介绍下常用路由过滤器的用法。
-
过滤器的声明周期: SpringCloud Gateway过滤器有两个pre和post
-
PRE:在请求被路由之前调用,可以利用这种过滤器实现身份验证,在集群中选择请求的微服务,纪律调试信息等。
-
POST:在请求被路由到微服务以后执行,这种过滤器可以为响应添加标准的HTTP Header、收集统计信息和指标、将响应从微服务发送给客户端。
-
-
过滤器的类型:从作用的范围分为两种GatewayFilter和GlobalFilter
- GatewayFilter:应用在单个路由和分组路由上
- GlobalFilter:应用到所有的路由上
4.1 局部过滤器
局部过滤器(GatewayFilter)是针对单个路由的过滤器,可以对访问的URL进行过滤,进行切面处理。在SpringCloud Gateway中内置了多种不同类型的局部过滤器。
4.1.1 AddRequestParamter GatewayFilter
-
给请求添加参数的过滤器
spring: application: name: lkj-gateway cloud: gateway: routes: - id: path_route #路由id uri: lkj-eureka-client/user/find filters: - AddRequestParameter=username,hello predicates: # 断言,路径相匹配的进行路由 - Method=GET
-
以上配置会对GET请求添加username=hello的请求参数,通过工具进行如下测试
http://localhost:9000/user/find
-
相当于发起该请求:
http://localhost:9000/user/find?username=hello
4.1.2 StripPrefix Gateway Filter
-
对指定数量的路径前缀进行去除的过滤器
spring: application: name: lkj-gateway cloud: gateway: routes: - id: path_route #路由id uri: lkj-eureka-client/user/find filters: - StripPrefix=1 predicates: # 断言,路径相匹配的进行路由 - Path=/user/**
-
以上配置会把以/user/开头的请求的路径去除两位,通过工具进行如下测试
http://localhost:9000/user/m/q/list
-
相当于发起该请求:
http://localhost:9000/user/q/list
4.1.3 PrefixPath GatewayFilter
-
与StripPrefix过滤器恰好相反,会对原有路径进行增加操作的过滤器。
spring: application: name: lkj-gateway cloud: gateway: routes: - id: path_route #路由id uri: lkj-eureka-client/user/find filters: - PrefixPath=/user predicates: # 断言,路径相匹配的进行路由 - Method=GET
-
以上配置会对所有GET请求添加/user路径前缀,通过工具进行如下测试
http://localhost:9000/q/list
-
相当于发起该请求:
http://localhost:9000/user/q/list
4.1.4 Hystrix GatewayFilter
Hystrix 过滤器允许你将断路器功能添加到网关路由中,使你的服务免受级联故障的影响,并提供服务降级处理。
-
要开启断路器功能,我们需要在pom.xml中添加Hystrix的相关依赖:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
-
然后添加相关服务降级的处理类:
@RestController public class FallbackController { @GetMapping("/fallback") public Object fallback(){ Map<String ,Object> result = new HashMap<>(); result.put("data",null); result.put("message","Get request fallback!"); result.put("code",500); return result; } }
-
在yml中添加相关配置,当路由出错时会转发到服务降级处理的控制器上:
spring: application: name: lkj-gateway cloud: gateway: routes: - id: hystrix_route #路由id uri: http://localhost:8081 filters: - name: Hystrix args: name: fallbackcmd fallbackUri: forward:/fallback - PrefixPath=/user predicates: # 断言,路径相匹配的进行路由 - Method=GET
-
关闭对应的路由服务,调用该服务的网关地址进行测试:http://localhost:9000/user/q/list,发现已经返回了服务降级的处理信息。
4.1.5 RequestRateLimiter GatewayFilter
RequestRateLimiter过滤器可以用于限流,使用RateLimiter实现来确定是否允许当前请求继续进行,如果请求太大默认会返回HTTP 429-太多请求状态。
-
在pom.xml中添加相关依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis-reactive</artifactId> </dependency>
-
添加限流策略的配置类,这里由两种策略:一种是根据请求参数中的username进行限流;另一种是根据访问IP进行限流;
@Configuration public class RedisRateLimiterConfig { @Bean KeyResolver userKeyResolver(){ return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("username")); } @Bean public KeyResolver ipKeyResolver(){ return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName()); } }
-
使用Redis来进行限流,所以需要添加Redis和RequestRateLimiter的配置,这里对所有的Get请求都进行了按IP来限流的操作:
spring: application: name: lkj-gateway cloud: gateway: routes: - id: hystrix_route #路由id uri: http://localhost:8081 filters: - name: RequestRateLimiter args: redis-rate-limiter.replenishReate: 1 #每秒允许处理的请求数量 redis-reta-limiter.burstCapacity: 2 #每秒最大处理的请求数量 key-resolver: "{@ipkeyResolver}" #限流策略,对应策略的Bean - PrefixPath=/user predicates: # 断言,路径相匹配的进行路由 - Method=GET
-
多次请求该地址:http://localhost:9000/user/q/list,会返回状态码为429的错误。
4.1.6 Retry GatewayFilter
对路由请求进行重试的过滤器,可以根据路由请求返回的HTTP状态码来确定是否进行重试。
-
修改配置文件
spring: application: name: lkj-gateway cloud: gateway: routes: - id: hystrix_route #路由id uri: http://localhost:8081 predicates: # 断言,路径相匹配的进行路由 - Method=GET filters: - name: Retry args: retries: 1 #需要进行重试的次数 statuses: BAD_GATEWAY #返回哪个状态码需要进行仇视,返回状态码为5xx进行重试 backoff: firstBackoff: 10ms maxBackoff: 50ms factor 2 basedOnPreviousValue: false
-
当调用返回500时会进行重试,访问测试地址:http://localhost:9000/user/q/list
-
可以发现user-service控制台报错2次,说明进行了一次重试。
4.2 全局过滤器
全局过滤器(GlobalFilter)作用于所有路由,SpringCloud Gateway提供了GlobalFilter接口,可以自定义过滤器。通过全局过滤器可以实现对权限的统一校验、安全验证等功能。
-
添加全局过滤器配置类
/** * 执行过滤器的业务逻辑 * @param exchange * @param chain * @return */ @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { System.out.println("执行了全局过滤器"); String token = exchange.getRequest().getQueryParams().getFirst("token"); if(token == null){ System.out.println("请登陆"); exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED); return exchange.getResponse().setComplete(); // 请求结束 } return chain.filter(exchange);// 继续执行后续的过滤器 } /** * 定义过滤器的执行顺序 * 值越小优先级越高 * @return */ @Override public int getOrder() { return 0; }
5. 跨域
-
网关跨域有两种方式:yml文件配置、配置类Bean注入。
spring: application: name: lkj-gateway cloud: gateway: globalcors: cors-configurations: '[**]': # 允许携带认证信息 # 允许跨域的源(网站域名/ip),设置*为全部 # 允许跨域请求里的head字段,设置*为全部 # 允许跨域的method,默认为GET和OPTIONS,设置*为全部 # 跨域允许的有效期 allow-credentials: true allowed-origins: - "http://localhost:8888" - "http://localhost:9999" allowed-headers: "*" allowed-methods: - OPTIONS - GET - POST max-age: 3600 # 允许response的head信息 # 默认仅允许如下6个: # Cache-Conrol # Content-Language # Content-Type # Expires # Last-Modified # Pragma #exposed-headers:
-
方法二:Bean注入配置类
@Bean public CorsWebFilter corsWebFilter(){ CorsConfiguration config = new CorsConfiguration(); config.addAllowedMethod("*"); config.addAllowedOrigin("*"); config.addAllowedHeader("*"); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser()); source.registerCorsConfiguration("/**",config); return new CorsWebFilter(source); }