教程:在 Java Spring 应用中使用动态配置

应用程序配置包含两个适用于 Spring 的库。

  • spring-cloud-azure-appconfiguration-config 需要 Spring Boot 并依赖于 spring-cloud-context
  • spring-cloud-azure-appconfiguration-config-web 需要 Spring Web 和 Spring Boot,并且还添加了对配置刷新自动检查的支持。

支持手动触发这两个库来检查刷新的配置值。

通过刷新操作,无需重启应用程序即可更新配置值,不过,这会导致在 @RefreshScope 中重新创建所有的 bean。 它检查对已配置触发器(包括元数据)的任何更改。 默认情况下,更改检查之间的最短时间(刷新间隔)设置为 30 秒。

spring-cloud-azure-appconfiguration-config-web 的自动刷新是基于活动(具体而言,是基于 Spring Web 的 ServletRequestHandledEvent)触发的。 如果 ServletRequestHandledEvent 未触发,则即使缓存过期时间已过,spring-cloud-azure-appconfiguration-config-web 的自动刷新也不会触发刷新。

使用手动刷新

若要使用手动刷新,请从使用应用程序配置的 Spring Boot 应用(例如,按适用于应用程序配置的 Spring Boot 快速入门创建的应用)着手。

应用程序配置公开了 AppConfigurationRefresh,后者可用来检查缓存是否已过期,如果已过期,将触发刷新。

  1. 更新 HelloController 以使用 AppConfigurationRefresh

    import com.azure.spring.cloud.config.AppConfigurationRefresh;
    
    ...
    
    import com.azure.spring.cloud.config.AppConfigurationRefresh;
    
    @RestController
    public class HelloController {
        private final MessageProperties properties;
    
        @Autowired(required = false)
        private AppConfigurationRefresh refresh;
    
        public HelloController(MessageProperties properties) {
            this.properties = properties;
        }
    
        @GetMapping
        public String getMessage() throws InterruptedException, ExecutionException {
            if (refresh != null) {
                refresh.refreshConfigurations();
            }
            return "Message: " + properties.getMessage();
        }
    }
    

    如果已触发刷新,AppConfigurationRefreshrefreshConfigurations() 将返回值为 true 的 Mono,否则返回值 false。 False 表示缓存过期时间未过、未发生更改,或者另一线程当前正在检查刷新。

  2. 更新 bootstrap.properties 以启用刷新

    spring.cloud.azure.appconfiguration.stores[0].monitoring.enabled=true
    spring.cloud.azure.appconfiguration.stores[0].monitoring.refresh-interval= 30s
    spring.cloud.azure.appconfiguration.stores[0].monitoring.triggers[0].key=sentinel
    
  3. 打开 Azure 门户并导航到与应用程序关联的应用程序配置资源。 选择“操作”下的“配置资源管理器”,然后通过选择“+ 创建”>“键值”并添加以下参数以创建新的键值对。

    密钥
    sentinel 1

    暂时将“标签”和“内容类型”保留为空 。

  4. 选择“应用”。

  5. 使用 Maven 生成 Spring Boot 应用程序,然后运行该程序。

    mvn clean package
    mvn spring-boot:run
    
  6. 打开浏览器窗口,访问 URL:http://localhost:8080。 将显示与密钥关联的消息。

    还可以使用 curl 来测试应用程序,例如:

    curl -X GET http://localhost:8080/
    
  7. 若要测试动态配置,请打开与应用程序关联的 Azure 应用程序配置门户。 选择“配置资源管理器”,并更新所显示的密钥的值,例如:

    /application/config.message Hello - Updated
  8. 将之前创建的 sentinel 密钥更新为新值。 此更改将在刷新间隔时间段过去后触发应用程序来刷新所有配置密钥。

    密钥
    sentinel 2
  9. 刷新浏览器页面两次,查看显示的新消息。 第一次触发刷新,第二次加载更改。

注意

该库仅在刷新间隔过去后检查更改,如果该时间段尚未过去,则不会显示任何更改,必须等待该时间段过去后才能触发刷新检查。

使用自动刷新

若要使用自动刷新,请从使用应用程序配置的 Spring Boot 应用(例如,按适用于应用程序配置的 Spring Boot 快速入门创建的应用)着手。

然后,在文本编辑器中打开 pom.xml 文件,并使用以下代码为 spring-cloud-azure-appconfiguration-config-web 添加 <dependency>

Spring Boot

<dependency>
    <groupId>com.azure.spring</groupId>
    <artifactId>spring-cloud-azure-appconfiguration-config-web</artifactId>
    <version>5.2.0</version>
</dependency>
  1. 更新 bootstrap.properties 以启用刷新

    spring.cloud.azure.appconfiguration.stores[0].monitoring.enabled=true
    spring.cloud.azure.appconfiguration.stores[0].monitoring.refresh-interval= 30s
    spring.cloud.azure.appconfiguration.stores[0].monitoring.triggers[0].key=sentinel
    
  2. 打开 Azure 门户并导航到与应用程序关联的应用程序配置资源。 选择“操作”下的“配置资源管理器”,然后通过选择“+ 创建”>“键值”并添加以下参数以创建新的键值对。

    密钥
    sentinel 1

    暂时将“标签”和“内容类型”保留为空 。

  3. 选择“应用”。

  4. 使用 Maven 生成 Spring Boot 应用程序,然后运行该程序。

    mvn clean package
    mvn spring-boot:run
    
  5. 打开浏览器窗口,访问 URL:http://localhost:8080。 将显示与密钥关联的消息。

    还可以使用 curl 来测试应用程序,例如:

    curl -X GET http://localhost:8080/
    
  6. 若要测试动态配置,请打开与应用程序关联的 Azure 应用程序配置门户。 选择“配置资源管理器”,并更新所显示的密钥的值,例如:

    /application/config.message Hello - Updated
  7. 将之前创建的 sentinel 密钥更新为新值。 此更改将在刷新间隔时间段过去后触发应用程序来刷新所有配置密钥。

    密钥
    sentinel 2
  8. 刷新浏览器页面两次,查看显示的新消息。 第一次触发刷新,第二次加载更改,因为第一个请求使用原始范围返回。

注意

该库仅在刷新间隔过去后检查更改。 如果刷新间隔尚未过去,则它不会检查更改;必须等待该间隔过去后才能触发刷新检查。

后续步骤

在本教程中,你已启用 Spring Boot 应用,可以通过应用程序配置动态刷新配置设置。 若要了解如何使用 Azure 托管标识来简化对应用程序配置的访问,请继续学习下一篇教程。