zoukankan      html  css  js  c++  java
  • springcloud-GateWay常用的Predicate

    在gateway的yml配置会有下面这个配置:

    spring:
      cloud:
        gateway:
          routes:
            - id: payment_routh
              uri: lb://cloud-payment-service
              predicates:
                - Path=/payment/get/**

      主要看predicates属性,这个属性其实还可以配置多个属性,Path只是其中一个。稍微底层一点,这里配置的predicates属性都有其对应的类来处理,如下:

       比如Path的话就由PathRoutePredicate垃圾处理,以此类推,但并不是想讲里面的类是怎么处理的,还是解释其他属性的含义和用法吧

      1.After。配置如下:

    predicates:
      - After=2021-02-24T15:17:25.170+08:00[Asia/Shanghai]

    上面配置表示请求必须是在这个时间后请求的,才匹配该断言(只是匹配当前断言而已,还需要考虑其他断言)。还有这个时间倒不难看懂,主要是如何将时间变成这个格式呢,然后获取呢?参考代码如下:

        public static void main(String[] args) {
            ZonedDateTime time = ZonedDateTime.now();
    
            System.out.println(time);
        }

      2.Before,Between;这两个应该不用说,也是时间相关的,上面会了,这个应该也不难。

      3.Cookie。配置如下:

    predicates:
      - Cookie=username,zzyy

      配置表示 请求必须带有 username的cookie,并且值为zzyy的;这里的值是可以使用正则表达式的

      4.Header,Host;这两个一起讲,因为基本差不多的,配置如下:

    predicates:
       - Header=zhangsan,d+
       - Host=**.baidu.com

      Header是要求请求存在一个请求头,头名称是zhangsan,头值是正整数(可使用正则表达式);Host表示请求中存在一个请求头,请求头名称为Host,值主要后缀是.baidu.com即可(也可以使用正则表达式)

      5.Path(略)

      6.Method;

    predicates:
      - Method=Get

      相信你们能看懂。

      7.Query。

    predicates:
      - Query=username,123

      表示请求中必须带有参数,参数名师username,值为123(同样可正则)

    一般测试请求会使用一些测试工具,但为了方便,使用命令行的curl命令完成测试。下面为测试用例,可参考:

    curl http://www.baidu.com
    curl http://www.baidu.com --cookie "username=123" #带cookie
    curl http://www.baidu.com --H "Host:localhost"  #带请求头
    curl http://www.baidu.com?username=zhangsan
  • 相关阅读:
    oracle 数据库服务名怎么查
    vmware vsphere 6.5
    vSphere虚拟化之ESXi的安装及部署
    ArcMap中无法添加ArcGIS Online底图的诊断方法
    ArcGIS中字段计算器(高级计算VBScript、Python)
    Bad habits : Putting NOLOCK everywhere
    Understanding the Impact of NOLOCK and WITH NOLOCK Table Hints in SQL Server
    with(nolock) or (nolock)
    What is “with (nolock)” in SQL Server?
    Changing SQL Server Collation After Installation
  • 原文地址:https://www.cnblogs.com/ibcdwx/p/14441822.html
Copyright © 2011-2022 走看看