首先参考博客
搭建SpringCloudAlibaba小项目: https://blog.csdn.net/m0_37607945/article/details/107705904
以下采用上面博客中的 spring-cloud-alibaba-consumer 项目进行讲解

在项目中引入依赖
<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
nacos控制台配置

在控制台中选择 配置管理->配置列表->+
在这里插入图片描述
添加如下的配置信息
在这里插入图片描述

控制台里面数据不能有空格 比如user.age=25写成user.age = 25导致读取失败

添加配置(必须在bootstrap.properties中)
# DataId 默认使用 `spring.application.name` 配置跟文件扩展名结合(配置格式默认使用 properties), GROUP 不配置默认使用 DEFAULT_GROUP。因此该配置文件对应的 Nacos Config 配置的 DataId 为 nacos-config.properties, GROUP 为 DEFAULT_GROUP
spring.application.name=spring-cloud-alibaba-nacos-consumer
spring.cloud.nacos.config.server-addr=192.168.56.1:8848

修改启动配置类main方法

public static void main(String[] args) {
    ConfigurableApplicationContext applicationContext = SpringApplication.run(AlibabaConsumerApplication.class, args);
    String userName = applicationContext.getEnvironment().getProperty("user.name");
    String userAge = applicationContext.getEnvironment().getProperty("user.age");
    System.err.println("user name :"+userName+"; age: "+userAge);

然后启动项目:
在这里插入图片描述
由此可见,从当前项目中可以读取到nacos控制台中添加的配置信息。

"C:\Program Files\Java\jdk1.8.0_121\bin\java.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.1\lib\idea_rt.jar=56491:D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_121\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\rt.jar;D:\springcloud\spring-cloud-alibaba-consumer\target\classes;D:\maven\repo\org\springframework\boot\spring-boot-starter-web\2.1.8.RELEASE\spring-boot-starter-web-2.1.8.RELEASE.jar;D:\maven\repo\org\springframework\boot\spring-boot-starter\2.1.8.RELEASE\spring-boot-starter-2.1.8.RELEASE.jar;D:\maven\repo\org\springframework\boot\spring-boot\2.1.8.RELEASE\spring-boot-2.1.8.RELEASE.jar;D:\maven\repo\org\springframework\boot\spring-boot-autoconfigure\2.1.8.RELEASE\spring-boot-autoconfigure-2.1.8.RELEASE.jar;D:\maven\repo\org\springframework\boot\spring-boot-starter-logging\2.1.8.RELEASE\spring-boot-starter-logging-2.1.8.RELEASE.jar;D:\maven\repo\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\maven\repo\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\maven\repo\org\apache\logging\log4j\log4j-to-slf4j\2.11.2\log4j-to-slf4j-2.11.2.jar;D:\maven\repo\org\apache\logging\log4j\log4j-api\2.11.2\log4j-api-2.11.2.jar;D:\maven\repo\org\slf4j\jul-to-slf4j\1.7.28\jul-to-slf4j-1.7.28.jar;D:\maven\repo\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;D:\maven\repo\org\springframework\spring-core\5.1.9.RELEASE\spring-core-5.1.9.RELEASE.jar;D:\maven\repo\org\springframework\spring-jcl\5.1.9.RELEASE\spring-jcl-5.1.9.RELEASE.jar;D:\maven\repo\org\yaml\snakeyaml\1.23\snakeyaml-1.23.jar;D:\maven\repo\org\springframework\boot\spring-boot-starter-json\2.1.8.RELEASE\spring-boot-starter-json-2.1.8.RELEASE.jar;D:\maven\repo\com\fasterxml\jackson\core\jackson-databind\2.9.9.3\jackson-databind-2.9.9.3.jar;D:\maven\repo\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;D:\maven\repo\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.9\jackson-datatype-jdk8-2.9.9.jar;D:\maven\repo\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.9\jackson-datatype-jsr310-2.9.9.jar;D:\maven\repo\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.9\jackson-module-parameter-names-2.9.9.jar;D:\maven\repo\org\springframework\boot\spring-boot-starter-tomcat\2.1.8.RELEASE\spring-boot-starter-tomcat-2.1.8.RELEASE.jar;D:\maven\repo\org\apache\tomcat\embed\tomcat-embed-core\9.0.24\tomcat-embed-core-9.0.24.jar;D:\maven\repo\org\apache\tomcat\embed\tomcat-embed-el\9.0.24\tomcat-embed-el-9.0.24.jar;D:\maven\repo\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.24\tomcat-embed-websocket-9.0.24.jar;D:\maven\repo\org\hibernate\validator\hibernate-validator\6.0.17.Final\hibernate-validator-6.0.17.Final.jar;D:\maven\repo\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;D:\maven\repo\org\jboss\logging\jboss-logging\3.3.3.Final\jboss-logging-3.3.3.Final.jar;D:\maven\repo\com\fasterxml\classmate\1.4.0\classmate-1.4.0.jar;D:\maven\repo\org\springframework\spring-web\5.1.9.RELEASE\spring-web-5.1.9.RELEASE.jar;D:\maven\repo\org\springframework\spring-beans\5.1.9.RELEASE\spring-beans-5.1.9.RELEASE.jar;D:\maven\repo\org\springframework\spring-webmvc\5.1.9.RELEASE\spring-webmvc-5.1.9.RELEASE.jar;D:\maven\repo\org\springframework\spring-aop\5.1.9.RELEASE\spring-aop-5.1.9.RELEASE.jar;D:\maven\repo\org\springframework\spring-context\5.1.9.RELEASE\spring-context-5.1.9.RELEASE.jar;D:\maven\repo\org\springframework\spring-expression\5.1.9.RELEASE\spring-expression-5.1.9.RELEASE.jar;D:\maven\repo\org\springframework\boot\spring-boot-starter-actuator\2.1.8.RELEASE\spring-boot-starter-actuator-2.1.8.RELEASE.jar;D:\maven\repo\org\springframework\boot\spring-boot-actuator-autoconfigure\2.1.8.RELEASE\spring-boot-actuator-autoconfigure-2.1.8.RELEASE.jar;D:\maven\repo\org\springframework\boot\spring-boot-actuator\2.1.8.RELEASE\spring-boot-actuator-2.1.8.RELEASE.jar;D:\maven\repo\io\micrometer\micrometer-core\1.1.6\micrometer-core-1.1.6.jar;D:\maven\repo\org\hdrhistogram\HdrHistogram\2.1.9\HdrHistogram-2.1.9.jar;D:\maven\repo\org\latencyutils\LatencyUtils\2.0.3\LatencyUtils-2.0.3.jar;D:\maven\repo\com\alibaba\cloud\spring-cloud-starter-alibaba-nacos-discovery\2.1.2.RELEASE\spring-cloud-starter-alibaba-nacos-discovery-2.1.2.RELEASE.jar;D:\springcloud\nacos-1.2.1\client\target\classes;D:\springcloud\nacos-1.2.1\common\target\classes;D:\maven\repo\org\slf4j\slf4j-api\1.7.28\slf4j-api-1.7.28.jar;D:\maven\repo\commons-io\commons-io\2.2\commons-io-2.2.jar;D:\maven\repo\org\apache\commons\commons-lang3\3.8.1\commons-lang3-3.8.1.jar;D:\springcloud\nacos-1.2.1\api\target\classes;D:\maven\repo\com\alibaba\fastjson\1.2.58\fastjson-1.2.58.jar;D:\maven\repo\com\google\guava\guava\22.0\guava-22.0.jar;D:\maven\repo\com\google\code\findbugs\jsr305\1.3.9\jsr305-1.3.9.jar;D:\maven\repo\com\google\errorprone\error_prone_annotations\2.0.18\error_prone_annotations-2.0.18.jar;D:\maven\repo\com\google\j2objc\j2objc-annotations\1.1\j2objc-annotations-1.1.jar;D:\maven\repo\org\codehaus\mojo\animal-sniffer-annotations\1.14\animal-sniffer-annotations-1.14.jar;D:\maven\repo\commons-codec\commons-codec\1.11\commons-codec-1.11.jar;D:\maven\repo\com\fasterxml\jackson\core\jackson-core\2.9.9\jackson-core-2.9.9.jar;D:\maven\repo\io\prometheus\simpleclient\0.5.0\simpleclient-0.5.0.jar;D:\maven\repo\com\alibaba\spring\spring-context-support\1.0.6\spring-context-support-1.0.6.jar;D:\maven\repo\org\springframework\cloud\spring-cloud-commons\2.1.5.RELEASE\spring-cloud-commons-2.1.5.RELEASE.jar;D:\maven\repo\org\springframework\security\spring-security-crypto\5.1.6.RELEASE\spring-security-crypto-5.1.6.RELEASE.jar;D:\maven\repo\org\springframework\cloud\spring-cloud-context\2.1.5.RELEASE\spring-cloud-context-2.1.5.RELEASE.jar;D:\maven\repo\org\springframework\cloud\spring-cloud-starter-netflix-ribbon\2.1.5.RELEASE\spring-cloud-starter-netflix-ribbon-2.1.5.RELEASE.jar;D:\maven\repo\org\springframework\cloud\spring-cloud-starter\2.1.5.RELEASE\spring-cloud-starter-2.1.5.RELEASE.jar;D:\maven\repo\org\springframework\security\spring-security-rsa\1.0.9.RELEASE\spring-security-rsa-1.0.9.RELEASE.jar;D:\maven\repo\org\bouncycastle\bcpkix-jdk15on\1.64\bcpkix-jdk15on-1.64.jar;D:\maven\repo\org\bouncycastle\bcprov-jdk15on\1.64\bcprov-jdk15on-1.64.jar;D:\maven\repo\org\springframework\cloud\spring-cloud-netflix-ribbon\2.1.5.RELEASE\spring-cloud-netflix-ribbon-2.1.5.RELEASE.jar;D:\maven\repo\org\springframework\cloud\spring-cloud-netflix-archaius\2.1.5.RELEASE\spring-cloud-netflix-archaius-2.1.5.RELEASE.jar;D:\maven\repo\org\springframework\cloud\spring-cloud-starter-netflix-archaius\2.1.5.RELEASE\spring-cloud-starter-netflix-archaius-2.1.5.RELEASE.jar;D:\maven\repo\com\netflix\archaius\archaius-core\0.7.6\archaius-core-0.7.6.jar;D:\maven\repo\commons-configuration\commons-configuration\1.8\commons-configuration-1.8.jar;D:\maven\repo\com\netflix\ribbon\ribbon\2.3.0\ribbon-2.3.0.jar;D:\maven\repo\com\netflix\ribbon\ribbon-transport\2.3.0\ribbon-transport-2.3.0.jar;D:\maven\repo\io\reactivex\rxnetty-contexts\0.4.9\rxnetty-contexts-0.4.9.jar;D:\maven\repo\io\reactivex\rxnetty-servo\0.4.9\rxnetty-servo-0.4.9.jar;D:\maven\repo\com\netflix\hystrix\hystrix-core\1.5.18\hystrix-core-1.5.18.jar;D:\maven\repo\javax\inject\javax.inject\1\javax.inject-1.jar;D:\maven\repo\io\reactivex\rxnetty\0.4.9\rxnetty-0.4.9.jar;D:\maven\repo\com\netflix\ribbon\ribbon-core\2.3.0\ribbon-core-2.3.0.jar;D:\maven\repo\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;D:\maven\repo\com\netflix\ribbon\ribbon-httpclient\2.3.0\ribbon-httpclient-2.3.0.jar;D:\maven\repo\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar;D:\maven\repo\org\apache\httpcomponents\httpclient\4.5.9\httpclient-4.5.9.jar;D:\maven\repo\org\apache\httpcomponents\httpcore\4.4.12\httpcore-4.4.12.jar;D:\maven\repo\com\sun\jersey\jersey-client\1.19.1\jersey-client-1.19.1.jar;D:\maven\repo\com\sun\jersey\jersey-core\1.19.1\jersey-core-1.19.1.jar;D:\maven\repo\javax\ws\rs\jsr311-api\1.1.1\jsr311-api-1.1.1.jar;D:\maven\repo\com\sun\jersey\contribs\jersey-apache-client4\1.19.1\jersey-apache-client4-1.19.1.jar;D:\maven\repo\com\netflix\servo\servo-core\0.12.21\servo-core-0.12.21.jar;D:\maven\repo\com\netflix\netflix-commons\netflix-commons-util\0.3.0\netflix-commons-util-0.3.0.jar;D:\maven\repo\com\netflix\ribbon\ribbon-loadbalancer\2.3.0\ribbon-loadbalancer-2.3.0.jar;D:\maven\repo\com\netflix\netflix-commons\netflix-statistics\0.1.1\netflix-statistics-0.1.1.jar;D:\maven\repo\io\reactivex\rxjava\1.3.8\rxjava-1.3.8.jar;D:\maven\repo\org\junit\jupiter\junit-jupiter\5.5.2\junit-jupiter-5.5.2.jar;D:\maven\repo\org\junit\jupiter\junit-jupiter-api\5.3.2\junit-jupiter-api-5.3.2.jar;D:\maven\repo\org\apiguardian\apiguardian-api\1.0.0\apiguardian-api-1.0.0.jar;D:\maven\repo\org\opentest4j\opentest4j\1.1.1\opentest4j-1.1.1.jar;D:\maven\repo\org\junit\platform\junit-platform-commons\1.3.2\junit-platform-commons-1.3.2.jar;D:\maven\repo\org\junit\jupiter\junit-jupiter-params\5.3.2\junit-jupiter-params-5.3.2.jar;D:\maven\repo\org\junit\jupiter\junit-jupiter-engine\5.3.2\junit-jupiter-engine-5.3.2.jar;D:\maven\repo\org\junit\platform\junit-platform-engine\1.3.2\junit-platform-engine-1.3.2.jar;D:\maven\repo\org\mockito\mockito-junit-jupiter\2.23.4\mockito-junit-jupiter-2.23.4.jar;D:\maven\repo\org\mockito\mockito-core\2.23.4\mockito-core-2.23.4.jar;D:\maven\repo\net\bytebuddy\byte-buddy\1.9.16\byte-buddy-1.9.16.jar;D:\maven\repo\net\bytebuddy\byte-buddy-agent\1.9.16\byte-buddy-agent-1.9.16.jar;D:\maven\repo\org\objenesis\objenesis\2.6\objenesis-2.6.jar;D:\maven\repo\com\alibaba\cloud\spring-cloud-starter-alibaba-nacos-config\2.1.2.RELEASE\spring-cloud-starter-alibaba-nacos-config-2.1.2.RELEASE.jar" com.alibaba.nacos.example.AlibabaConsumerApplication
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.8.RELEASE)
2020-08-03 10:08:33.009  INFO 7320 --- [           main] c.a.n.c.c.impl.LocalConfigInfoProcessor  : LOCAL_SNAPSHOT_PATH:C:\Users\Administrator\nacos\config
2020-08-03 10:08:33.043  INFO 7320 --- [           main] c.a.nacos.client.config.impl.Limiter     : limitTime:5.0
2020-08-03 10:08:33.130  WARN 7320 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : Ignore the empty nacos configuration and get it based on dataId[spring-cloud-alibaba-nacos-consumer] & group[DEFAULT_GROUP]
2020-08-03 10:08:33.174  INFO 7320 --- [           main] c.a.nacos.client.config.utils.JVMUtil    : isMultiInstance:false
2020-08-03 10:08:33.180  INFO 7320 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-spring-cloud-alibaba-nacos-consumer.properties,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-spring-cloud-alibaba-nacos-consumer,DEFAULT_GROUP'}]
2020-08-03 10:08:33.184  INFO 7320 --- [           main] c.a.n.e.AlibabaConsumerApplication       : No active profile set, falling back to default profiles: default
2020-08-03 10:08:33.790  WARN 7320 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'nacos-config' contains invalid characters, please migrate to a valid format.
2020-08-03 10:08:33.795  WARN 7320 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'nacos-discovery' contains invalid characters, please migrate to a valid format.
2020-08-03 10:08:34.026  WARN 7320 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'service-registry' contains invalid characters, please migrate to a valid format.
2020-08-03 10:08:34.193  INFO 7320 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=f42d18b7-4399-3a81-98f8-430b8098994d
2020-08-03 10:08:34.525  INFO 7320 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 7083 (http)
2020-08-03 10:08:34.558  INFO 7320 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-08-03 10:08:34.559  INFO 7320 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.24]
2020-08-03 10:08:34.717  INFO 7320 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-08-03 10:08:34.718  INFO 7320 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1519 ms
2020-08-03 10:08:35.586  INFO 7320 --- [           main] com.alibaba.nacos.client.naming          : initializer namespace from System Property :null
2020-08-03 10:08:35.587  INFO 7320 --- [           main] com.alibaba.nacos.client.naming          : initializer namespace from System Environment :null
2020-08-03 10:08:35.588  INFO 7320 --- [           main] com.alibaba.nacos.client.naming          : initializer namespace from System Property :null
2020-08-03 10:08:35.962  WARN 7320 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2020-08-03 10:08:35.962  INFO 7320 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2020-08-03 10:08:35.967  WARN 7320 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2020-08-03 10:08:35.968  INFO 7320 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2020-08-03 10:08:36.143  INFO 7320 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-03 10:08:36.273  INFO 7320 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'Nacso-Watch-Task-Scheduler'
2020-08-03 10:08:37.192  INFO 7320 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 21 endpoint(s) beneath base path '/actuator'
2020-08-03 10:08:37.331  INFO 7320 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 7083 (http) with context path ''
2020-08-03 10:08:37.336  INFO 7320 --- [           main] com.alibaba.nacos.client.naming          : [BEAT] adding beat: {"cluster":"cosumer-group","ip":"192.168.56.1","metadata":{"preserved.register.source":"SPRING_CLOUD","consumerkey2":"consumervalue2","consumerkey1":"consumervalue1"},"period":5000,"port":7083,"scheduled":false,"serviceName":"DEFAULT_GROUP@@spring-cloud-alibaba-nacos-consumer","stopped":false,"weight":20.0} to beat map.
2020-08-03 10:08:37.435  INFO 7320 --- [           main] com.alibaba.nacos.client.naming          : [REGISTER-SERVICE] public registering service DEFAULT_GROUP@@spring-cloud-alibaba-nacos-consumer with instance: {"clusterName":"cosumer-group","enabled":true,"ephemeral":true,"healthy":true,"instanceHeartBeatInterval":5000,"instanceHeartBeatTimeOut":15000,"ip":"192.168.56.1","ipDeleteTimeout":30000,"metadata":{"preserved.register.source":"SPRING_CLOUD","consumerkey2":"consumervalue2","consumerkey1":"consumervalue1"},"port":7083,"weight":20.0}
2020-08-03 10:08:37.445  INFO 7320 --- [           main] c.a.c.n.registry.NacosServiceRegistry    : nacos registry, DEFAULT_GROUP spring-cloud-alibaba-nacos-consumer 192.168.56.1:7083 register finished
2020-08-03 10:08:38.008  INFO 7320 --- [           main] c.a.n.e.AlibabaConsumerApplication       : Started AlibabaConsumerApplication in 8.098 seconds (JVM running for 9.567)
2020-08-03 10:08:38.022  INFO 7320 --- [           main] c.a.n.client.config.impl.ClientWorker    : [fixed-192.168.56.1_8848] [subscribe] spring-cloud-alibaba-nacos-consumer.properties+DEFAULT_GROUP
2020-08-03 10:08:38.024  INFO 7320 --- [           main] c.a.nacos.client.config.impl.CacheData   : [fixed-192.168.56.1_8848] [add-listener] ok, tenant=, dataId=spring-cloud-alibaba-nacos-consumer.properties, group=DEFAULT_GROUP, cnt=1
2020-08-03 10:08:38.024  INFO 7320 --- [           main] c.a.n.client.config.impl.ClientWorker    : [fixed-192.168.56.1_8848] [subscribe] spring-cloud-alibaba-nacos-consumer+DEFAULT_GROUP
2020-08-03 10:08:38.024  INFO 7320 --- [           main] c.a.nacos.client.config.impl.CacheData   : [fixed-192.168.56.1_8848] [add-listener] ok, tenant=, dataId=spring-cloud-alibaba-nacos-consumer, group=DEFAULT_GROUP, cnt=1
user name :spring-cloud-alibaba-nacos-consumer; age: 18
2020-08-03 10:08:38.079  INFO 7320 --- [2.168.56.1_8848] c.a.n.client.config.impl.ClientWorker    : get changedGroupKeys:[]
2020-08-03 10:08:39.017  INFO 7320 --- [-169.254.10.142] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-08-03 10:08:39.017  INFO 7320 --- [-169.254.10.142] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-08-03 10:08:39.042  INFO 7320 --- [-169.254.10.142] o.s.web.servlet.DispatcherServlet        : Completed initialization in 25 ms
Endpoint访问

