Observação
O acesso a essa página exige autorização. Você pode tentar entrar ou alterar diretórios.
O acesso a essa página exige autorização. Você pode tentar alterar os diretórios.
在本快速入门中,您将 Azure App Configuration 集成到 Java Spring 应用程序中,以集中存储和管理与代码分离的应用程序设置。
先决条件
- 具有活动订阅的Azure帐户。 创建试用版订阅。
- App Configuration 存储,如“创建存储教程”中所示。
- 支持的版本为 17 的 Java 开发工具包 (JDK)。
- Apache Maven 版本 3.0 或更高版本。
- Spring Boot 应用程序。 如果您没有一个,请使用Spring Initializr创建Maven项目。 请务必选择 Maven Project,在 Dependencies 下,添加 Spring Web 依赖项,然后选择 Java 版本 8 或更高版本。
添加键值
将以下键值添加到 App Configuration 存储中,并将 Label 和 Content Type保留默认值。 有关如何使用 Azure portal 或 CLI 将键值添加到存储区的详细信息,请转到 创建键值。
| 密钥 | 值 |
|---|---|
| /application/config.message | 你好 |
连接到 App Configuration 存储库
拥有 App Configuration 存储后,可以使用 Spring Cloud Azure Config 起步器,使应用程序与您创建的 App Configuration 存储进行通信。
若要安装 Spring Cloud Azure Config 初学者模块,请将以下依赖项添加到 pom.xml 文件中:
<dependencies>
...
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-appconfiguration-config-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>spring-cloud-azure-dependencies</artifactId>
<version>7.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
默认情况下,库通过托管标识连接到应用配置存储。 按照说明为凭据分配应用程序配置数据读取者角色。 在运行应用程序之前,请务必预留足够的时间让权限生效。 然后创建一个名为 AppConfigCredential.java 的新文件,并添加以下行:
import org.springframework.stereotype.Component; import com.azure.data.appconfiguration.ConfigurationClientBuilder; import com.azure.identity.DefaultAzureCredentialBuilder; import com.azure.spring.cloud.appconfiguration.config.ConfigurationClientCustomizer; @Component public class AppConfigCredential implements ConfigurationClientCustomizer { @Override public void customize(ConfigurationClientBuilder builder, String endpoint) { builder.credential(new DefaultAzureCredentialBuilder().build()); } }注释
此外,可以使用 Spring Cloud Azure 身份验证提供身份验证信息。 使用 Azure Spring 配置进行身份验证时,它将对所有 Azure Spring 库使用相同的身份验证。
然后创建一个配置“Bootstrap Configuration”,方法是在
spring.factories目录下创建resources/META-INF文件,添加以下行,并将com.example.MyApplication更新为您的应用程序名称和包名:org.springframework.cloud.bootstrap.BootstrapConfiguration=\ com.example.MyApplication在应用的资源目录下创建名为 application.properties 的新文件,并将以下行添加到该文件。
spring.config.import=azureAppConfiguration spring.cloud.azure.appconfiguration.stores[0].endpoint= ${APP_CONFIGURATION_ENDPOINT}
从App Configuration存储读取
要使用 Spring Cloud Azure Config 启动器使应用程序与您创建的 App Configuration 存储进行通信,请按照以下步骤配置应用程序。
创建名为 MyProperties.java 的新 Java 文件,并添加以下行:
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "config") public class MyProperties { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }创建名为 HelloController.java 的新 Java 文件,并添加以下行:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Autowired private MyProperties properties; @GetMapping public String getMessage() { return "Message: " + properties.getMessage(); } }打开自动生成的单元测试并进行更新,以禁用Azure App Configuration,否则在运行单元测试时,它会尝试从该服务加载配置。
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() { } }
在本地生成并运行应用
设置名为APP_CONFIGURATION_ENDPOINT的环境变量,并将其设置为您的 App Configuration 存储的访问密钥。 在命令行中,运行以下命令并重启命令提示符,以使更改生效:
setx APP_CONFIGURATION_ENDPOINT "<endpoint-of-your-app-configuration-store>"如果使用 Windows PowerShell,请运行以下命令:
$Env:APP_CONFIGURATION_ENDPOINT = "<endpoint-of-your-app-configuration-store>"如果使用 macOS 或 Linux,则请运行以下命令:
export APP_CONFIGURATION_ENDPOINT='<endpoint-of-your-app-configuration-store>'打开根目录的命令提示符,并运行以下命令,使用 Maven 生成 Spring Boot 应用程序并运行它。
mvn clean package mvn spring-boot:run应用程序运行以后,请使用 curl 测试该应用程序,例如 :
curl -X GET http://localhost:8080/您可以看到您在应用配置存储区中输入的消息。
清理资源
如果不想继续使用本文中创建的资源,请删除此处创建的资源组以避免产生费用。
重要
删除资源组的操作不可逆。 将永久删除资源组以及其中的所有资源。 请确保不要意外删除错误的资源组或资源。 如果在包含要保留的其他资源的资源组中创建了本文的资源,请从相应的窗格中单独删除每个资源,而不是删除该资源组。
- 登录到 Azure portal,然后选择 Resource groups。
- 在“按名称筛选”框中,输入资源组的名称。
- 在结果列表中,选择资源组名称以查看概述。
- 选择“删除资源组”。
- 系统会要求确认是否删除资源组。 重新键入资源组的名称进行确认,然后选择“删除”。
片刻之后,将会删除该资源组及其所有资源。
后续步骤
在本次快速入门指南中,你创建了一个新的 App Configuration 存储,并将其应用于 Java Spring 应用。 有关详细信息,请参阅 spring on Azure。 有关进一步的问题,请参阅 reference 文档,其中提供了有关 Spring Cloud Azure App Configuration 库工作原理的所有详细信息。 若要了解如何使 Java Spring 应用能够动态刷新配置设置,请继续学习下一个教程。