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

快速入门:使用 Azure 应用配置创建 Java Spring 应用

在本快速入门中,会将 Azure 应用程序配置合并到 Java Spring 应用程序中,以集中存储和管理与代码分离的应用程序设置。

先决条件

  • 具有活动订阅的 Azure 帐户。 免费创建一个
  • 应用程序配置存储区。 创建存储区
  • 受支持的 Java 开发工具包 (JDK) 版本 11。
  • Apache Maven 版本 3.0 或更高版本。
  • Spring Boot 应用程序。 如果没有,请使用 Spring Initializr 创建一个 Maven 项目。 请务必选择“Maven 项目”,并在“依赖项”下添加“Spring Web”依赖项,然后选择“Java 版本 8 或更高版本”。

添加键值

将以下键值添加到应用程序配置存储区,并让“标签”和“内容类型”保留默认值。 有关如何使用 Azure 门户或 CLI 将键值添加到存储区的详细信息,请转到创建键值

密钥
/application/config.message 你好

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

现在,你已拥有应用程序配置存储,可以使用 Spring Cloud Azure Config 初学者让应用程序与所创建的应用程序配置存储进行通信。

要安装 Spring Cloud Azure Config 初学者模块,请将以下依赖项添加到 pom.xml 文件:

<dependency>
    <groupId>com.azure.spring</groupId>
    <artifactId>spring-cloud-azure-appconfiguration-config-web</artifactId>
</dependency>

<dependencyManagement>
    <dependencies>
        <dependency>
        <groupId>com.azure.spring</groupId>
        <artifactId>spring-cloud-azure-dependencies</artifactId>
        <version>5.8.0</version>
        <type>pom</type>
        <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

编写应用程序代码

要使用 Spring Cloud Azure Config 初学者来让应用程序与所创建的应用程序配置存储通信,请使用以下步骤配置应用程序。

  1. 创建名为 MessageProperties.java 的新 Java 文件,并添加以下行:

    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;
        }
    }
    
  2. 创建名为 HelloController.java 的新 Java 文件,并添加以下行:

    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();
        }
    }
    
  3. 在主应用程序 Java 文件中,添加 @EnableConfigurationProperties 以使 MessageProperties.java 配置属性类生效并将其注册到 Spring 容器。

    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);
        }
    }
    
  4. 打开自动生成的单元测试并更新以禁用 Azure 应用程序配置,或者它将在运行单元测试时尝试从服务加载。

    import org.junit.jupiter.api.Test;
    import org.springframework.boot.test.context.SpringBootTest;
    
    @SpringBootTest(properties = "spring.cloud.azure.appconfiguration.enabled=false")
    class DemoApplicationTests {
    
        @Test
        void contextLoads() {
        }
    
    }
    
  5. 在应用的资源目录下创建名为 bootstrap.properties 的新文件,并将以下行添加到该文件中。

    spring.cloud.azure.appconfiguration.stores[0].connection-string= ${APP_CONFIGURATION_CONNECTION_STRING}
    
  6. 设置名为“APP_CONFIGURATION_CONNECTION_STRING”的环境变量,并将其设置为应用配置存储的访问密钥 。 在命令行中,运行以下命令并重启命令提示符,以使更改生效:

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

    如果使用 Windows PowerShell,请运行以下命令:

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

    如果使用 macOS 或 Linux,则请运行以下命令:

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

在本地生成并运行应用

  1. 打开根目录的命令提示符,并运行以下命令,使用 Maven 生成 Spring Boot 应用程序并运行它。

    mvn clean package
    mvn spring-boot:run
    
  2. 应用程序运行以后,请使用 curl 测试该应用程序,例如 :

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

    可看到在应用程序配置存储区中输入的消息。

清理资源

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

重要

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

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

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

后续步骤

本快速入门介绍了如何创建新的应用程序配置存储区,并将其用于 Java Spring 应用。 有关详细信息,请参阅 Azure 上的 Spring。 有关更多问题,请参阅参考文档,其中包含有关 Spring Cloud Azure 应用程序配置库如何工作的所有详细信息。 若要了解如何使 Java Spring 应用能够动态刷新配置设置,请继续学习下一个教程。