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

使用zuul的配置

zuul:
routes:
demo:
sensitiveHeaders: Access-Control-Allow-Origin,Access-Control-Allow-Methods
path: /demo/**
stripPrefix: true
url: http://demo.com.cn/

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

使用spring cloud gateway的配置

spring:
cloud:
gateway:
default-filters:
- AddResponseHeader=X-Response-Default-Foo, Default-Bar
routes:
- id: demo
uri: http://demo.com.cn:80
order: 8999 ## 越小越优先
predicates:
- Path=/demo/**
filters:
- 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

public class RewritePathGatewayFilterFactory implements GatewayFilterFactory {

	public static final String REGEXP_KEY = "regexp";
public static final String REPLACEMENT_KEY = "replacement"; @Override
public List<String> argNames() {
return Arrays.asList(REGEXP_KEY, REPLACEMENT_KEY);
} @Override
public GatewayFilter apply(Tuple args) {
final String regex = args.getString(REGEXP_KEY);
String replacement = args.getString(REPLACEMENT_KEY).replace("$\\", "$");
return apply(regex, replacement);
} public GatewayFilter apply(String regex, String replacement) {
return (exchange, chain) -> {
ServerHttpRequest req = exchange.getRequest();
addOriginalRequestUrl(exchange, req.getURI());
String path = req.getURI().getPath();
String newPath = path.replaceAll(regex, replacement); ServerHttpRequest request = mutate(req)
.path(newPath)
.build(); exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, request.getURI()); return chain.filter(exchange.mutate().request(request).build());
};
}
}

主要是这段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. 通过编写一个简单的日志类库来加深了解C#的文件访问控制

    在程序的开发调试过程及发布运行后的状态监控中,日志都有着极其重要的分量,通过在关键逻辑节点将关键数据记录到日志文件当中能帮助我们尽快找到程序问题所在.网上有不少专业成熟的日志组件可用,比如log4ne ...

  2. php 查询mysql数据批量转为PDF文件一(mac使用配置wkhtmltopdf html导出PDF)

    数据转标准PDF查文档,查资料先转HTML标准格式再html转PDF 转PDF wkhtmltopdf工具是最佳选择 首先下载wkhtmltopdf https://wkhtmltopdf.org/d ...

  3. [NOIP2014D1]

    T1 Problem 洛谷 Solution 一道非常裸的模拟题.直接枚举每次猜拳就可以了. Code #include<cmath> #include<cstdio> #in ...

  4. validation-api各注解的用法

    入参用@Valid,要不下面实体类中的注解不生效 @AssertFalse 被注解的元素必须为false@AssertTrue 被注解的元素必须为True@DecimalMax(value) 被注解的 ...

  5. Java final类&所有构造方法均为private的类(类型说明符&访问控制符)

    1. final是类型说明符,表示关闭继承,即final类不能有子类: 但final类可能可以在类外创建对象(即final类的构造方法可以不是private型): 在同一包中时,可以在任何另外一个类中 ...

  6. PHP运算符知识

    1.三目运算符: $a =1; echo $a>0 ? '大于0':$a==0 ? '等于0':'小于0'; 貌似应该输出:大于0 其实: 然而,上面语句的实际输出是't',因为三元运算符是从左 ...

  7. 开发一个简单的chrome插件-解析本地markdown文件

    准备软件环境 1. 软件环境 首先,需要使用到的软件和工具环境如下: 一个最新的chrome浏览器 编辑器vscode 2. 使用的js库 代码高亮库:prismjs https://prismjs. ...

  8. 浅谈Object.assign()

    Object.assign()方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象.返回值为目标对象. 1 Object.assign 是 ES6 新添加的接口,主要的用途是用来合并多个 Ja ...

  9. ImportError: No module named _tkinter, please install the python-tk package ubuntu运行tkinter错误

    这是由于Python的版本没有包含tkinter的模块,只需要把tk的package安装就可以了. 一般在Linux才出现,windows版本一般已经包含了tkinter模块. apt-get ins ...

  10. Sublime Text3 调色板 ColorPicker插件安装及快捷键

    一.安装 第一步:打开菜单栏下的tools>command palette或是快捷键ctrl+shift+p输入PI 点击第一个安装包等待跳出窗口,输入ColorPicker,待安装完成 第二步 ...