谓词 Before · SpringCloud微服务实战 · 看云

2.谓词 Before

导航

下面我们来讲一下谓词Before,很明显和谓词After 是相对的,区别就是该谓词匹配在指定日期时间之前发生的请求。谓词的使用都在同一个项目里面,因此我们主要讲解配置和测试情况。

本节代码地址


同样再提一下,如果想打印当前时区的时间,用以下代码

System.out.println(ZonedDateTime.now());

2.1 应用配置

可以看出配置的上半部分是没有变化的,后面在配置源码验证的时候修改一下spring.profiles.active即可,这里我们在配置中将路由生效的时间设置在
2091-01-08T18:30:11.965+08:00[Asia/Shanghai],因此是可以正常转发的,如果当前时间已经超过了设置的时间,就会返回404。

server:
  port: 8699
spring:
  application:
    name: fw-gateways-gateway
  profiles:
    active: before_route
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
---
spring:
  cloud:
    gateway:
      routes:
        - id: before_route
          uri: lb://fw-cloud-ribbon-server
          predicates:
            - Before=2091-01-08T18:30:11.965+08:00[Asia/Shanghai]
  profiles: before_route

2.2 应用启动并测试

别忘了启动Eureka 和 Ribbon 服务程序
4c8001ea11eae80a2996e76825414f7f_MD5.webp

我们浏览器测试验证当前的谓词功能
浏览器或Postman输入localhost:8699/user/1
b105c7c8103e078b995c75f6750252f6_MD5.webp