Spring Cloud Gateway-路由谓词工厂

1、The After Route Predicate Factory 

当请求的时间在设定的时间之后,请求才能访问到服务,请求在设定时间之前访问,则返回404

server:
  port: 10020
spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
        - id: after_route
          uri: lb://consumer-service
           #当请求的时间在设定时间之后,请求才会转发到微服务
           #请求在设定时间之前访问返回404
          predicates:
            - After=2023-07-14T17:50:42.579+08:00[Asia/Shanghai]

设定时间>现在时间 返回404

  • 设置 设定时间>现在时间 返回404  

predicates:
  - After=2023-07-14T17:59:42.579+08:00[Asia/Shanghai]

 

2、The Before Route Predicate Factory

请求时间没有超过设定的时间,请求才能访问服务,超过时间的请求,则返回404

server:
  port: 10020

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: before_route
          uri: lb://consumer-service
         
          predicates:
            - Before=2023-07-14T18:55:42.579+08:00[Asia/Shanghai]

  •  修改设定时间< 现在时间

3、The Bwtween Route Predicate Factory

 请求时间在设定时间之间,请求可以访问服务,不在设定时间之间,直接返回查询不到服务

server:
  port: 10020

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: between_route
          uri: lb://consumer-service
          #设置时间区间
          predicates:
            - Between=2023-07-14T18:45:42.579+08:00[Asia/Shanghai],2023-07-14T18:59:42.579+08:00[Asia/Shanghai]

  •  修改时间区间,时间区间内不包含现在的时间

- Between=2023-07-14T18:45:42.579+08:00[Asia/Shanghai],2023-07-14T18:50:42.579+08:00[Asia/Shanghai] 

 

4、The Cookie Route Predicate Factory

server:
  port: 10020

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: cookie_route
          uri: lb://consumer-service
          #设置访问请求的cookie名称和cookie值符合的正则表达式
          predicates:
            - Cookie=mycookie,[a-zA-Z]
  • 使用postman访问,设置访问请求的Cookie

 

 

  •  修改cookie的值

 

  •  修改cookie的名称

 

 

5、The Header Route Predicate Factory

server:
  port: 10020

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: Header_route
          uri: lb://consumer-service
          #设置请求头的名称和请求头的值符合的正则表达式
          #只有请求的名称和设置一致并且请求头的值满足正则表达式,请求才能发送给服务
          predicates:
            - Header=Request-Id,[a-zA-Z]

  • 修改请求头中值

 

  •  修改请求头中的名称

6、The Host Route Predicate Factory

server:
  port: 10020

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: host_route
          uri: lb://consumer-service
          #只有请求头的名称为host,并且符合**.somehost,请求才能转发给服务
          predicates:
            - Host=**.somehost

 

  • 修改请求头中Host的值

 

  •  修改请求头中的key名称

 

7、The Method Route Predicate Factory

server:
  port: 10020

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: method_route
          uri: lb://consumer-service
          #设置能够访问的请求方法
          predicates:
            - Method=GET,POST

 

8、The Path Route Predicate Factory

server:
  port: 10020

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: path_route
          uri: lb://consumer-service
          #设置允许访问的路径
          #只有请求路径中为/consumer/**,请求才能访问到服务
          predicates:
            - Path=/consumer/**

 

9、The Query Route Predicate Factory

server:
  port: 10020

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: query_route
          uri: lb://consumer-service
          #设置允许访问的携带参数的名称
          #只有请求携带有data的参数,请求才能访问到服务
          predicates:
            - Query=data

  •  修改携带参数的名称

 

10、The RemoteAddr Route Predicate Factory

server:
  port: 10020

spring:
  application:
    name: gateway-service
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      routes:
        - id: remoteAddr_route
          uri: lb://consumer-service
          #设置允许访问的ip地址
          #只有访问的ip地址在此网段内,请求才能访问服务
          predicates:
            - RemoteAddr=127.0.0.1/10

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值