You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Camel路由连接REST服务接收Event-Stream事件流

问题:Camel路由无法接收HTTP长连接的Server-Sent Events(SSE)

我需要在Camel路由中通过HTTP GET请求连接服务并保持长连接接收事件,但Camel始终收不到服务发送的事件。用Postman测试可以正常接收,确认服务运行状态正常。

我的Camel路由配置了和Postman一致的请求头:

.setHeader(Exchange.HTTP_METHOD, constant(org.apache.camel.component.http.HttpMethods.GET))
.setHeader("Connection", simple("keep-alive"))
.setHeader("Accept", simple("text/event-stream"))
.setHeader("Accept-Encoding", simple("gzip, deflate, br"))
.setHeader("Postman-Token", simple("6ec7dfb3-6a4e-46ad-9f29-329a854f2649"))
.to("http://localhost:5000/events")

已确认服务收到请求并生成事件消息返回,但Camel路由完全未收到这些事件,同时并行的Postman连接能正常接收事件。

解决方法

Camel默认的HTTP组件(camel-http或camel-http4)默认是阻塞式一次性读取完整响应,而SSE是分块传输的长连接,需要针对性配置:

  • 启用流式传输模式
    在HTTP端点URL后添加streaming=true参数,让Camel实时读取分块数据,而非等待完整响应:

    .to("http://localhost:5000/events?streaming=true")
    
  • 禁用响应缓存(针对camel-http4)
    关闭响应缓存,避免Camel缓存完整响应后才返回数据:

    .to("http://localhost:5000/events?streaming=true&disableStreamCache=true")
    
  • 适配压缩编码
    若服务返回压缩格式的SSE,需添加对应编码参数(以gzip为例):

    .to("http://localhost:5000/events?streaming=true&disableStreamCache=true&contentEncoding=gzip")
    
  • 使用专门的SSE组件(推荐)
    直接使用Camel的camel-sse组件,它专为Server-Sent Events设计,无需手动处理长连接和分块逻辑:

    from("sse://localhost:5000/events")
        .log("Received SSE event: ${body}");
    
  • 验证后续处理逻辑
    确保路由在.to()之后有明确的处理步骤(如日志输出、处理器),避免事件被静默丢弃:

    .to("http://localhost:5000/events?streaming=true")
    .log("Received event: ${body}");
    

内容的提问来源于stack exchange,提问作者amccarron

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 16:02:40