vlambda博客
学习文章列表

第六篇:nacos 应用初探

上篇已经介绍了了我们nacos server的搭建,也差un构建了我们的项目架构,并已经把nacos server 模块已经创建完毕,项目结构如下:

本篇我们将创建Nacos Client,以此来测试Nacos的功能以及使用方法。


使用上一篇的方法,创建nacos-client子模块,客户端配置参考官网:

Nacos Spring Cloud 快速开始

https://nacos.io/zh-cn/docs/quick-start-spring-cloud.html


作为配置中心使用

1、添加依赖:
 <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2.1.2.RELEASE</version> </dependency>

注意:spring-cloud-starter-alibaba-nacos-config的版本和SpringBoot的版本是对应的,版本不对会报以下错误:

"C:\Program Files\Java\jdk1.8.0_111\bin\java.exe" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:55901,suspend=y,server=n -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:C:\Users\back_\AppData\Local\JetBrains\IntelliJIdea2020.1\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_111\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\rt.jar;D:\ProjectWork\Tolerance\tolerance\nacos-client\target\classes;D:\Tools\maven_repo\com\alibaba\cloud\spring-cloud-starter-alibaba-nacos-config\2.2.1.RELEASE\spring-cloud-starter-alibaba-nacos-config-2.2.1.RELEASE.jar;D:\Tools\maven_repo\com\alibaba\spring\spring-context-support\1.0.6\spring-context-support-1.0.6.jar;D:\Tools\maven_repo\com\alibaba\nacos\nacos-client\1.2.1\nacos-client-1.2.1.jar;D:\Tools\maven_repo\com\alibaba\nacos\nacos-common\1.2.1\nacos-common-1.2.1.jar;D:\Tools\maven_repo\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;D:\Tools\maven_repo\commons-io\commons-io\2.2\commons-io-2.2.jar;D:\Tools\maven_repo\org\apache\commons\commons-lang3\3.8.1\commons-lang3-3.8.1.jar;D:\Tools\maven_repo\com\alibaba\nacos\nacos-api\1.2.1\nacos-api-1.2.1.jar;D:\Tools\maven_repo\com\google\guava\guava\22.0\guava-22.0.jar;D:\Tools\maven_repo\com\google\code\findbugs\jsr305\1.3.9\jsr305-1.3.9.jar;D:\Tools\maven_repo\com\google\errorprone\error_prone_annotations\2.0.18\error_prone_annotations-2.0.18.jar;D:\Tools\maven_repo\com\google\j2objc\j2objc-annotations\1.1\j2objc-annotations-1.1.jar;D:\Tools\maven_repo\org\codehaus\mojo\animal-sniffer-annotations\1.14\animal-sniffer-annotations-1.14.jar;D:\Tools\maven_repo\commons-codec\commons-codec\1.11\commons-codec-1.11.jar;D:\Tools\maven_repo\com\fasterxml\jackson\core\jackson-core\2.9.7\jackson-core-2.9.7.jar;D:\Tools\maven_repo\com\fasterxml\jackson\core\jackson-databind\2.9.7\jackson-databind-2.9.7.jar;D:\Tools\maven_repo\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;D:\Tools\maven_repo\io\prometheus\simpleclient\0.5.0\simpleclient-0.5.0.jar;D:\Tools\maven_repo\org\yaml\snakeyaml\1.23\snakeyaml-1.23.jar;D:\Tools\maven_repo\org\springframework\cloud\spring-cloud-commons\2.2.2.RELEASE\spring-cloud-commons-2.2.2.RELEASE.jar;D:\Tools\maven_repo\org\springframework\security\spring-security-crypto\5.1.2.RELEASE\spring-security-crypto-5.1.2.RELEASE.jar;D:\Tools\maven_repo\org\springframework\cloud\spring-cloud-context\2.2.2.RELEASE\spring-cloud-context-2.2.2.RELEASE.jar;D:\Tools\maven_repo\org\springframework\boot\spring-boot-starter-web\2.1.1.RELEASE\spring-boot-starter-web-2.1.1.RELEASE.jar;D:\Tools\maven_repo\org\springframework\boot\spring-boot-starter\2.1.1.RELEASE\spring-boot-starter-2.1.1.RELEASE.jar;D:\Tools\maven_repo\org\springframework\boot\spring-boot\2.1.1.RELEASE\spring-boot-2.1.1.RELEASE.jar;D:\Tools\maven_repo\org\springframework\boot\spring-boot-autoconfigure\2.1.1.RELEASE\spring-boot-autoconfigure-2.1.1.RELEASE.jar;D:\Tools\maven_repo\org\springframework\boot\spring-boot-starter-logging\2.1.1.RELEASE\spring-boot-starter-logging-2.1.1.RELEASE.jar;D:\Tools\maven_repo\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\Tools\maven_repo\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\Tools\maven_repo\org\apache\logging\log4j\log4j-to-slf4j\2.11.1\log4j-to-slf4j-2.11.1.jar;D:\Tools\maven_repo\org\apache\logging\log4j\log4j-api\2.11.1\log4j-api-2.11.1.jar;D:\Tools\maven_repo\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;D:\Tools\maven_repo\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;D:\Tools\maven_repo\org\springframework\spring-core\5.1.3.RELEASE\spring-core-5.1.3.RELEASE.jar;D:\Tools\maven_repo\org\springframework\spring-jcl\5.1.3.RELEASE\spring-jcl-5.1.3.RELEASE.jar;D:\Tools\maven_repo\org\springframework\boot\spring-boot-starter-json\2.1.1.RELEASE\spring-boot-starter-json-2.1.1.RELEASE.jar;D:\Tools\maven_repo\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.7\jackson-datatype-jdk8-2.9.7.jar;D:\Tools\maven_repo\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.7\jackson-datatype-jsr310-2.9.7.jar;D:\Tools\maven_repo\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.7\jackson-module-parameter-names-2.9.7.jar;D:\Tools\maven_repo\org\springframework\boot\spring-boot-starter-tomcat\2.1.1.RELEASE\spring-boot-starter-tomcat-2.1.1.RELEASE.jar;D:\Tools\maven_repo\org\apache\tomcat\embed\tomcat-embed-core\9.0.13\tomcat-embed-core-9.0.13.jar;D:\Tools\maven_repo\org\apache\tomcat\embed\tomcat-embed-el\9.0.13\tomcat-embed-el-9.0.13.jar;D:\Tools\maven_repo\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.13\tomcat-embed-websocket-9.0.13.jar;D:\Tools\maven_repo\org\hibernate\validator\hibernate-validator\6.0.13.Final\hibernate-validator-6.0.13.Final.jar;D:\Tools\maven_repo\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;D:\Tools\maven_repo\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;D:\Tools\maven_repo\com\fasterxml\classmate\1.4.0\classmate-1.4.0.jar;D:\Tools\maven_repo\org\springframework\spring-web\5.1.3.RELEASE\spring-web-5.1.3.RELEASE.jar;D:\Tools\maven_repo\org\springframework\spring-beans\5.1.3.RELEASE\spring-beans-5.1.3.RELEASE.jar;D:\Tools\maven_repo\org\springframework\spring-webmvc\5.1.3.RELEASE\spring-webmvc-5.1.3.RELEASE.jar;D:\Tools\maven_repo\org\springframework\spring-aop\5.1.3.RELEASE\spring-aop-5.1.3.RELEASE.jar;D:\Tools\maven_repo\org\springframework\spring-context\5.1.3.RELEASE\spring-context-5.1.3.RELEASE.jar;D:\Tools\maven_repo\org\springframework\spring-expression\5.1.3.RELEASE\spring-expression-5.1.3.RELEASE.jar;D:\Tools\maven_repo\com\alibaba\fastjson\1.2.58\fastjson-1.2.58.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2020.1.1\lib\idea_rt.jar" com.tolerance.nacosclient.NacosClientApplicationConnected to the target VM, address: '127.0.0.1:55901', transport: 'socket'2020-05-09 19:43:52.546 WARN 21584 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bootstrapImportSelectorConfiguration': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationPropertiesBean2020-05-09 19:43:52.558 INFO 21584 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.2020-05-09 19:43:52.577 ERROR 21584 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bootstrapImportSelectorConfiguration': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationPropertiesBean at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:584) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:139) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:212) [spring-cloud-context-2.2.2.RELEASE.jar:2.2.2.RELEASE] at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:117) [spring-cloud-context-2.2.2.RELEASE.jar:2.2.2.RELEASE] at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:74) [spring-cloud-context-2.2.2.RELEASE.jar:2.2.2.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) [spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) [spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) [spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127) [spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:75) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:347) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:306) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at com.tolerance.nacosclient.NacosClientApplication.main(NacosClientApplication.java:12) [classes/:na]Caused by: java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationPropertiesBean at org.springframework.cloud.context.properties.ConfigurationPropertiesBeans.postProcessBeforeInitialization(ConfigurationPropertiesBeans.java:94) ~[spring-cloud-context-2.2.2.RELEASE.jar:2.2.2.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:419) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1737) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] ... 26 common frames omittedCaused by: java.lang.ClassNotFoundException: org.springframework.boot.context.properties.ConfigurationPropertiesBean at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_111] at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_111] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_111] at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_111] ... 30 common frames omitted
2020-05-09 19:43:52.579 ERROR 21584 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bootstrapImportSelectorConfiguration': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationPropertiesBean at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:584) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:139) ~[spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:212) ~[spring-cloud-context-2.2.2.RELEASE.jar:2.2.2.RELEASE] at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:117) ~[spring-cloud-context-2.2.2.RELEASE.jar:2.2.2.RELEASE] at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:74) ~[spring-cloud-context-2.2.2.RELEASE.jar:2.2.2.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127) ~[spring-context-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:75) ~[spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54) ~[spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:347) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:306) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.1.RELEASE.jar:2.1.1.RELEASE] at com.tolerance.nacosclient.NacosClientApplication.main(NacosClientApplication.java:12) [classes/:na]Caused by: java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationPropertiesBean at org.springframework.cloud.context.properties.ConfigurationPropertiesBeans.postProcessBeforeInitialization(ConfigurationPropertiesBeans.java:94) ~[spring-cloud-context-2.2.2.RELEASE.jar:2.2.2.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:419) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1737) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576) ~[spring-beans-5.1.3.RELEASE.jar:5.1.3.RELEASE] ... 26 common frames omittedCaused by: java.lang.ClassNotFoundException: org.springframework.boot.context.properties.ConfigurationPropertiesBean at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_111] at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_111] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_111] at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_111] ... 30 common frames omitted
Disconnected from the target VM, address: '127.0.0.1:55901', transport: 'socket'
Process finished with exit code 1

