SpringCloud 本地属性覆盖

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

SpringCloud 本地属性覆盖

架构核心技术   2022-05-23 我要评论

Spring Cloud 本地属性覆盖

注:使用版本版本 spring cloud F SR2

当前在项目中使用了Spring cloud 配置中心模式,使用spring.cloud.config.server.overrides对一些公共配置进行下发,比如kafka bus 的server 配置等等,但是在一些特殊情况下需要本地使用其他的kafka配置,所以就有了配置上的冲突。但是远程配置的优先级默认高于本地配置。

优先级如下

  • 1.命令行参数
  • 2.java:comp/env 里的 JNDI 属性
  • 3.JVM 系统属性
  • 4.操作系统环境变量
  • 5.RandomValuePropertySource 属性类生成的 random.* 属性
  • 6.应用以外的 application.properties(或 yml)文件
  • 7.打包在应用内的 application.properties(或 yml)文件
  • 8.在应用 @Configuration 配置类中,用 @PropertySource 注解声明的属性文件
  • 9.SpringApplication.setDefaultProperties 声明的默认属性

所以 本地kafka配置不能生效了。

官方给出了解决方案如下

2.4 Overriding the Values of Remote Properties
The property sources that are added to your application by the bootstrap context are often “remote” (from example, from Spring Cloud Config Server). By default, they cannot be overridden locally. If you want to let your applications override the remote properties with their own System properties or config files, the remote property source has to grant it permission by setting spring.cloud.config.allowOverride=true (it does not work to set this locally). Once that flag is set, two finer-grained settings control the location of the remote properties in relation to system properties and the application’s local configuration:
spring.cloud.config.overrideNone=true: Override from any local property source.
spring.cloud.config.overrideSystemProperties=false: Only system properties, command line arguments, and environment variables (but not the local config files) should override the remote settings.

也就是说

如果想要远程配置优先级高,那么allowOverride设置为false,如果想要本地配置优先级高那么allowOverride设置为true

spring.cloud.config.allowOverride=true

overrideNone为true时本地配置优先级高,包括系统环境变量、本地配置文件等等

spring.cloud.config.overrideNone=true

只有系统环境变量或者系统属性才能覆盖远程配置文件的配置,本地配置文件中配置优先级低于远程配置

spring.cloud.config.overrideSystemProperties=false

看起来很美好,配置下就可以了,但真正配置的时候要注意配置的位置,否则配置加载就会变的很混乱了。

一般配置有三个地方

  • 本地配置
  • 远程 properties
  • config server 下发配置

因为本地优先级低于远程配置,所以建议配置spring.cloud.config.overrideNone=true 在远程git properties中即可。

但是这样配置也会有一点点小坑,因为会默认本地有的配置就会优先采用,比如kafka的本地默认配置

"kafkaBinderDefaultProperties": {
       "spring.kafka.consumer.valueDeserializer": "org.apache.kafka.common.serialization.ByteArrayDeserializer",
       "spring.kafka.producer.keySerializer": "org.apache.kafka.common.serialization.ByteArraySerializer",
       "spring.kafka.consumer.keyDeserializer": "org.apache.kafka.common.serialization.ByteArrayDeserializer",
       "logging.level.kafka.server.KafkaConfig": "ERROR",
       "logging.level.org.I0Itec.zkclient": "ERROR",
       "spring.kafka.producer.valueSerializer": "org.apache.kafka.common.serialization.ByteArraySerializer",
       "logging.level.kafka.admin.AdminClient.AdminConfig": "ERROR"
   }

如果我们想配置kafka序列化的模式比如在远程配置成key string 那就不会生效了,所以在使用的时候要注意类型即可。

总结下:所以,如果想在项目中覆盖远程配置,在远程配置中添加spring.cloud.config.overrideNone=true 即可,千万不要加在本地bootstrap.properties那样会无效的。

Spring Cloud Config本地配置覆盖远程配置

Spring Cloud Config 配置的优先级

远程配置 > 本地配置 > java代码配置

当需要本地配置优先时,可以限制远程配置的优先级

配置

spring:
  cloud:
    config:
      allow-override: true
      override-none: true
      override-system-properties: false

参数解释

  • allow-override:决定override-system-properties是否启用,默认为true,false=禁用用户的配置
  • override-system-properties:用来标识外部配置是否能够覆盖系统属性,默认为true;
  • override-none:当allow-override和override-none同时为true,远程配置的优先级降低,不能覆盖其他配置;

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们