0


springcloud gateway 配置predicates 多个path;路由请求头新增参数

一、 在一个微服务节点的predicates下配置多个path: - Path=/test/batis/test,/test/testJmeter,具体配置如下:

spring:
  
  application:
    name: xxxx
  profiles:
    active: dev
  cloud:
    inetutils:
      preferred-networks: ^192\.168\.[\d]\.[\d]+$
      
    gateway:
      enabled: true
      httpclient:
        connect-timeout: 1000
        response-timeout: 5s

      default-filters:
        - StripPrefix=0
        - name: Hystrix
          args:
            name: fallbackcmd
            fallbackUri: forward:/fallback

      discovery:
        locator:
          # 是否和服务注册与发现组件结合,设置为 true 后可以直接使用应用名称调用服务
          enabled: true
      # 路由(routes:路由,它由唯一标识(ID)、目标服务地址(uri)、一组断言(predicates)和一组过滤器组成(filters)。filters 不是必需参数。)
      routes:
        - id: management-admin
          uri: lb://management-admin
          predicates:
            - Path=/admin/**,/openbase/**

二、 在请求头中新增header参数,yml文件配置如下:

spring:
  application:
    name: openapi-gate-way
  profiles:
    active: dev
  cloud:
    inetutils:
      preferred-networks: ^192\.168\.[\d]\.[\d]+$
    gateway:
      enabled: true
      httpclient:
        connect-timeout: 3000
        response-timeout: 5s

      default-filters:
        - StripPrefix=0
        - name: Hystrix
          args:
            name: fallbackcmd
            fallbackUri: forward:/fallback

      routes:
        
        - id: openapi-base          
          uri: lb://openapi-base         
          predicates:            
            - Path=/openbase/**
          filters:
            # 新增请求头参数
            - AddRequestHeader=name, 小明

三、自定义filter中新增请求头参数:

@Component
public class DemoFilter implements GatewayFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    
        ServerHttpRequest request = exchange.getRequest().mutate()
                .header("name", "xxx")
                .header("age", 12)
                .build();

        return chain.filter(exchange.mutate().request(request).build());
    }

    @Override
    public int getOrder() {
        return 1;
    }
}

本文转载自: https://blog.csdn.net/cn_yaojin/article/details/130868939
版权归原作者 cn_yaojin 所有, 如有侵权,请联系我们删除。

“springcloud gateway 配置predicates 多个path;路由请求头新增参数”的评论:

还没有评论