版本对应关系参考:

版本说明

https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E

第六篇:nacos 应用初探

2、在application.properties文件添加:

spring.cloud.nacos.config.server-addr=localhost:8000spring.application.name=nacos-config


说明:之所以需要配置 spring.application.name ,是因为它是构成 Nacos 配置管理 dataId字段的一部分。

在 Nacos Spring Cloud 中,dataId 的完整格式如下:

${prefix}-${spring.profile.active}.${file-extension}
  • prefix 默认为 spring.application.name 的值,也可以通过配置项 spring.cloud.nacos.config.prefix来配置。

  • spring.profile.active 即为当前环境对应的 profile,详情可以参考 Spring Boot文档。 注意:当 spring.profile.active 为空时,对应的连接符 - 也将不存在,dataId 的拼接格式变成 ${prefix}.${file-extension}

  • file-exetension 为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension 来配置。目前只支持 properties 和 yaml 类型。



3、在Nacos 配置中心中,添加nacos-config.properties配置

第六篇:nacos 应用初探

点击+新建:

第六篇:nacos 应用初探

这里使用了默认的DEFAULT_GROUP组,这里可根据实际情况来进行分组,以便来区分不用的环境,当然,需要在客户端的bootstrap.properties来添加分组信息才能取到非默认组的文件

