Azure Learn AZ-204 The Hard Way | 實作 AZ-204 Lab 08 API Management


  1. Lab08 - API Management
    1. Pre Setting
    2. Azure API Management
    3. 定義 API & Operation
  2. Azure API Management 相關知識
  3. 實戰 Azure API Management 心得

實作 AZ-204 證照測驗準備的實驗課程,從實驗課程中精熟 AZ-204 的測驗重點以及工具操作。本次要實驗的是如何使用 Azure API Management。

logo

Lab08 - API Management

詳細的實驗流程可以參考微軟在 GitHub上的專案

圖片來源:微軟

Pre Setting

首先建立 Web App httpapics,使用 Resource Group 為 ApiService,Publish 選擇 Docker Container、作業系統使用 Linux。

在 Docker tab,選擇 Single Container、Docker Hub、Public,並使用 kennethreitz/httpbin:latest 作為 Image。

這邊會使用 httpbin 以容器的方式啟動,方便扮演本次專案 API Management 背後連動的 API 服務。

Azure API Management

API Management 又稱為 Reverse Proxy,相對於代理 Client 的 Proxy,Reverse Proxy 是扮演伺服器群的代理角色。

建立 API Management services proapics,Organization name、Administrator email 隨興輸入 Contoso 以及 admin@contoso.com

注意 Pricing tier 要選擇 Consumption ,如果選擇 Developer 會建立到天荒地老。

定義 API & Operation

proapics 選擇 APIs,進行建立 HTTP API,顯示名稱為 HTTPBin API,Web service URL 則輸入先前建立的 httpbin App Service URL (https://httpapics.azurewebsites.net/)。

接著加入 Operation,第一個 Operation 命名 Echo Headers,使用 GET Method,URL 為 /。

從 Echo Headers 可以觀察到 Frontend、Inbound、Outbound 及 Backend 流程,分別對應 API Managemnt 的前端到背後連動的後端 API,我們可以在 InBound 及 Outbound 加入處理、過濾等控制,調整 API 的處理。

這邊的範例是在 Inbound 上加入 Policy,在 Headers append header source 值則設定為 azure-api-mgmt

並且將 Backend 的連動 Override 為 https://httpapics.azurewebsites.net/headers

接著可以在 Test 進行測試。

再來做一個 Get Legacy Data 的 Operation,讓原本回傳的 XML 改為回傳 Json,不需要改寫 API 只要透過調整 Outbound 加入 Policy 即可 (帥耶 Azure)

<outbound>
    <base />
    <xml-to-json kind="direct" apply="always" consider-accept-header="false" />
</outbound>

最後測試的 Generate Payload Operation 為在 Outbound 設定 Maximum payload size 用以限制最大的回傳上限。

Azure API Management 相關知識

實戰 Azure API Management 心得