快速入门:使用 Azure 应用配置创建 Java Spring 应用Quickstart: Create a Java Spring app with Azure App Configuration

在本快速入门中,会将 Azure 应用程序配置合并到 Java Spring 应用程序中,以集中存储和管理与代码分离的应用程序设置。In this quickstart, you incorporate Azure App Configuration into a Java Spring app to centralize storage and management of application settings separate from your code.

先决条件Prerequisites

创建应用配置存储区Create an App Configuration store

  1. 若要创建新应用程序配置存储区,请登录 Azure 门户To create a new App Configuration store, sign in to the Azure portal. 在主页的左上角,选择“创建资源” 。In the upper-left corner of the home page, select Create a resource. 在“搜索市场”框中,输入“应用程序配置”并选择 Enter 。In the Search the Marketplace box, enter App Configuration and select Enter.

    搜索应用配置

  2. 在搜索结果中选择“应用程序配置”,然后选择“创建” 。Select App Configuration from the search results, and then select Create.

    选择“创建”

  3. 在“创建应用配置”窗格中,输入以下设置:On the Create App Configuration pane, enter the following settings:

    设置Setting 建议的值Suggested value 说明Description
    订阅Subscription 订阅Your subscription 选择要用来测试应用配置的 Azure 订阅。Select the Azure subscription that you want to use to test App Configuration. 如果帐户只有一个订阅,则会自动选择该订阅并且不显示“订阅”列表 。If your account has only one subscription, it's automatically selected and the Subscription list isn't displayed.
    资源组Resource group AppConfigTestResources AppConfigTestResources 为应用程序配置存储区资源选择或创建资源组。Select or create a resource group for your App Configuration store resource. 此组可用于组织多个资源,删除该资源组可以同时删除这些资源。This group is useful for organizing multiple resources that you might want to delete at the same time by deleting the resource group. 有关详细信息,请参阅使用资源组管理 Azure 资源For more information, see Use resource groups to manage your Azure resources.
    资源名称Resource name 全局唯一名称Globally unique name 输入要用于应用程序配置存储区资源的唯一资源名称。Enter a unique resource name to use for the App Configuration store resource. 该名称必须是包含 5 到 50 个字符的字符串,只能包含数字、字母和 - 字符。The name must be a string between 5 and 50 characters and contain only numbers, letters, and the - character. 该名称的开头或末尾不能是 - 字符。The name can't start or end with the - character.
    位置Location 中国东部 2China East 2 使用“位置”指定在其中托管应用配置存储区的地理位置 。Use Location to specify the geographic location in which your app configuration store is hosted. 为获得最佳性能,请在应用程序的其他组件所在的同一区域创建资源。For the best performance, create the resource in the same region as other components of your application.
    定价层Pricing tier 免费Free 选择所需的定价层。Select the desired pricing tier. 有关详细信息,请参阅应用配置定价页For more information, see the App Configuration pricing page.
  4. 选择“查看 + 创建”以验证自己的设置 。Select Review + create to validate your settings.

  5. 选择“创建”。Select Create. 部署可能需要几分钟。The deployment might take a few minutes.

  6. 部署完成后,导航到“应用程序配置”资源。After the deployment finishes, navigate to the App Configuration resource. 选择“设置” > “访问密钥”。Select Settings > Access keys. 记下只读主密钥连接字符串。Make a note of the primary read-only key connection string. 稍后将使用此连接字符串来配置应用程序,以与创建的应用程序配置存储区进行通信。You'll use this connection string later to configure your application to communicate with the App Configuration store that you created.

  1. 选择“配置资源管理器” > “+ 创建” > “键-值”来添加以下键值对 :Select Configuration Explorer > + Create > Key-value to add the following key-value pairs:

    密钥Key Value
    /application/config.message/application/config.message 你好Hello

    暂时将“标签”和“内容类型”保留为空 。Leave Label and Content Type empty for now.

  2. 选择“应用”。 Select Apply.

创建 Spring Boot 应用Create a Spring Boot app

使用 Spring Initializr 创建一个新 Spring Boot 项目。Use the Spring Initializr to create a new Spring Boot project.

  1. 浏览到 https://start.spring.io/Browse to https://start.spring.io/.

  2. 指定以下选项:Specify the following options:

    • 使用 Java 生成一个 Maven 项目。Generate a Maven project with Java.
    • 指定一个其值大于或等于 2.0 的 Spring Boot 版本。Specify a Spring Boot version that's equal to or greater than 2.0.
    • 指定应用程序的“组”和“项目”名称。 Specify the Group and Artifact names for your application.
    • 添加 Spring Web 依赖项。Add the Spring Web dependency.
  3. 指定上述选项后,选择“生成项目” 。After you specify the previous options, select Generate Project. 出现提示时,将项目下载到本地计算机中的路径。When prompted, download the project to a path on your local computer.

