你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
在本快速入门中,你将在Azure 应用程序配置中创建一个功能标志,并使用它动态控制 Spring Boot 应用来创建功能管理的端到端实现。
Spring Boot 功能管理库不依赖于任何 Azure 库。 它们可以通过其 Spring Boot 配置提供程序无缝集成到应用程序配置。
Prerequisites
- 具有活动订阅的 Azure 帐户。 免费创建一个。
- 应用配置存储区,如 创建存储区的教程所示。
- 支持版本 17 的 Java 开发工具包 SDK 。
- Apache Maven 版本 3.0 或更高版本。
添加功能标志
将名为“Beta”的功能标志添加到应用程序配置存储区,并将“标签”和“描述”保留为其默认值。 有关如何使用 Azure 门户或 CLI 将功能标志添加到存储区的详细信息,请转到创建功能标志。 在此阶段,应取消选中“启用功能标志”复选框。
创建控制台应用
创建新的 Spring Boot 项目:
指定以下选项:
- 使用 Java 生成一个 Maven 项目。
- 指定等于或大于 3.0 的 Spring Boot 版本。
- 指定应用程序的“组”和“项目”名称。 本文使用
com.example和demo。
指定上述选项后,选择“生成项目”。 下载项目并将其解压缩到本地计算机。
在应用的根目录中找到 pom.xml ,并在文本编辑器中打开它。
将以下内容添加到以下列表
<dependencies>:<dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-appconfiguration-config</artifactId> </dependency> <dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-feature-management</artifactId> </dependency>添加以下
<dependencyManagement>节来管理 Spring Cloud Azure 库版本:<dependencyManagement> <dependencies> <dependency> <groupId>com.azure.spring</groupId> <artifactId>spring-cloud-azure-dependencies</artifactId> <version>7.2.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>导航到应用的
resources目录并打开application.properties或application.yaml文件。使用
DefaultAzureCredential向应用程序配置存储区进行身份验证。 若要使授权正常工作,需要向应用使用的凭证授予 应用配置数据读取者 角色。 有关说明,请参阅 使用令牌凭据进行身份验证。在运行应用程序之前,请务必留出足够的时间来传播权限。在应用的包目录中,用以下代码更新
DemoApplication.java文件:import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import com.azure.spring.cloud.feature.management.FeatureManager; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Bean public CommandLineRunner runner(FeatureManager featureManager) { return args -> { System.out.println("Beta is enabled: " + featureManager.isEnabled("Beta")); }; } }设置名为 AZURE_APPCONFIG_ENDPOINT的环境变量,并将其设置为应用配置存储的终结点。 在命令行中,运行以下命令并重启命令提示符,以使更改生效:
setx AZURE_APPCONFIG_ENDPOINT "<endpoint-of-your-app-configuration-store>"重启命令提示符以使更改生效。 输出环境变量的值以验证它的设置是否正确。
使用 Maven 生成并运行 Spring Boot 应用程序。
mvn clean package mvn spring-boot:run在应用程序配置门户中,选择“功能管理器”,使用“已启用”列中的开关将“Beta”功能标志的状态更改为“开”。
密钥 状态 Beta 启用 重新启动应用程序。 应用程序将打印以下内容:
Beta is enabled: true
清理资源
如果不想继续使用本文中创建的资源,请删除此处创建的资源组以避免产生费用。
重要说明
删除资源组的操作不可逆。 将永久删除资源组以及其中的所有资源。 请确保不要意外删除错误的资源组或资源。 如果在包含要保留的其他资源的资源组中创建了本文的资源,请从相应的窗格中单独删除每个资源,而不是删除该资源组。
- 登录到 Azure 门户,然后选择“资源组”。
- 在“按名称筛选”框中,输入资源组的名称。
- 在结果列表中,选择资源组名称以查看概述。
- 选择“删除资源组”。
- 系统会要求确认是否删除资源组。 重新键入资源组的名称进行确认,然后选择“删除”。
片刻之后,将会删除该资源组及其所有资源。
后续步骤
在本快速入门中,你创建了一个新的应用配置存储区,并使用它通过 功能管理库管理 Spring Boot 应用中的功能。