如何在 Zuul 代理中进行 URL 重写?
来到我的 Zuul 过滤器的请求之一是 URI /hello/World,我想将其重定向到 /myapp/test.这个/myapp/test是在Eureka注册的服务.
One of the request that comes to my Zuul Filter is of URI /hello/World which i want to redirect to /myapp/test. This /myapp/test is a service that is registered in Eureka.
zuul:
routes:
xyz:
path: /hello/World
url: http://localhost:1234/myapp/test
stripPrefix: true
当我尝试上述配置时,传入的 URI 后缀为配置的 URL,如 http://localhost:1234/myapp/test/World.我遇到的一些链接似乎表明 Zuul 中还没有 URL 重写功能.
When i try the above configuration, the incoming URI is suffixed to the configured URL like http://localhost:1234/myapp/test/World . Few of the links which i came across seem to be stating that URL Rewrite feature is not yet available in Zuul.
在 Zuul 层有没有其他方法可以做到这一点?
Is there any other way this can be done at the Zuul Layer ?
注意:此时,我无法在 Web 服务器或任何其他层中执行此反向代理,因为我的 Zuul 过滤器是直接接收请求的过滤器.
Note: At this point of time, i cannot do this reverse proxying in the Webserver or any other layer since, my Zuul filter is the one that is receiving the request directly.
使用 @Adelin 解决方案,几乎没有改进
Using @Adelin solution, with little improvements
使用 'url' 属性作为自定义 Url 重写的路径(我在示例中禁用了 Eureka):
Use 'url' property as path to prepend for customizing the Url rewriting (I have disabled Eureka in my example) :
ribbon.eureka.enabled=false
zuul.routes.route1.path=/route1/**
zuul.routes.route1.serviceId=service1
zuul.routes.route1.url=/path/to/prepend
service1.ribbon.listOfServers=http://server1
然后实现以下过滤器:
/**
* Fixing missing URL rewriting when using ribbon
*/
@Component
public class CustomPathZuulFilter extends ZuulFilter {
@Autowired
private ZuulProperties zuulProperties;
@Override
public String filterType() {
return FilterConstants.PRE_TYPE;
}
@Override
public int filterOrder() {
return FilterConstants.PRE_DECORATION_FILTER_ORDER + 1;
}
@Override
public boolean shouldFilter() {
// override PreDecorationFilter only if executed previously successfully
return RequestContext.getCurrentContext().getFilterExecutionSummary().toString()
.contains("PreDecorationFilter[SUCCESS]");
}
@Override
public Object run() {
final RequestContext context = RequestContext.getCurrentContext();
if (context.get(FilterConstants.SERVICE_ID_KEY) == null || context.getRouteHost() != null) {
// not a Ribbon route
return null;
}
// get current ZuulRoute
final String proxy = (String) context.get(FilterConstants.PROXY_KEY);
final ZuulRoute zuulRoute = this.zuulProperties.getRoutes().get(proxy);
// patch URL by prefixing it with zuulRoute.url
final Object originalRequestPath = context.get(FilterConstants.REQUEST_URI_KEY);
final String modifiedRequestPath = zuulRoute.getUrl() + originalRequestPath;
context.put(FilterConstants.REQUEST_URI_KEY, modifiedRequestPath);
// patch serviceId because :
// - has been set to route.location in PreDecorationFilter
// - route.location has been set to zuulRoute.location in SimpleRouteLocator
// - zuulRoute.location return zuulRoute.url if set
context.set(FilterConstants.SERVICE_ID_KEY, zuulRoute.getServiceId());
return null;
}
}
现在对 /route1 的调用将被代理到 http://server1/path/to/prepend
Now calls to /route1 will be proxified to http://server1/path/to/prepend
此解决方案还与不使用 Ribbon 的共存路由兼容.
This solution is also compatible with co-existing routes not using Ribbon.
不使用 Ribbon 的共存路由示例:
Example of a co-existing route not using Ribbon :
zuul.routes.route2.path=/route2/**
zuul.routes.route2.url=http://server2/some/path
对 /route2 的调用将被代理到 http://server2/some/path 由 SimpleHostRoutingFilter(如果未禁用)
Calls to /route2 will be proxified to http://server2/some/path by SimpleHostRoutingFilter (if not disabled)