串流要求和回應

本頁適用於 ApigeeApigee Hybrid

查看 Apigee Edge 說明文件。

本主題的重點

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

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

什麼是要求和回應串流?

根據預設,系統會停用 HTTP 串流,並將 HTTP 要求和回應酬載寫入記憶體中的緩衝區,然後再由 API Proxy 管道處理。您可以啟用串流功能來變更這項行為。啟用串流後,系統會串流要求和回應酬載,而無須修改用戶端應用程式 (針對回應) 和目標端點 (針對要求)。

何時該啟用串流功能?

如果 API 代理程式處理非常大的請求和/或回應 (如需大小限制,請參閱「串流功能的其他注意事項」),建議您啟用串流功能。

還有什麼關於串流的注意事項?

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

已編碼的資料

啟用串流功能後,Apigee 會在將要求或回應酬載傳送至連線至 Apigee 或後端目標伺服器的用戶端前,不會對其進行編碼或解碼。如需更多資訊,請參閱 TargetEndpoint 傳輸屬性規格表格中的 request.streaming.enabledresponse.streaming.enabled 資料列。

如何啟用要求和回應串流

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

您可以在 Proxy 的 Develop 檢視畫面中,透過 Apigee UI 找到這些設定檔。如果您是在本機開發,這些定義檔案會位於 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 範例。

以下是支援串流的代理伺服器範例: