从Spring Boot应用程序中的Zookeeper中读取类(带下划线的路径)

问题描述 投票:0回答:1

我们在Zookeeper节点中存储多种情况的一些标准配置。它是简单值(字符串,布尔值,整数等)的平面列表。因此,现在我们有了一个用相应的字段描述此配置的类,并使用带有不同前缀的ConfigurationProperties批注来填充其实例。

class DatasourceConfig {
   var pid: String? = null
   var className: String? = null
   var poolSize: Int = 30
   var minIdle: Int = 10
   var maxIdle: Int = 10
   var conTimeout: Long = 100500
   ...
}

问题是,现在我们需要从路径中带有下划线的节点读取该配置的多个实例。 ConfigurationProperties在前缀中不支持蛇形或驼峰形,仅支持烤肉串形。

    @ConfigurationProperties(prefix = "smth.new_path.datasources.aaa")
    fun aaaDataSourceConfig() = DatasourceConfig()

    @ConfigurationProperties(prefix = "smth.new_path.datasources.bbb")
    fun bbbDataSourceConfig() = DatasourceConfig()

这导致错误:

APPLICATION FAILED TO START
***************************

Description:

Configuration property name 'new_path' is not valid:

    Invalid characters: '_'
    Reason: Canonical names should be kebab-case ('-' separated), lowercase alpha-numeric characters and must start with a letter

没有机会重命名节点。这不是一个选择。

Zookeper根节点是使用bootstrap.yml中的spring.cloud.zookeeper.config.root设置的,我猜想Spring Cloud Zookeeper用于读取值。如果我将此根设置为“ new_path”,则ConfigurationProperties可以工作,但是我还需要应用程序中其他路径的值。

配置列表很长,并且使用了多次,所以我想避免对每个属性使用@Value注释。

除了ConfigurationProperties之外,还有其他方法可以调整ConfigurationProperties吗?或Spring Cloud Zookeeper使其协同工作?

spring spring-boot apache-zookeeper spring-cloud-zookeeper
1个回答
0
投票

事实证明没有问题:您不必重命名Zookeeper节点。

ConfigurationProperties使用宽松的绑定,因此虽然前缀应该在kebab情况下,例如new-path,但仍可以在名为new_path或newPath的节点上正常使用。

在此描述:https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/html/boot-features-external-config.html#boot-features-external-config-relaxed-binding

© www.soinside.com 2019 - 2024. All rights reserved.