无法使用Spring Cloud Config和Discovery获取logback-spring.xml属性文件

nmy*_*myk 4 logback spring-boot spring-cloud-config spring-cloud-consul spring-logback

我正在使用Discovery的第一个bootstrap功能和Consul作为Discovery Server,Config Server的URL位于启动期间,我能够获得application.properties.我还需要logback-spring.xml从Config服务器获取配置,我不知道如何.

我应该在logging.config={???}logback-spring.xml属性中指定什么不将URL硬编码到Config Server?

在Consul集成之前,我使用根据Serving Plain文本文档形成的url和属性中的硬编码配置服务器URL,它工作正常,但现在我们想避免这种情况.

从我调试的内容来看,在重新初始化日志系统期间没有使用Discovery客户端PropertySourceBootstrapConfiguration.

nmy*_*myk 9

我使用自定义引导程序配置以"自定义"方式解决我的问题,因为我没有在文档和源代码中找到解决方案.

示例:添加新文件src/main/resources/META-INF/spring.factories并添加自定义引导程序配置:org.springframework.cloud.bootstrap.BootstrapConfiguration=sample.custom.CustomPropertySourceLocator

在CustomPropertySourceLocator中创建属性,该属性将指向配置服务器URL(通过发现查找)

@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()));
  }
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我们创建了具有一个属性的自定义属性源configserver.discovered.uri.我们可以在我们的代码(使用@Value)或其他属性文件中使用此属性(即使它们位于config-server存储中).

logging.config=${configserver.discovered.uri}/<path to the text file>/logback-spring.xml 其中,<path to text file>应根据形成服务纯文本文档,以及如何配置你的配置服务器的方式.