使用camel rest-dsl进行相互调用

时间:2018-01-23 20:43:40

标签: java rest apache-camel

我们有两个应用程序A和B配置了camel rest-dsl来监听来电。触发其中任何一个都可以正常工作。但是,每当我们有A呼叫B并且在呼叫B中呼叫A时,我们在A上获得HTTP 500,尽管B没有报告任何问题并且A上的应答处理正确执行。

我们已将其缩小到以下示例:

系统A:

from("timer://myTimer?period=600s")
    .process(new Processor() {...})
    .setHeader(Exchange.HTTP_METHOD, constant("PUT"))
    .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
    .marshal().json(JsonLibrary.Jackson)
    .to("http4://localhost:8080/B/call")
    .end();

rest("/A").post("callResponse")
    .type(BResponse.class).consumes("application/json")
    .to("direct:handle");

from("direct:handle")
    .to("stream:out")
    .end();

系统B

rest("/B").put("/call")
    .consumes("application/json")
    .type(BRequest.class)
    .to("direct:doB");

from("direct:doB")
    .process(requestToResponse)
    .marshal().json(JsonLibrary.Jackson)
    .setHeader(Exchange.HTTP_METHOD, constant("POST"))
    .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
    .to("http4://localhost:8081/A/callResponse?bridgeEndpoint=true")
    .end();
}

在处理器和路由中使用大量日志记录,我们可以看到BRequest对象已正确创建,我们也可以看到它在B处未受损。我们可以跟踪它转换为{ {1}}并且此响应再次正确地发送到A.我们可以看到A-Rest-Endpoint的所有阶段都要正确执行,并且在最后BResponse之后,在A处抛出异常并带有消息&# 34; HTTP操作无法使用状态码500"。

调用http://localhost:8081/B/call

我们最初的目标是解除A和B的执行,使A通过休息呼叫B,立即得到答案并继续工作,而B异步处理请求。这显然不会发生(A等待B等待A)并且可能是问题的一部分?

1 个答案:

答案 0 :(得分:2)

我拿了你的代码并删除了不必要的部分并运行它,还包括seda队列和交换模式集的完整日志记录。我没有收到任何错误,并且在正确的位置生成了日志。这是我的代码:

系统A

public void configure() {
    restConfiguration().component("undertow").host("localhost").port(8080).bindingMode(RestBindingMode.auto);
    from("timer://myTimer?period=600s")
            .setBody(constant("{\"hello\":\"hello\"}"))
            .setHeader(Exchange.HTTP_METHOD, constant("PUT"))
            .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))

            .marshal().json(JsonLibrary.Jackson)
            .to("http4://localhost:8080/B/call")
            .end();

    rest("/A").post("callResponse")
            .consumes("application/json")
            .to("seda:handle");

    from("seda:handle")
    .to("log:com.mycompany.handle?showAll=true&multiline=true")
            .to("stream:out")
            .end();
}

系统B

    public void configure() throws Exception {
        restConfiguration().component("undertow").host("localhost").port(8080).bindingMode(RestBindingMode.auto);
        rest("/B").put("/call")
                .consumes("application/json")
                //.type(BRequest.class)
                .to("direct:doB");

        from("direct:doB")
        .to("log:com.mycompany.B?showAll=true&multiline=true")
                .marshal().json(JsonLibrary.Jackson)

                .setHeader(Exchange.HTTP_METHOD, constant("POST"))
                .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
                .to("log:com.mycompany.BeforeSendingToA?showAll=true&multiline=true")
                .to("http4://localhost:8080/A/callResponse?bridgeEndpoint=true")
                .to("log:com.mycompany.AfterSendingToA?showAll=true&multiline=true")
                .end(); 
}

骆驼应用启动时的日志:

