【问题标题】:Passing along a data stream using Netflix Feign使用 Netflix Feign 传递数据流
【发布时间】:2015-02-08 01:41:41
【问题描述】:

在工作中,我们使用 Netflix 的 Feign Client 来帮助处理服务之间的请求。但是,我对它明显缺乏流式传输数据的能力感到困惑,尤其是考虑到 Netflix 众所周知的流式视频商业模式。我显然在这里遗漏了一些东西。

为了解释,假设Service AService B 的Feign 客户端请求数据流,Service B 在响应中发送数据流。此时,Feign Client中的execute()方法被调用:

@Override public Response execute(Request request, Options options) throws IOException {
  HttpURLConnection connection = convertAndSend(request, options);
  return convertResponse(connection);
}

HttpURLConnection convertAndSend(Request request, Options options) throws IOException {
  final HttpURLConnection connection = (HttpURLConnection) new URL(request.url()).openConnection();

  /** SNIP **/

  if (request.body() != null) {
    if (contentLength != null) {
      connection.setFixedLengthStreamingMode(contentLength);
    } else {
      connection.setChunkedStreamingMode(8196);
    }
    connection.setDoOutput(true);
    OutputStream out = connection.getOutputStream();
    if (gzipEncodedRequest) {
      out = new GZIPOutputStream(out);
    }
    try {
      out.write(request.body()); // PROBLEM
    } finally {
      try {
        out.close();
      } catch (IOException suppressed) {
      }
    }
  }
  return connection;
}

标记为PROBLEM 的行让我感到困惑。

  1. request 对象甚至没有任何类型的流可供读取,只有一个 byte[] body
  2. 在传出端,整个正文立即写入OutputStream。它不应该改为分块数据吗?

例如

// pseudocode
try {
  location = 0
  bufferSize = 2048
  buffer = request.body().read(location, bufferSize)
  while(out.readyToRead() && buffer.length > 0) {
    out.write(buffer)
    location += bufferSize
    buffer = request.body().read(location, bufferSize)
  }
}

如果请求有一个流而不仅仅是byte[] body,您可以进一步改进它以在数据可用时发送数据。

我对这个服务架构领域非常陌生。我错过了什么?

【问题讨论】:

    标签: java netflix netflix-feign


    【解决方案1】:

    Feign 是为控制平面 API 设计的,这些 API 通常不会从向上流中受益。不过,支持向下流式传输。

    我不关心如何提高缓冲的工作效率(例如替代字节数组)。请记住,feign 的大部分设计都围绕模板表单(json 或 xml)并尽可能地重用它们(例如,在重传时,缓冲 + 固定长度很容易且可预测)。

    如果将“流”设计与 http 客户端耦合,我想我会非常高兴。 IOTW,一种以在传输中有意义的方式处理流的子类型。比如普通java的InputStream,OkHttp的OkIo缓冲区,Netty的Netty Buffer等。

    Spencer 打开此邮件进行调查https://github.com/Netflix/feign/issues/220

    【讨论】:

    • 抱歉,IOTW 代表什么? (我拥有世界?本周形象?)
    猜你喜欢
    • 2017-10-01
    • 2018-09-24
    • 2019-07-27
    • 2016-12-09
    • 2017-04-14
    • 2016-02-06
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    相关资源
    最近更新 更多