时间:2020-11-18 00:19:42 | 栏目:JAVA代码 | 点击:次
前言
在日常开发中,除了修改请求参数、设置响应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
Dynamically re-route all Zuul proxied requests to single URL