点击发布:

第六篇:nacos 应用初探

点击确定,然后点击返回:

第六篇:nacos 应用初探

注,由于我本次导入的有问题,部分中文显示有问题,后续会进行处理。


4、在客户端添加测试类:

通过 Spring Cloud 原生注解  @RefreshScope 实现配置自动更新:
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")@RefreshScopepublic class TestController {
@Value("${testValue:ttt}")    private String testValue;     @RequestMapping("/get") public String get() { System.out.println("testValue======"+testValue); return testValue; }}

修改启动类,在启动类获取参数:

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


5、启动验证:

"C:\Program Files\Java\jdk1.8.0_111\bin\java.exe" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:59065,suspend=y,server=n -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:C:\Users\back_\AppData\Local\JetBrains\IntelliJIdea2020.1\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_111\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_111\jre\lib\rt.jar;D:\ProjectWork\Tolerance\tolerance\nacos-client\target\classes;D:\Tools\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;D:\Tools\maven_repo\com\alibaba\spring\spring-context-support\1.0.6\spring-context-support-1.0.6.jar;D:\Tools\maven_repo\com\alibaba\nacos\nacos-client\1.2.1\nacos-client-1.2.1.jar;D:\Tools\maven_repo\com\alibaba\nacos\nacos-common\1.2.1\nacos-common-1.2.1.jar;D:\Tools\maven_repo\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;D:\Tools\maven_repo\commons-io\commons-io\2.2\commons-io-2.2.jar;D:\Tools\maven_repo\org\apache\commons\commons-lang3\3.8.1\commons-lang3-3.8.1.jar;D:\Tools\maven_repo\com\alibaba\nacos\nacos-api\1.2.1\nacos-api-1.2.1.jar;D:\Tools\maven_repo\com\google\guava\guava\22.0\guava-22.0.jar;D:\Tools\maven_repo\com\google\code\findbugs\jsr305\1.3.9\jsr305-1.3.9.jar;D:\Tools\maven_repo\com\google\errorprone\error_prone_annotations\2.0.18\error_prone_annotations-2.0.18.jar;D:\Tools\maven_repo\com\google\j2objc\j2objc-annotations\1.1\j2objc-annotations-1.1.jar;D:\Tools\maven_repo\org\codehaus\mojo\animal-sniffer-annotations\1.14\animal-sniffer-annotations-1.14.jar;D:\Tools\maven_repo\commons-codec\commons-codec\1.11\commons-codec-1.11.jar;D:\Tools\maven_repo\com\fasterxml\jackson\core\jackson-core\2.9.7\jackson-core-2.9.7.jar;D:\Tools\maven_repo\com\fasterxml\jackson\core\jackson-databind\2.9.7\jackson-databind-2.9.7.jar;D:\Tools\maven_repo\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;D:\Tools\maven_repo\io\prometheus\simpleclient\0.5.0\simpleclient-0.5.0.jar;D:\Tools\maven_repo\org\yaml\snakeyaml\1.23\snakeyaml-1.23.jar;D:\Tools\maven_repo\org\springframework\cloud\spring-cloud-commons\2.1.5.RELEASE\spring-cloud-commons-2.1.5.RELEASE.jar;D:\Tools\maven_repo\org\springframework\security\spring-security-crypto\5.1.2.RELEASE\spring-security-crypto-5.1.2.RELEASE.jar;D:\Tools\maven_repo\org\springframework\cloud\spring-cloud-context\2.1.5.RELEASE\spring-cloud-context-2.1.5.RELEASE.jar;D:\Tools\maven_repo\org\springframework\boot\spring-boot-starter-web\2.1.1.RELEASE\spring-boot-starter-web-2.1.1.RELEASE.jar;D:\Tools\maven_repo\org\springframework\boot\spring-boot-starter\2.1.1.RELEASE\spring-boot-starter-2.1.1.RELEASE.jar;D:\Tools\maven_repo\org\springframework\boot\spring-boot\2.1.1.RELEASE\spring-boot-2.1.1.RELEASE.jar;D:\Tools\maven_repo\org\springframework\boot\spring-boot-autoconfigure\2.1.1.RELEASE\spring-boot-autoconfigure-2.1.1.RELEASE.jar;D:\Tools\maven_repo\org\springframework\boot\spring-boot-starter-logging\2.1.1.RELEASE\spring-boot-starter-logging-2.1.1.RELEASE.jar;D:\Tools\maven_repo\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\Tools\maven_repo\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\Tools\maven_repo\org\apache\logging\log4j\log4j-to-slf4j\2.11.1\log4j-to-slf4j-2.11.1.jar;D:\Tools\maven_repo\org\apache\logging\log4j\log4j-api\2.11.1\log4j-api-2.11.1.jar;D:\Tools\maven_repo\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;D:\Tools\maven_repo\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;D:\Tools\maven_repo\org\springframework\spring-core\5.1.3.RELEASE\spring-core-5.1.3.RELEASE.jar;D:\Tools\maven_repo\org\springframework\spring-jcl\5.1.3.RELEASE\spring-jcl-5.1.3.RELEASE.jar;D:\Tools\maven_repo\org\springframework\boot\spring-boot-starter-json\2.1.1.RELEASE\spring-boot-starter-json-2.1.1.RELEASE.jar;D:\Tools\maven_repo\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.7\jackson-datatype-jdk8-2.9.7.jar;D:\Tools\maven_repo\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.7\jackson-datatype-jsr310-2.9.7.jar;D:\Tools\maven_repo\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.7\jackson-module-parameter-names-2.9.7.jar;D:\Tools\maven_repo\org\springframework\boot\spring-boot-starter-tomcat\2.1.1.RELEASE\spring-boot-starter-tomcat-2.1.1.RELEASE.jar;D:\Tools\maven_repo\org\apache\tomcat\embed\tomcat-embed-core\9.0.13\tomcat-embed-core-9.0.13.jar;D:\Tools\maven_repo\org\apache\tomcat\embed\tomcat-embed-el\9.0.13\tomcat-embed-el-9.0.13.jar;D:\Tools\maven_repo\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.13\tomcat-embed-websocket-9.0.13.jar;D:\Tools\maven_repo\org\hibernate\validator\hibernate-validator\6.0.13.Final\hibernate-validator-6.0.13.Final.jar;D:\Tools\maven_repo\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;D:\Tools\maven_repo\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;D:\Tools\maven_repo\com\fasterxml\classmate\1.4.0\classmate-1.4.0.jar;D:\Tools\maven_repo\org\springframework\spring-web\5.1.3.RELEASE\spring-web-5.1.3.RELEASE.jar;D:\Tools\maven_repo\org\springframework\spring-beans\5.1.3.RELEASE\spring-beans-5.1.3.RELEASE.jar;D:\Tools\maven_repo\org\springframework\spring-webmvc\5.1.3.RELEASE\spring-webmvc-5.1.3.RELEASE.jar;D:\Tools\maven_repo\org\springframework\spring-aop\5.1.3.RELEASE\spring-aop-5.1.3.RELEASE.jar;D:\Tools\maven_repo\org\springframework\spring-context\5.1.3.RELEASE\spring-context-5.1.3.RELEASE.jar;D:\Tools\maven_repo\org\springframework\spring-expression\5.1.3.RELEASE\spring-expression-5.1.3.RELEASE.jar;D:\Tools\maven_repo\com\alibaba\fastjson\1.2.58\fastjson-1.2.58.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2020.1.1\lib\idea_rt.jar" com.tolerance.nacosclient.NacosClientApplicationConnected to the target VM, address: '127.0.0.1:59065', transport: 'socket'
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.1.RELEASE)
2020-05-09 20:14:55.899 INFO 6588 --- [ main] c.a.n.c.c.impl.LocalConfigInfoProcessor : LOCAL_SNAPSHOT_PATH:C:\Users\back_\nacos\config2020-05-09 20:14:55.952 INFO 6588 --- [ main] c.a.nacos.client.config.impl.Limiter : limitTime:5.02020-05-09 20:14:56.006 WARN 6588 --- [ main] c.a.c.n.c.NacosPropertySourceBuilder : Ignore the empty nacos configuration and get it based on dataId[nacos-config] & group[DEFAULT_GROUP]2020-05-09 20:14:56.037 INFO 6588 --- [ main] c.a.nacos.client.config.utils.JVMUtil : isMultiInstance:false2020-05-09 20:14:56.046 INFO 6588 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-nacos-config.properties,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-nacos-config,DEFAULT_GROUP'}]2020-05-09 20:14:56.053 INFO 6588 --- [ main] c.t.nacosclient.NacosClientApplication : No active profile set, falling back to default profiles: default2020-05-09 20:14:56.863 INFO 6588 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=209a7821-0e6d-3d4d-95f1-e4cb495348df2020-05-09 20:14:57.385 INFO 6588 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)2020-05-09 20:14:57.424 INFO 6588 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]2020-05-09 20:14:57.425 INFO 6588 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/9.0.132020-05-09 20:14:57.436 INFO 6588 --- [ main] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_111\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:\Tools\Xshell 6\;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;D:\Program Files\nodejs\;C:\Program Files\Java\jdk1.8.0_111\bin;D:\Program Files\Git\cmd;D:\Tools\apache-maven-3.6.3\bin;C:\Users\back_\AppData\Local\Microsoft\WindowsApps;C:\Users\back_\AppData\Roaming\npm;C:\Users\back_\AppData\Local\BypassRuntm;C:\Users\back_\AppData\Local\GitHubDesktop\bin;D:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.3.3\bin;;D:\Program Files\Microsoft VS Code\bin;.]2020-05-09 20:14:57.648 INFO 6588 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2020-05-09 20:14:57.648 INFO 6588 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1575 ms2020-05-09 20:14:58.154 INFO 6588 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'2020-05-09 20:15:01.540 INFO 6588 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''2020-05-09 20:15:04.139 INFO 6588 --- [ main] c.t.nacosclient.NacosClientApplication : Started NacosClientApplication in 14.678 seconds (JVM running for 17.212)2020-05-09 20:15:04.147 INFO 6588 --- [ main] c.a.n.client.config.impl.ClientWorker : [fixed-localhost_8000] [subscribe] nacos-config.properties+DEFAULT_GROUP2020-05-09 20:15:04.149 INFO 6588 --- [ main] c.a.nacos.client.config.impl.CacheData : [fixed-localhost_8000] [add-listener] ok, tenant=, dataId=nacos-config.properties, group=DEFAULT_GROUP, cnt=12020-05-09 20:15:04.150 INFO 6588 --- [ main] c.a.n.client.config.impl.ClientWorker : [fixed-localhost_8000] [subscribe] nacos-config+DEFAULT_GROUP2020-05-09 20:15:04.150 INFO 6588 --- [ main] c.a.nacos.client.config.impl.CacheData : [fixed-localhost_8000] [add-listener] ok, tenant=, dataId=nacos-config, group=DEFAULT_GROUP, cnt=1user name :nacos-config-properties; age: 902020-05-09 20:15:04.171 INFO 6588 --- [-localhost_8000] c.a.n.client.config.impl.ClientWorker : get changedGroupKeys:[]

