串流要求和回應

本頁內容適用於 ApigeeApigee Hybrid

查看 Apigee Edge 說明文件。

本主題的學習內容

閱讀本主題後,您會瞭解:

  • Apigee 上的要求和回應串流
  • 要求和回應串流的使用時機
  • 如何啟用要求和回應串流

什麼是要求和回應串流?

根據預設,HTTP 串流會停用,且 HTTP 要求和回應負載會寫入記憶體中的緩衝區,然後再由 API Proxy 管道處理。如要變更這項行為,請啟用串流功能。啟用串流後,系統會將要求和回應的酬載串流至用戶端應用程式 (回應) 和目標端點 (要求),且不會進行修改。

何時該啟用串流?

如果 API Proxy 處理的要求和/或回應非常龐大 (如需大小限制,請參閱「關於串流,還有哪些事項需要瞭解?」一文),您可能需要啟用串流。

串流還有哪些注意事項?

訊息酬載大小上限為 30 MB。 在非串流要求和回應中,如果超過該大小,就會導致 protocol.http.TooBigBody 錯誤。

編碼資料

啟用串流時,Apigee 不會編碼或解碼要求或回應酬載,再傳送至連線至 Apigee 或後端目標伺服器的用戶端。詳情請參閱「 TargetEndpoint Transport Property Specification」表格中的 request.streaming.enabledresponse.streaming.enabled 列。

如何啟用要求和回應串流

如要啟用要求串流,您需要在 Proxy 套件的 ProxyEndpoint 和 TargetEndpoint 定義中新增 request.streaming.enabled 屬性,並將其設為 true。同樣地,請設定 response.streaming.enabled 屬性,啟用回應串流。

您可以在 Apigee UI 中,透過 Proxy 的「開發」檢視畫面找到這些設定檔。如果您在本機開發,這些定義檔位於 apiproxy/proxiesapiproxy/targets

這個範例說明如何在 TargetEndpoint 定義中啟用要求和回應串流。

<TargetEndpoint name="default">
  <HTTPTargetConnection>
    <URL>http://mocktarget.apigee.net</URL>
    <Properties>
      <Property name="response.streaming.enabled">true</Property>
      <Property name="request.streaming.enabled">true</Property>
      <Property name="supports.http10">true</Property>
      <Property name="request.retain.headers">User-Agent,Referer,Accept-Language</Property>
      <Property name="retain.queryparams">apikey</Property>
    </Properties>
  </HTTPTargetConnection>
</TargetEndpoint>

以下範例說明如何在 ProxyEndpoint 定義中啟用回應和要求串流:

<ProxyEndpoint name="default">
  <HTTPProxyConnection>
    <BasePath>/v1/weather</BasePath>
    <Properties>
      <Property name="allow.http10">true</Property>
      <Property name="response.streaming.enabled">true</Property>
      <Property name="request.streaming.enabled">true</Property>
    </Properties>
  </HTTPProxyConnection>
</ProxyEndpoint>

如要進一步瞭解如何設定端點定義,請參閱「端點屬性參考資料」。

相關程式碼範例

GitHub 上的 API Proxy 範例可輕鬆下載及使用。

支援串流的 Proxy 範例包括: