首页 文章

使用Apache Camel将请求的主机保留到 endpoints

提问于
浏览
2

我在REST API之前使用Apache Camel就像一个智能HTTP代理 . 我有一个配置文件,其中包含要配置的路由,它运行良好 .

为避免复杂性,我将通过以下方式总结代码:

camelContext.addRoutes(new RouteBuilder(){
            @Override
            public void configure() throws Exception {
                from("servlet:///v1.3?matchOnUriPrefix=true")
                    .to("http4://localhost:8080/my-rest-api-v1.3.0?bridgeEndpoint=true&throwExceptionOnFailure=false");
                from("servlet:///v1.2?matchOnUriPrefix=true")
                    .to("http4://localhost:8080/my-rest-api-v1.2.1?bridgeEndpoint=true&throwExceptionOnFailure=false");
            }
        });

我的问题是在 endpoints 服务器上 . 当我从HttpServletRequest中检索请求URL时,它会给我一个“http://localhost:8080/my-rest-api-v1.3.0/resources/companies/ " instead of " http://my.site.com/my-rest-api”(这是我的代理的URL) .

如何将请求的主机名传输到我的 endpoints ?

我没有找到如何使用Apache Camel .

1 回答

  • 0

    HTTP请求在其Header中具有特性(如'host'),并且要在camel中使用此属性,您只需将 localhost:8080 替换为 ${header.host} 并使用recipientList EIP(这样您就可以使用简单语言创建URI):

    camelContext.addRoutes(new RouteBuilder(){
            @Override
            public void configure() throws Exception {
                from("servlet:///v1.3?matchOnUriPrefix=true")
                    .recipientList(simple("http4://${header.host}/my-rest-api-v1.3.0?bridgeEndpoint=true&throwExceptionOnFailure=false"));
                from("servlet:///v1.2?matchOnUriPrefix=true")
                    .recipientList(simple("http4://${header.host}/my-rest-api-v1.2.1?bridgeEndpoint=true&throwExceptionOnFailure=false"));
            }
        });
    

    更新:我根据下一个链接更新了上面的代码:http://camel.apache.org/how-do-i-use-dynamic-uri-in-to.html(要使用动态uri,你必须使用收件人列表EIP) .

相关问题