springcloud expansion project configuration is loaded from the local config directory

Nacos Enlightened by Ali open source, after application startup configuration from Nacos service loaded into the application, thinking locally developed when loading configuration can be loaded from a local store, such efficiency can accelerate the development

First of all

We look at SpringCloud project application Nacos service bootstrap.yamlconfiguration is 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 add the console configuration Nacos

After the above, so that the application can be arranged to take from Nacos.

Problems

I believe that when developed here If you load the configuration from the file system alternative Nacos, can speed up the development efficiency, but also to feel comfortable Coding business code.

After analyzing the startup configuration spring.factoriesand configuration classesNacosConfigProperties

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

Find the NacosConfigBootstrapConfigurationcode is 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 inside NacosPropertySourceLocatorthe interface implementedPropertySourceLocator

/**
 * 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);

}

To understand how to do it here.

Specific look I share git repository https://github.com/lyg123/SpringCloudLocalCofigDemo

The new bootstarp.yamlconfiguration 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

Such application startup configuration is loaded from the local file system or Nacos service load

Guess you like

Origin www.cnblogs.com/li-yg/p/11419732.html