首页 > 其他分享 >gateway网关的断言(predicate)和过滤(filter)

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

时间:2023-02-13 11:32:33浏览次数:57  
标签:网关 predicate 请求 Route Predicate Factory filter org 路由


文章目录

  • ​​1、GateWay路由断言工厂​​
  • ​​1.1 After Route Predicate Factory​​
  • ​​1.2 Before Route Predicate Factory​​
  • ​​1.3 Between Route Predicate Factory​​
  • ​​1.4 Cookie Route Predicate Factory​​
  • ​​1.5 Header Route Predicate Factory​​
  • ​​1.6 Host Route Predicate Factory​​
  • ​​1.7 Method Route Predicate Factorty​​
  • ​​1.8 Path Route Predicate Factory​​
  • ​​1.9 Query Route Predicate Factory​​
  • ​​1.10 RemoteAddr Route Predicate Factory​​
  • ​​1.11 Weight Route Predicate Factory​​
  • ​​2、Gateway过滤器工厂​​
  • ​​2.1 AddRequestParameter GatewayFilter​​
  • ​​2.2 The AddRequestHeader GatewayFilter Factory​​
  • ​​2.3 The AddResponseHeader GatewayFilter Factory​​
  • ​​2.4 StripPath GatewayFilter​​
  • ​​2.5 PrefixPath GatewayFilter​​
  • ​​3、通过网关提供web路径查看路由详细规则​​
  • ​​4、自定义网关全局filter​​

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

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

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


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

1.2 Before Route Predicate Factory

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

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


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

1.3 Between Route Predicate Factory

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

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


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

1.4 Cookie Route Predicate Factory

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

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


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

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


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

1.5 Header Route Predicate Factory

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

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


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

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

或者:

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


结果:

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

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

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

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


上述代码意思是: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是给请求添加参数的过滤器,具体代码如下:

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


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

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

相当于发起如下请求:

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

2.2 The AddRequestHeader GatewayFilter Factory

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

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


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

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


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

2.3 The AddResponseHeader GatewayFilter Factory

响应加入指定的头信息

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


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

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

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​

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

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;
}
}

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


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

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

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


标签:网关,predicate,请求,Route,Predicate,Factory,filter,org,路由
From: https://blog.51cto.com/u_15961549/6053845

相关文章

  • Spring Cloud Gateway(微服务网关)
    文章目录​​1、什么是服务网关?​​​​1.1说明​​​​1.2为什么需要网关?​​​​1.3网关组件在微服务中的架构​​​​2、服务网关组件Gateway​​​​2.1SpringClou......
  • .Net Core使用Ocelot网关(一) -负载,限流,熔断,Header转换
    原文网址:https://www.cnblogs.com/linhuiy/p/12029652.html1.什么是API网关API网关是微服务架构中的唯一入口,它提供一个单独且统一的API入口用于访问内部一个或多个API。......
  • 如何设计一个高性能网关(二)
    一、背景21年发布的开源项目ship-gate收获了100+start,但是作为网关它还缺少一项重要的能力——集群化部署的能力,有了这个能力就可以无状态的横向扩展,再通过nginx等服务器......
  • Filter过滤器 快速入门
    Filter过滤器举例:饮水机空调 web中的过滤器:当访问服务器的资源时过滤器可以将请求拦截下来完成一些特殊功能过滤器的作用一般用来完成通过的操作如......
  • ASP.NET Core - IStartupFilter 与 IHostingStartup
    1.IStartupFilter  上面讲到的方式虽然能够根据不同环境将Startup中的启动逻辑进行分离,但是有些时候我们还会可以根据应用中的功能点将将一系列相关中间件的注册封装到......
  • Gateway服务网关 (入门到使用)
    Gateway服务网关Gateway也要作为微服务注册到nacos中Zuul也是网关但比较老是一种阻塞式编程;SpringCloudGateway是SpringCloud的一个全新项目,该项目是基于Spring......
  • springmvc的Interceptor拦截器和servlet的filter过滤器
    springmvc的Interceptor拦截器和servlet的filter过滤器1、springmvc的Interceptor拦截器和servlet的filter过滤器springboot实现方式packagecom.liubaihui.lianxi01.co......
  • [Typescript] Combine Branded type with type predicates
    import{it}from'vitest';import{Brand}from'../helpers/Brand';typeValid<T>=Brand<T,'Valid'>;interfacePasswordValues{password:string;co......
  • 基于 Ubuntu 服务器配置原生的 Socks5 网关代理服务器
    常见的代理协议有http、https、socks4/5这三种,http协议的代理搭建方案最简单,但是http代理无法访问https网站,https代理无法实现调用远端dns,所以我个人推荐使用Scoks5协议......
  • nginx 实用(网关服务和动静分离)
     还有一些静态资源 ......