【问题标题】:Camel reverse proxy - no response stream caching骆驼反向代理 - 没有响应流缓存
【发布时间】:2014-09-11 17:35:23
【问题描述】:

我正在尝试实现一个内存高效的 http 反向代理,它只适用于流。 Jetty 消费者将输入流放入交换中,我可以将其与 http 生产者连接以转发请求。那里没问题。

但是,我所知道的所有 http 生产者(Jetty、http4、netty-http)都将响应流读取到堆内存中,并将其内容以某种形式而不是流的句柄放入交换中。而且他们似乎都没有提供让他们这样做的选项。

我发现了这个thread,它描述了同样的问题并提出了解决方案。但是查看 Camel 2.13.1 中 http4 HttpProducer 的代码,看起来提议的更改毕竟没有进入 Camel 代码库。

有什么方法可以使用 Camel 实现仅流式方法?所以,我可以用最少的内存占用做一些事情:

<route id="reverse_proxy" streamCache="false">
    <from ref="jetty.http.server"/>
    <bean ref="streamHolder" method="enableCaching"/>
    <bean ref="streamHolder" method="parsePayloadHeaderInfoAndDoStuff"/>
    <bean ref="streamHolder" method="resetStream"/>
    <to ref="http.client"/> <!-- Register completion synchronization hook to close stream. -->
    <bean ref="streamHolder" method="enableCaching"/>
    <bean ref="streamHolder" method="parsePayloadResponseHeaderAndDoStuff"/>
    <bean ref="streamHolder" method="resetStream"/>
</route>

编辑 - 关于输入流在内存中的确切位置的附加信息:

http4:一切都发生在 org.apache.camel.component.http4.HttpProducer::process() -> populateResponse(..) -> extractResponseBody(..) -> doExtractResponseBodyAsStream() ;在这里,原始流被复制到CachedOutputStream 的一个实例中。

Jetty: org.eclipse.jetty.client.AsyncHttpConnection::handle() -> org.eclipse.jetty.http.HttpParser::parseNext() 将填充一个字节数组 org.eclipse.jetty.client.ContentExchange 这是一个 CachedExchange 这是一个 @ 987654330@.

netty-http:构建将 HttpResponse 内容组装为复合 ChannelBuffer 的管道。封装的通道缓冲区构成了完整的响应流。

我已经调试了所有三个客户端,并且没有偶然发现一个未采用的分支,这将使我将原始输入流作为交换主体。

这可以通过这样简单的路线重现:

<camelContext id="pep-poc">
    <endpoint id="jetty.http.server" uri="jetty:http://{{http.host.server}}:{{http.port.server}}/my/frontend?disableStreamCache=true"/>
    <endpoint id="http.client" uri="jetty:http://{{http.host.client}}:{{http.port.client}}/large_response.html?bridgeEndpoint=true&amp;throwExceptionOnFailure=false&amp;disableStreamCache=true"/>

    <route id="reverse_proxy" startupOrder="10" streamCache="false">
        <from ref="jetty.http.server"/>
        <to ref="http.client"/>
    </route>
</camelContext>

我有一个 Apache2 以 large_response.html 返回一个 750MB 的文件。

编辑 2

这确实是所有可用的 HTTP 生产者的问题。在Camel mailing list 和相应的JIRA ticket 上查看此线程。

【问题讨论】:

    标签: apache-camel


    【解决方案1】:

    它们不会将流读入内存,除非您按需访问消息正文,并告诉 Camel 将其作为 String 类型等读入内存。

    查看这个食谱示例如何做一个基于流的代理

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 2013-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      相关资源
      最近更新 更多