【问题标题】:Read class from Zookeeper (path with underscore) in Spring Boot application在 Spring Boot 应用程序中从 Zookeeper 读取类(带下划线的路径)
【发布时间】:2020-03-04 16:55:54
【问题描述】:

我们在 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】:

    事实证明没有问题:您不必重命名 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-06
      • 2018-03-17
      • 2020-05-30
      • 2015-02-02
      • 2013-12-22
      相关资源
      最近更新 更多