连接到应用程序配置存储区Connect to an App Configuration store

  1. 从本地系统提取文件后,即可使用简单的 Spring Boot 应用程序进行编辑。After you extract the files on your local system, your simple Spring Boot application is ready for editing. 在应用的根目录中找到 pom.xml 文件 。Locate the pom.xml file in the root directory of your app.

  2. 在文本编辑器中打开 pom.xml 文件,将 Spring Cloud Azure Config Starter 添加到 <dependencies> 列表:Open the pom.xml file in a text editor, and add the Spring Cloud Azure Config starter to the list of <dependencies>:

    Spring Cloud 1.1.xSpring Cloud 1.1.x

    <dependency>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>spring-cloud-azure-appconfiguration-config</artifactId>
        <version>1.1.5</version>
    </dependency>
    

    Spring Cloud 1.2.xSpring Cloud 1.2.x

    <dependency>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>spring-cloud-azure-appconfiguration-config</artifactId>
        <version>1.2.7</version>
    </dependency>
    
  3. 在应用的包目录中创建名为 MessageProperties.java 的新 Java 文件 。Create a new Java file named MessageProperties.java in the package directory of your app. 添加以下行:Add the following lines:

    package com.example.demo;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    @ConfigurationProperties(prefix = "config")
    public class MessageProperties {
        private String message;
    
        public String getMessage() {
            return message;
        }
    
        public void setMessage(String message) {
            this.message = message;
        }
    }
    
  4. 在应用的包目录中创建新的名为 HelloController.java 的 Java 文件 。Create a new Java file named HelloController.java in the package directory of your app. 添加以下行:Add the following lines:

    package com.example.demo;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloController {
        private final MessageProperties properties;
    
        public HelloController(MessageProperties properties) {
            this.properties = properties;
        }
    
        @GetMapping
        public String getMessage() {
            return "Message: " + properties.getMessage();
        }
    }
    
  5. 打开主应用程序 Java 文件,并添加 @EnableConfigurationProperties以启用此功能。Open the main application Java file, and add @EnableConfigurationProperties to enable this feature.

    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    
    @SpringBootApplication
    @EnableConfigurationProperties(MessageProperties.class)
    public class DemoApplication {
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    }
    
  6. 在应用的资源目录下创建名为 bootstrap.properties 的新文件,并将以下行添加到该文件中。Create a new file named bootstrap.properties under the resources directory of your app, and add the following lines to the file. 使用应用程序配置存储区的相应属性替换示例值。Replace the sample values with the appropriate properties for your App Configuration store.

    spring.cloud.azure.appconfiguration.stores[0].connection-string= ${APP_CONFIGURATION_CONNECTION_STRING}
    
  7. 设置名为“APP_CONFIGURATION_CONNECTION_STRING”的环境变量,并将其设置为应用配置存储的访问密钥 。Set an environment variable named APP_CONFIGURATION_CONNECTION_STRING, and set it to the access key to your App Configuration store. 在命令行中,运行以下命令并重启命令提示符,以使更改生效:At the command line, run the following command and restart the command prompt to allow the change to take effect:

    setx APP_CONFIGURATION_CONNECTION_STRING "connection-string-of-your-app-configuration-store"
    

    如果使用 Windows PowerShell,请运行以下命令:If you use Windows PowerShell, run the following command:

    $Env:APP_CONFIGURATION_CONNECTION_STRING = "connection-string-of-your-app-configuration-store"
    

    如果使用 macOS 或 Linux,则请运行以下命令:If you use macOS or Linux, run the following command:

    export APP_CONFIGURATION_CONNECTION_STRING='connection-string-of-your-app-configuration-store'
    

在本地生成并运行应用Build and run the app locally

  1. 使用 Maven 生成 Spring Boot 应用程序,然后运行该程序,例如:Build your Spring Boot application with Maven and run it, for example:

    mvn clean package
    mvn spring-boot:run
    
  2. 应用程序运行以后,请使用 curl 测试该应用程序,例如 :After your application is running, use curl to test your application, for example:

    curl -X GET http://localhost:8080/
    

    可看到在应用程序配置存储区中输入的消息。You see the message that you entered in the App Configuration store.

清理资源Clean up resources

如果不想继续使用本文中创建的资源,请删除此处创建的资源组以避免产生费用。If you do not want to continue using the resources created in this article, delete the resource group you created here to avoid charges.

重要

删除资源组的操作不可逆。Deleting a resource group is irreversible. 将永久删除资源组以及其中的所有资源。The resource group and all the resources in it are permanently deleted. 请确保不要意外删除错误的资源组或资源。Make sure that you don't accidentally delete the wrong resource group or resources. 如果在包含要保留的其他资源的资源组中创建了本文的资源,请从相应的窗格中单独删除每个资源,而不是删除该资源组。If you created the resources for this article inside a resource group that contains other resources you want to keep, delete each resource individually from its respective pane instead of deleting the resource group.

  1. 登录到 Azure 门户,然后选择“资源组”。Sign in to the Azure portal, and select Resource groups.
  2. 在“按名称筛选”框中,输入资源组的名称。In the Filter by name box, enter the name of your resource group.
  3. 在结果列表中,选择资源组名称以查看概述。In the result list, select the resource group name to see an overview.
  4. 选择“删除资源组”。Select Delete resource group.
  5. 系统会要求确认是否删除资源组。You're asked to confirm the deletion of the resource group. 重新键入资源组的名称进行确认,然后选择“删除” 。Enter the name of your resource group to confirm, and select Delete.

片刻之后,将会删除该资源组及其所有资源。After a few moments, the resource group and all its resources are deleted.

后续步骤Next steps

本快速入门介绍了如何创建新的应用程序配置存储区,并将其用于 Java Spring 应用。In this quickstart, you created a new App Configuration store and used it with a Java Spring app. 有关详细信息,请参阅 Azure 上的 SpringFor more information, see Spring on Azure. 若要了解如何使 Java Spring 应用能够动态刷新配置设置,请继续学习下一个教程。To learn how to enable your Java Spring app to dynamically refresh configuration settings, continue to the next tutorial.