Unable to get logback-spring.xml property file using Spring Cloud Config and Discovery

回眸只為那壹抹淺笑 提交于 2019-12-01 19:47:06

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.

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

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()));
  }
}

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 where <path to text file> should be formed according to the Serving Plain Text Documentation and the way how you configured your config-server.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!