gpt4 book ai didi

java - 使用 camel rest-dsl 相互调用

转载 作者:行者123 更新时间:2023-11-29 04:22:12 27 4
gpt4 key购买 nike

我们有两个应用程序 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 操作调用 http://localhost:8081/B/call 失败,状态代码为 500”。

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

最佳答案

我获取了您的代码并删除了不必要的部分并运行了它,还包含了带有 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();
}

camel 应用程序启动时我的日志:

[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 错误。

关于java - 使用 camel rest-dsl 相互调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48410468/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com