2015-05-11 68 views
2

我在Apache REST API之前使用Apache Camel,就像智能​​HTTP代理一樣。我有一個配置文件,可以配置路由並且工作得很好。通過Apache Camel保持請求主機端點

爲了避免複雜性,我將summerize代碼:

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"); 
      } 
     }); 

我的問題是端點服務器上。當我從我的HttpServletRequest中檢索請求URL時,它給了我一個「http://localhost:8080/my-rest-api-v1.3.0/resources/companies/」而不是「http://my.site.com/my-rest-api」(這是我的代理的URL)。

如何將請求的主機名傳輸到我的端點?

我不知道如何用Apache Camel做到這一點。

回答

0

HTTP請求都有性的判定(如「主機」)在其頭部,並用駱駝這個屬性,你只需要與${header.host}取代localhost:8080和使用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)。

+0

好的。它似乎在這裏工作。我在Java工作。你在Groovy工作,還是由Camel指定? –

+0

尼古拉斯,你是對的。簡單的語言不適用於(..)。要構建URI,您必須使用recipientList模式:http://camel.apache.org/how-do-i-use-dynamic-uri-in-to.html。我已經更新了我的答案。 –