You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 2.6.6集成Azure App Configuration刷新功能失效求助

Azure App Configuration集成Spring Webflux配置无法自动刷新问题排查

环境信息

  • Java版本:11
  • Spring Boot版本:2.6.6
  • azure-keyvault-secrets-spring-boot-starter:2.3.2

当前配置与依赖

pom.xml依赖

<dependency>
    <groupId>com.azure.spring</groupId>
    <artifactId>azure-spring-cloud-starter-appconfiguration-config</artifactId>
    <version>2.10.0</version>
</dependency>

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

bootstrap.properties配置

spring.cloud.azure.appconfiguration.stores[0].connection-string=Endpoint=......
spring.cloud.azure.appconfiguration.name=my-appname
spring.cloud.azure.appconfiguration.stores[0].selects[0].key-filter=my-app-name
spring.cloud.azure.appconfiguration.stores[0].monitoring.triggers[0].key=sentinal
spring.cloud.azure.appconfiguration.stores[0].monitoring.triggers[0].label=stage

spring.cloud.azure.appconfiguration.stores[0].monitoring.push-notification.primary-token.name=my-token
spring.cloud.azure.appconfiguration.stores[0].monitoring.push-notification.primary-token.secret=my-secret

spring.cloud.azure.appconfiguration.refresh-interval=10s

spring.cloud.azure.feature.management.fail-fast=false

配置类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
@Data
@ConfigurationProperties("config")
@RefreshScope
public class MyAzureAppConfig {
    private String message;
    private int coolOffPeriod;
}

问题描述

应用启动时可正常加载Azure App Configuration中的配置,但配置变更后无法自动刷新获取新值,已确认触发键存在、推送令牌与密钥配置正确,需要实现无需重启应用即可接收配置变更。

排查与解决方案

1. 统一Spring Cloud Azure依赖版本

当前azure-spring-cloud-starter-appconfiguration-config(2.10.0)与azure-spring-cloud-appconfiguration-config-web(2.11.0)版本不一致,会导致刷新逻辑兼容性问题。建议将两个依赖版本统一为相同版本,例如都使用2.11.0:

<dependency>
    <groupId>com.azure.spring</groupId>
    <artifactId>azure-spring-cloud-starter-appconfiguration-config</artifactId>
    <version>2.11.0</version>
</dependency>

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

2. 修正bootstrap.properties配置

  • 开启自动刷新:添加配置项显式启用刷新功能(部分版本默认未开启):
    spring.cloud.azure.appconfiguration.refresh-enabled=true
    
  • 检查触发键拼写:配置中monitoring.triggers[0].key为sentinal,若Azure App Configuration中的触发键实际是sentinel(常见标准拼写),会导致无法触发刷新,需修正拼写与Azure端完全一致。
  • 调整key-filter规则:当前key-filter=my-app-name,但配置类绑定的是config.*前缀的配置。需确保Azure中的配置键(如config.message)匹配该filter规则,或修改filter为config.*以覆盖目标配置:
    spring.cloud.azure.appconfiguration.stores[0].selects[0].key-filter=config.*
    

3. 优化配置类注解

  • 移除冗余的@Configuration注解,保留@Component即可(@ConfigurationProperties配合@Component更适合配置绑定场景):
    @Component
    @Data
    @ConfigurationProperties("config")
    @RefreshScope
    public class MyAzureAppConfig {
        private String message;
        private int coolOffPeriod;
    }
    
  • 确保在业务代码中通过依赖注入获取配置类实例,而非手动new对象,否则@RefreshScope无法生效。例如在Webflux Controller中注入:
    @RestController
    public class ConfigController {
        private final MyAzureAppConfig appConfig;
    
        public ConfigController(MyAzureAppConfig appConfig) {
            this.appConfig = appConfig;
        }
    
        @GetMapping("/config/message")
        public Mono<String> getMessage() {
            return Mono.just(appConfig.getMessage());
        }
    }
    

4. 验证刷新机制

  • 手动触发刷新:引入Spring Boot Actuator依赖,暴露refresh端点验证手动刷新是否生效:
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    
    添加端点配置:
    management.endpoints.web.exposure.include=refresh
    
    调用POST /actuator/refresh,若配置能刷新,则说明自动推送通知存在问题,需检查Azure端的推送配置(如事件订阅是否正确指向应用地址)。
  • 查看应用日志:搜索AppConfigurationRefresh关键词,确认是否有刷新触发的日志或错误信息(如令牌验证失败、触发键未匹配等),定位具体问题。

内容的提问来源于stack exchange,提问作者EarthsPiligrim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 06:25:00