网关过滤器GatewayFilter,

        如果说Route Predicate 决定路由到哪个路径,那么过滤器就是允许修改HTTP请求的一些属性。

spring cloud 内置了一部分过滤器,也可以自定义过滤器。

 

自定义过滤器:

Global Filters:全局过滤器,不需要配置路由,系统初始化作用到所有路由上。

GatewayFilter:需要配置某个路由,才能过滤。如果需要使用全局路由,需要配置Default Filters。

import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import reactor.core.publisher.Mono;

/**
 * @创建人 steffens
 * @创建时间 2019/6/20
 * @描述 文件创建
 */
@Configuration
public class ExampleConfiguration {
    @Bean
    @Order(-1)
    public GlobalFilter filterOne() {
        return (exchange, chain) -> {
            System.out.println("First pre filter");
            return chain.filter(exchange).then(Mono.fromRunnable(() -> {
                System.out.println("third post filter");
            }));
        };
    }
}

 

 

 

AddRequestHeader GatewayFilter:增加请求头,如下配置,增加了一个X-Response-Foo:Bar的请求头。

springcloudgateway 添加header的Filter spring cloud gateway 自定义filter_spring

springcloudgateway 添加header的Filter spring cloud gateway 自定义filter_spring_02

 

 

AddRequestParameter GatewayFilter:增加一个请求参数,会增加一个foo值为bar的参数

springcloudgateway 添加header的Filter spring cloud gateway 自定义filter_自定义_03

 

AddResponseHeader GatewayFilter:增加一个响应参数

springcloudgateway 添加header的Filter spring cloud gateway 自定义filter_spring_04

springcloudgateway 添加header的Filter spring cloud gateway 自定义filter_springcloud_05

 

Hystrix GatewayFilter:熔断器过滤器,Hystrix允许断路器引入网关路由,来避免下级调用者受故障的影响,并支持设置故障时的后备响应。fallbackUri设置的是故障时后备响应的接口。

springcloudgateway 添加header的Filter spring cloud gateway 自定义filter_springcloud_06

 

PrefixPath GatewayFilter:为请求路径增加前缀,http://localhost:8085/hello 会自动请求到 http://localhost:8085/mypath/hello。

springcloudgateway 添加header的Filter spring cloud gateway 自定义filter_自定义_07

 

PreserveHostHeader GatewayFilter:过滤器刷选请求的属性,以确保是原始的请求而不是HTTP客户端的请求。

springcloudgateway 添加header的Filter spring cloud gateway 自定义filter_限流_08

 

RequestRateLimiter GatewayFilter:限流过滤器,GatewayFilter 限流是使用的令牌桶限流算法。令牌桶有两个核心参数,桶容量 burstCapacity,通过速率:replenishRate。如果在单位时间超过令牌桶的容量,后面进来的请求将会被丢弃。令牌桶是用redis作为存储空间。这时候会返回429状态码给客户端。首先我们要配置pom文件对redis的依赖:spring-boot-starter-data-redis-reactive。然后java文件需要实现 KeyResolver类来自定义限流的策略,可以根据uri限流,也可以根据host,也可以根据用户user。最后我们要配置过滤器文件。replenishRate:流量速率我们设置成1,burstCapacity:令牌桶容量我们设置成1,key-resolver是我们自定义解析器的的对象名。这时候一直请求http://localhost:8082/,返回状态一会为200,一会为429,这说明限流成功。

springcloudgateway 添加header的Filter spring cloud gateway 自定义filter_springcloud_09

springcloudgateway 添加header的Filter spring cloud gateway 自定义filter_spring_10

springcloudgateway 添加header的Filter spring cloud gateway 自定义filter_spring_11

 

RedirectTo GatewayFilter:重定向过滤器,如302的状态吗会直接重定向到 http://www.baidu.com。

springcloudgateway 添加header的Filter spring cloud gateway 自定义filter_限流_12

RemoveRequestHeader GatewayFilter:过滤器去除请求头参数

RemoveResponseHeader GatewayFilter:过滤器去除响应头参数

 

 

RewritePath GatewayFilter:重写路径过滤器。/test/(?<segment>.*) 会被重写成 /$\{segment},如下面配置访问 http://localhost:8082/test/hello路径,其实用 /hello 替换成 /test/hello。

springcloudgateway 添加header的Filter spring cloud gateway 自定义filter_spring_13

 

RewriteResponseHeader GatewayFilter:重写响应头过滤器。会用password=***替换掉 password=123。

springcloudgateway 添加header的Filter spring cloud gateway 自定义filter_spring_14

SetResponseHeader GatewayFilter:设置响应头过滤器。

SetStatus GatewayFilter:设置响应状态过滤器。

StripPrefix GatewayFilter:去除路径过滤器。

Retry GatewayFilter:重试过滤器,retries:重试次数,statuses:需要重试的状态吗,methods:应该重试的方法,series:org.springframework.http.HttpStatus.Series。

RequestSize GatewayFilter:请求大小过滤器。

Modify Request Body GatewayFilter:修改请求体过滤器。

Modify Response Body GatewayFilter:修改响应体过滤器。

 

 

Default Filters:默认路由,如果要配置全局路由,使用这个配置。

springcloudgateway 添加header的Filter spring cloud gateway 自定义filter_自定义_15

Global Filters:全局过滤器,不需要配置路由,系统初始化作用到所有路由上。

GatewayFilter:需要配置某个路由,才能过滤。如果需要使用全局路由,需要配置Default Filters。