Spring boot 无法使用spring云配置和发现获取logback-spring.xml属性文件
我正在使用feature and Concur作为发现服务器,配置服务器的url位于启动期间,我能够获取Spring boot 无法使用spring云配置和发现获取logback-spring.xml属性文件,spring-boot,logback,spring-cloud-config,spring-cloud-consul,spring-logback,Spring Boot,Logback,Spring Cloud Config,Spring Cloud Consul,Spring Logback,我正在使用feature and Concur作为发现服务器,配置服务器的url位于启动期间,我能够获取应用程序.properties。我还需要从配置服务器获取logbackspring.xml配置,但我不知道如何获取 我应该在logging.config={???}logback spring.xml属性中指定什么来不硬编码配置服务器的url 在Concur集成之前,我使用的url是根据属性中的硬编码配置服务器url形成的,工作正常,但现在我们希望避免这种情况 从我调试的情况来看,在Prope
应用程序.properties
。我还需要从配置服务器获取logbackspring.xml
配置,但我不知道如何获取
我应该在logging.config={???}logback spring.xml
属性中指定什么来不硬编码配置服务器的url
在Concur集成之前,我使用的url是根据属性中的硬编码配置服务器url形成的,工作正常,但现在我们希望避免这种情况
从我调试的情况来看,在PropertySourceBootstrapConfiguration
中重新初始化日志系统时,没有使用Discovery client 我过去常常以“自定义”的方式解决问题,因为我在文档和源代码中没有找到解决方案
示例:添加新文件src/main/resources/META-INF/spring.factories
并添加自定义引导配置:org.springframework.cloud.bootstrap.BootstrapConfiguration=sample.custom.CustomPropertySourceLocator
在CustomPropertySourceLocator中,创建指向配置服务器url的属性(通过发现查找)
@配置
公共类CustomPropertySourceLocator实现PropertySourceLocator{
私有最终字符串configServiceName;
私人最终发现客户发现客户;
公共客户财产资源运营者(
@值(“${spring.cloud.config.discovery.service id}”)字符串configServiceName,
发现客户端(发现客户端){
this.configServiceName=configServiceName;
this.discoveryClient=discoveryClient;
}
@凌驾
公共属性源定位(环境){
列表实例=this.discoveryClient.getInstances(this.configServiceName);
ServiceInstance ServiceInstance=instances.get(0);
返回新的MapPropertySource(“customProperty”,
Collections.singletonMap(“configserver.discovered.uri”,serviceInstance.getUri());
}
}
在上面的代码中,我们创建了自定义属性源,该属性源将有一个属性configserver.discovered.uri
。我们可以在代码(使用@Value)或其他属性文件(即使它们位于配置服务器存储中)中使用此属性
logging.config=${configserver.discovered.uri}//logback spring.xml
其中,
应根据配置服务器的类型和配置方式形成
@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()));
}
}