串流呼叫可支援比簡單要求/回應更複雜的互動模式,允許透過單一連線傳送或接收多則訊息。
Java 適用的 Cloud 用戶端程式庫支援三種串流呼叫:
- 伺服器串流:伺服器會將回應串流傳回給您。
- 用戶端串流:您向伺服器傳送要求串流。
- 雙向串流:您可以向伺服器傳送要求串流,伺服器也可以向您傳回回應串流。
串流實作是根據伺服器、用戶端和雙向串流的 gRPC-Java 實作進行模擬。
支援各種傳輸方式的串流
使用 gRPC 時,系統完全支援串流,但 HttpJson 僅部分支援。請參閱下表,瞭解串流支援情形。
| 串流類型 | gRPC | HttpJson |
|---|---|---|
| 伺服器串流 | 支援 | 支援 |
| 用戶端串流 | 支援 | 不支援 |
| 雙向串流 | 支援 | 不支援 |
gRPC 和 HttpJson 都支援一元呼叫 (非串流)。
判斷串流類型
如要判斷呼叫的串流類型,請檢查傳回的 Callable 類型:
ServerStreamingCallable:伺服器串流。ClientStreamingCallable:用戶端串流。BidiStreamingCallable:雙向串流。
例如,使用 Java-Aiplatform 和 Java-Speech:
// Server Streaming
ServerStreamingCallable<ReadTensorboardBlobDataRequest, ReadTensorboardBlobDataResponse> callable = aiplatformClient.readTensorboardBlobDataCallable();
// Bidirectional Streaming
BidiStreamingCallable<StreamingRecognizeRequest, StreamingRecognizeResponse> callable = speechClient.streamingRecognizeCallable();
撥打串流通話
發出串流呼叫的方式會因您使用的是伺服器串流或雙向串流而異。
伺服器串流
伺服器串流不需要額外實作。ServerStream 類別可讓您疊代回覆串流。以下範例說明如何呼叫 Server Streaming API (以 Java-Maps-Routing 為例):
try (RoutesClient routesClient = RoutesClient.create()) {
ServerStreamingCallable<ComputeRouteMatrixRequest, RouteMatrixElement> computeRouteMatrix =
routesClient.computeRouteMatrixCallable();
ServerStream<RouteMatrixElement> stream = computeRouteMatrix.call(
ComputeRouteMatrixRequest.newBuilder().build());
for (RouteMatrixElement element : stream) {
// Do something with response
}
}
在這個範例中,用戶端會傳送單一 ComputeRouteMatrixRequest,並接收回應串流。
雙向串流
雙向串流需要額外實作才能發出呼叫。以下步驟以 Java-Speech 為例,說明如何實作雙向串流呼叫。
首先,請按照下列程式碼實作 ResponseObserver 介面:
class BidiResponseObserver<T> implements ResponseObserver<T> {
private final List<T> responses = new ArrayList<>();
private final SettableApiFuture<List<T>> future = SettableApiFuture.create();
@Override
public void onStart(StreamController controller) {
// no-op
}
@Override
public void onResponse(T response) {
responses.add(response);
}
@Override
public void onError(Throwable t) {
future.setException(t);
}
@Override
public void onComplete() {
future.set(responses);
}
public SettableApiFuture<List<T>> getFuture() {
return future;
}
}
然後按照下列步驟操作:
建立觀察器例項:
BidiResponseObserver<StreamingRecognizeResponse> responseObserver = new BidiResponseObserver<>();將觀察器傳遞至可呼叫物件:
ClientStream<EchoRequest> clientStream = speechClient.streamingRecognizeCallable().splitCall(responseObserver);將要求傳送至伺服器,並在完成後關閉串流:
clientStream.send(StreamingRecognizeRequest.newBuilder().build()); clientStream.send(StreamingRecognizeRequest.newBuilder().build()); // ... other requests ... clientStream.send(StreamingRecognizeRequest.newBuilder().build()); clientStream.closeSend();疊代回應:
List<StreamingRecognizeResponse> responses = responseObserver.getFuture().get(); for (StreamingRecognizeResponse response : responses) { // Do something with response }
不支援的串流錯誤
如果用戶端程式庫同時支援 gRPC 和 HTTP/JSON 傳輸,您可能會不小心將用戶端程式庫設為叫用不支援的串流呼叫。舉例來說,下列設定顯示 Java-Speech 的 HttpJson 用戶端發出雙向串流呼叫:
// SpeechClient is configured to use HttpJson
try (SpeechClient speechClient = SpeechClient.create(SpeechSettings.newHttpJsonBuilder().build())) {
// Bidi Callable is not supported in HttpJson
BidiStreamingCallable<StreamingRecognizeRequest, StreamingRecognizeResponse> callable = speechClient.streamingRecognizeCallable();
...
}
這不會導致編譯錯誤,但會顯示為執行階段錯誤:
Not implemented: streamingRecognizeCallable(). REST transport is not implemented for this method yet.
Important: The client library MUST be configured with gRPC to use client or bidirectional streaming.