你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:使用 Azure 应用程序配置创建 JavaScript 应用

在本快速入门中,你将使用 Azure 应用程序配置 JavaScript 提供程序客户端库通过 Azure 应用程序配置来集中存储和管理应用程序设置。

适用于 JavaScript 的应用程序配置提供程序基于 Azure SDK for JavaScript 构建,旨在更轻松地用于更丰富的功能。 它允许以 Map 对象的形式访问应用程序配置中的键值。 它提供了从多个标签组合配置、密钥前缀剪裁、Key Vault 引用自动解析等功能。 作为示例,本教程演示如何在 Node.js 应用中使用 JavaScript 提供程序。

先决条件

添加键值

将以下键值添加到应用程序配置存储区。 有关如何使用 Azure 门户或 CLI 将键值添加到存储区的详细信息,请转到创建键值

密钥 内容类型
message 来自 Azure 应用程序配置的消息 留空
app.greeting Hello World 留空
app.json {"myKey":"myValue"} application/json

创建 Node.js 控制台应用

在本教程中,你将创建一个 Node.js 控制台应用并从你的应用程序配置存储中加载数据。

  1. 为名为 app-configuration-quickstart 的项目创建一个新目录。

    mkdir app-configuration-quickstart
    
  2. 切换到新创建的 app-configuration-quickstart 目录。

    cd app-configuration-quickstart
    
  3. 使用 npm install 命令安装 Azure 应用配置提供程序。

    npm install @azure/app-configuration-provider
    

连接到应用程序配置存储区

以下示例演示如何从 Azure 应用程序配置检索配置数据并在应用程序中使用它。 默认情况下,键值作为 Map 对象加载,允许你使用完整键名访问每个键值。 但是,如果应用程序使用配置对象,则可以使用 constructConfigurationObject 帮助程序 API,该 API 根据从 Azure 应用程序配置加载的键值创建配置对象。

在 app-configuration-quickstart 目录中创建名为 app.js 的文件,并从每个示例复制代码

示例 1:使用默认选择器加载键值

在此示例中,将使用连接字符串连接到 Azure 应用程序配置并加载键值,而无需指定高级选项。 默认情况下,它会加载所有不带标签的键值。

const { load } = require("@azure/app-configuration-provider");
const connectionString = process.env.AZURE_APPCONFIG_CONNECTION_STRING;

async function run() {
    console.log("Sample 1: Load key-values with default selector");

    // Connect to Azure App Configuration using a connection string and load all key-values with null label.
    const settings = await load(connectionString);

    console.log("---Consume configuration as a Map---");
    // Find the key "message" and print its value.
    console.log('settings.get("message"):', settings.get("message"));           // settings.get("message"): Message from Azure App Configuration
    // Find the key "app.greeting" and print its value.
    console.log('settings.get("app.greeting"):', settings.get("app.greeting")); // settings.get("app.greeting"): Hello World
    // Find the key "app.json" whose value is an object.
    console.log('settings.get("app.json"):', settings.get("app.json"));         // settings.get("app.json"): { myKey: 'myValue' }

    console.log("---Consume configuration as an object---");
    // Construct configuration object from loaded key-values, by default "." is used to separate hierarchical keys.
    const config = settings.constructConfigurationObject();
    // Use dot-notation to access configuration
    console.log("config.message:", config.message);             // config.message: Message from Azure App Configuration
    console.log("config.app.greeting:", config.app.greeting);   // config.app.greeting: Hello World
    console.log("config.app.json:", config.app.json);           // config.app.json: { myKey: 'myValue' }
}

run().catch(console.error);

示例 2:使用选择器加载特定键值

在此示例中,将通过指定 selectors 选项来加载键值的子集。 仅加载以“app.”开头的键。 请注意,可以根据需求指定多个选择器,每个选择器都具有 keyFilterlabelFilter 属性。

const { load } = require("@azure/app-configuration-provider");
const connectionString = process.env.AZURE_APPCONFIG_CONNECTION_STRING;

async function run() {
    console.log("Sample 2: Load specific key-values using selectors");

    // Load a subset of keys starting with "app." prefix.
    const settings = await load(connectionString, {
        selectors: [{
            keyFilter: "app.*"
        }],
    });

    console.log("---Consume configuration as a Map---");
    // The key "message" is not loaded as it does not start with "app."
    console.log('settings.has("message"):', settings.has("message"));           // settings.has("message"): false
    // The key "app.greeting" is loaded
    console.log('settings.has("app.greeting"):', settings.has("app.greeting")); // settings.has("app.greeting"): true
    // The key "app.json" is loaded
    console.log('settings.has("app.json"):', settings.has("app.json"));         // settings.has("app.json"): true

    console.log("---Consume configuration as an object---");
    // Construct configuration object from loaded key-values
    const config = settings.constructConfigurationObject({ separator: "." });
    // Use dot-notation to access configuration
    console.log("config.message:", config.message);         // config.message: undefined
    console.log("config.app.greeting:", config.greeting);   // config.app.greeting: Hello World
    console.log("config.app.json:", config.json);           // config.app.json: { myKey: 'myValue' }
}

