Spring cloud project configuration extension loaded from local config directory

Keywords: Java Spring Redis git github

Inspired by Ali Open Source Nacos, this paper loads configuration from Nacos service to application after application start-up, and wonders whether loading configuration can be loaded from local storage during local development, which can also speed up development efficiency.

First

Let's look at the bootstrap.yaml configuration for the Spring Cloud project to apply Nacos services as follows

spring:
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml
      discovery:
        server-addr: 127.0.0.1:8848
  application:
    name: demo
  profiles:
    active: db,redis,rabbit,es,zk

Then configure it in the Nacos console

After that, the application can take the configuration from Nacos.

Problem point

The author believes that if Nacos can be loaded from the file system instead of Nacos, the development efficiency can be accelerated and the Coding business code can be in a good mood.

Solutions

Analysis

After analysis, start configuration spring.factories and configuration class NacosConfigProperties

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.cloud.alibaba.nacos.NacosConfigBootstrapConfiguration

Find the NacosConfigBootstrapConfiguration code as follows

@Configuration
@ConditionalOnProperty(name = "spring.cloud.nacos.config.enabled", matchIfMissing = true)
public class NacosConfigBootstrapConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public NacosConfigProperties nacosConfigProperties() {
        return new NacosConfigProperties();
    }

    @Bean
    public NacosPropertySourceLocator nacosPropertySourceLocator(
            NacosConfigProperties nacosConfigProperties) {
        return new NacosPropertySourceLocator(nacosConfigProperties);
    }

}

The key is the interface PropertySourceLocator implemented by NacosPropertySourceLocator

/**
 * Strategy for locating (possibly remote) property sources for the Environment.
 * Implementations should not fail unless they intend to prevent the application from
 * starting.
 *
 * @author Dave Syer
 *
 */
public interface PropertySourceLocator {

    /**
     * @param environment The current Environment.
     * @return A PropertySource, or null if there is none.
     * @throws IllegalStateException if there is a fail-fast condition.
     */
    PropertySource<?> locate(Environment environment);

}

When you get here, you know what to do.

See the git library I shared at https://github.com/lyg123/Spring CloudLocal CofigDemo

The new bootstarp.yaml configuration is as follows

spring:
  cloud:
    nacos:
      config:
        enabled: false
        server-addr: 127.0.0.1:8848
        file-extension: yaml
      discovery:
        server-addr: 127.0.0.1:8848
  application:
    name: demo
  profiles:
    active: db,redis,rabbit,es,zk

In this way, the application startup configuration can be loaded from the local file system or from the Nacos service.

Posted by lauthiamkok on Sat, 05 Oct 2019 19:19:15 -0700