使用Spring Cloud Config和Discovery无法获取logback-spring.xml属性文件 [英] Unable to get logback-spring.xml property file using Spring Cloud Config and Discovery
问题描述
I'm using Discovery first bootstrap feature and Consul as a Discovery Server, url to Config Server is located during start-up and I was able to get application.properties
. I need also to get logback-spring.xml
configuration from Config server and I don't know how.
我应该在logging.config={???}logback-spring.xml
属性中指定什么,以免将url硬编码到Config Server?
What should I specify in logging.config={???}logback-spring.xml
property to not hardcode url to Config Server?
Before Consul integration I was using url formed according to Serving Plain text documentation with hardcoded Config server url in properties and it was working fine, but now we want to avoid this.
根据我的调试,在PropertySourceBootstrapConfiguration
中重新初始化日志记录系统期间没有使用Discovery客户端.
From what I debugged there is no usage of Discovery client during reinitializing logging system in PropertySourceBootstrapConfiguration
.
推荐答案
I used Customizing Bootstrap Configuration to resolve my issue in a 'custom' way because I didn't find the solution in the documentation and source code.
示例:添加新文件src/main/resources/META-INF/spring.factories
并在其中添加自定义引导程序配置:org.springframework.cloud.bootstrap.BootstrapConfiguration=sample.custom.CustomPropertySourceLocator
Example: Add new file src/main/resources/META-INF/spring.factories
and add there custom bootstrap configuration: org.springframework.cloud.bootstrap.BootstrapConfiguration=sample.custom.CustomPropertySourceLocator
在CustomPropertySourceLocator中,创建指向配置服务器URL(通过发现查找)的属性
In CustomPropertySourceLocator create property that will point to config server url (looked up via discovery)
@Configuration
public class CustomPropertySourceLocator implements PropertySourceLocator {
private final String configServiceName;
private final DiscoveryClient discoveryClient;
public CustomPropertySourceLocator(
@Value("${spring.cloud.config.discovery.service-id}") String configServiceName,
DiscoveryClient discoveryClient){
this.configServiceName = configServiceName;
this.discoveryClient = discoveryClient;
}
@Override
public PropertySource<?> locate(Environment environment) {
List<ServiceInstance> instances = this.discoveryClient.getInstances(this.configServiceName);
ServiceInstance serviceInstance = instances.get(0);
return new MapPropertySource("customProperty",
Collections.singletonMap("configserver.discovered.uri", serviceInstance.getUri()));
}
}
在上面的代码中,我们创建了具有一个属性configserver.discovered.uri
的自定义属性源.我们可以在代码中(使用@Value)或其他属性文件(即使它们位于配置服务器存储中)使用此属性.
In code above we created custom property source that will have one property configserver.discovered.uri
. We can use this property in our code (using @Value) or in other property files (even if they are located in the config-server storage).
logging.config=${configserver.discovered.uri}/<path to the text file>/logback-spring.xml
应根据提供纯文本文档<path to text file> >以及配置服务器的方式.
logging.config=${configserver.discovered.uri}/<path to the text file>/logback-spring.xml
where <path to text file>
should be formed according to the Serving Plain Text Documentation and the way how you configured your config-server.
这篇关于使用Spring Cloud Config和Discovery无法获取logback-spring.xml属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!