注:看到打印 user name :nacos-config-properties; age: 90 说明加载配置中心成功

浏览器调用:http://localhost:8080/config/get

响应:testNacos-properties

第六篇:nacos 应用初探

6、修改配置中心的值进行验证是否可以在线更新:

    点击编辑:

第六篇:nacos 应用初探

第六篇:nacos 应用初探

第六篇:nacos 应用初探

点击发布

第六篇:nacos 应用初探

第六篇:nacos 应用初探

第六篇:nacos 应用初探

此时我们观察项目和请求:

2020-05-09 20:19:52.587 INFO 6588 --- [-localhost_8000] c.a.n.client.config.impl.ClientWorker : [fixed-localhost_8000] [polling-resp] config changed. dataId=nacos-config.properties, group=DEFAULT_GROUP2020-05-09 20:19:52.587 INFO 6588 --- [-localhost_8000] c.a.n.client.config.impl.ClientWorker : get changedGroupKeys:[nacos-config.properties+DEFAULT_GROUP]2020-05-09 20:19:52.607 INFO 6588 --- [-localhost_8000] c.a.n.client.config.impl.ClientWorker : [fixed-localhost_8000] [data-received] dataId=nacos-config.properties, group=DEFAULT_GROUP, tenant=null, md5=141aeebd3d4f746d566a19b1977e2640, content=user.name=nacos-config-properties-updateuser.age=60testValue=testNacos-properties-update, type=text2020-05-09 20:19:52.609 INFO 6588 --- [-localhost_8000] c.a.nacos.client.config.impl.CacheData : [fixed-localhost_8000] [notify-context] dataId=nacos-config.properties, group=DEFAULT_GROUP, md5=141aeebd3d4f746d566a19b1977e26402020-05-09 20:19:57.233 WARN 6588 --- [-localhost_8000] c.a.c.n.c.NacosPropertySourceBuilder : Ignore the empty nacos configuration and get it based on dataId[nacos-config] & group[DEFAULT_GROUP]2020-05-09 20:19:57.240 INFO 6588 --- [-localhost_8000] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-nacos-config.properties,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-nacos-config,DEFAULT_GROUP'}]2020-05-09 20:19:57.242 INFO 6588 --- [-localhost_8000] o.s.boot.SpringApplication : No active profile set, falling back to default profiles: default2020-05-09 20:19:57.262 INFO 6588 --- [-localhost_8000] o.s.boot.SpringApplication : Started application in 4.647 seconds (JVM running for 310.337)2020-05-09 20:19:57.278 INFO 6588 --- [-localhost_8000] o.s.c.e.event.RefreshEventListener : Refresh keys changed: [user.age, user.name, testValue]2020-05-09 20:19:57.279 INFO 6588 --- [-localhost_8000] c.a.nacos.client.config.impl.CacheData : [fixed-localhost_8000] [notify-ok] dataId=nacos-config.properties, group=DEFAULT_GROUP, md5=141aeebd3d4f746d566a19b1977e2640, listener=com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1@13ac4aa 2020-05-09 20:19:57.279 INFO 6588 --- [-localhost_8000] c.a.nacos.client.config.impl.CacheData : [fixed-localhost_8000] [notify-listener] time cost=4670ms in ClientWorker, dataId=nacos-config.properties, group=DEFAULT_GROUP, md5=141aeebd3d4f746d566a19b1977e2640, listener=com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1@13ac4aa 2020-05-09 20:20:26.786 INFO 6588 --- [-localhost_8000] c.a.n.client.config.impl.ClientWorker : get changedGroupKeys:[]

