spring cloud zuul修改请求url的方法
作者:xixicat 时间:2022-09-03 01:44:19
前言
在日常开发中,除了修改请求参数、设置响应header,响应body外,还有一种需求就是url重新,或者是修改url,这里简述一下怎么在zuul修改url。话不多说了,来一起看看详细的介绍吧。
转发配置
demo:
ribbon:
NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
listOfServers: 192.168.99.100,192.168.99.101
zuul:
routes:
demo:
path: /demo/**
stripPrefix: true
serviceId: demo
filter配置
@Component
public class UrlPathFilter extends ZuulFilter{
@Override
public String filterType() {
return FilterConstants.PRE_TYPE;
}
@Override
public int filterOrder() {
return FilterConstants.PRE_DECORATION_FILTER_ORDER + 1;
}
@Override
public boolean shouldFilter() {
final String serviceId = (String) RequestContext.getCurrentContext().get("proxy");
return "demo".equals(serviceId);
}
@Override
public Object run() {
RequestContext context = RequestContext.getCurrentContext();
Object originalRequestPath = context.get(FilterConstants.REQUEST_URI_KEY);
//http://localhost:10000/demo/list/data
//-->/api/prefix/list/data
String modifiedRequestPath = "/api/prefix" + originalRequestPath;
context.put(FilterConstants.REQUEST_URI_KEY, modifiedRequestPath);
return null;
}
}
这样就大功告成了
总结
doc
PreDecorationFilter
Dynamically re-route all Zuul proxied requests to single URL
来源:https://segmentfault.com/a/1190000010749253
标签:springcloud,zuul,请求url
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Cloud 覆写远端的配置属性实例详解
2022-09-10 22:06:23
![](https://img.aspxhome.com/file/2023/6/104026_0s.png)
Android实现列表时间轴
2021-10-19 12:55:04
![](https://img.aspxhome.com/file/2023/4/126674_0s.jpg)
超详细讲解Java异常
2023-10-02 07:30:16
![](https://img.aspxhome.com/file/2023/3/80853_0s.png)
深入剖析java中的集合框架
2022-04-27 18:48:16
![](https://img.aspxhome.com/file/2023/4/125334_0s.png)
Android UI实现SlidingMenu侧滑菜单效果
2021-12-08 16:03:34
![](https://img.aspxhome.com/file/2023/3/138363_0s.jpg)
Java常用JVM参数实战
2023-03-15 10:33:39
Kotlin Navigation可视化开发详解
2022-10-18 10:55:56
![](https://img.aspxhome.com/file/2023/4/111714_0s.png)
Java BufferedImage转换为MultipartFile方式
2022-07-13 10:02:46
mybatis主从表关联查询,返回对象带有集合属性解析
2023-06-15 16:10:59
![](https://img.aspxhome.com/file/2023/6/79236_0s.jpg)
Spring Boot集成MyBatis的方法
2021-11-03 23:11:05
![](https://img.aspxhome.com/file/2023/6/64486_0s.png)
Java使用DateTimeFormatter格式化输入的日期时间
2023-03-09 04:52:38
struts2单个文件上传的两种实现方式
2023-04-23 05:33:30
![](https://img.aspxhome.com/file/2023/4/88734_0s.jpg)
Android控件之SeekBar的用法总结
2022-10-25 00:15:38
![](https://img.aspxhome.com/file/2023/3/137583_0s.png)
Java容器类源码详解 Deque与ArrayDeque
2021-10-15 12:43:21
![](https://img.aspxhome.com/file/2023/4/82444_0s.png)
分享Spring Cloud OpenFeign 的五个优化技巧
2022-06-23 08:53:30
![](https://img.aspxhome.com/file/2023/1/79891_0s.webp)
JavaWeb使用POI操作Excel文件实例
2022-06-12 18:19:30
![](https://img.aspxhome.com/file/2023/5/72385_0s.png)
一篇文章带你深入了解Java基础(2)
2023-12-09 21:06:23
![](https://img.aspxhome.com/file/2023/1/81991_0s.jpg)
Android开发实现的ViewPager引导页功能(动态加载指示器)详解
2021-10-16 17:40:35
![](https://img.aspxhome.com/file/2023/5/138205_0s.gif)
C# 泛型集合类List<T>使用总结
2021-08-30 02:04:01
![](https://img.aspxhome.com/file/2023/0/113780_0s.jpg)
Android仿腾讯视频实现悬浮窗效果
2021-06-16 00:49:23
![](https://img.aspxhome.com/file/2023/8/110728_0s.gif)