如何檢查我的應用程式是否使用生產層級 API
建置應用程式時,您可能會使用仍處於預覽狀態的 API。 當您與應用程式一起建置的新功能時,通常會使用預覽 API。 將應用程式發行至生產環境之前,您應該確定您使用的是生產層級 API。 當您使用服務等級協定 (SLA) 所支援的穩定 API 時,您的應用程式會更健全。
若要檢查您的應用程式是否使用生產層級 API,您可以使用 ApiCenterProductionVersionPlugin
外掛程式。 此外掛程式會比較來自您應用程式之 API 要求的相關信息,以及來自 Azure API 中心的資訊,以及任何非生產 API 要求的報告。 它也會建議您使用之 API 的生產版本。
在您開始使用 Intune 之前
若要偵測非生產 API 要求,您需要有 Azure API 中心 實例,其中包含您在組織中所使用的 API 相關信息。
建立 Azure API 中心實例
開始之前,請先建立 Azure API 中心實例,並註冊您在組織中使用的 API。 針對每個 API,定義您使用的版本,並指定其生命周期階段。
會 ApiCenterProductionVersionPlugin
使用這項資訊來檢查應用程式所使用的 API 是否屬於生產或非生產 API。
複製 API 中心資訊
從 [Azure API 中心實例概觀] 頁面,複製 API 中心實例的名稱、資源群組的名稱和訂用帳戶標識碼。 您需要這項資訊來設定 ApiCenterProductionVersionPlugin
,使其可以連線到您的 Azure API 中心實例。
設定開發 Proxy
若要檢查您的應用程式是否使用生產層級 API,您必須在開發 Proxy 設定檔案中啟用 ApiCenterProductionVersionPlugin
。 若要建立應用程式所使用的 API 報告,請新增記者。
啟用 ApiCenterProductionVersionPlugin
在 檔案中 devproxyrc.json
,新增下列組態:
{
"$schema": "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v0.19.0/rc.schema.json",
"plugins": [
{
"name": "ApiCenterProductionVersionPlugin",
"enabled": true,
"pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
"configSection": "apiCenterProductionVersionPlugin"
}
],
"urlsToWatch": [
"https://jsonplaceholder.typicode.com/*"
],
"apiCenterProductionVersionPlugin": {
"subscriptionId": "00000000-0000-0000-0000-000000000000",
"resourceGroupName": "demo",
"serviceName": "contoso-api-center",
"workspaceName": "default"
}
}
在 subscriptionId
、 resourceGroupName
和 serviceName
屬性中,提供 Azure API 中心實例的相關信息。
在屬性中 urlsToWatch
,指定您的應用程式所使用的URL。
提示
使用 Dev Proxy 工具組 Visual Studio Code 擴充功能輕鬆管理開發 Proxy 設定。
新增記者
會產生 ApiCenterProductionVersionPlugin
應用程式正在使用的 API 報告。 若要檢視此報告,請將記者新增至您的 Dev Proxy 組態檔。 開發人員 Proxy 提供數 個記者。 在此範例中,您會使用 純文本記者。
使用純文字記者的參考來更新您的 devproxyrc.json
檔案:
{
"$schema": "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v0.19.0/rc.schema.json",
"plugins": [
{
"name": "ApiCenterProductionVersionPlugin",
"enabled": true,
"pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll",
"configSection": "apiCenterProductionVersionPlugin"
},
{
"name": "PlainTextReporter",
"enabled": true,
"pluginPath": "~appFolder/plugins/dev-proxy-plugins.dll"
}
],
"urlsToWatch": [
"https://jsonplaceholder.typicode.com/*"
],
"apiCenterProductionVersionPlugin": {
"subscriptionId": "00000000-0000-0000-0000-000000000000",
"resourceGroupName": "demo",
"serviceName": "contoso-api-center",
"workspaceName": "default"
}
}
檢查您的應用程式是否使用生產層級 API
若要檢查您的應用程式是否使用生產層級 API,您需要連線到您的 Azure 訂用帳戶、執行 Dev Proxy,並讓它攔截來自您應用程式的 API 要求。 然後,開發人員 Proxy 會比較 API 要求的相關信息與 Azure API 中心的資訊,以及報告任何非生產 API 的相關信息。
連接到 Azure 訂用帳戶
開發人員 Proxy 會使用來自 Azure API 中心的信息來判斷您的應用程式所使用的 API 是否為生產層級。 若要取得這項資訊,它需要連線到您的 Azure 訂用帳戶。 您可以透過數種方式連線到您的 Azure 訂用帳戶。
執行開發 Proxy
連線到您的 Azure 訂用帳戶之後,請啟動 Dev Proxy。 如果您從檔案 devproxyrc.json
所在的相同資料夾啟動 Dev 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 要求,並將相關信息儲存在記憶體中。 在 Dev 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 停止錄製。 開發人員 Proxy 會連線到 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
當 Dev 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,再將其發行至生產環境。