Spring boot 如何在zuul中进行基于内容的动态路由?

Spring boot 如何在zuul中进行基于内容的动态路由?,spring-boot,routing,microservices,netflix-zuul,Spring Boot,Routing,Microservices,Netflix Zuul,我正在研究一个基于微服务的体系结构,其中包括Spring Boot、Eureka和Zuul的关键级别。我的问题如下: 服务1:/api/v1/Service1 POST 服务2:/api/v2/Service2 POST application.yml看起来像 zuul: routes: service1: path: /api/v1/** service2: path: /api/v1/**

我正在研究一个基于微服务的体系结构,其中包括Spring Boot、Eureka和Zuul的关键级别。我的问题如下:

服务1:/api/v1/Service1 POST
服务2:/api/v2/Service2 POST

application.yml看起来像

zuul: routes: service1: path: /api/v1/** service2: path: /api/v1/** common: path: /common/endpoint 我无法弄清楚如何将请求进一步转发到这些服务,然后获得通过公共端点发送回的响应

我做错了什么?我如何才能继续,这样我就不必使用任何服务的硬编码url。请引导我通过这个

我已经尝试过一些方法,比如:
1.使用DiscoveryClient获取实例并使用上下文的setRouteHost方法。它总是抛出ZumFilter异常URL不正确/在通过DiscoveryClient获得的服务的URL之后追加公共/端点。
2.我尝试了我想到的最愚蠢的事情,使用RestTemplate发出请求并获得响应,然后将其放入上下文响应中,这也不起作用,但是请求被转发到服务,但我不会收到任何响应


感谢您的帮助

您的客户端试图调用哪个URL?我认为你让这件事变得更加困难了。您的客户是否知道其调用的是服务1还是服务2?即使我没有使用它,它也会在末尾附加端点,例如,如果DiscoveryClient生成url,则被调用的url是,这毫无意义。您是否查看过这篇文章@ankurkushwaha,它确实出现在搜索结果中,但它是非常基本的,我已经知道,它没有提供任何可以解决我问题的信息。

public class CommonEndpointFilter extends ZuulFilter {

    @Override
    public boolean shouldFilter() {
        RequestContext ctx = RequestContext.getCurrentContext();
        if ((ctx.get("proxy") != null) && ctx.get("proxy").equals("common")) {
            return true;
        }
        return false;
    }

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();

        InputStream in = (InputStream) ctx.get("requestEntity");
        if (in == null) {
            try {
                in = request.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String body = null;
        try {
            body = StreamUtils.copyToString(in, Charset.forName("UTF-8"));

            if (body.indexOf("somethingrelatedtoservice1") != -1) {
                //forward the request to Service1: /api/v1/service1
            } else if (body.indexOf("somethingrelatedtoservice2") != -1) {
                //forward the request to Service2: /api/v1/service2
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String filterType() {
        return FilterConstants.ROUTE_TYPE;
    }

    @Override
    public int filterOrder() {
        return 0;
    }
}