run().catch(console.error);

示例 3:从键加载键值和剪裁前缀

在此示例中,将使用选项 trimKeyPrefixes 加载键值。 加载键值后,将从所有键中剪裁前缀“app.”。 如果希望通过筛选特定键前缀来加载特定于应用程序的配置,但不希望代码每次访问配置时都携带该前缀,此功能非常有用。

const { load } = require("@azure/app-configuration-provider");
const connectionString = process.env.AZURE_APPCONFIG_CONNECTION_STRING;

async function run() {
    console.log("Sample 3: Load key-values and trim prefix from keys");

    // Load all key-values with no label, and trim "app." prefix from all keys.
    const settings = await load(connectionString, {
        selectors: [{
            keyFilter: "app.*"
        }],
        trimKeyPrefixes: ["app."]
    });

    console.log("---Consume configuration as a Map---");
    // The original key "app.greeting" is trimmed as "greeting".
    console.log('settings.get("greeting"):', settings.get("greeting")); // settings.get("greeting"): Hello World
    // The original key "app.json" is trimmed as "json".
    console.log('settings.get("json"):', settings.get("json"));         // settings.get("json"): { myKey: 'myValue' }

    console.log("---Consume configuration as an object---");
    // Construct configuration object from loaded key-values with trimmed keys.
    const config = settings.constructConfigurationObject();
    // Use dot-notation to access configuration
    console.log("config.greeting:", config.greeting);   // config.greeting: Hello World
    console.log("config.json:", config.json);           // config.json: { myKey: 'myValue' }
}

run().catch(console.error);

运行应用程序

  1. 设置名为 AZURE_APPCONFIG_CONNECTION_STRING 的环境变量,并将其设置为应用程序配置存储的连接字符串。 在命令行中运行以下命令:

    要使用 Windows 命令提示符在本地运行应用,请运行以下命令并将 <app-configuration-store-connection-string> 替换为你的应用程序配置存储的连接字符串:

    setx AZURE_APPCONFIG_CONNECTION_STRING "<app-configuration-store-connection-string>"
    
  2. 使用以下命令输出环境变量的值,以验证其设置是否正确。

    使用 Windows 命令提示符时,重启命令提示符使更改生效,然后运行以下命令:

    echo %AZURE_APPCONFIG_CONNECTION_STRING%
    
  3. 正确设置环境变量后,请运行以下命令以在本地运行应用:

    node app.js
    

    对于每个示例,你应该看到以下输出:

    示例 1

    Sample 1: Load key-values with default selector
    ---Consume configuration as a Map---
    settings.get("message"): Message from Azure App Configuration
    settings.get("app.greeting"): Hello World
    settings.get("app.json"): { myKey: 'myValue' }
    ---Consume configuration as an object---
    config.message: Message from Azure App Configuration
    config.app.greeting: Hello World
    config.app.json: { myKey: 'myValue' }
    

    示例 2

    Sample 2: Load specific key-values using selectors
    ---Consume configuration as a Map---
    settings.has("message"): false
    settings.has("app.greeting"): true
    settings.has("app.json"): true
    ---Consume configuration as an object---
    config.message: undefined
    config.app.greeting: Hello World
    config.app.json: { myKey: 'myValue' }
    

    示例 3

    Sample 3: Load key-values and trim prefix from keys
    ---Consume configuration as a Map---
    settings.get("greeting"): Hello World
    settings.get("json"): { myKey: 'myValue' }
    ---Consume configuration as an object---
    config.greeting: Hello World
    config.json: { myKey: 'myValue' }
    

清理资源

如果不想继续使用本文中创建的资源,请删除此处创建的资源组以避免产生费用。

重要

删除资源组的操作不可逆。 将永久删除资源组以及其中的所有资源。 请确保不要意外删除错误的资源组或资源。 如果在包含要保留的其他资源的资源组中创建了本文的资源,请从相应的窗格中单独删除每个资源,而不是删除该资源组。

  1. 登录到 Azure 门户,然后选择“资源组”。
  2. 在“按名称筛选”框中,输入资源组的名称
  3. 在结果列表中,选择资源组名称以查看概述。
  4. 选择“删除资源组”。
  5. 系统会要求确认是否删除资源组。 重新键入资源组的名称进行确认,然后选择“删除” 。

片刻之后,将会删除该资源组及其所有资源。

后续步骤

在本快速入门中,你创建了一个新的应用程序配置存储,并学习了如何在 Node.js 应用中使用应用程序配置 JavaScript 提供程序访问键值。

如需更多代码示例,请访问: