spring cloud gateway 2.0

spring cloud gateway 2 深入了解 - Pr

2018-07-12  本文已影响0人  wine_5664

示例项目:https://github.com/14251104246/spring-cloud-gateway-demo/tree/Predicate

spring cloud gateway 通过谓词(Predicate)来匹配来自用户的请求

为了方便,使用postman测试不同的谓词的效果

路径谓词(Predicate)—— 最简单的谓词

spring:
  cloud:
    gateway:
      routes:
        # 匹配指定路径的路由
      - id: path_route
        uri: http://www.baidu.com
        predicates:
        - Path=/baidu/**
        filters:
        - StripPrefix=1
image.png

cookie谓词

spring:
  cloud:
    gateway:
      routes:
        # 匹配指定cookie的路由
      - id: cookie_route
        uri: http://www.taobao.com
        predicates:
        - Cookie=chocolate, value
image.png

请求头谓词

spring:
  cloud:
    gateway:
      routes:
        # 匹配指定请求头的路由
      - id: header_route
        uri: http://www.jd.com/
        predicates:
        - Header=X-Request-Id, \d+
image.png

请求参数谓词

spring:
  cloud:
    gateway:
      routes:
        # 匹配指定请求参数的路由
      - id: cookie_route
        uri: http://tmall.com/
        predicates:
        - Query=param1,value
image.png

其他谓词

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: http://example.org
        predicates:
        - Host=**.somehost.org
spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: http://example.org
        predicates:
        - Method=GET
spring:
  cloud:
    gateway:
      routes:
      - id: remoteaddr_route
        uri: http://example.org
        predicates:
        - RemoteAddr=192.168.1.1/24

谓词组合

spring:
  cloud:
    gateway:
      routes:
        # 匹配指定请求参数和请求头的路由
      - id: compose_route
        uri: http://www.oschina.net/
        predicates:
        - Query=param2
        - Header=Header-Route,value
image.png
上一篇下一篇

猜你喜欢

热点阅读