【问题标题】:Mutual calls using camel rest-dsl使用 camel rest-dsl 相互调用
【发布时间】:2018-07-02 18:56:35
【问题描述】:

我们有两个应用程序 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。在那里我们可以跟踪它以转换为 BResponse并且此响应再次正确发送到 A。我们可以看到 A-Rest-Endpoint 的所有阶段都正确执行,并且在最终 .to("stream:out)" 之后,异常在 A 处引发,并显示消息“HTTP operation failed invoking http://localhost:8081/B/call状态码为 500"。

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

【问题讨论】:

  • 只是猜测,因为我目前没有时间测试您的示例 - 由于 ExchangePattern 为 InOutA 可能会期待同步响应。因此,当在B 中设置 HTTP 标头时,可能会混淆对来自A 的原始调用的响应。您可以尝试在调用 B 之前将 ExchangePattern 设置为 InOnly,看看是否可行。
  • 我同意@noMad17,似乎 .to("stream:out") 末尾的 A 正试图发回回复。如果您不需要响应,请将 exchangePattern 设置为 InOut,并使用异步的 seda 队列,而不是同步的直接。
  • 嗨@noMad17,我们尝试将Exchangepattern 设置为InOnly,方法是在RestDefinition 中指定并添加到.to 参数中。它没有任何效果。我们还可以看到,BInOut 接收消息,而AInOnly 发送消息。我们还尝试在RestDefinition 中添加.responseMessage() 部分,同样没有效果。
  • 嗨@SoucianceEqdamRashti,我们根据您的要求尝试了seda-queues,不幸的是,它没有改变任何东西。您是否知道 - 偶然 - 一个教程或代码示例,展示了应该如何完成?
  • @Jonathan,我会看看今晚能不能测试一下,然后回复你。

标签: java rest apache-camel


【解决方案1】:

我获取了您的代码并删除了不必要的部分并运行了它,还包括带有 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 错误。

【讨论】:

  • 您好,您的解决方案适用于我们的设置。我们已经确定了差异。您的代码有效,因为它使用了undertow。更改为 jetty/netty/restlet 不起作用。与我们的示例相同。一旦我们切换到低潮,它就会像小猫一样发出咕噜声。知道这可能是什么吗?
  • 嗯,有趣的观察。我想不出一个原因,但今晚我会用 netty 和 restlet 测试它并回复你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-12
相关资源
最近更新 更多