[ouciance.random.MainApp.main()] HttpComponent                  INFO  Created ClientConnectionManager org.apache.http.impl.conn.PoolingHttpClientConnectionManager@4466f1ee
[ouciance.random.MainApp.main()] DefaultCamelContext            INFO  StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
[ouciance.random.MainApp.main()] SedaEndpoint                   INFO  Endpoint seda://handle is using shared queue: seda://handle with size: 2147483647
[ouciance.random.MainApp.main()] DefaultCamelContext            INFO  Route: route3 started and consuming from: timer://myTimer?period=600s
[ouciance.random.MainApp.main()] DefaultCamelContext            INFO  Route: route4 started and consuming from: seda://handle
[ouciance.random.MainApp.main()] DefaultUndertowHost            INFO  Starting Undertow server on http://localhost:8080
[ouciance.random.MainApp.main()] xnio                           INFO  XNIO version 3.3.8.Final
[ouciance.random.MainApp.main()] nio                            INFO  XNIO NIO Implementation Version 3.3.8.Final
[ouciance.random.MainApp.main()] DefaultCamelContext            INFO  Route: route1 started and consuming from: http://localhost:8080/A/callResponse?httpMethodRestrict=POST%2COPTIONS&matchOnUriPrefix=false
[ouciance.random.MainApp.main()] DefaultCamelContext            INFO  Route: route5 started and consuming from: direct://doB
[ouciance.random.MainApp.main()] DefaultCamelContext            INFO  Route: route2 started and consuming from: http://localhost:8080/B/call?httpMethodRestrict=PUT%2COPTIONS&matchOnUriPrefix=false
[ouciance.random.MainApp.main()] DefaultCamelContext            INFO  Total 5 routes, of which 5 are started
[ouciance.random.MainApp.main()] DefaultCamelContext            INFO  Apache Camel 2.20.1 (CamelContext: camel-1) started in 1.696 seconds

计时器启动并发送请求/响应时的日志。