第六篇:nacos 应用初探

我们发现更新已经成功


配置中心的更多使用方式

下面我们尝试一次最复杂的配置方式
在非默认空间,非默认分组的配置文件的参数加载
properties模式
1、我们现在nacos 创建名称为 nacosTest 的命名空间:

第六篇:nacos 应用初探

点击新建命名空间:

第六篇:nacos 应用初探

第六篇:nacos 应用初探

回到配置列表,选择刚刚建立的命名空间:

第六篇:nacos 应用初探


根据Nacos 配置管理 dataId 生成的命名规则:

${prefix}-${spring.profile.active}.${file-extension}

在Nacos中创建名称为nacos-test.properties的配置文件:

创建时为了模拟最复杂情况,这里我们不使用默认分组,而是创建新的分组TEST_GROUP,配置格式必须选择Properties

第六篇:nacos 应用初探

第六篇:nacos 应用初探

项目resources 目录下创建bootstrap.properties文件,
#配置中心地址spring.cloud.nacos.config.server-addr=localhost:8000#读取文件类型,支持properties和yamlspring.cloud.nacos.config.file-extension=properties#读取文件的名称前缀,默认是spring.application.name的值spring.cloud.nacos.config.prefix=nacos-test#读取文件所在的命名空间 命名空间的ID,目前不支持名称spring.cloud.nacos.config.namespace=9403ddc2-f9b1-4220-b22c-3572ddaf9c6b#读取文件所在的分组spring.cloud.nacos.config.group=TEST_GROUP#构成 Nacos 配置管理 dataId字段的一部分 完整命名规则为:${prefix}-${spring.profile.active}.${file-extension}spring.application.name=nacos-test

