Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apollo集成springboot加载配置时启动顺序太低,导致某些配置不生效 #1211

Closed
cyejing opened this issue Jun 29, 2018 · 9 comments
Labels
feature request Categorizes issue as related to a new feature.

Comments

@cyejing
Copy link

cyejing commented Jun 29, 2018

apollo集成springboot加载配置时启动顺序太低,导致某些配置不生效
不生效的配置有:
logging.level logging.file
原因是apollo加载配置的顺序太慢导致的,应该在LoggingApplicationListener加载之前才可以正常
推荐:应该继承EnvironmentPostProcessor方法,在准备环境阶段就去加载apollo的配置

@stateIs0
Copy link

开启 apollo.bootstrap.enabled = true 了吗

@cyejing
Copy link
Author

cyejing commented Jun 29, 2018

开启了,但是还是不起作用。因为没有LoggingApplicationListener这个类的顺序高

@nobodyiam
Copy link
Member

日志这个比较特殊,它的优先级肯定是最高的,不过可以在加载apollo配置后刷新一下,具体可以参考一下demo项目的做法:https://github.com/ctripcorp/apollo-use-cases/tree/master/spring-boot-logger,而且也支持动态刷新。

@cyejing
Copy link
Author

cyejing commented Jun 30, 2018

启动完之后再刷新这种操作自然是繁琐的做法,建议你可以参照一下 SpringCloudConfig的做法改一下你们的配置加载类,比如用这个org.springframework.cloud.bootstrap.BootstrapConfigurationorg.springframework.cloud.config.client.ConfigServiceBootstrapConfiguration使得apollo的配置文件有更高的加载顺序

@nobodyiam
Copy link
Member

如我前面所说的,日志的优先级肯定是最高的。

spring cloud config的做法也是在配置加载后刷新了一下Logging system,不过我们也可以考虑一下是否把这个能力内置到apollo客户端中去。

https://github.com/spring-cloud/spring-cloud-commons/blob/master/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java#L110

@nobodyiam
Copy link
Member

@cyejing
Copy link
Author

cyejing commented Jul 2, 2018

我觉得在外部再添加方法去刷新日志的设置这种做法非常繁琐,不是一个优良的设计。内置到apollo客户端还差不多,因为这些日志的配置本来就是apollo配置里面的内容,结果因为不生效,需要用户再去刷新。这不是很不好用吗?
而且我其实已经有实现了,正如我前面说的,springboot在设置日志级别的时候是靠这个类去设置https://github.com/spring-projects/spring-boot/blob/4eff07f6c0c7131690b0bbd4a13b7edd58347726/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java#L238
只需要在这个类前面把apollo的配置加载到environment里面即可。
推荐:应该继承EnvironmentPostProcessor方法,在准备环境阶段就去加载apollo的配置

@nobodyiam
Copy link
Member

LoggingApplicationListener这个类貌似是spring boot 2.0.0以后才有的,感觉上应该是把spring cloud的一些能力一直到spring boot 2.0了。

另外,之前apollo一度是适配spring boot启动阶段的各种event的(SpringApplicationRunListener),可是无奈spring boot每次升级都会有不兼容的情况,所以后来就还是改成和spring集成了,详见此commit

EnvironmentPostProcessor或许是一个可以切入的点,不过粗略看了一下,它也是在ApplicationEnvironmentPreparedEvent中触发的,和LoggingApplicationListener初始化LoggingSystem属于同一个阶段,尚不确定是否一定可以,后续我们再看一下,不过确实是一个不错的思路,非常感谢!

@kinggggg
Copy link

kinggggg commented Dec 5, 2018

开启 apollo.bootstrap.enabled = true 了吗

it works for me.

@nobodyiam nobodyiam added feature request Categorizes issue as related to a new feature. and removed enhancement labels Nov 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Categorizes issue as related to a new feature.
Projects
None yet
Development

No branches or pull requests

4 participants