zl程序教程

您现在的位置是:首页 >  后端

当前栏目

gateway网关的断言(predicate)和过滤(filter)

gateway 过滤 网关 filter 断言 Predicate
2023-06-13 09:17:16 时间

文章目录

1、GateWay路由断言工厂

  Spring Cloud Gateway包括许多内置的路由断言(Route Predicate)工厂,所有这些Predicate都与HTTP请求的不同属性匹配。多个Route Predicate工厂可以进行组合。 官方文档:https://docs.spring.io/spring-cloud-gateway/docs/2.2.9.RELEASE/reference/html/#gateway-request-predicates-factories

1.1 After Route Predicate Factory

在指定时间之后的请求会匹配该路由,具体代码如下:

上述代码的意思是只有在2021-09-01 21:53:53时间之后的请求才会匹配该路由。

1.2 Before Route Predicate Factory

在指定时间之前的请求会匹配该路由,具体代码如下:

只有在2021-09-01 21:57:57时间之前的请求才会匹配该路由。

1.3 Between Route Predicate Factory

在指定时间区间内的请求会匹配该路由,具体代码如下:

只有在2021-09-01 21:50:53到2021-09-01 22:01:53时间段内的请求才会匹配该路由。

带有指定Cookie的请求会匹配该路由,具体代码如下:

cookie的值可用正则表达式匹配:

上述意思是字母或者数字组成的值都可以被路由匹配。

1.5 Header Route Predicate Factory

带有指定请求头的请求会匹配该路由

这个可以用postman工具或者curl命令测试:

curl http://localhost:7979/product/list -H "X-Request-Id:12"

或者:

结果:

1.6 Host Route Predicate Factory

带有指定Host的请求会匹配该路由,具体代码如下:

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: https://example.org
        predicates:
        - Host=**.somehost.org,**.anotherhost.org

使用curl工具发送带有请求有为Host:www.anotherhost.org的请求可以匹配该路由:

curl http://localhost:9201/user/1 -H "Host:Www.anotherhost.org"

1.7 Method Route Predicate Factorty

发送指定方法的请求会匹配该路由,具体代码如下:

上述代码意思是:get请求的方法才可以匹配该路由。

1.8 Path Route Predicate Factory

发送指定路径的请求会匹配该路由,具体代码如下:

spring:
  cloud:
    gateway:
      routes:
      - id: path_route
        uri: https://example.org
        predicates:
        - Path=/red/{segment},/blue/{segment}

使用curl工具发送/red/1 或者/blue/1 路径请求可以匹配该路由,具体如下:

curl http://localhost:8080/red/1
curl http://localhost:8080/blue/1

1.9 Query Route Predicate Factory

带指定查询参数的请求可以匹配该路由:

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: https://example.org
        predicates:
        - Query=green

使用curl工具发送带green=1查询参数的请求可以匹配该路由:

http://localhost:9201/hello?green=1

1.10 RemoteAddr Route Predicate Factory

从指定远程地址发起的请求可以匹配该路由:

spring:
  cloud:
    gateway:
      routes:
      - id: remoteaddr_route
        uri: https://example.org
        predicates:
        - RemoteAddr=192.168.1.1/24

使用curl工具从192.168.1.1发起请求可以匹配该路由:

curl http://localhost:9201/hello

1.11 Weight Route Predicate Factory

使用权重来路由相应请求,以下代码表示有80%的请求会被路由到weighthigh.org、20%会被路由到weightlow.org。

spring:
  cloud:
    gateway:
      routes:
      - id: weight_high
        uri: https://weighthigh.org
        predicates:
        - Weight=group1, 8
      - id: weight_low
        uri: https://weightlow.org
        predicates:
        - Weight=group1, 2

2、Gateway过滤器工厂

  路由过滤器可用于修改进入的HTTP请求和返回的HTTP响应。Spring Cloud Gateway内置了多种路由过滤器,由GatewayFilter的工厂类禅城。下面介绍常用路由过滤器的用法。 官方文档:https://docs.spring.io/spring-cloud-gateway/docs/2.2.9.RELEASE/reference/html/#gatewayfilter-factories

2.1 AddRequestParameter GatewayFilter

AddRequestParameter GatewayFilter是给请求添加参数的过滤器,具体代码如下:

以上配置回对请求添加color=blue的请求参数,通过curl工具使用以下命令进行测试:

curl http://localhost:7979/product/list

相当于发起如下请求:

curl http://localhost:8799/product/list?color=blue

2.2 The AddRequestHeader GatewayFilter Factory

用来给当前路由对象的所有请求加入指定请求头信息(键和值)

我们可以在服务被调用方打印携带的头信息:

2.3 The AddResponseHeader GatewayFilter Factory

响应加入指定的头信息

使用portman发送http://localhost:7979/product/list请求之后,查看头信息:

2.4 StripPath GatewayFilter

StripPath GatewayFilter是对指定数量的路径前缀进行去除的过滤器。

spring:
  cloud:
    gateway:
      routes:
      - id: nameRoot
        uri: http://localhost:8080
        predicates:
        - Path=/user-service/**
        filters:
        - StripPrefix=1

以上配置会把以/user-service/开头的请求的路径去除两位,可以利用curl工具使用以下命令进行测试:

curl http://localhost:9201/user-service/a/user/1

相当于发起如下请求:

curl http://localhost:8080/user/1

2.5 PrefixPath GatewayFilter

与StripPrefix过滤器恰好相反,PrefixPath GatewayFilter是回对原有路径进行增加操作的过滤器:

spring:
  cloud:
    gateway:
      routes:
      - id: prefixpath_route
        uri: http://localhost:8080
        predicates:
         - Method=GET 
        filters:
        - PrefixPath=/user

以上配置回对所有GET请求添加/user路径前缀,可以利用curl工具使用如下命令进行测试:

curl http://localhost:9201/1

相当于发起如下请求:

curl http://localhost:8080/user/1

更多的过滤器请查看官网。

3、通过网关提供web路径查看路由详细规则

查看网关路由规则详细路径必须在网关配置文件中暴露当前路径

management:
  endpoints:
    web:
      exposure:
        include: "*" #开启所有web端点暴露

访问:http://localhost:7979/actuator/gateway/routes

4、自定义网关全局filter

package com.baizhi.filters;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 自定义网关全局filter
 */
@Configuration
public class CustomerGlobalFilter  implements GlobalFilter,Ordered {

    //类似javaweb的doFilter
    //exchange   :request response 封装了request response
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //httprequest对象
        ServerHttpRequest request = exchange.getRequest();
        //httpresponse对象
        ServerHttpResponse response = exchange.getResponse();
        System.out.println("经过全局Filter处理......");


        Mono<Void> filter = chain.filter(exchange); //放行filter继续向后执行
        System.out.println("响应回来filter的处理......");
        return filter;
    }


    /**
     * @return int数字:用来指定filter执行顺序 默认顺序按照自然数字进行排序
     * -1 在所有filter执行之前执行
     */
    @Override
    public int getOrder() {
        return 0;
    }
}

从下面控制台的打印情况来看,每次请求都会经过全局过滤器。