[ouciance.random.MainApp.main()] DefaultCamelContext            INFO  Apache Camel 2.20.1 (CamelContext: camel-1) started in 1.696 seconds
[                 XNIO-1 task-1] B                              INFO  Exchange[
, Id: ID-moeed-Dator-1517066840557-0-3
, ExchangePattern: InOut
, Properties: {CamelCharsetName=ISO-8859-1, CamelCreatedTimestamp=Sat Jan 27 16:27:23 CET 2018, CamelExternalRedelivered=false, CamelMessageHistory=[DefaultMessageHistory[routeId=route2, node=route2], DefaultMessageHistory[routeId=route5, node=to4]], CamelToEndpoint=log://com.mycompany.B?multiline=true&showAll=true}
, Headers: {Accept-Encoding=gzip,deflate, breadcrumbId=ID-moeed-Dator-1517066840557-0-1, CamelHttpCharacterEncoding=ISO-8859-1, CamelHttpMethod=PUT, CamelHttpPath=, CamelHttpQuery=, CamelHttpRawQuery=, CamelHttpUri=/B/call, CamelHttpUrl=http://localhost:8080/B/call, Connection=Keep-Alive, Content-Length=23, Content-Type=application/json, firedTime=Sat Jan 27 16:27:23 CET 2018, Host=localhost:8080, User-Agent=Apache-HttpClient/4.5.3 (Java/1.8.0_71)}
, BodyType: String
, Body: {"hello":"hello"}
, Out: null: 
]
[                 XNIO-1 task-1] BeforeSendingToA               INFO  Exchange[
, Id: ID-moeed-Dator-1517066840557-0-3
, ExchangePattern: InOut
, Properties: {CamelCharsetName=ISO-8859-1, CamelCreatedTimestamp=Sat Jan 27 16:27:23 CET 2018, CamelExternalRedelivered=false, CamelMessageHistory=[DefaultMessageHistory[routeId=route2, node=route2], DefaultMessageHistory[routeId=route5, node=to4], DefaultMessageHistory[routeId=route5, node=marshal2], DefaultMessageHistory[routeId=route5, node=setHeader3], DefaultMessageHistory[routeId=route5, node=setHeader4], DefaultMessageHistory[routeId=route5, node=to5]], CamelToEndpoint=log://com.mycompany.BeforeSendingToA?multiline=true&showAll=true}
, Headers: {Accept-Encoding=gzip,deflate, breadcrumbId=ID-moeed-Dator-1517066840557-0-1, CamelHttpCharacterEncoding=ISO-8859-1, CamelHttpMethod=POST, CamelHttpPath=, CamelHttpQuery=, CamelHttpRawQuery=, CamelHttpUri=/B/call, CamelHttpUrl=http://localhost:8080/B/call, Connection=Keep-Alive, Content-Length=23, Content-Type=application/json, firedTime=Sat Jan 27 16:27:23 CET 2018, Host=localhost:8080, User-Agent=Apache-HttpClient/4.5.3 (Java/1.8.0_71)}
, BodyType: byte[]
, Body: "{\"hello\":\"hello\"}"
, Out: null: 
]
[l-1) thread #2 - seda://handle] handle                         INFO  Exchange[
, Id: ID-moeed-Dator-1517066840557-0-8
, ExchangePattern: InOut
, Properties: {CamelCharsetName=ISO-8859-1, CamelCorrelationId=ID-moeed-Dator-1517066840557-0-5, CamelCreatedTimestamp=Sat Jan 27 16:27:23 CET 2018, CamelExternalRedelivered=false, CamelMessageHistory=[DefaultMessageHistory[routeId=route1, node=route1], DefaultMessageHistory[routeId=route4, node=to2]], CamelToEndpoint=log://com.mycompany.handle?multiline=true&showAll=true}
, Headers: {Accept-Encoding=gzip,deflate, breadcrumbId=ID-moeed-Dator-1517066840557-0-1, CamelHttpCharacterEncoding=ISO-8859-1, CamelHttpMethod=POST, CamelHttpPath=, CamelHttpQuery=, CamelHttpRawQuery=, CamelHttpUri=/A/callResponse, CamelHttpUrl=http://localhost:8080/A/callResponse, Connection=Keep-Alive, Content-Length=23, Content-Type=application/json, firedTime=Sat Jan 27 16:27:23 CET 2018, Host=localhost:8080, User-Agent=Apache-HttpClient/4.5.3 (Java/1.8.0_71)}
, BodyType: String
, Body: {"hello":"hello"}
, Out: null: 
]
{"hello":"hello"}
[                 XNIO-1 task-1] AfterSendingToA                INFO  Exchange[
, Id: ID-moeed-Dator-1517066840557-0-3
, ExchangePattern: InOut
, Properties: {CamelCharsetName=UTF-8, CamelCreatedTimestamp=Sat Jan 27 16:27:23 CET 2018, CamelExternalRedelivered=false, CamelMessageHistory=[DefaultMessageHistory[routeId=route2, node=route2], DefaultMessageHistory[routeId=route5, node=to4], DefaultMessageHistory[routeId=route5, node=marshal2], DefaultMessageHistory[routeId=route5, node=setHeader3], DefaultMessageHistory[routeId=route5, node=setHeader4], DefaultMessageHistory[routeId=route5, node=to5], DefaultMessageHistory[routeId=route5, node=to6], DefaultMessageHistory[routeId=route5, node=to7]], CamelSkipGzipEncoding=true, CamelToEndpoint=log://com.mycompany.AfterSendingToA?multiline=true&showAll=true}
, Headers: {Accept-Encoding=gzip,deflate, breadcrumbId=ID-moeed-Dator-1517066840557-0-1, CamelHttpCharacterEncoding=ISO-8859-1, CamelHttpMethod=POST, CamelHttpPath=, CamelHttpQuery=, CamelHttpRawQuery=, CamelHttpResponseCode=200, CamelHttpResponseText=OK, CamelHttpUri=/B/call, CamelHttpUrl=http://localhost:8080/B/call, Connection=keep-alive, Content-Length=23, Content-Type=application/json, Date=Sat, 27 Jan 2018 15:27:23 GMT, firedTime=Sat Jan 27 16:27:23 CET 2018, User-Agent=Apache-HttpClient/4.5.3 (Java/1.8.0_71)}
, BodyType: org.apache.camel.converter.stream.CachedOutputStream.WrappedInputStream
, Body: [Body is instance of java.io.InputStream]
, Out: null: 
]

正如您所看到的,我没有得到您可能的http错误。