Nacos config提供了配置中心的解決方案,且功能很是的強大適用,提供單機與集羣模式java
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>0.2.1.RELEASE</version> </dependency>
2.bootstrap.properties配置文件web
#nacos配置中心地址 spring.cloud.nacos.config.server-addr=10.136.15.122:8848 #默認應用名稱,配置中心中data-id 默認爲name+file-extension spring.application.name=example 沒有配置狀況下用name做爲前綴 #spring.cloud.nacos.config.prefix #應用文件格式支持properties,yml兩種 spring.cloud.nacos.config.file-extension=properties
3.java應用代碼spring
package com.nacos; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/config") @RefreshScope public class ConfigController { @Value("${useLocalCache:false}") private boolean useLocalCache; /** * http://localhost:8080/config/get */ @RequestMapping("/get") public boolean get() { return useLocalCache; } }
package com.nacos; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class NacosConfigApplication { public static void main(String[] args) { SpringApplication.run(NacosConfigApplication.class, args); } }
4.啓動應用,nacos配置中心配置參數,訪問/config/get 結果爲true ,結果配置成功bootstrap
#配置中心地址 spring.cloud.nacos.config.server-addr=10.136.15.122:8848 #應用名稱,非必須,若是沒有配置prefix,默認以name爲前綴 #spring.application.name=example #data-id前綴 spring.cloud.nacos.config.prefix=example #文件類型,支持properties和yml兩種數據格式 spring.cloud.nacos.config.file-extension=properties #環境,能夠隔離不一樣配置環境之間的配置,如dev,uat,pro spring.profiles.active=dev #命名空間 也是起隔離做用的,隔離不一樣應用這之間的做用 spring.cloud.nacos.config.namespace=c04b0cdf-91c7-470a-b6a9-423da6cc7a2b #分組起隔離同一命名空間下,不一樣的分組 spring.cloud.nacos.config.group=test #加載額外配置,除加載上面主配置文件外,額外加載的公有配置功能 spring.cloud.nacos.config.ext-config[0].data-id=test.properties spring.cloud.nacos.config.ext-config[0].refresh=true
NacosPropertySourceLocator類是Nacos Config的核心執行類,實現了PropertySourceLocator接口,在SpringCloud項目啓動中就會加載執行的類,具體以下app
PropertySourceBootstrapConfiguration 是SpringCloud下的配置類實現了ApplicationContextInitializer接口,執行init方法,具本緣由能夠查詢ApplicationContextInitializer相關接口的文檔說明,代碼以下maven
public void initialize(ConfigurableApplicationContext applicationContext) { CompositePropertySource composite = new CompositePropertySource( BOOTSTRAP_PROPERTY_SOURCE_NAME); AnnotationAwareOrderComparator.sort(this.propertySourceLocators); boolean empty = true; ConfigurableEnvironment environment = applicationContext.getEnvironment(); for (PropertySourceLocator locator : this.propertySourceLocators) { PropertySource<?> source = null; source = locator.locate(environment); if (source == null) { continue; } logger.info("Located property source: " + source); composite.addPropertySource(source); empty = false; } if (!empty) { MutablePropertySources propertySources = environment.getPropertySources(); String logConfig = environment.resolvePlaceholders("${logging.config:}"); LogFile logFile = LogFile.get(environment); if (propertySources.contains(BOOTSTRAP_PROPERTY_SOURCE_NAME)) { propertySources.remove(BOOTSTRAP_PROPERTY_SOURCE_NAME); } insertPropertySources(propertySources, composite); reinitializeLoggingSystem(environment, logConfig, logFile); setLogLevels(applicationContext, environment); handleIncludedProfiles(environment); } }
發現最終執行的locate方法,咱們查看下NacosPropertySourceLocator的locate的實現以下ui
public PropertySource<?> locate(Environment env) { ConfigService configService = nacosConfigProperties.configServiceInstance(); if (null == configService) { LOGGER.warn( "no instance of config service found, can't load config from nacos"); return null; } long timeout = nacosConfigProperties.getTimeout(); nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService, timeout); String name = nacosConfigProperties.getName(); /*配置的分組*/ String nacosGroup = nacosConfigProperties.getGroup(); /*配置的前綴*/ String dataIdPrefix = nacosConfigProperties.getPrefix(); if (StringUtils.isEmpty(dataIdPrefix)) { dataIdPrefix = name; } /*前綴沒有,則取spring.application.name*/ if (StringUtils.isEmpty(dataIdPrefix)) { dataIdPrefix = env.getProperty("spring.application.name"); } List<String> profiles = Arrays.asList(env.getActiveProfiles()); nacosConfigProperties.setActiveProfiles(profiles.toArray(new String[0])); String fileExtension = nacosConfigProperties.getFileExtension(); CompositePropertySource composite = new CompositePropertySource( NACOS_PROPERTY_SOURCE_NAME); loadSharedConfiguration(composite); loadExtConfiguration(composite); /*加載配置方法*/ loadApplicationConfiguration(composite, nacosGroup, dataIdPrefix, fileExtension); return composite; }
經過這個方法,大概能明白咱們上面的一些配置的做用,下面再看下loadApplicationConfiguration 加載配置的方法,loadSharedConfiguration,loadExtConfiguration是加載擴展配置的方式,這裏就詳細說明了this
private void loadApplicationConfiguration( CompositePropertySource compositePropertySource, String nacosGroup, String dataIdPrefix, String fileExtension) { loadNacosDataIfPresent(compositePropertySource, dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension, true); for (String profile : nacosConfigProperties.getActiveProfiles()) { String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension; loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup, fileExtension, true); } }
這裏能夠看到,會優先加載dateIdPrefix+DOT+fileExtension的配置,然後在加載環境的配置從而覆蓋以前的配置,這就是上面配置dev的做用spa
private void loadNacosDataIfPresent(final CompositePropertySource composite, final String dataId, final String group, String fileExtension, boolean isRefreshable) { if (NacosContextRefresher.loadCount.get() != 0) { NacosPropertySource ps; if (!isRefreshable) { ps = NacosPropertySourceRepository.getNacosPropertySource(dataId); } else { ps = nacosPropertySourceBuilder.build(dataId, group, fileExtension, true); } composite.addFirstPropertySource(ps); } else { NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group, fileExtension, isRefreshable); composite.addFirstPropertySource(ps); } }
再往下就是拉取配置中心的數據的過程,就不看下去了3d