Gateway

280 阅读1分钟
spring:
   cloud:
    # Spring Cloud Gateway 配置项,对应 GatewayProperties 类
    gateway:
      # 路由配置项,对应 RouteDefinition 数组
      routes:
        - id: yudaoyuanma # 路由的编号
          uri: http://www.iocoder.cn # 路由到的目标地址
          predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
            - Path=/blog
          filters:
            - StripPrefix=1
        - id: oschina # 路由的编号
          uri: https://www.oschina.net # 路由的目标地址
          predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组
            - Path=/oschina
          filters: # 过滤器,对请求进行拦截,实现自定义的功能,对应 FilterDefinition 数组
            - StripPrefix=1

先进行predicates\color{red}{predicates}判断是否满足条件,满足的情况则执行filters\color{red}{filters}进行一些逻辑处理,最后路由到定义好的uri\color{red}{uri}

predicates

  • 配置文件中只需要填写前缀 命名规范为xxxRoutePredicateFactory
    例如Path 对应的PathRoutePredicateFactory

filters

  • 配置文件中只需要填写前缀 命名规范为xxxGatewayFilterFactory
    例如StripPrefix 对应的StripPrefixGatewayFilterFactory

自定义类

继承自AbstractGatewayFilterFactory T为内部类,为传参对象 StripPrefix=1 其中1为parts的值