如何在Zuul代理中进行URL重写?

时间:2015-08-05 10:24:29

标签: url-rewriting rewrite netflix-zuul

我的Zuul过滤器的一个请求是URI / hello / World ,我想重定向到 / myapp / test 即可。此/ myapp / test是在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重写功能。

还有其他方法可以在Zuul层完成吗?

注意:此时,我无法在Web服务器或任何其他层执行此反向代理,因为我的Zuul过滤器是直接接收请求的过滤器。

3 个答案:

答案 0 :(得分:1)

这是@Vikash链接中的已发布解决方案

@Component
public class CustomPathZuulFilter extends ZuulFilter
{
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return PreDecorationFilter.FILTER_ORDER + 1;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext context = RequestContext.getCurrentContext();
        Object originalRequestPath = context.get(REQUEST_URI_KEY);
        String modifiedRequestPath = "/api/microservicePath" + originalRequestPath;
        context.put(REQUEST_URI_KEY, modifiedRequestPath);

        return null;
    }
}

答案 1 :(得分:0)

您是否尝试过创建过滤器甚至路径过滤器?

这样你就可以拦截请求,并改变路由。

请参阅Zuul Filters

答案 2 :(得分:0)

使用@Adelin解决方案,几乎没有改善

使用'url'属性作为添加自定义Url重写的路径(在我的示例中,我禁用了Eureka):

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

此解决方案还与不使用功能区的共存路由兼容。

不使用功能区的共存路由示例:

zuul.routes.route2.path=/route2/**
zuul.routes.route2.url=http://server2/some/path

/ route2 的调用将由SimpleHostRoutingFilter(如果未禁用)代理为 http://server2/some/path