【发布时间】:2015-02-08 01:41:41
【问题描述】:
在工作中,我们使用 Netflix 的 Feign Client 来帮助处理服务之间的请求。但是,我对它明显缺乏流式传输数据的能力感到困惑,尤其是考虑到 Netflix 众所周知的流式视频商业模式。我显然在这里遗漏了一些东西。
为了解释,假设Service A 向Service 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 的行让我感到困惑。
-
request对象甚至没有任何类型的流可供读取,只有一个byte[] body。 - 在传出端,整个正文立即写入
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