启动客户端:

 可以看出已经生效

第六篇:nacos 应用初探

第六篇:nacos 应用初探

修改nacos配置中心的值:

第六篇:nacos 应用初探

查看后台:

2020-05-10 03:47:55.641 INFO 22004 --- [2c-3572ddaf9c6b] c.a.n.client.config.impl.ClientWorker : [fixed-localhost_8000-9403ddc2-f9b1-4220-b22c-3572ddaf9c6b] [polling-resp] config changed. dataId=nacos-test.properties, group=TEST_GROUP, tenant=9403ddc2-f9b1-4220-b22c-3572ddaf9c6b2020-05-10 03:47:55.642 INFO 22004 --- [2c-3572ddaf9c6b] c.a.n.client.config.impl.ClientWorker : get changedGroupKeys:[nacos-test.properties+TEST_GROUP+9403ddc2-f9b1-4220-b22c-3572ddaf9c6b]2020-05-10 03:47:55.661 INFO 22004 --- [2c-3572ddaf9c6b] c.a.n.client.config.impl.ClientWorker : [fixed-localhost_8000-9403ddc2-f9b1-4220-b22c-3572ddaf9c6b] [data-received] dataId=nacos-test.properties, group=TEST_GROUP, tenant=9403ddc2-f9b1-4220-b22c-3572ddaf9c6b, md5=98c4296d9fac3808060e7e38741f2777, content=user.name=nacosTest-TEST_GROUP-nacos-test.properties-2user.age=2testValue=nacosTest-TEST_GROUP-n..., type=properties2020-05-10 03:47:55.663 INFO 22004 --- [2c-3572ddaf9c6b] c.a.nacos.client.config.impl.CacheData : [fixed-localhost_8000-9403ddc2-f9b1-4220-b22c-3572ddaf9c6b] [notify-context] dataId=nacos-test.properties, group=TEST_GROUP, md5=98c4296d9fac3808060e7e38741f27772020-05-10 03:47:59.235 WARN 22004 --- [2c-3572ddaf9c6b] c.a.c.n.c.NacosPropertySourceBuilder : Ignore the empty nacos configuration and get it based on dataId[nacos-test] & group[TEST_GROUP]

可以看到时时更新到了后台




本次测试同时使用用了命名空间和分组来加大复杂程度,基本可以满足日常工作中的需求,比如用命名空间来隔离生产、测试环境,用分组来隔离同环境下不同项目或应用程序的配置文件。
yaml类型文件的话,差别不大,主要是语法格式的问题,各位可以自行测试。


↓扫码关注不迷路,更多操作持续更新中↓