server:
port: 9527
spring:
application:
name: cloud-gateway
cloud:
gateway:
discovery:
locator:
enabled: true #开启从注册中心动态创建路由的功能, 利用微服务名进行路由
rout es:
- id: payment_routh #路由的ID, 没有固定规则但要求唯一, 建议配合服务名
uri: lb://cloud-payment-service #匹配后提供服务的路由地址
predicates:
- Path=/payment/get/** #断言, 路径相匹配的进行路由.
- id: payment_routh2
uri: lb://cloud-payment-service
predicates:
- Path=/payment/lb/**
eureka:
instance:
hostname: cloud-gateway-service
client:
service-url:
register-with-eureka: true
fetch-registry: true
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
predicates:
- Path=/payment/lb/**
- After=2020-07-02T17:18:55.216+08:00[Asia/Shanghai]
public static void main(String[] args) { ZonedDateTime zbj = ZonedDateTime.now(); //默认时区 System.out.println(zbj); //2020-07-02T17:18:55.216+08:00[Asia/Shanghai] }
predicates:
- Path=/payment/lb/**
- Before=2020-08-02T17:18:55.216+08:00[Asia/Shanghai]
predicates:
- Path=/payment/lb/**
- Between=2020-08-08T10:59:34.102+08:00[Asia/Shanghai], 2020-09-08T10:59:34.102+08:00[Asia/Shanghai]
predicates:
- Path=/payment/lb/**
- Cookie=username, aakk
predicates:
- Path=/payment/lb/**
- Header=X-Request-Id, \d+ #请求头要由X-Request-Id的属性, 值为整数
predicates:
- Path=/payment/lb/**
- Host=**.somehost.org, **.anotherhost.org
predicates:
- Path=/payment/lb/**
- Method=GET
predicates:
- Path=/payment/lb/**
predicates:
- Path=/payment/lb/**
- Query=username, \d+ #要有参数名username并且值还必须时整数才能访问
@Slf4j @Configuration public class GatewayConfig { @Bean @Order(0) //优先级 public GlobalFilter myGlobalFilter() { return (exchange, chain) -> { log.info("****come in MyLogGatewayFilter" + new Date()); String uname = exchange.getRequest().getQueryParams().getFirst("uname"); if(uname == null) { log.info("*****用户名为null, 非法用户"); exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE); return exchange.getResponse().setComplete(); } return chain.filter(exchange); }; } }
原文:https://www.cnblogs.com/binwenhome/p/13226189.html