zl程序教程

您现在的位置是:首页 >  后端

当前栏目

每天20分钟之spring-cloud-gateway基础三自定义断言工厂

SpringCloudgateway基础 自定义 20 分钟 工厂
2023-06-13 09:11:18 时间

各类断言工厂(路由判断)

path路由断言工厂

  • 配置
        - id: pathInfo
          uri: http://www.example.com/
          predicates:
            - Path=/abcd/{segment}
  • 访问地址:http://127.0.0.1:10011/abcd/12312

query路由断言工厂

  • 配置
        - id: queryInfo
          uri: http://www.example.com/
          predicates:
            - Query= foo,bb
  • 访问地址 http://127.0.0.1:10011/abcdeeee?foo=bb

method路由断言工厂`

  • 配置
- id: methodnfo
          uri: http://www.example.com/
          predicates:
            - Method= DELETE
  • 访问地址:
curl --location --request DELETE 'http://127.0.0.1:10011/adfasfsdfdsd'

head 路由断言工厂

  • 配置
        - id: headinfo
          uri: http://www.example.com/
          predicates:
            - Header=x-ragnar-traceid,[\w\d]+
  • 访问地址
curl --location --request GET 'http://127.0.0.1:10011/adfasfsdfdsd12312' \
--header 'x-ragnar-traceid: 123213123'

自定义路由断言工厂

自定义断言工厂代码

@Slf4j
@Component
public class GrayRoutePredicateFactory extends AbstractRoutePredicateFactory<GrayCfg> {

    public GrayRoutePredicateFactory() {
        super(GrayCfg.class);
    }

    @Override
    public Predicate<ServerWebExchange> apply(GrayCfg cfg) {

        return serverWebExchange -> {
            log.info("enter GrayRoutePredicateFactory"+cfg.isGrayStatus());
            if (cfg.isGrayStatus()) {
                log.info(" GrayRoutePredicateFactory hit   start gray");
                return true;
            }

            return false;
        };
    }
}

自定义断言工厂配置

        - id: grayinfo
          uri: http://www.baidu.com/
          predicates:
            - Path=/eee/**
            - name: Gray
              args:
                grayStatus: true