1

尝试设置 Sprint 启动应用程序以从Azure App Configuration加载配置,并参考Azure Key Vault条目以获取包含敏感信息的属性。

使用应用程序配置工作正常,并且在将 Key Vault 引用添加到应用程序配置时出现问题。

为了连接到 Key Vault,AzureConfigBootstrapConfiguration查找KeyVaultCredentialProvider加载时不可用的 bean:

@Bean
    public AzureConfigPropertySourceLocator sourceLocator(AzureCloudConfigProperties properties,
            AppConfigProviderProperties appProperties, ClientStore clients, ApplicationContext context) {
        KeyVaultCredentialProvider keyVaultCredentialProvider = null;
        try {
            keyVaultCredentialProvider = context.getBean(KeyVaultCredentialProvider.class);
        } catch (NoUniqueBeanDefinitionException e) {
            LOGGER.error("Failed to find unique TokenCredentialProvider Bean for authentication.", e);
            if (properties.isFailFast()) {
                throw e;
            }
        } catch (NoSuchBeanDefinitionException e) {
            LOGGER.info("No TokenCredentialProvider found.");
        }
        return new AzureConfigPropertySourceLocator(properties, appProperties, clients, keyVaultCredentialProvider);
    }

试图创建具有最高优先级的 bean,但它不起作用:

@Configuration
public class DemoConfiguration {
    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public KeyVaultCredentialProvider keyVaultCredentialProvider() {
        return uri -> new EnvironmentCredentialBuilder().build();
    }
}

还尝试在 bean 和课堂上使用@Primaryand ,但没有一个替代品有效。@Priority@AutoConfigureBefore(AzureConfigBootstrapConfiguration.class)DemoConfiguration

问题: 你知道在初始化KeyVaultCredentialProvider之前如何创建bean吗?AzureConfigBootstrapConfiguration

4

3 回答 3

0

解决方案:

由于 Azure App Configuration 使用 BootstrapConfiguration,因此解决方案是创建META-INF/spring.factories文件以使用所需的 bean 启用配置,例如:

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.davidcampos.autoconfigure.DemoConfiguration
于 2020-02-25T20:44:14.967 回答
0

您是否在 spring.factories 中设置了 DemoConfiguration?

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.DemoConfiguration

那应该可以找到它。

于 2020-02-25T19:32:31.457 回答
0

如果不知道确切的异常和在您的案例中抛出的堆栈跟踪,很难给出任何提示。

但是,如果它确实是运行时缺少的配置,另一种强制执行您自己的配置顺序的方法是:

public static void main(String[] args) {
    SpringApplication.run(
       new Class[]{ YourSpringBootApplication.class,
           KeyVaultCredentialProvider.class, 
           AzureConfigBootstrapConfiguration.class // , ...
       }, args);
}

Class数组包含在应用程序启动时加载的主要源列表。所以这个列表不需要包含所有的组件和配置。

于 2020-02-25T13:05:08.750 回答