職場では、Netflix のFeign Clientを使用してサービス間のリクエストを処理しています。しかし、特に Netflix のよく知られているビデオ ストリーミングのビジネス モデルを考えると、データをストリーミングする能力が明らかに欠如していることに私は戸惑っています。ここで明らかに何かが欠けています。
説明するために、データのストリームをService A
Feign Client に要求し、応答でストリームを送信するとします。この時点で、Feign Client のメソッドが呼び出されます。Service B
Service B
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
ではなくストリームが含まれていた場合は、それをさらに改善して、データが利用可能になったときにデータを送信できます。
私は、このサービス アーキテクチャの分野に非常に慣れていません。私は何が欠けていますか?