spring cloud gateway的stripPrefix配置
序
本文主要研究下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配置的更多相关文章
- API网关spring cloud gateway和负载均衡框架ribbon实战
通常我们如果有一个服务,会部署到多台服务器上,这些微服务如果都暴露给客户,是非常难以管理的,我们系统需要有一个唯一的出口,API网关是一个服务,是系统的唯一出口.API网关封装了系统内部的微服务,为客 ...
- Spring Cloud Alibaba学习笔记(16) - Spring Cloud Gateway 内置的路由谓词工厂
Spring Cloud Gateway路由配置的两种形式 Spring Cloud Gateway的路由配置有两种形式,分别是路由到指定的URL以及路由到指定的微服务,在上文博客的示例中我们就已经使 ...
- Spring Cloud Gateway 跨域 CORS 配置方式实现
网上找了一堆文章全是说这样写无效 globalcors: cors-configurations: '[/**]': allowCredentials: true allowedOriginPatte ...
- Spring Cloud Gateway + Nacos(1)简单配置
当初我学习时候就是参考这位大佬的博客: Nacos集成Spring Cloud Gateway 基础使用 现在学习到spring cloud alibaba 使用nacos做服务中心,dubbo做通信 ...
- CORS跨源资源共享概念及配置(Kubernetes Ingress和Spring Cloud Gateway)
我最新最全的文章都在南瓜慢说 www.pkslow.com,欢迎大家来喝茶! 1 跨源资源共享CORS 跨源资源共享 (CORS) (或通俗地译为跨域资源共享)是一种基于HTTP 头的机制,该机制通过 ...
- Spring Cloud Gateway实战之二:更多路由配置方式
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- 从0开始构建你的api网关--Spring Cloud Gateway网关实战及原理解析
API 网关 API 网关出现的原因是微服务架构的出现,不同的微服务一般会有不同的网络地址,而外部客户端可能需要调用多个服务的接口才能完成一个业务需求,如果让客户端直接与各个微服务通信,会有以下的问题 ...
- SpringCloud无废话入门05:Spring Cloud Gateway路由、filter、熔断
1.什么是路由网关 截至目前为止的例子中,我们创建了一个service,叫做:HelloService,然后我们把它部署到了两台服务器(即提供了两个provider),然后我们又使用ribbon将其做 ...
- Nacos整合Spring Cloud Gateway实践
Spring Cloud Gateway官网:http://spring.io/projects/spring-cloud-gateway Eureka1.0的问题和Nacos对比:https://w ...
随机推荐
- git提交步骤
1,为了确定在本地分支下操作,可以用命令查看一下是否在本地分支 git branch 2,可以查看状态,是否添加了哪些内容 git status 3,如果确认无误,使用命令进行提交本地代码,并加上注释 ...
- SSM--spring框架
他是SpringFramework创始人,interface21 CEO Spring 的作者:Rod Johnson 一 :Spring的核心IOC和AOP(本处详解IOC) IOC:控制反转:( ...
- maven项目的配置
软件151 王帅 1.增加web.xml Maven项目最重要的配置文件是pom.xml,pom是“项目对象模型”的意思.现在pom.xml中有一个错误,提示缺少web.xml: 展开目录src—m ...
- python操作samba
最近在部署完xxl-job后,陆续将一些日常性执行的python脚本迁移到上面去:其中部分脚本涉及到对samaba的操作,先后尝试了pysmb.fs.smbfs.pysmbclient pysmb 安 ...
- MarkdownPan2 简单使用指南
markdown 简单使用指南 一级标题 二级标题 三级标题加代码 四级标题 这里是加粗 这里是正文and English 888 这里有正文嵌入代码这种样式 这里是代码块 这种使用的代码块 还有引用 ...
- Java 平时作业六
编写一个 Java 应用程序,使用 Java 的输入输出流技术将 Input.txt 的内容(Input.txt 为文本 文件)逐行读出, 每读出一行就顺序为其添加行号(从 1 开始,逐行递增),并写 ...
- SQL server 一些小结
数据库表常用术语 关系 关系即二维表,每一个关系都有一个关系名,就是表名记录 表中的行字段 表中的列 也称属性域 取值范围关联 不同数据库表之间的数据联系关键字 属性或属性的组合,可以用于唯一标识一条 ...
- browser_action' is only allowed for extensions but this is a legacy packaged app
manifest.json中不可包含调试代码: "app": { "launch": { "local_path": ...
- nim调用GetSystemPowerStatus判断笔记本电脑是否接通外接电源
title: nim调用GetSystemPowerStatus判断笔记本电脑是否接通外接电源 tags:nim,winapi,dll --- 转载条件:如果你需要转载本文,你需要做到完整转载本文所有 ...
- 2018-2019-2 《网络对抗技术》Exp2 后门原理与实践 Week3 20165326
Exp2 后门原理与实践 目录 1. 实验原理 2. 实验内容 3. 问题回答 4. 心得体会 实验原理简介 后门:不经过正常流程而访问系统的通道 ncat:底层工具,进行基本的TCP UDP数据收发 ...