Nacos Config 内部提供了一个 Endpoint, 对应的 endpoint id 为 nacos-config。

Endpoint 暴露的 json 中包含了三种属性:

  1. Sources: 当前应用配置的数据信息
  2. RefreshHistory: 配置刷新的历史记录
  3. NacosConfigProperties: 当前应用 Nacos 的基础配置信息

访问地址: http://localhost:7083/actuator/nacos-config
在这里插入图片描述

"NacosConfigProperties": { "serverAddr": "192.168.56.1:8848", "username": "", "password": "", "encode": null, "group": "DEFAULT_GROUP", "prefix": null, "fileExtension": "properties", "timeout": 3000, "maxRetry": null, "configLongPollTimeout": null, "configRetryTime": null, "enableRemoteSyncConfig": false, "endpoint": null, "namespace": null, "accessKey": null, "secretKey": null, "contextPath": null, "clusterName": null, "name": null, "sharedConfigs": null, "extensionConfigs": null, "refreshEnabled": true, "sharedDataids": null, "extConfig": null, "refreshableDataids": null, "configServiceProperties": { "secretKey": "", "namespace": "", "username": "", "enableRemoteSyncConfig": "false", "configLongPollTimeout": "", "configRetryTime": "", "encode": "", "serverAddr": "192.168.56.1:8848", "maxRetry": "", "clusterName": "", "password": "", "accessKey": "", "endpoint": "" "RefreshHistory": [ "timestamp": "2020-08-03 12:35:30", "dataId": "spring-cloud-alibaba-nacos-consumer.properties", "group": "DEFAULT_GROUP", "md5": "96683d34e786b438b008cfd576263fd7" "Sources": [ "lastSynced": "2020-08-03 12:32:28", "dataId": "spring-cloud-alibaba-nacos-consumer.properties" "lastSynced": "2020-08-03 12:32:28", "dataId": "spring-cloud-alibaba-nacos-consumer"
使用其他格式
  1. 在nacos控制台新增配置
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
  2. 修改bootstrap.properties配置文件
# DataId 默认使用 `spring.application.name` 配置跟文件扩展名结合(配置格式默认使用 properties), GROUP 不配置默认使用 DEFAULT_GROUP。因此该配置文件对应的 Nacos Config 配置的 DataId 为 nacos-config.properties, GROUP 为 DEFAULT_GROUP
spring.application.name=spring-cloud-alibaba-nacos-consumer
spring.cloud.nacos.config.server-addr=192.168.56.1:8848
# 默认为properties 如果采用其他格式 必须进行配置
spring.cloud.nacos.config.file-extension=yaml
  1. 重新启动项目
    在这里插入图片描述
支持配置的自动更新

修改main方法如下:

public static void main(String[] args) throws InterruptedException {
    ConfigurableApplicationContext applicationContext = SpringApplication.run(AlibabaConsumerApplication.class, args);
    while(true) {
        //当动态配置刷新时,会更新到 Enviroment中,因此这里每隔一秒中从Enviroment中获取配置
        String userName = applicationContext.getEnvironment().getProperty("user.name");
        String userAge = applicationContext.getEnvironment().getProperty("user.age");
        System.err.println("user name :" + userName + "; age: " + userAge);
        TimeUnit.SECONDS.sleep(1);

从consul控制台修改配置user.age为20
在这里插入图片描述
这是可以从控制台看出属性已经动态刷新了
在这里插入图片描述

user name :spring-cloud-alibaba-nacos-consumer.yaml; age: 19
2020-08-03 10:45:15.145  INFO 11824 --- [2.168.56.1_8848] c.a.n.client.config.impl.ClientWorker    : [fixed-192.168.56.1_8848] [polling-resp] config changed. dataId=spring-cloud-alibaba-nacos-consumer.yaml, group=DEFAULT_GROUP
2020-08-03 10:45:15.145  INFO 11824 --- [2.168.56.1_8848] c.a.n.client.config.impl.ClientWorker    : get changedGroupKeys:[spring-cloud-alibaba-nacos-consumer.yaml+DEFAULT_GROUP]
2020-08-03 10:45:15.157  INFO 11824 --- [2.168.56.1_8848] c.a.n.client.config.impl.ClientWorker    : [fixed-192.168.56.1_8848] [data-received] dataId=spring-cloud-alibaba-nacos-consumer.yaml, group=DEFAULT_GROUP, tenant=null, md5=2686fcfa497a8fd06411571b3a7ce096, content=user.name: spring-cloud-alibaba-nacos-consumer.yaml   
user.age: 20, type=yaml
2020-08-03 10:45:15.158  INFO 11824 --- [2.168.56.1_8848] c.a.nacos.client.config.impl.CacheData   : [fixed-192.168.56.1_8848] [notify-context] dataId=spring-cloud-alibaba-nacos-consumer.yaml, group=DEFAULT_GROUP, md5=2686fcfa497a8fd06411571b3a7ce096
user name :spring-cloud-alibaba-nacos-consumer.yaml; age: 19
user name :spring-cloud-alibaba-nacos-consumer.yaml; age: 19
2020-08-03 10:45:17.146  WARN 11824 --- [2.168.56.1_8848] c.a.c.n.c.NacosPropertySourceBuilder     : Ignore the empty nacos configuration and get it based on dataId[spring-cloud-alibaba-nacos-consumer] & group[DEFAULT_GROUP]
2020-08-03 10:45:17.153  INFO 11824 --- [2.168.56.1_8848] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-spring-cloud-alibaba-nacos-consumer.yaml,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-spring-cloud-alibaba-nacos-consumer,DEFAULT_GROUP'}]
2020-08-03 10:45:17.155  INFO 11824 --- [2.168.56.1_8848] o.s.boot.SpringApplication               : No active profile set, falling back to default profiles: default
2020-08-03 10:45:17.173  INFO 11824 --- [2.168.56.1_8848] o.s.boot.SpringApplication               : Started application in 2.009 seconds (JVM running for 41.089)
2020-08-03 10:45:17.303  INFO 11824 --- [2.168.56.1_8848] o.s.c.e.event.RefreshEventListener       : Refresh keys changed: [user.age]
2020-08-03 10:45:17.304  INFO 11824 --- [2.168.56.1_8848] c.a.nacos.client.config.impl.CacheData   : [fixed-192.168.56.1_8848] [notify-ok] dataId=spring-cloud-alibaba-nacos-consumer.yaml, group=DEFAULT_GROUP, md5=2686fcfa497a8fd06411571b3a7ce096, listener=com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1@68df8db1 
2020-08-03 10:45:17.304  INFO 11824 --- [2.168.56.1_8848] c.a.nacos.client.config.impl.CacheData   : [fixed-192.168.56.1_8848] [notify-listener] time cost=2146ms in ClientWorker, dataId=spring-cloud-alibaba-nacos-consumer.yaml, group=DEFAULT_GROUP, md5=2686fcfa497a8fd06411571b3a7ce096, listener=com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1@68df8db1 
user name :spring-cloud-alibaba-nacos-consumer.yaml; age: 20
user name :spring-cloud-alibaba-nacos-consumer.yaml; age: 20
user name :spring-cloud-alibaba-nacos-consumer.yaml; age: 20

关闭动态刷新功能(默认开启,不建议关闭)

# 关闭刷新功能
spring.cloud.nacos.config.refresh.enabled=false 
支持更细粒度的配置(profile)

首先修改项目中的默认激活版本(bootstrap.properties

spring.profiles.active=develop
 

${spring.profiles.active} 当通过配置文件来指定时必须放在 bootstrap.properties 文件中。

此时的DataId的格式为 ${spring.application.name}-${profile}.${file-extension:properties}
克隆一份配置为spring-cloud-alibaba-nacos-consumer-develop.yaml,然后再进行编辑
在这里插入图片描述
在这里插入图片描述

user.name: spring-cloud-alibaba-nacos-consumer-develop.yaml   
user.age: 23
current.env: develop-env

修改启动类的main方法如下

public static void main(String[] args) throws InterruptedException {
    ConfigurableApplicationContext applicationContext = SpringApplication.run(AlibabaConsumerApplication.class, args);
    while(true) {
        //当动态配置刷新时,会更新到 Enviroment中,因此这里每隔一秒中从Enviroment中获取配置
        String userName = applicationContext.getEnvironment().getProperty("user.name");
        String userAge = applicationContext.getEnvironment().getProperty("user.age");
        //获取当前部署的环境
        String currentEnv = applicationContext.getEnvironment().getProperty("current.env");
        System.err.println("in "+currentEnv+" enviroment; "+"user name :" + userName + "; age: " + userAge);
        TimeUnit.SECONDS.sleep(10);
"C:\Program Files\Java\jdk1.8.0_121\bin\java.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.1\lib\idea_rt.jar=60443:D:\Program Files\JetBrains\IntelliJ IDEA 2019.3.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_121\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_121\jre\lib\rt.jar;D:\springcloud\spring-cloud-alibaba-consumer\target\classes;D:\maven\repo\org\springframework\boot\spring-boot-starter-web\2.1.8.RELEASE\spring-boot-starter-web-2.1.8.RELEASE.jar;D:\maven\repo\org\springframework\boot\spring-boot-starter\2.1.8.RELEASE\spring-boot-starter-2.1.8.RELEASE.jar;D:\maven\repo\org\springframework\boot\spring-boot\2.1.8.RELEASE\spring-boot-2.1.8.RELEASE.jar;D:\maven\repo\org\springframework\boot\spring-boot-autoconfigure\2.1.8.RELEASE\spring-boot-autoconfigure-2.1.8.RELEASE.jar;D:\maven\repo\org\springframework\boot\spring-boot-starter-logging\2.1.8.RELEASE\spring-boot-starter-logging-2.1.8.RELEASE.jar;D:\maven\repo\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\maven\repo\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\maven\repo\org\apache\logging\log4j\log4j-to-slf4j\2.11.2\log4j-to-slf4j-2.11.2.jar;D:\maven\repo\org\apache\logging\log4j\log4j-api\2.11.2\log4j-api-2.11.2.jar;D:\maven\repo\org\slf4j\jul-to-slf4j\1.7.28\jul-to-slf4j-1.7.28.jar;D:\maven\repo\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;D:\maven\repo\org\springframework\spring-core\5.1.9.RELEASE\spring-core-5.1.9.RELEASE.jar;D:\maven\repo\org\springframework\spring-jcl\5.1.9.RELEASE\spring-jcl-5.1.9.RELEASE.jar;D:\maven\repo\org\yaml\snakeyaml\1.23\snakeyaml-1.23.jar;D:\maven\repo\org\springframework\boot\spring-boot-starter-json\2.1.8.RELEASE\spring-boot-starter-json-2.1.8.RELEASE.jar;D:\maven\repo\com\fasterxml\jackson\core\jackson-databind\2.9.9.3\jackson-databind-2.9.9.3.jar;D:\maven\repo\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;D:\maven\repo\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.9\jackson-datatype-jdk8-2.9.9.jar;D:\maven\repo\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.9\jackson-datatype-jsr310-2.9.9.jar;D:\maven\repo\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.9\jackson-module-parameter-names-2.9.9.jar;D:\maven\repo\org\springframework\boot\spring-boot-starter-tomcat\2.1.8.RELEASE\spring-boot-starter-tomcat-2.1.8.RELEASE.jar;D:\maven\repo\org\apache\tomcat\embed\tomcat-embed-core\9.0.24\tomcat-embed-core-9.0.24.jar;D:\maven\repo\org\apache\tomcat\embed\tomcat-embed-el\9.0.24\tomcat-embed-el-9.0.24.jar;D:\maven\repo\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.24\tomcat-embed-websocket-9.0.24.jar;D:\maven\repo\org\hibernate\validator\hibernate-validator\6.0.17.Final\hibernate-validator-6.0.17.Final.jar;D:\maven\repo\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;D:\maven\repo\org\jboss\logging\jboss-logging\3.3.3.Final\jboss-logging-3.3.3.Final.jar;D:\maven\repo\com\fasterxml\classmate\1.4.0\classmate-1.4.0.jar;D:\maven\repo\org\springframework\spring-web\5.1.9.RELEASE\spring-web-5.1.9.RELEASE.jar;D:\maven\repo\org\springframework\spring-beans\5.1.9.RELEASE\spring-beans-5.1.9.RELEASE.jar;D:\maven\repo\org\springframework\spring-webmvc\5.1.9.RELEASE\spring-webmvc-5.1.9.RELEASE.jar;D:\maven\repo\org\springframework\spring-aop\5.1.9.RELEASE\spring-aop-5.1.9.RELEASE.jar;D:\maven\repo\org\springframework\spring-context\5.1.9.RELEASE\spring-context-5.1.9.RELEASE.jar;D:\maven\repo\org\springframework\spring-expression\5.1.9.RELEASE\spring-expression-5.1.9.RELEASE.jar;D:\maven\repo\org\springframework\boot\spring-boot-starter-actuator\2.1.8.RELEASE\spring-boot-starter-actuator-2.1.8.RELEASE.jar;D:\maven\repo\org\springframework\boot\spring-boot-actuator-autoconfigure\2.1.8.RELEASE\spring-boot-actuator-autoconfigure-2.1.8.RELEASE.jar;D:\maven\repo\org\springframework\boot\spring-boot-actuator\2.1.8.RELEASE\spring-boot-actuator-2.1.8.RELEASE.jar;D:\maven\repo\io\micrometer\micrometer-core\1.1.6\micrometer-core-1.1.6.jar;D:\maven\repo\org\hdrhistogram\HdrHistogram\2.1.9\HdrHistogram-2.1.9.jar;D:\maven\repo\org\latencyutils\LatencyUtils\2.0.3\LatencyUtils-2.0.3.jar;D:\maven\repo\com\alibaba\cloud\spring-cloud-starter-alibaba-nacos-discovery\2.1.2.RELEASE\spring-cloud-starter-alibaba-nacos-discovery-2.1.2.RELEASE.jar;D:\springcloud\nacos-1.2.1\client\target\classes;D:\springcloud\nacos-1.2.1\common\target\classes;D:\maven\repo\org\slf4j\slf4j-api\1.7.28\slf4j-api-1.7.28.jar;D:\maven\repo\commons-io\commons-io\2.2\commons-io-2.2.jar;D:\maven\repo\org\apache\commons\commons-lang3\3.8.1\commons-lang3-3.8.1.jar;D:\springcloud\nacos-1.2.1\api\target\classes;D:\maven\repo\com\alibaba\fastjson\1.2.58\fastjson-1.2.58.jar;D:\maven\repo\com\google\guava\guava\22.0\guava-22.0.jar;D:\maven\repo\com\google\code\findbugs\jsr305\1.3.9\jsr305-1.3.9.jar;D:\maven\repo\com\google\errorprone\error_prone_annotations\2.0.18\error_prone_annotations-2.0.18.jar;D:\maven\repo\com\google\j2objc\j2objc-annotations\1.1\j2objc-annotations-1.1.jar;D:\maven\repo\org\codehaus\mojo\animal-sniffer-annotations\1.14\animal-sniffer-annotations-1.14.jar;D:\maven\repo\commons-codec\commons-codec\1.11\commons-codec-1.11.jar;D:\maven\repo\com\fasterxml\jackson\core\jackson-core\2.9.9\jackson-core-2.9.9.jar;D:\maven\repo\io\prometheus\simpleclient\0.5.0\simpleclient-0.5.0.jar;D:\maven\repo\com\alibaba\spring\spring-context-support\1.0.6\spring-context-support-1.0.6.jar;D:\maven\repo\org\springframework\cloud\spring-cloud-commons\2.1.5.RELEASE\spring-cloud-commons-2.1.5.RELEASE.jar;D:\maven\repo\org\springframework\security\spring-security-crypto\5.1.6.RELEASE\spring-security-crypto-5.1.6.RELEASE.jar;D:\maven\repo\org\springframework\cloud\spring-cloud-context\2.1.5.RELEASE\spring-cloud-context-2.1.5.RELEASE.jar;D:\maven\repo\org\springframework\cloud\spring-cloud-starter-netflix-ribbon\2.1.5.RELEASE\spring-cloud-starter-netflix-ribbon-2.1.5.RELEASE.jar;D:\maven\repo\org\springframework\cloud\spring-cloud-starter\2.1.5.RELEASE\spring-cloud-starter-2.1.5.RELEASE.jar;D:\maven\repo\org\springframework\security\spring-security-rsa\1.0.9.RELEASE\spring-security-rsa-1.0.9.RELEASE.jar;D:\maven\repo\org\bouncycastle\bcpkix-jdk15on\1.64\bcpkix-jdk15on-1.64.jar;D:\maven\repo\org\bouncycastle\bcprov-jdk15on\1.64\bcprov-jdk15on-1.64.jar;D:\maven\repo\org\springframework\cloud\spring-cloud-netflix-ribbon\2.1.5.RELEASE\spring-cloud-netflix-ribbon-2.1.5.RELEASE.jar;D:\maven\repo\org\springframework\cloud\spring-cloud-netflix-archaius\2.1.5.RELEASE\spring-cloud-netflix-archaius-2.1.5.RELEASE.jar;D:\maven\repo\org\springframework\cloud\spring-cloud-starter-netflix-archaius\2.1.5.RELEASE\spring-cloud-starter-netflix-archaius-2.1.5.RELEASE.jar;D:\maven\repo\com\netflix\archaius\archaius-core\0.7.6\archaius-core-0.7.6.jar;D:\maven\repo\commons-configuration\commons-configuration\1.8\commons-configuration-1.8.jar;D:\maven\repo\com\netflix\ribbon\ribbon\2.3.0\ribbon-2.3.0.jar;D:\maven\repo\com\netflix\ribbon\ribbon-transport\2.3.0\ribbon-transport-2.3.0.jar;D:\maven\repo\io\reactivex\rxnetty-contexts\0.4.9\rxnetty-contexts-0.4.9.jar;D:\maven\repo\io\reactivex\rxnetty-servo\0.4.9\rxnetty-servo-0.4.9.jar;D:\maven\repo\com\netflix\hystrix\hystrix-core\1.5.18\hystrix-core-1.5.18.jar;D:\maven\repo\javax\inject\javax.inject\1\javax.inject-1.jar;D:\maven\repo\io\reactivex\rxnetty\0.4.9\rxnetty-0.4.9.jar;D:\maven\repo\com\netflix\ribbon\ribbon-core\2.3.0\ribbon-core-2.3.0.jar;D:\maven\repo\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;D:\maven\repo\com\netflix\ribbon\ribbon-httpclient\2.3.0\ribbon-httpclient-2.3.0.jar;D:\maven\repo\commons-collections\commons-collections\3.2.2\commons-collections-3.2.2.jar;D:\maven\repo\org\apache\httpcomponents\httpclient\4.5.9\httpclient-4.5.9.jar;D:\maven\repo\org\apache\httpcomponents\httpcore\4.4.12\httpcore-4.4.12.jar;D:\maven\repo\com\sun\jersey\jersey-client\1.19.1\jersey-client-1.19.1.jar;D:\maven\repo\com\sun\jersey\jersey-core\1.19.1\jersey-core-1.19.1.jar;D:\maven\repo\javax\ws\rs\jsr311-api\1.1.1\jsr311-api-1.1.1.jar;D:\maven\repo\com\sun\jersey\contribs\jersey-apache-client4\1.19.1\jersey-apache-client4-1.19.1.jar;D:\maven\repo\com\netflix\servo\servo-core\0.12.21\servo-core-0.12.21.jar;D:\maven\repo\com\netflix\netflix-commons\netflix-commons-util\0.3.0\netflix-commons-util-0.3.0.jar;D:\maven\repo\com\netflix\ribbon\ribbon-loadbalancer\2.3.0\ribbon-loadbalancer-2.3.0.jar;D:\maven\repo\com\netflix\netflix-commons\netflix-statistics\0.1.1\netflix-statistics-0.1.1.jar;D:\maven\repo\io\reactivex\rxjava\1.3.8\rxjava-1.3.8.jar;D:\maven\repo\org\junit\jupiter\junit-jupiter\5.5.2\junit-jupiter-5.5.2.jar;D:\maven\repo\org\junit\jupiter\junit-jupiter-api\5.3.2\junit-jupiter-api-5.3.2.jar;D:\maven\repo\org\apiguardian\apiguardian-api\1.0.0\apiguardian-api-1.0.0.jar;D:\maven\repo\org\opentest4j\opentest4j\1.1.1\opentest4j-1.1.1.jar;D:\maven\repo\org\junit\platform\junit-platform-commons\1.3.2\junit-platform-commons-1.3.2.jar;D:\maven\repo\org\junit\jupiter\junit-jupiter-params\5.3.2\junit-jupiter-params-5.3.2.jar;D:\maven\repo\org\junit\jupiter\junit-jupiter-engine\5.3.2\junit-jupiter-engine-5.3.2.jar;D:\maven\repo\org\junit\platform\junit-platform-engine\1.3.2\junit-platform-engine-1.3.2.jar;D:\maven\repo\org\mockito\mockito-junit-jupiter\2.23.4\mockito-junit-jupiter-2.23.4.jar;D:\maven\repo\org\mockito\mockito-core\2.23.4\mockito-core-2.23.4.jar;D:\maven\repo\net\bytebuddy\byte-buddy\1.9.16\byte-buddy-1.9.16.jar;D:\maven\repo\net\bytebuddy\byte-buddy-agent\1.9.16\byte-buddy-agent-1.9.16.jar;D:\maven\repo\org\objenesis\objenesis\2.6\objenesis-2.6.jar;D:\maven\repo\com\alibaba\cloud\spring-cloud-starter-alibaba-nacos-config\2.1.2.RELEASE\spring-cloud-starter-alibaba-nacos-config-2.1.2.RELEASE.jar" com.alibaba.nacos.example.AlibabaConsumerApplication
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.8.RELEASE)
2020-08-03 11:01:22.278  INFO 10624 --- [           main] c.a.n.c.c.impl.LocalConfigInfoProcessor  : LOCAL_SNAPSHOT_PATH:C:\Users\Administrator\nacos\config
2020-08-03 11:01:22.318  INFO 10624 --- [           main] c.a.nacos.client.config.impl.Limiter     : limitTime:5.0
2020-08-03 11:01:22.355  WARN 10624 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : Ignore the empty nacos configuration and get it based on dataId[spring-cloud-alibaba-nacos-consumer] & group[DEFAULT_GROUP]
2020-08-03 11:01:22.362  INFO 10624 --- [           main] c.a.nacos.client.config.utils.JVMUtil    : isMultiInstance:false
2020-08-03 11:01:22.432  INFO 10624 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-spring-cloud-alibaba-nacos-consumer-develop.yaml,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-spring-cloud-alibaba-nacos-consumer.yaml,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-spring-cloud-alibaba-nacos-consumer,DEFAULT_GROUP'}]
2020-08-03 11:01:22.438  INFO 10624 --- [           main] c.a.n.e.AlibabaConsumerApplication       : The following profiles are active: develop
2020-08-03 11:01:23.139  WARN 10624 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'nacos-config' contains invalid characters, please migrate to a valid format.
2020-08-03 11:01:23.145  WARN 10624 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'nacos-discovery' contains invalid characters, please migrate to a valid format.
2020-08-03 11:01:23.425  WARN 10624 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'service-registry' contains invalid characters, please migrate to a valid format.
2020-08-03 11:01:23.656  INFO 10624 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=f42d18b7-4399-3a81-98f8-430b8098994d
2020-08-03 11:01:24.084  INFO 10624 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 7083 (http)
2020-08-03 11:01:24.117  INFO 10624 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-08-03 11:01:24.118  INFO 10624 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.24]
2020-08-03 11:01:24.386  INFO 10624 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-08-03 11:01:24.386  INFO 10624 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1932 ms
2020-08-03 11:01:25.517  INFO 10624 --- [           main] com.alibaba.nacos.client.naming          : initializer namespace from System Property :null
2020-08-03 11:01:25.518  INFO 10624 --- [           main] com.alibaba.nacos.client.naming          : initializer namespace from System Environment :null
2020-08-03 11:01:25.519  INFO 10624 --- [           main] com.alibaba.nacos.client.naming          : initializer namespace from System Property :null
2020-08-03 11:01:26.062  WARN 10624 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2020-08-03 11:01:26.062  INFO 10624 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2020-08-03 11:01:26.070  WARN 10624 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2020-08-03 11:01:26.070  INFO 10624 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2020-08-03 11:01:26.318  INFO 10624 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-03 11:01:26.472  INFO 10624 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'Nacso-Watch-Task-Scheduler'
2020-08-03 11:01:27.752  INFO 10624 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 21 endpoint(s) beneath base path '/actuator'
2020-08-03 11:01:27.962  INFO 10624 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 7083 (http) with context path ''
2020-08-03 11:01:27.969  INFO 10624 --- [           main] com.alibaba.nacos.client.naming          : [BEAT] adding beat: {"cluster":"cosumer-group","ip":"192.168.56.1","metadata":{"preserved.register.source":"SPRING_CLOUD","consumerkey2":"consumervalue2","consumerkey1":"consumervalue1"},"period":5000,"port":7083,"scheduled":false,"serviceName":"DEFAULT_GROUP@@spring-cloud-alibaba-nacos-consumer","stopped":false,"weight":20.0} to beat map.
2020-08-03 11:01:28.099  INFO 10624 --- [           main] com.alibaba.nacos.client.naming          : [REGISTER-SERVICE] public registering service DEFAULT_GROUP@@spring-cloud-alibaba-nacos-consumer with instance: {"clusterName":"cosumer-group","enabled":true,"ephemeral":true,"healthy":true,"instanceHeartBeatInterval":5000,"instanceHeartBeatTimeOut":15000,"ip":"192.168.56.1","ipDeleteTimeout":30000,"metadata":{"preserved.register.source":"SPRING_CLOUD","consumerkey2":"consumervalue2","consumerkey1":"consumervalue1"},"port":7083,"weight":20.0}
2020-08-03 11:01:28.113  INFO 10624 --- [           main] c.a.c.n.registry.NacosServiceRegistry    : nacos registry, DEFAULT_GROUP spring-cloud-alibaba-nacos-consumer 192.168.56.1:7083 register finished
2020-08-03 11:01:28.893  INFO 10624 --- [           main] c.a.n.e.AlibabaConsumerApplication       : Started AlibabaConsumerApplication in 10.834 seconds (JVM running for 12.734)
2020-08-03 11:01:28.914  INFO 10624 --- [           main] c.a.n.client.config.impl.ClientWorker    : [fixed-192.168.56.1_8848] [subscribe] spring-cloud-alibaba-nacos-consumer.yaml+DEFAULT_GROUP
2020-08-03 11:01:28.916  INFO 10624 --- [           main] c.a.nacos.client.config.impl.CacheData   : [fixed-192.168.56.1_8848] [add-listener] ok, tenant=, dataId=spring-cloud-alibaba-nacos-consumer.yaml, group=DEFAULT_GROUP, cnt=1
2020-08-03 11:01:28.917  INFO 10624 --- [           main] c.a.n.client.config.impl.ClientWorker    : [fixed-192.168.56.1_8848] [subscribe] spring-cloud-alibaba-nacos-consumer-develop.yaml+DEFAULT_GROUP
2020-08-03 11:01:28.917  INFO 10624 --- [           main] c.a.nacos.client.config.impl.CacheData   : [fixed-192.168.56.1_8848] [add-listener] ok, tenant=, dataId=spring-cloud-alibaba-nacos-consumer-develop.yaml, group=DEFAULT_GROUP, cnt=1
2020-08-03 11:01:28.918  INFO 10624 --- [           main] c.a.n.client.config.impl.ClientWorker    : [fixed-192.168.56.1_8848] [subscribe] spring-cloud-alibaba-nacos-consumer+DEFAULT_GROUP
2020-08-03 11:01:28.918  INFO 10624 --- [           main] c.a.nacos.client.config.impl.CacheData   : [fixed-192.168.56.1_8848] [add-listener] ok, tenant=, dataId=spring-cloud-alibaba-nacos-consumer, group=DEFAULT_GROUP, cnt=1
2020-08-03 11:01:28.930  INFO 10624 --- [2.168.56.1_8848] c.a.n.client.config.impl.ClientWorker    : get changedGroupKeys:[]
in develop-env enviroment; user name :spring-cloud-alibaba-nacos-consumer-develop.yaml; age: 23
2020-08-03 11:01:29.638  INFO 10624 --- [-169.254.10.142] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-08-03 11:01:29.638  INFO 10624 --- [-169.254.10.142] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-08-03 11:01:29.654  INFO 10624 --- [-169.254.10.142] o.s.web.servlet.DispatcherServlet        : Completed initialization in 15 ms
in develop-env enviroment; user name :spring-cloud-alibaba-nacos-consumer-develop.yaml; age: 23
in develop-env enviroment; user name :spring-cloud-alibaba-nacos-consumer-develop.yaml; age: 23
2020-08-03 11:01:58.627  INFO 10624 --- [2.168.56.1_8848] c.a.n.client.config.impl.ClientWorker    : get changedGroupKeys:[]
in develop-env enviroment; user name :spring-cloud-alibaba-nacos-consumer-develop.yaml; age: 23
自定义 namespace 的配置

首先在控制台添加一个命名空间consumer
在这里插入图片描述
然后修改配置文件(bootstrap.properties

# DataId 默认使用 `spring.application.name` 配置跟文件扩展名结合(配置格式默认使用 properties), GROUP 不配置默认使用 DEFAULT_GROUP。因此该配置文件对应的 Nacos Config 配置的 DataId 为 nacos-config.properties, GROUP 为 DEFAULT_GROUP
spring.application.name=spring-cloud-alibaba-nacos-consumer
spring.cloud.nacos.config.server-addr=192.168.56.1:8848
# 默认为properties 如果采用其他格式 必须进行配置
spring.cloud.nacos.config.file-extension=yaml
# 关闭刷新功能
# spring.cloud.nacos.config.refresh.enabled=false
spring.profiles.active=develop
# 命名空间  常用场景之一是不同环境的注册的区分隔离,例如开发测试环境和生产环境的资源(如配置、服务)隔离等 需要在nacos控制台(命名空间)添加信息
spring.cloud.nacos.config.namespace=fe4903bf-4125-4a48-b8aa-f639836681b1
 

在没有明确指定 ${spring.cloud.nacos.config.namespace} 配置的情况下, 默认使用的是 Nacos 上 Public 这个namespace。
在这里插入图片描述

在这里插入图片描述
配置如下:

user.name: spring-cloud-alibaba-nacos-consumer-develop.yaml   
user.age: 25
current.env: develop-env namespace=fe4903bf-4125-4a48-b8aa-f639836681b1
 

该配置必须放在 bootstrap.properties 文件中。此外 spring.cloud.nacos.config.namespace 的值是 namespace 对应的 id,id 值可以在 Nacos 的控制台获取。并且在添加配置时注意不要选择其他的 namespae,否则将会导致读取不到正确的配置。

重新启动项目,查看控制台结果如下
在这里插入图片描述

自定义 Group 的配置

修改bootstrap.properties文件

# DataId 默认使用 `spring.application.name` 配置跟文件扩展名结合(配置格式默认使用 properties), GROUP 不配置默认使用 DEFAULT_GROUP。因此该配置文件对应的 Nacos Config 配置的 DataId 为 nacos-config.properties, GROUP 为 DEFAULT_GROUP
spring.application.name=spring-cloud-alibaba-nacos-consumer
spring.cloud.nacos.config.server-addr=192.168.56.1:8848
# 默认为properties 如果采用其他格式 必须进行配置
spring.cloud.nacos.config.file-extension=yaml
# 关闭刷新功能
# spring.cloud.nacos.config.refresh.enabled=false
spring.profiles.active=develop
# 命名空间  常用场景之一是不同环境的注册的区分隔离,例如开发测试环境和生产环境的资源(如配置、服务)隔离等 需要在nacos控制台(命名空间)添加信息
spring.cloud.nacos.config.namespace=fe4903bf-4125-4a48-b8aa-f639836681b1
spring.cloud.nacos.config.group=DEVELOP_GROUP

在nacos控制台添加配置,可以通过配置克隆的方式
在这里插入图片描述
在这里插入图片描述

user.name: spring-cloud-alibaba-nacos-consumer-develop.yaml DEVELOP_GROUP  
user.age: 26
current.env: develop-env namespace=fe4903bf-4125-4a48-b8aa-f639836681b1
自定义扩展的 Data Id 配置

通过自定义扩展的 Data Id 配置,既可以解决多个应用间配置共享的问题,又可以支持一个应用有多个配置文件。

# config external configuration
# 1、Data Id 在默认的组 DEFAULT_GROUP,不支持配置的动态刷新
spring.cloud.nacos.config.extension-configs[0]=ext-config-common01.properties
# 2、Data Id 不在默认的组,不支持动态刷新
spring.cloud.nacos.config.extension-configs[1].data-id=ext-config-common02.properties
spring.cloud.nacos.config.extension-configs[1].group=GLOBALE_GROUP
# 3、Data Id 既不在默认的组,也支持动态刷新
spring.cloud.nacos.config.extension-configs[2].data-id=ext-config-common03.properties
spring.cloud.nacos.config.extension-configs[2].group=REFRESH_GROUP
spring.cloud.nacos.config.extension-configs[2].refresh=true
 

多个 Data Id 同时配置时,他的优先级关系是 spring.cloud.nacos.config.ext-config[n].data-id 其中 n 的值越大,优先级越高。

spring.cloud.nacos.config.ext-config[n].data-id 的值必须带文件扩展名,文件扩展名既可支持 properties,又可以支持 yaml/yml。 此时 spring.cloud.nacos.config.file-extension 的配置对自定义扩展配置的 Data Id 文件扩展名没有影响。

共享Data Id 配置
# 更加清晰的在多个应用间配置共享的 Data Id 支持多个共享 Data Id 的配置
# 通过 spring.cloud.nacos.config.shared-configs 来支持多个共享配置的 Data Id 时, 多个共享配置间的一个优先级的关系我们约定:按照配置出现的先后顺序,即后面的优先级要高于前面
spring.cloud.nacos.config.shared-configs[0]=bootstrap-common.properties
spring.cloud.nacos.config.shared-configs[1]=all-common.properties
# 哪些共享配置的 Data Id 在配置变化时,应用中是否可动态刷新, 感知到最新的配置值,默认false
spring.cloud.nacos.config.shared-configs[0].refresh=true
配置的优先级

Nacos Config 目前提供了三种配置能力从 Nacos 拉取相关的配置

A: 通过 spring.cloud.nacos.config.shared-configs 支持多个共享 Data Id 的配置

B: 通过 spring.cloud.nacos.config.extension-configs[n].data-id 的方式支持多个扩展 Data Id 的配置

C: 通过内部相关规则(应用名、应用名+ Profile )自动生成相关的 Data Id 配置

当三种方式共同使用时,他们的一个优先级关系是:A (共享)< B (自定义扩展)< C(默认规则)

nacos使用原因 Spring cloud自带的有配置中心config;但是它更多的支持git同步;在公网上搭建git,不太现实,所以,不使用Nacos使用的mysql存储配置文件,集群模式也比较简单,所以,推荐使用。 下载最新的nacos服务 https://github.com/alibaba/nacos/releases 从这里下载最新的包; 1.解压缩之后,修改conf目录下的application.properties;把数据库相关的配置打开: If use MySQL
1. 简介 Nacos 提供用于存储配置和其他元数据的 key/value 存储,为分布式系统中的外部化配置提供服务器端和客户端支持。使用 Spring Cloud Alibaba Nacos Config,您可以在 Nacos Server 集中管理你 Spring Cloud 应用的外部属性配置。 Spring Cloud Alibaba Nacos ConfigConfig Server 和 Client的替代方案,客户端和服务器上的概念与 Spring Environment 和 Prope
tar -zxvf nacos-server-1.3.1.tar.gz cd nacos/bin 启动:./startup.sh -m standalone (单机启动,查看启动日志:/usr/local/nacos/logs/start.out) 访问:ip:8848/nacos (默认用户名密码:nacos/nacos) PS:单机环境必须带-m standalone参数启动,否则无法启动,不带参数启动的是集群环境;
Spring Cloud 阿里巴巴 Nacos 配置 Nacos 是一个易于使用的动态服务发现、配置和服务管理平台,用于构建云原生应用程序。 使用 Spring Cloud Alibaba Nacos Config 基于 Spring Cloud 的编程模型快速接入 Nacos 配置管理能力。 如何引入 Nacos Config 进行配置 请使用组 ID 为com.alibaba.cloud和工件 ID 为的启动器spring-cloud-starter-alibaba-nacos-configNacos可以做到服务注册,服务配置和消息总线这三种功能,可以认为是eureka+config+bus三个组件结合到了一起。 先说一下2020年9月23日时nacos的最新版本是1.3.2。 大家可以到nacos的官网:**https://nacos.io/zh-cn/**进行下载 zip的是windows版本而tar.gz是linux版本 下载完成之后解压之后通过cmd进入nacos-server-1.3.2\nacos\bin文件夹 进入之后运行sta
nacos配置中心的 Maven 依赖名称是 `spring-cloud-starter-alibaba-nacos-config`,选项 A 是正确的。 该依赖是 Spring Cloud Alibaba 中集成 Nacos 配置中心的 Starter,包含了 Nacos 配置中心的核心依赖和 Spring Cloud 配置的相关依赖。可以在 Spring Boot 项目的 pom.xml 文件中添加以下依赖来使用 Nacos 配置中心: ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> 添加该依赖后,可以在 Spring Boot 应用程序中使用 `@RefreshScope` 注解来实现动态刷新配置。