【发布时间】:2021-11-14 09:54:26
【问题描述】:
问题
如何将Azure AppConfiguration 与SpringBoot 2.5.x 或更高版本集成?
信息
我正在尝试将 Azure AppConfiguration 资源与 Spring Boot 2.5.4 项目一起使用。不幸的是,据我所知,我无法让它从 AppConfiguration 读取设置,甚至无法连接到它。
项目是用Spring Initializr新创建的,我只在其中添加了
- Spring Boot 入门网站
- Spring Boot Starter 安全性
- Spring Boot Starter WebSocket
之后我尝试关注Microsoft Quickstart documentation,但没有成功。文档提到它使用Spring 2.4.x,所以我认为一些更改破坏了它。
我还尝试通过查看一些Azure Spring Boot Code Samples 来确定问题。
到目前为止,所有示例都使用了我在搜索过程中学到的bootstrap.properties 文件,该文件已被弃用。将设置移动到application.yml 或启用use-legacy-processing: true 也不起作用。有什么想法吗?
pom.xml
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>azure-spring-cloud-appconfiguration-config</artifactId>
<version>2.0.0</version>
</dependency>
...
application.yml
spring:
config:
use-legacy-processing: true
profiles:
active: "develop"
application:
name: "MySampleService"
cloud:
azure:
appconfiguration:
stores:
- connection-string: "SomeAzureAppConfigurationResourceConnectionString"
label: ${spring.profiles.active}
#mysampleservice:
# message: "this is a message from file"
应用配置资源 我不完全确定设置名称的格式。我尝试构建基于this documentation的格式。
配置类应该没问题,因为mysampleservice 中的注释会导致使用的消息蜂的值。
感谢任何提示!
更多信息来详细说明接受的答案
答案中链接的文档是指两个不同的包。在 maven 存储库的开头链接的一个是spring-cloud-azure-appconfiguration-config,而在后面使用的一个是azure-spring-cloud-appconfiguration-config。第二个适用于bootstrap.properties 文件。
工作pom.xml和bootstrap.properties:
...
<dependencies>
<!-- Dependency to load configuration from azure app configuration resource. Note that additional settings are required in bootstrap.properties
Documentation of settings: https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/appconfiguration/azure-spring-cloud-starter-appconfiguration-config
-->
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>azure-spring-cloud-appconfiguration-config-web</artifactId>
<version>2.1.0</version>
</dependency>
...
# Use this to enable or disable the cloud config, disabling it results in application.yaml beeing used.
spring.cloud.azure.appconfiguration.enabled=true
# Connection string to azure app configuration resource
spring.cloud.azure.appconfiguration.stores[0].connection-string= Endpoint=https://myofficeconfiguration.azconfig.io;Id=zUcT-l9-s0:PFYfW7WM0/Pz7WZOnH3v;Secret=JTB9myJqGekDAJ5m8Z1vjmkJZrPd88JbOEE3EqoqJYs=
# Configured filters for settings in the previous defined app configuration resource
spring.cloud.azure.appconfiguration.stores[0].selects[0].key-filter = /mysampleservice/
spring.cloud.azure.appconfiguration.stores[0].selects[0].label-filter = Sample
spring.cloud.azure.appconfiguration.stores[0].selects[1].key-filter = /notificationservice/
spring.cloud.azure.appconfiguration.stores[0].selects[1].label-filter = Sample2
【问题讨论】:
标签: spring spring-boot azure spring-cloud azure-app-configuration