Azure 應用程式組態支援應用程式組態

檔案是儲存組態資料最常見的方式之一。 為協助您快速開始使用,應用程式組態有工具可以協助您匯入組態檔,因此您不需要手動輸入資料。 如果您規劃在匯入資料後,在 Azure 應用程式組態中管理您的資料,則這項作業是一次性的資料移轉。 在某些其他情況下,例如,當您採用組態即程式碼時,您可以繼續管理檔案中的組態資料,然後在 CI/CD 程序期間反復匯入資料。 您可能會發現以下兩個案例之一適用您的情形:

  • 您將組態檔保留為您之前使用的格式。 如果您想要使用該檔案做為應用程式的後援組態或是開發期間的本機組態,此格式非常實用。 匯入組態檔時,請指定要如何將資料轉換為 Azure 應用程式組態的索引鍵/值。 此選項是入口網站、Azure CLI、Azure 管線推送工作、GitHub Actions 等應用程式組態匯入工具的預設檔案內容設定檔
  • 您保留的組態檔格式包含所有應用程式組態的索引鍵/值屬性。 當您匯入檔案時,不需要指定任何轉換規則,因為索引鍵/值的所有屬性都已在檔案中。 此選項在應用程式組態匯入工具中稱為 KVSet 檔案內容設定檔。 如果您想要在一個檔案中管理所有的應用程式組態資料 (包括一般索引鍵/值、Key Vault 參考和功能旗標),然後一次匯入所有資料,則這個選項非常實用。

本文的其餘部分將詳細討論兩種檔案內容設定檔,並使用 Azure CLI 做為範例。 相同的概念也適用於其他的應用程式組態匯入工具。

檔案內容設定檔:預設值

應用程式組態工具中的預設檔案內容設定檔是指現有程式設計架構或系統廣泛採用的傳統組態檔結構描述。 應用程式組態支援 JSON、Yaml 或屬性檔案格式。

下列範例是名為 appsettings.json 的組態檔,其中包含一個組態設定和一個功能旗標。

{
    "Logging": {
        "LogLevel": {
            "Default": "Warning"
        }
    },
    "FeatureManagement": {
        "Beta": false
    }
}

執行下列 CLI 命令,將檔案匯入至應用程式組態並加上 dev 標籤,並使用冒號 (:) 做為分隔符號壓平合併索引鍵名稱。 您可以選擇新增參數 "--profile appconfig/default"。 範例會略過這個部分,因為這是預設值。

az appconfig kv import --label dev --separator : --name <your store name> --source file --path appsettings.json --format json

Key Vault 參考在匯入期間需要特定的內容類型,因此您將其保留在別的檔案中。 下列範例是名為 keyvault-refs.json 的檔案。

{
    "Database:ConnectionString": {
        "uri": "https://<your-vault-name>.vault.azure.net/secrets/db-secret"
    }  
}

執行下列 CLI 命令,使用 test 標籤與 Key Vault 參考內容類型加以匯入。

az appconfig kv import --label test --content-type "application/vnd.microsoft.appconfig.keyvaultref+json;charset=utf-8" --name <your store name> --source file --path keyvault-refs.json --format json

下表顯示應用程式組態存放區中的所有匯入資料。

機碼 標籤 內容類型
.appconfig.featureflag/Beta {"id":"Beta","description":"","enabled":false,"conditions":{"client_filters":[]}} 開發 application/vnd.microsoft.appconfig.ff+json;charset=utf-8
Logging:LogLevel:Default 警告 開發
Database:ConnectionString {"uri":"https://<您的保存庫名稱>.vault.azure.net/secrets/db-secret"} 測試 application/vnd.microsoft.appconfig.keyvaultref+json;charset=utf-8

檔案內容設定檔:KVSet

應用程式組態工具中的 KVSet 檔案內容設定檔是指檔案結構描述,其中包含應用程式組態索引鍵/值的所有屬性,包括索引鍵、值、標籤、內容類型和標記。 此檔案是 JSON 格式。 如需結構描述的規格,請參閱 KVSet 檔案結構描述

下列範例是以 KVSet 檔案內容設定檔為基礎的檔案,名為 appconfigdata.json,其中包含功能旗標、Key Vault 參考和一般索引鍵/值。

{
  "items": [
    {
      "key": ".appconfig.featureflag/Beta",
      "value": "{\"id\":\"Beta\",\"description\":\"Beta feature\",\"enabled\":true,\"conditions\":{\"client_filters\":[]}}",
      "label": "dev",
      "content_type": "application/vnd.microsoft.appconfig.ff+json;charset=utf-8",
      "tags": {}
    },
    {
      "key": "Database:ConnectionString",
      "value": "{\"uri\":\"https://<your-vault-name>.vault.azure.net/secrets/db-secret\"}",
      "label": "test",
      "content_type": "application/vnd.microsoft.appconfig.keyvaultref+json;charset=utf-8",
      "tags": {}
    },
    {
      "key": "Logging:LogLevel:Default",
      "value": "Debug",
      "label": "dev",
      "content_type": null,
      "tags": {}
    }
  ]
}

提示

如果您已遵循上一節中的範例,而且您的應用程式組態存放區中有資料,則您可以使用 CLI 命令將資料匯出至檔案:

az appconfig kv export --profile appconfig/kvset --label * --name <your store name> --destination file --path appconfigdata.json --format json 

匯出檔案後,將 Beta 功能旗標的 enabled 屬性更新為 true,將 Logging:LogLevel:Default 變更為 Debug

執行下列 CLI 命令並搭配參數 "--profile appconfig/kvset",將檔案匯入至您的應用程式組態存放區。 您不需要指定任何資料轉換規則,例如分隔符號、標籤或內容類型,就像您在預設檔案內容設定檔一節中所做的一樣,因為所有資訊都已在檔案中。

az appconfig kv import --profile appconfig/kvset --name <your store name> --source file --path appconfigdata.json --format json

注意

目前支援 KVSet 檔案內容設定檔的版本有:

下表顯示應用程式組態存放區中的所有匯入資料。

機碼 標籤 內容類型
.appconfig.featureflag/Beta {"id":"Beta","description":"Beta feature","enabled":true,"conditions":{"client_filters":[]}} 開發 application/vnd.microsoft.appconfig.ff+json;charset=utf-8
Logging:LogLevel:Default 偵錯 開發
Database:ConnectionString {"uri":"https://<您的保存庫名稱>.vault.azure.net/secrets/db-secret"} 測試 application/vnd.microsoft.appconfig.keyvaultref+json;charset=utf-8

下一步