1、创建测试类获取中国当前时间
import java.time.ZonedDateTime;
public class T2 {
public static void main(String[] args) {
ZonedDateTime zbj = ZonedDateTime.now();
System.out.println(zbj);
}
}
得到上海时间:2020-09-19T21:50:08.857+08:00[Asia/Shanghai]
2、配置在特定时间之后路由才有效
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: payment_routh # payment_route 路由的ID,没有固定的规则但要求唯一
# uri: http://localhost:8001 #匹配后提供服务的路径地址
uri: lb://cloud-payment-service #匹配后提供服务的路由地址
predicates:
- Path=/payment/get/** #断言,路径相匹配进行路由
- id: payment_routh2 # payment_route
# uri: http://localhost:8001
uri: lb://cloud-payment-service #匹配后提供服务的路由地址
predicates:
- Path=/payment/lb/**
- After=2020-09-19T21:55:08.857+08:00[Asia/Shanghai]
eureka:
instance:
hostname: cloud-gateway-service
client:
# 将自己注册进eureka中心
register-with-eureka: true
# 是否抓取注册信息,默认为true
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
3、访问:http://localhost:9527/payment/lb
返回了404,因为时间还没到就不会开放
过了时间,恢复正常访问
除了After,还有很多其他的如Before、Between等
携带特定cookie才可访问:
配置cookie的predicate
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: payment_routh # payment_route 路由的ID,没有固定的规则但要求唯一
# uri: http://localhost:8001 #匹配后提供服务的路径地址
uri: lb://cloud-payment-service #匹配后提供服务的路由地址
predicates:
- Path=/payment/get/** #断言,路径相匹配进行路由
- id: payment_routh2 # payment_route
# uri: http://localhost:8001
uri: lb://cloud-payment-service #匹配后提供服务的路由地址
predicates:
- Path=/payment/lb/**
- After=2020-09-19T21:55:08.857+08:00[Asia/Shanghai] # 这个时间之后才能访问
- Cookie=username,zzyy # 携带这个cookie才能访问
eureka:
instance:
hostname: cloud-gateway-service
client:
# 将自己注册进eureka中心
register-with-eureka: true
# 是否抓取注册信息,默认为true
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
携带特定请求头才可访问:
配置header的predicate
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: payment_routh # payment_route 路由的ID,没有固定的规则但要求唯一
# uri: http://localhost:8001 #匹配后提供服务的路径地址
uri: lb://cloud-payment-service #匹配后提供服务的路由地址
predicates:
- Path=/payment/get/** #断言,路径相匹配进行路由
- id: payment_routh2 # payment_route
# uri: http://localhost:8001
uri: lb://cloud-payment-service #匹配后提供服务的路由地址
predicates:
- Path=/payment/lb/**
- After=2020-09-19T21:55:08.857+08:00[Asia/Shanghai] # 这个时间之后才能访问
- Hearder=X-Request-Id, \d+ # 请求头携带这个参数且正则匹配为整数才可访问
eureka:
instance:
hostname: cloud-gateway-service
client:
# 将自己注册进eureka中心
register-with-eureka: true
# 是否抓取注册信息,默认为true
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
GET请求方式才可访问:
配置method的predicate
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: payment_routh # payment_route 路由的ID,没有固定的规则但要求唯一
# uri: http://localhost:8001 #匹配后提供服务的路径地址
uri: lb://cloud-payment-service #匹配后提供服务的路由地址
predicates:
- Path=/payment/get/** #断言,路径相匹配进行路由
- id: payment_routh2 # payment_route
# uri: http://localhost:8001
uri: lb://cloud-payment-service #匹配后提供服务的路由地址
predicates:
- Path=/payment/lb/**
- After=2020-09-19T21:55:08.857+08:00[Asia/Shanghai] # 这个时间之后才能访问
- Method=GET
eureka:
instance:
hostname: cloud-gateway-service
client:
# 将自己注册进eureka中心
register-with-eureka: true
# 是否抓取注册信息,默认为true
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
带参数的Get请求才能访问:
配置query的predicate
这里带上参数username且值为整数的GET请求才能访问
curl get http://localhost:9527/payment/lb?useranme=1
server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: payment_routh # payment_route 路由的ID,没有固定的规则但要求唯一
# uri: http://localhost:8001 #匹配后提供服务的路径地址
uri: lb://cloud-payment-service #匹配后提供服务的路由地址
predicates:
- Path=/payment/get/** #断言,路径相匹配进行路由
- id: payment_routh2 # payment_route
# uri: http://localhost:8001
uri: lb://cloud-payment-service #匹配后提供服务的路由地址
predicates:
- Path=/payment/lb/**
- Method=GET
- Query=username,\d+
eureka:
instance:
hostname: cloud-gateway-service
client:
# 将自己注册进eureka中心
register-with-eureka: true
# 是否抓取注册信息,默认为true
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka