建置應用程式時,您可能會使用仍處於預覽狀態的 API。 當您整合與應用程式一起建置的新功能時,通常會使用預覽 API。 將應用程式發行至生產環境之前,您應該確定您使用的是生產層級 API。 當您使用服務等級協定 (SLA) 所支援的穩定 API 時,您的應用程式會更為健全。
若要檢查您的應用程式是否使用生產層級 API,您可以使用 ApiCenterProductionVersionPlugin 外掛程式。 此外掛程式會比較來自您應用程式的 API 要求資訊與來自 Azure API 中心的資訊,並報告任何非生產環境的 API 要求。 其也會建議您所使用 API 的生產版本。
在開始之前
若要偵測非生產 API 要求,您需要有 Azure API 中心 實例,其中包含您在組織中所使用的 API 相關信息。
建立 Azure API 中心實例
開始之前,請先建立 Azure API 中心實例,並註冊您在組織中使用的 API。 針對每個 API,定義您使用的版本,並指定其生命週期階段。
ApiCenterProductionVersionPlugin 使用這項資訊來檢查應用程式所使用的 API 是否屬於生產環境或非生產環境。
複製 API 中心資訊
從 Azure API 中心執行個體 [概觀] 頁面,複製 API 中心執行個體的 [名稱]、[資源群組] 的名稱和 [訂用帳戶識別碼]。 您需要此資訊來設定 ApiCenterProductionVersionPlugin,使其可連線到您的 Azure API 中心執行個體。
設定開發代理
若要檢查您的應用程式是否使用生產層級 API,您需要在開發 Proxy 設定檔中啟用 ApiCenterProductionVersionPlugin。 若要建立應用程式所使用的 API 報告,請新增回報器。
啟用 ApiCenterProductionVersionPlugin
在 devproxyrc.json 檔案中,新增下列設定:
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.0/rc.schema.json",
"plugins": [
{
"name": "ApiCenterProductionVersionPlugin",
"enabled": true,
"pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
"configSection": "apiCenterProductionVersionPlugin"
}
],
"urlsToWatch": [
"https://jsonplaceholder.typicode.com/*"
],
"apiCenterProductionVersionPlugin": {
"subscriptionId": "aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e",
"resourceGroupName": "demo",
"serviceName": "contoso-api-center",
"workspaceName": "default"
}
}
在 subscriptionId、resourceGroupName 和 serviceName 屬性中,提供 Azure API 中心執行個體的相關資訊。
在 urlsToWatch 屬性中,指定應用程式使用的 URL。
提示
使用開發 Proxy 工具組 (英文) Visual Studio Code 延伸模組輕鬆管理開發 Proxy 設定。
新增回報器
ApiCenterProductionVersionPlugin 會產生應用程式正在使用的 API 報表。 若要檢視此報表,請將回報器新增至您的開發 Proxy 設定檔。 開發代理提供數個報告生成器。 在此範例中,您會使用純文字回報器。
請在您的 devproxyrc.json 檔案中添加對純文字回報器的引用,以進行更新。
{
"$schema": "https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.0/rc.schema.json",
"plugins": [
{
"name": "ApiCenterProductionVersionPlugin",
"enabled": true,
"pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
"configSection": "apiCenterProductionVersionPlugin"
},
{
"name": "PlainTextReporter",
"enabled": true,
"pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll"
}
],
"urlsToWatch": [
"https://jsonplaceholder.typicode.com/*"
],
"apiCenterProductionVersionPlugin": {
"subscriptionId": "aaaa0a0a-bb1b-cc2c-dd3d-eeeeee4e4e4e",
"resourceGroupName": "demo",
"serviceName": "contoso-api-center",
"workspaceName": "default"
}
}
檢查您的應用程式是否使用生產層級 API
若要檢查您的應用程式是否使用生產層級 API,您必須連線到您的 Azure 訂用帳戶、執行開發 Proxy,並使其攔截來自您應用程式的 API 要求。 然後,開發 Proxy 會比較 API 請求的資訊與 Azure API 中心的資訊,並回報任何屬於非生產環境的 API。
連接到 Azure 訂用帳戶
開發 Proxy 會使用來自 Azure API 中心的資訊,判斷您應用程式使用的 API 是否為生產層級。 若要取得這項資訊,該功能需要連線到您的 Azure 訂用帳戶。 您可以透過數種方式 (部分機器翻譯) 連線到您的 Azure 訂用帳戶。
執行開發環境 Proxy
連線到您的 Azure 訂用帳戶之後,請啟動 Dev Proxy 開發工具。 如果您從 devproxyrc.json 檔案所在的相同資料夾啟動開發 Proxy,其會自動載入設定。 否則,請使用 --config-file 選項指定設定檔的路徑。
當開發 Proxy 啟動時,其會檢查是否可以連線到您的 Azure 訂用帳戶。 連線成功時,您會看到類似下列的訊息:
info Plugin ApiCenterProductionVersionPlugin connecting to Azure...
info Listening on 127.0.0.1:8000...
Hotkeys: issue (w)eb request, (r)ecord, (s)top recording, (c)lear screen
Press CTRL+C to stop Dev Proxy
按下 r 鍵以開始錄製來自您應用程式的 API 要求。
使用您的應用程式
像平常一樣使用您的應用程式。 開發 Proxy 會攔截 API 要求,並將相關資訊儲存在記憶體中。 在開發 Proxy 執行的命令列中,您應該會看到應用程式提出的 API 要求相關資訊。
info Plugin ApiCenterProductionVersionPlugin connecting to Azure...
info Listening on 127.0.0.1:8000...
Hotkeys: issue (w)eb request, (r)ecord, (s)top recording, (c)lear screen
Press CTRL+C to stop Dev Proxy
◉ Recording...
req ╭ GET https://jsonplaceholder.typicode.com/posts?api-version=v1.0
api ╰ Passed through
req ╭ GET https://jsonplaceholder.typicode.com/users?api-version=beta
api ╰ Passed through
檢查 API 版本
按下 s 鍵可停止錄製。 開發代理連接到 API 中心實例,並將請求的相關資訊與 API 中心的資訊進行比較。
info Plugin ApiCenterProductionVersionPlugin connecting to Azure...
info Listening on 127.0.0.1:8000...
Hotkeys: issue (w)eb request, (r)ecord, (s)top recording, (c)lear screen
Press CTRL+C to stop Dev Proxy
◉ Recording...
req ╭ GET https://jsonplaceholder.typicode.com/posts?api-version=v1.0
api ╰ Passed through
req ╭ GET https://jsonplaceholder.typicode.com/users?api-version=beta
api ╰ Passed through
○ Stopped recording
info Checking if recorded API requests use production APIs as defined in API Center...
info Loading APIs from API Center...
info Analyzing recorded requests...
warn Request GET https://jsonplaceholder.typicode.com/users?api-version=beta uses API version beta which is defined as Preview. Upgrade to a production version of the API. Recommended versions: v1.0
info DONE
開發 Proxy 完成分析時,會在名為 ApiCenterProductionVersionPlugin_PlainTextReporter.txt 的檔案中建立包含下列內容的報表:
Non-production APIs:
GET https://jsonplaceholder.typicode.com/users?api-version=beta
Production APIs:
GET https://jsonplaceholder.typicode.com/posts?api-version=v1.0
摘要
ApiCenterProductionVersionPlugin使用,您可以檢查您的應用程式是否使用生產層級 API。 外掛程式會比較來自您應用程式的 API 要求相關資訊與 Azure API 中心的資訊,並回報任何非生產 API 要求。 其也會建議您所使用 API 的生產版本。 確認應用程式所使用的 API,可協助您確定您的應用程式使用穩定 API,這是 SLA 所支援的穩定 API,可讓您的應用程式更健全。 您可以手動執行這項檢查,或整合您的 CI/CD 管線,以便確認您的應用程式使用生產層級 API,再發行至生產環境。