共用方式為


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

儲存配置資料的常見方式是使用檔案。 如果您想要使用 Azure 應用程式設定來管理組態資料,但您目前使用檔案,則不需要手動輸入資料。 您可以使用工具匯 入組態檔

如果您計劃在 App Configuration 中管理資料,則匯入作業是一次性資料移轉。 另一個選項是繼續管理檔案中的組態資料,並在持續整合和持續交付 (CI/CD) 程序中定期匯入檔案。 當您採用 配置即代碼時,就會出現這種情況。

當您使用設定檔時,可以使用兩個檔案內容設定檔:

  • 預設檔案內容設定檔:傳統組態檔結構描述
  • KVSet 檔案內容設定檔:包含所有 App Configuration 索引鍵值屬性的結構描述

本文討論這兩個檔案內容設定檔。 它還提供了匯入和匯出設定檔的範例。 這些範例會使用 Azure CLI,但本文中的概念也適用於其他 App Configuration 匯入方法。

檔案內容設定檔:預設值

在應用程式設定工具中,預設檔案內容設定檔是現有程式設計架構和系統廣泛採用的傳統組態檔結構描述。 此設定檔用於應用程式組態匯入工具,例如 Azure 入口網站、Azure CLI、Azure Pipelines 中的 Azure 應用程式組態匯入工作,以及 GitHub Actions。 應用程式設定支援 JSON、YAML 和屬性檔案格式。

如果您想要使用檔案作為應用程式的後援設定或開發期間的本機設定,此設定檔非常實用。 匯入組態檔時,您可以指定要如何將資料轉換為 App Configuration 索引鍵/值和功能旗標。

下列組態檔 appsettings.json提供預設檔案內容設定檔的範例。 此檔案包含一個組態設定和一個功能旗標。

{
    "Logging": {
        "LogLevel": {
            "Default": "Warning"
        }
    },
    "feature_management": {
        "feature_flags": [
            {
                "id": "Beta",
                "enabled": false
            }
        ]
    }
}

若要將此檔案匯入應用程式設定,請執行下列 Azure CLI 命令。 它會將 dev 標籤套用到設定和功能標記,並使用冒號(:)作為分隔符號來展平鍵名稱。

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

您可以選擇性地將下列參數新增至上述命令: --profile appconfig/default。 參數是選用的,因為預設設定檔是 appconfig/default

Azure 金鑰保存庫參考在匯入期間需要特定的內容類型。 因此,您會將它們保存在個別檔案中,如下列檔案所示, keyvault-refs.json

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

若要匯入此檔案,請執行下列 Azure CLI 命令。 它會將test標籤套用至 Key Vault 參考,並使用 Key Vault 的參考內容類型。

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

下表顯示 App Configuration 存放區中所有匯入的資料:

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

檔案內容設定檔:KVSet

在 App Configuration 工具中,KVSet 檔案內容設定檔是包含 App Configuration 索引鍵值所有屬性的檔案結構描述。 包括鍵名稱、其值、其標籤、內容類型和其標記。 由於索引鍵值的所有屬性都在檔案中,因此匯入檔案時不需要指定轉換規則。

當您使用 KVSet 設定檔時,您可以在一個檔案中定義一般索引鍵值、金鑰保存庫參考和功能旗標。 因此,如果您想要在一個檔案中管理所有 App Configuration 資料,並在一個步驟中匯入它,此設定檔會很有幫助。

使用此設定檔的檔案是 JSON 格式。 如需綱目規格,請參閱 KVSet 檔案綱目

下列檔案 appconfigdata.json是以 KVSet 檔案內容設定檔為基礎。 此檔案包含功能旗標、金鑰保存庫參考,以及標準索引鍵/值。

{
  "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://<Key-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": {}
    }
  ]
}

在上一節中,範例示範如何將資料匯入 App Configuration 存放區。 您可以使用下列 Azure CLI 命令,將該資料匯出至檔案:

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

匯出檔案後,請在文字編輯器中開啟檔案,然後進行下列變更:

  • Beta功能旗幟的屬性enabled設定為true
  • Logging:LogLevel:Default 屬性設定為 Debug

若要將更新的檔案匯入 App Configuration 存放區,請執行下列 CLI 命令,其中包含參數 --profile appconfig/kvset 。 您不需要像預設檔案內容設定檔那樣指定資料轉換規則,例如分隔符號、標籤或內容類型。 所有需要的資訊都已在檔案中。

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

附註

下列項目目前支援 KVSet 檔案內容設定檔:

下表顯示 App Configuration 存放區中所有匯入的資料:

Key 標籤 內容類型
.appconfig.featureflag/Beta {“id”:“測試版”,“描述”:“測試版功能”,“enabled”:true,“條件”:{“client_filters”:[]}} dev application/vnd.microsoft.appconfig.ff+json;charset=utf-8
Logging:LogLevel:Default 偵錯 dev
Database:ConnectionString {“uri”:“https://<金鑰保存庫名稱>.vault.azure.net/secrets/db-secret“} 測試 application/vnd.microsoft.appconfig.keyvaultref+json;charset=utf-8

後續步驟