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那样会无效的。

转载于:https://juejin.im/post/5d0467d26fb9a07efd470ac5

猜你喜欢

转载自blog.csdn.net/weixin_34319111/article/details/93176209