本文主要研究下spring cloud gateway的stripPrefix配置

使用zuul的配置

  1. zuul:
  2. routes:
  3. demo:
  4. sensitiveHeaders: Access-Control-Allow-Origin,Access-Control-Allow-Methods
  5. path: /demo/**
  6. stripPrefix: true
  7. url: http://demo.com.cn/

这里的stripPrefix默认为true,也就是所有/demo/xxxx的请求转发给http://demo.com.cn/xxxx ,去除掉demo前缀

使用spring cloud gateway的配置

  1. spring:
  2. cloud:
  3. gateway:
  4. default-filters:
  5. - AddResponseHeader=X-Response-Default-Foo, Default-Bar
  6. routes:
  7. - id: demo
  8. uri: http://demo.com.cn:80
  9. order: 8999 ## 越小越优先
  10. predicates:
  11. - Path=/demo/**
  12. filters:
  13. - RewritePath=/demo/(?<segment>.*), /$\{segment}

spring cloud gateway貌似没有现成的stripPrefix的配置,不过可以通过rewritepath来实现

spring-cloud-gateway-core-2.0.0.M6-sources.jar!/org/springframework/cloud/gateway/filter/factory/RewritePathGatewayFilterFactory.java

  1. public class RewritePathGatewayFilterFactory implements GatewayFilterFactory {
  2. public static final String REGEXP_KEY = "regexp";
  3. public static final String REPLACEMENT_KEY = "replacement";
  4. @Override
  5. public List<String> argNames() {
  6. return Arrays.asList(REGEXP_KEY, REPLACEMENT_KEY);
  7. }
  8. @Override
  9. public GatewayFilter apply(Tuple args) {
  10. final String regex = args.getString(REGEXP_KEY);
  11. String replacement = args.getString(REPLACEMENT_KEY).replace("$\\", "$");
  12. return apply(regex, replacement);
  13. }
  14. public GatewayFilter apply(String regex, String replacement) {
  15. return (exchange, chain) -> {
  16. ServerHttpRequest req = exchange.getRequest();
  17. addOriginalRequestUrl(exchange, req.getURI());
  18. String path = req.getURI().getPath();
  19. String newPath = path.replaceAll(regex, replacement);
  20. ServerHttpRequest request = mutate(req)
  21. .path(newPath)
  22. .build();
  23. exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, request.getURI());
  24. return chain.filter(exchange.mutate().request(request).build());
  25. };
  26. }
  27. }

主要是这段String newPath = path.replaceAll(regex, replacement),这里相当于regex是/demo/(?<segment>.*),replacement是/${segment}

小结

spring cloud gateway利用RewritePath可以实现原来的zuul的stripPrefix的效果,而且功能更强大。

spring cloud gateway的stripPrefix配置的更多相关文章

  1. API网关spring cloud gateway和负载均衡框架ribbon实战

    通常我们如果有一个服务,会部署到多台服务器上,这些微服务如果都暴露给客户,是非常难以管理的,我们系统需要有一个唯一的出口,API网关是一个服务,是系统的唯一出口.API网关封装了系统内部的微服务,为客 ...

  2. Spring Cloud Alibaba学习笔记(16) - Spring Cloud Gateway 内置的路由谓词工厂

    Spring Cloud Gateway路由配置的两种形式 Spring Cloud Gateway的路由配置有两种形式,分别是路由到指定的URL以及路由到指定的微服务,在上文博客的示例中我们就已经使 ...

  3. Spring Cloud Gateway 跨域 CORS 配置方式实现

    网上找了一堆文章全是说这样写无效 globalcors: cors-configurations: '[/**]': allowCredentials: true allowedOriginPatte ...

  4. Spring Cloud Gateway + Nacos(1)简单配置

    当初我学习时候就是参考这位大佬的博客: Nacos集成Spring Cloud Gateway 基础使用 现在学习到spring cloud alibaba 使用nacos做服务中心,dubbo做通信 ...

  5. CORS跨源资源共享概念及配置(Kubernetes Ingress和Spring Cloud Gateway)

    我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 跨源资源共享CORS 跨源资源共享 (CORS) (或通俗地译为跨域资源共享)是一种基于HTTP 头的机制,该机制通过 ...

  6. Spring Cloud Gateway实战之二:更多路由配置方式

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  7. 从0开始构建你的api网关--Spring Cloud Gateway网关实战及原理解析

    API 网关 API 网关出现的原因是微服务架构的出现,不同的微服务一般会有不同的网络地址,而外部客户端可能需要调用多个服务的接口才能完成一个业务需求,如果让客户端直接与各个微服务通信,会有以下的问题 ...

  8. SpringCloud无废话入门05:Spring Cloud Gateway路由、filter、熔断

    1.什么是路由网关 截至目前为止的例子中,我们创建了一个service,叫做:HelloService,然后我们把它部署到了两台服务器(即提供了两个provider),然后我们又使用ribbon将其做 ...

  9. Nacos整合Spring Cloud Gateway实践

    Spring Cloud Gateway官网:http://spring.io/projects/spring-cloud-gateway Eureka1.0的问题和Nacos对比:https://w ...

随机推荐

  1. 使用有序GUID:提升其在各数据库中作为主键时的性能

    原文出处:https://www.codeproject.com/articles/388157/guids-as-fast-primary-keys-under-multiple-database  ...

  2. 静态代码扫描之阿里java代码规范IDEA插件

    前言 2017年2月9日,首次公布<阿里巴巴Java开发手册>; 2017年9月25日,阿里巴巴集团发布了<阿里巴巴Java开发手册>PDF终极版; 2017年10月14日,在 ...

  3. Linux c codeblock的使用(三):使用函数库

    (一)概念 什么是函数库呢?一下子说概念大家可能不太熟悉,但是这实际上是大家在windows系统上经常见到的东西.没错,就是那些后缀为DLL的文件. linux上实际也有自己的函数库文件,文件类型为. ...

  4. python之路-bytes数据类型

    一. python 3最重要的新特性大概要算是对文本和二进制数据作了更为清晰的区分.文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示.python 3不会以任意隐式的方式混用 ...

  5. vue中父子组件的通信

    1.父组件向子组件传递数据 父组件传递:data = parent.data 子组件接收props: {data:{}} 2.子组件向父组件传递数据(https://vuefe.cn/v2/guide ...

  6. [Leetcode easy]存些水题34、20、700

    leetcode 34 最早出现和最后出现 class Solution { public int[] searchRange(int[] nums, int target) { int []ans= ...

  7. Python 随笔-1

    python的发展史: python 2.7            July 3,2010  目前业内主流使用的工业版本 主讲3.0 32bit = 内存的最大寻址空间为2*32    4G的空间 6 ...

  8. spring cloud微服务下手动回滚事务

    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); 这里使用的场景是,跨服务调用接口,比如:用户信息和用户积分 ...

  9. spring-boot整合mybatis(web mysql logback配置)

    pom.xml相关的配置说明. 配置文件看着比价多,在创建spring-boot项目的时候,自需要添加web,mysql,mybatis三个选项即可 <?xml version="1. ...

  10. kubernetes二进制部署k8s-master集群controller-manager服务unhealthy问题

    一.问题现象 我们使用二进制部署k8s的高可用集群时,在部署多master时,kube-controller-manager服务提示Unhealthy [root@ceph-01 system]# k ...