Routes 当标头值为时,骆驼路由打开

Routes 当标头值为时,骆驼路由打开,routes,apache-camel,esb,Routes,Apache Camel,Esb,我想根据RESTURL模式将消息路由到不同的服务,我在myRouteBuilder类中的路由定义如下: String module1RestUrls = "/client,/price,/client/add,/client/hello"; String module2RestUrls = "/order,/order/invoice,/suppliers"; from("jetty:http://192.168.1.1?matchOnUriPrefix=true&traceE

我想根据RESTURL模式将消息路由到不同的服务,我在myRouteBuilder类中的路由定义如下:

String module1RestUrls = "/client,/price,/client/add,/client/hello";
String module2RestUrls = "/order,/order/invoice,/suppliers";

from("jetty:http://192.168.1.1?matchOnUriPrefix=true&traceEnabled=true")
   .process(customProcessor)
     .choice()
       .when(simple("${module1RestUrls} contains ${header('CamelHttpUri')}"))
           .to("http4://" + module1Url + "?bridgeEndpoint=true")
       .when(simple("${module2RestUrls} contains ${header('CamelHttpUri')}"))
           .to("http4://" + module2Url + "?bridgeEndpoint=true")
       .otherwise()
           .to("http4://" + genericUrl + "?bridgeEndpoint=true");
from("jetty:http://"+myUrl+"?matchOnUriPrefix=true&traceEnabled=true")      
   .process(customProcessor)
     .choice()
    .when(simple("${in.headers.firstUrn} contains ${in.headers.CamelHttpUri}"))
  .to("http4://" + first1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .when(simple("${in.headers.secondUrn} contains ${in.headers.CamelHttpUri}"))
      .to("http4://" + second1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .otherwise()
      .to("http4://" + thirdUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");
public void process(Exchange exchange) throws Exception {       
    String[] first1Url = messageSource.getMessage("first1Url", null, null).split(",");
    String[] second1Url= messageSource.getMessage("second1Url", null, null).split(",");

    exchange.getIn().setHeader("firstUrn ", first1Url );
    exchange.getIn().setHeader("secondUrn ", second1Url);

    Map<String, Object> headers = exchange.getIn().getHeaders();     

    exchange.getOut().setHeaders(headers);        
    exchange.getOut().setBody(exchange.getIn().getBody(String.class), String.class);
}
module1RestUrls和module2RestUrls目前是硬编码的,但稍后可以从属性文件加载。 我的问题是我一直在

Caused by: org.apache.camel.language.simple.types.SimpleIllegalSyntaxException: Unknown   
function: module1RestUrls at location 0
${module1RestUrls} contains ${header('CamelHttpUri')}
*
from("jetty:http://"+myUrl+"?matchOnUriPrefix=true&amp;traceEnabled=true")      
   .process(customProcessor)
     .choice()
    .when(simple("${in.headers.firstUrn} contains ${in.headers.CamelHttpUri}"))
  .to("http4://" + first1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .when(simple("${in.headers.secondUrn} contains ${in.headers.CamelHttpUri}"))
      .to("http4://" + second1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .otherwise()
      .to("http4://" + thirdUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");
如果有人能帮忙,那就太好了。我在谷歌上搜索并尝试了不同的方法,但似乎没有任何效果

from("jetty:http://"+myUrl+"?matchOnUriPrefix=true&amp;traceEnabled=true")      
   .process(customProcessor)
     .choice()
    .when(simple("${in.headers.firstUrn} contains ${in.headers.CamelHttpUri}"))
  .to("http4://" + first1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .when(simple("${in.headers.secondUrn} contains ${in.headers.CamelHttpUri}"))
      .to("http4://" + second1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .otherwise()
      .to("http4://" + thirdUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");
全轨迹跟踪--------------

from("jetty:http://"+myUrl+"?matchOnUriPrefix=true&amp;traceEnabled=true")      
   .process(customProcessor)
     .choice()
    .when(simple("${in.headers.firstUrn} contains ${in.headers.CamelHttpUri}"))
  .to("http4://" + first1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .when(simple("${in.headers.secondUrn} contains ${in.headers.CamelHttpUri}"))
      .to("http4://" + second1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .otherwise()
      .to("http4://" + thirdUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");

根据堆栈,似乎无法用局部变量替换${module1RestUrls}。 您是否尝试用以下方式表达:

header('CamelHttpUri').in("/client","/price","/client/add","/client/hello")
from("jetty:http://"+myUrl+"?matchOnUriPrefix=true&amp;traceEnabled=true")      
   .process(customProcessor)
     .choice()
    .when(simple("${in.headers.firstUrn} contains ${in.headers.CamelHttpUri}"))
  .to("http4://" + first1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .when(simple("${in.headers.secondUrn} contains ${in.headers.CamelHttpUri}"))
      .to("http4://" + second1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .otherwise()
      .to("http4://" + thirdUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");
如果需要外部化这些变量,可以从属性值构建一个字符串数组,并在in(…)中使用它

from("jetty:http://"+myUrl+"?matchOnUriPrefix=true&amp;traceEnabled=true")      
   .process(customProcessor)
     .choice()
    .when(simple("${in.headers.firstUrn} contains ${in.headers.CamelHttpUri}"))
  .to("http4://" + first1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .when(simple("${in.headers.secondUrn} contains ${in.headers.CamelHttpUri}"))
      .to("http4://" + second1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .otherwise()
      .to("http4://" + thirdUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");

例如,您可以使用Spring将属性值注入字符串中,然后使用正则表达式对其进行解析以生成字符串数组。

鉴于您正在使用JAVA DSL,请执行以下操作:

PropertiesComponent pc = new PropertiesComponent();
pc.setLocation("camel.properties"); // the path to your properties file
context.addComponent("properties", pc);
<bean id="bridgePropertyPlaceholder"
    class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
    <property name="location" value="classpath:camel.properties" />
</bean>
from("jetty:http://"+myUrl+"?matchOnUriPrefix=true&amp;traceEnabled=true")      
   .process(customProcessor)
     .choice()
    .when(simple("${in.headers.firstUrn} contains ${in.headers.CamelHttpUri}"))
  .to("http4://" + first1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .when(simple("${in.headers.secondUrn} contains ${in.headers.CamelHttpUri}"))
      .to("http4://" + second1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .otherwise()
      .to("http4://" + thirdUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");
或者,在Spring DSL中,您可以如下指示属性文件:

PropertiesComponent pc = new PropertiesComponent();
pc.setLocation("camel.properties"); // the path to your properties file
context.addComponent("properties", pc);
<bean id="bridgePropertyPlaceholder"
    class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
    <property name="location" value="classpath:camel.properties" />
</bean>
from("jetty:http://"+myUrl+"?matchOnUriPrefix=true&amp;traceEnabled=true")      
   .process(customProcessor)
     .choice()
    .when(simple("${in.headers.firstUrn} contains ${in.headers.CamelHttpUri}"))
  .to("http4://" + first1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .when(simple("${in.headers.secondUrn} contains ${in.headers.CamelHttpUri}"))
      .to("http4://" + second1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .otherwise()
      .to("http4://" + thirdUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");
其中module1RestUrls是包含这些字符串值列表的属性文件中的键

from("jetty:http://"+myUrl+"?matchOnUriPrefix=true&amp;traceEnabled=true")      
   .process(customProcessor)
     .choice()
    .when(simple("${in.headers.firstUrn} contains ${in.headers.CamelHttpUri}"))
  .to("http4://" + first1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .when(simple("${in.headers.secondUrn} contains ${in.headers.CamelHttpUri}"))
      .to("http4://" + second1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .otherwise()
      .to("http4://" + thirdUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");
另外,我认为contains只是尝试匹配字符串,而不是真正在列表中搜索字符串。正如我上面所说的,这就是你要找的

from("jetty:http://"+myUrl+"?matchOnUriPrefix=true&amp;traceEnabled=true")      
   .process(customProcessor)
     .choice()
    .when(simple("${in.headers.firstUrn} contains ${in.headers.CamelHttpUri}"))
  .to("http4://" + first1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .when(simple("${in.headers.secondUrn} contains ${in.headers.CamelHttpUri}"))
      .to("http4://" + second1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .otherwise()
      .to("http4://" + thirdUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");

谢谢

谢谢你,很好,我是这样做的

from("jetty:http://"+myUrl+"?matchOnUriPrefix=true&amp;traceEnabled=true")      
   .process(customProcessor)
     .choice()
    .when(simple("${in.headers.firstUrn} contains ${in.headers.CamelHttpUri}"))
  .to("http4://" + first1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .when(simple("${in.headers.secondUrn} contains ${in.headers.CamelHttpUri}"))
      .to("http4://" + second1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .otherwise()
      .to("http4://" + thirdUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");
我在MyRouteBuilder扩展的RouteBuilder类上添加了以下内容

from("jetty:http://"+myUrl+"?matchOnUriPrefix=true&amp;traceEnabled=true")      
   .process(customProcessor)
     .choice()
    .when(simple("${in.headers.firstUrn} contains ${in.headers.CamelHttpUri}"))
  .to("http4://" + first1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .when(simple("${in.headers.secondUrn} contains ${in.headers.CamelHttpUri}"))
      .to("http4://" + second1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .otherwise()
      .to("http4://" + thirdUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");
我编写了customProcessor,如下所示

String module1RestUrls = "/client,/price,/client/add,/client/hello";
String module2RestUrls = "/order,/order/invoice,/suppliers";

from("jetty:http://192.168.1.1?matchOnUriPrefix=true&amp;traceEnabled=true")
   .process(customProcessor)
     .choice()
       .when(simple("${module1RestUrls} contains ${header('CamelHttpUri')}"))
           .to("http4://" + module1Url + "?bridgeEndpoint=true")
       .when(simple("${module2RestUrls} contains ${header('CamelHttpUri')}"))
           .to("http4://" + module2Url + "?bridgeEndpoint=true")
       .otherwise()
           .to("http4://" + genericUrl + "?bridgeEndpoint=true");
from("jetty:http://"+myUrl+"?matchOnUriPrefix=true&amp;traceEnabled=true")      
   .process(customProcessor)
     .choice()
    .when(simple("${in.headers.firstUrn} contains ${in.headers.CamelHttpUri}"))
  .to("http4://" + first1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .when(simple("${in.headers.secondUrn} contains ${in.headers.CamelHttpUri}"))
      .to("http4://" + second1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .otherwise()
      .to("http4://" + thirdUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");
public void process(Exchange exchange) throws Exception {       
    String[] first1Url = messageSource.getMessage("first1Url", null, null).split(",");
    String[] second1Url= messageSource.getMessage("second1Url", null, null).split(",");

    exchange.getIn().setHeader("firstUrn ", first1Url );
    exchange.getIn().setHeader("secondUrn ", second1Url);

    Map<String, Object> headers = exchange.getIn().getHeaders();     

    exchange.getOut().setHeaders(headers);        
    exchange.getOut().setBody(exchange.getIn().getBody(String.class), String.class);
}
现在问题解决了,AWS beanstalk和camel在调查这方面还有其他问题

from("jetty:http://"+myUrl+"?matchOnUriPrefix=true&amp;traceEnabled=true")      
   .process(customProcessor)
     .choice()
    .when(simple("${in.headers.firstUrn} contains ${in.headers.CamelHttpUri}"))
  .to("http4://" + first1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .when(simple("${in.headers.secondUrn} contains ${in.headers.CamelHttpUri}"))
      .to("http4://" + second1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .otherwise()
      .to("http4://" + thirdUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");
---只是更新一下, 更好的解决方案是使用Ralf建议的recipientList模式

from("jetty:http://"+myUrl+"?matchOnUriPrefix=true&amp;traceEnabled=true")      
   .process(customProcessor)
     .choice()
    .when(simple("${in.headers.firstUrn} contains ${in.headers.CamelHttpUri}"))
  .to("http4://" + first1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .when(simple("${in.headers.secondUrn} contains ${in.headers.CamelHttpUri}"))
      .to("http4://" + second1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .otherwise()
      .to("http4://" + thirdUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");
在扩展的RouteBuilder上

from("jetty:http://"+myUrl+"?matchOnUriPrefix=true&amp;traceEnabled=true")      
   .process(customProcessor)
     .choice()
    .when(simple("${in.headers.firstUrn} contains ${in.headers.CamelHttpUri}"))
  .to("http4://" + first1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .when(simple("${in.headers.secondUrn} contains ${in.headers.CamelHttpUri}"))
      .to("http4://" + second1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .otherwise()
      .to("http4://" + thirdUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");
from("servlet:///?matchOnUriPrefix=true") 
    .process(customPreProcessor) 
        .recipientList(header("urlNew"))
                .process(customPostProcessor);
在客户预处理器上,我们有以下内容:

from("jetty:http://"+myUrl+"?matchOnUriPrefix=true&amp;traceEnabled=true")      
   .process(customProcessor)
     .choice()
    .when(simple("${in.headers.firstUrn} contains ${in.headers.CamelHttpUri}"))
  .to("http4://" + first1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .when(simple("${in.headers.secondUrn} contains ${in.headers.CamelHttpUri}"))
      .to("http4://" + second1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .otherwise()
      .to("http4://" + thirdUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");
public void process(Exchange exchange) throws Exception {

    String CamelHttpUri = (String) exchange.getIn().getHeader("CamelHttpUri");
    String CamelHttpQuery = (String) exchange.getIn().getHeader("CamelHttpQuery");
    String urlReceived = CamelHttpUri;
    StringBuilder urlToMatch = new StringBuilder();
    urlToMatch.append(urlReceived);

    if (!StringUtils.isEmpty(CamelHttpQuery)) {
        urlToMatch.append("?").append(CamelHttpQuery);
    }
    String endpointUrl = getEndPointUrl(serviceUrl, restUrl, urlToMatch.toString());
    }

    if (StringUtils.isEmpty(endpointUrl)) {
        endpointUrl = "http4://" + dummyUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false";
    }

    exchange.getIn().setHeader("CamelHttpUri", urlReceived);
    exchange.getIn().setHeader("CamelHttpQuery", CamelHttpQuery);
    exchange.getIn().setHeader("restUrl", endpointUrl);

    exchange.getOut().setHeaders(exchange.getIn().getHeaders());
    exchange.getOut().setBody(exchange.getIn().getBody(String.class), String.class);
}

private String getEndPointUrl(String serviceUrls, String restUrl, String urlToMatch) {
    List<String> serviceUrlList = Arrays.asList(serviceUrls.split("\\s*,\\s*"));
    for (String urlPattern: serviceUrlList ) {
        Pattern pattern = Pattern.compile(urlPattern);
        Matcher matcher = pattern.matcher(urlToMatch);
        if (matcher.matches()) {
            return "http4://" + restUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false";
        }
    }
    return "";
}
公共作废进程(Exchange)引发异常{
字符串CamelHttpUri=(字符串)exchange.getIn().getHeader(“CamelHttpUri”);
字符串CamelHttpQuery=(字符串)exchange.getIn().getHeader(“CamelHttpQuery”);
字符串urlReceived=CamelHttpUri;
StringBuilder urlToMatch=新StringBuilder();
urlToMatch.append(urlReceived);
如果(!StringUtils.isEmpty(CamelHttpQuery)){
urlToMatch.append(“?”).append(CamelHttpQuery);
}
字符串endpointUrl=getEndPointUrl(serviceUrl、restUrl、urlToMatch.toString());
}
if(StringUtils.isEmpty(endpointUrl)){
endpointUrl=“http4://”+dummyUrl+“?bridgeEndpoint=true&throweExceptionOnFailure=false”;
}
exchange.getIn().setHeader(“CamelHttpUri”,urlReceived);
exchange.getIn().setHeader(“CamelHttpQuery”,CamelHttpQuery);
exchange.getIn().setHeader(“restUrl”,endpointUrl);
exchange.getOut().setHeaders(exchange.getIn().getHeaders());
exchange.getOut().setBody(exchange.getIn().getBody(String.class)、String.class);
}
私有字符串getEndPointUrl(字符串serviceUrls、字符串restUrl、字符串urlToMatch){
List serviceUrlList=Arrays.asList(serviceUrls.split(“\\s*,\\s*”);
用于(字符串URL模式:serviceUrlList){
Pattern=Pattern.compile(urlPattern);
Matcher Matcher=pattern.Matcher(urlToMatch);
if(matcher.matches()){
返回“http4://”+restUrl+“?bridgeEndpoint=true&throweExceptionOnFailure=false”;
}
}
返回“”;
}

在CusomPostProcessor上,我们可以在将结果发送到客户端之前添加任何逻辑

感谢zenbeni,如果我这样做的话,它会工作,但我试图将rest URL放在属性文件或变量中,并将其包含在“when”检查中,以根据头uri进行验证,任何建议都会很好。它可以如上所述工作,但我需要将rest URL放在变量或属性文件中,而不是将其添加到“when”中有没有办法做到这一点?
from("jetty:http://"+myUrl+"?matchOnUriPrefix=true&amp;traceEnabled=true")      
   .process(customProcessor)
     .choice()
    .when(simple("${in.headers.firstUrn} contains ${in.headers.CamelHttpUri}"))
  .to("http4://" + first1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .when(simple("${in.headers.secondUrn} contains ${in.headers.CamelHttpUri}"))
      .to("http4://" + second1Url + "?bridgeEndpoint=true&throwExceptionOnFailure=false")
    .otherwise()
      .to("http4://" + thirdUrl + "?bridgeEndpoint=true&throwExceptionOnFailure=false");