欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

十四、SpringCloud学习笔记之Gateway-Predicate的使用

程序员文章站 2022-06-13 13:28:56
...

Predicate的使用

启动我们的gatewat9527

十四、SpringCloud学习笔记之Gateway-Predicate的使用
Route Predicate Factories:
十四、SpringCloud学习笔记之Gateway-Predicate的使用
Spring Cloud Gateway将路由匹配作为Spring WebFlux HandlerMapping基础架构的一部分。

Spring Cloud Gateway包括许多内置的Route Predicate工厂。所有这些Predicate都与HTTP请求的不同属性匹配。多个RoutePredicate厂可以进行组合

Spring Cloud Gateway创建Route对象时,使用RoutePredicateFactory创建Predicate对象,Predicate 对象可以赋值给Route。Spring Cloud Gateway包含许多内置的Route Predicate Factories.

所有这些谓词都匹配HTTP请求的不同属性。多种谓词工厂可以组合,并通过逻辑and。

常用的Route Predicate

十四、SpringCloud学习笔记之Gateway-Predicate的使用

1.After Route Predicate

十四、SpringCloud学习笔记之Gateway-Predicate的使用
after路由谓词工厂接受一个参数,一个datetime。此谓词匹配发生在指定日期时间之后的请求。

获取当前时区时间:

package com.liang.cloud.utils;
import java.time.ZonedDateTime;
public class Time {
    public static void main(String[] args) {
        ZonedDateTime zonedDateTime = ZonedDateTime.now();
        System.out.println(zonedDateTime);

//        ZonedDateTime zonedDateTime1 = ZonedDateTime.now(ZoneId.of("America/New_York")); 获取指定时区时间

    }
}

yml:

- id: to_baidu_mil #路由的ID,没有固定规则但要求唯一,建议配合服务名
        uri: https://news.baidu.com/mil
        predicates:
          - Path=/mil   #断言,路径相匹配的进行路由
          - After=2020-06-04T13:57:23.583+08:00[Asia/Shanghai]

十四、SpringCloud学习笔记之Gateway-Predicate的使用
可以正常访问,当修改时间为
十四、SpringCloud学习笔记之Gateway-Predicate的使用
十四、SpringCloud学习笔记之Gateway-Predicate的使用

2.Before Route Predicate

十四、SpringCloud学习笔记之Gateway-Predicate的使用

before route谓词工厂接受一个参数,即datetime。此谓词匹配发生在指定日期时间之前的请求。

同上,只有请求时间在指定日期之前可访问

3.Between Route Predicate Factory

十四、SpringCloud学习笔记之Gateway-Predicate的使用
同上:请求的时间需要在指定的两个时间段内

路由之间的谓词工厂接受两个参数,datetime1和datetime2。此谓词匹配发生在datetime1之后和datetime2之前的请求。datetime2参数必须位于datetime1之后。

4.Cookie Route Predicate Factory

十四、SpringCloud学习笔记之Gateway-Predicate的使用
cookie路由谓词工厂接受两个参数,cookie名称和一个正则表达式。此谓词匹配具有给定名称且其值与正则表达式匹配的cookie。
yml:

十四、SpringCloud学习笔记之Gateway-Predicate的使用

官网下载curl:
https://curl.haxx.se/download.html
十四、SpringCloud学习笔记之Gateway-Predicate的使用

十四、SpringCloud学习笔记之Gateway-Predicate的使用
解压:
添加环境变量:
十四、SpringCloud学习笔记之Gateway-Predicate的使用
%CURL_HOME%;

十四、SpringCloud学习笔记之Gateway-Predicate的使用
十四、SpringCloud学习笔记之Gateway-Predicate的使用

使用curl测试:

curl http://localhost:9527/mil

十四、SpringCloud学习笔记之Gateway-Predicate的使用
携带cookie访问

curl http://localhost:9527/mil --Cookie username=zhangl

十四、SpringCloud学习笔记之Gateway-Predicate的使用
成功返回百度页面

5.Header Route Predicate Factory

十四、SpringCloud学习笔记之Gateway-Predicate的使用
头路由谓词工厂接受两个参数,头名称和一个正则表达式。此谓词与具有给定名称的头匹配,该头的值与正则表达式匹配。

yml:
十四、SpringCloud学习笔记之Gateway-Predicate的使用
十四、SpringCloud学习笔记之Gateway-Predicate的使用
携带整数访问
十四、SpringCloud学习笔记之Gateway-Predicate的使用

6.Host Route Predicate

十四、SpringCloud学习笔记之Gateway-Predicate的使用
主机路由谓词工厂接受一个参数:主机名模式列表。该模式是一个ant样式的模式。作为分隔符。此谓词匹配与模式匹配的主机头。
yml:
十四、SpringCloud学习笔记之Gateway-Predicate的使用
curl -H Host:www.liang.com localhost:9527/mil

十四、SpringCloud学习笔记之Gateway-Predicate的使用

7.Method Route Predicate

十四、SpringCloud学习笔记之Gateway-Predicate的使用
方法路由谓词工厂接受一个或多个参数:要匹配的HTTP方法
如果请求方法是GET或POST,则此路由匹配。

yml
十四、SpringCloud学习笔记之Gateway-Predicate的使用
十四、SpringCloud学习笔记之Gateway-Predicate的使用

8.Path Route Predicate

十四、SpringCloud学习笔记之Gateway-Predicate的使用
路径路由谓词工厂接受两个参数:Spring PathMatcher模式列表和一个称为matchOptionalTrailingSeparator的可选标志。

yml
十四、SpringCloud学习笔记之Gateway-Predicate的使用
十四、SpringCloud学习笔记之Gateway-Predicate的使用

9.Query Route Predicate

-十四、SpringCloud学习笔记之Gateway-Predicate的使用
查询路由谓词工厂接受两个参数:一个必需的参数和一个可选的regexp。
十四、SpringCloud学习笔记之Gateway-Predicate的使用
十四、SpringCloud学习笔记之Gateway-Predicate的使用

总结

yml

server:
  port: 9527
spring:
  application:
    name: cloud-gateway
  cloud:
    gateway:
      routes:
      - id: payment_routh #路由的ID,没有固定规则但要求唯一,建议配合服务名
        uri: lb://cloud-payment-service
        predicates:
          - Path=/payment/get/**   #断言,路径相匹配的进行路由
          - After=2020-06-04T13:57:23.583+08:00[Asia/Shanghai]

      - id: payment_routh2
        uri: lb://cloud-payment-service
        predicates:
          - Path=/payment/payment/lb/**   #断言,路径相匹配的进行路由

      - id: to_baidu_mil #路由的ID,没有固定规则但要求唯一,建议配合服务名
        uri: https://news.baidu.com/mil
        predicates:
          - Path=/mil   #断言,路径相匹配的进行路由
          - After=2020-06-04T13:57:23.583+08:00[Asia/Shanghai]
          - Method=GET
          - Query=username, \d+ #要有参数名称并且是正整数才能路由
#          - Cookie=username,zhangl     #并且Cookie是username=zhangl
#          - Header=X-Request-Id, \d+   #请求头中要有X-Request-Id属性并且值为整数的正则表达式
#          - Host=**.liang.com
eureka:
  instance:
    hostname: cloud-gateway-service
  client:
    service-url:
      register-with-eureka: true
      fetch-registry: true
      defaultZone: http://eureka7001.com:7001/eureka
说白了,Predicate就是为了实现一组匹配规则,让请求过来找到对应的Route进行处理
相关标签: java curl