第六篇:nacos 应用初探

Posted 重塑之路

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第六篇: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 FilesJavajdk1.8.0_111injava.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:Usersack_AppDataLocalJetBrainsIntelliJIdea2020.1captureAgentdebugger-agent.jar -Dfile.encoding=UTF-8 -classpath "C:Program FilesJavajdk1.8.0_111jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_111jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_111jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_111jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_111jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_111jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_111jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_111jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_111jrelibext
ashorn.jar;C:Program FilesJavajdk1.8.0_111jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_111jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_111jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_111jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_111jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_111jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_111jrelibjce.jar;C:Program FilesJavajdk1.8.0_111jrelibjfr.jar;C:Program FilesJavajdk1.8.0_111jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_111jrelibjsse.jar;C:Program FilesJavajdk1.8.0_111jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_111jrelibplugin.jar;C:Program FilesJavajdk1.8.0_111jrelib
esources.jar;C:Program FilesJavajdk1.8.0_111jrelib
t.jar;D:ProjectWorkTolerance	olerance
acos-client	argetclasses;D:Toolsmaven_repocomalibabacloudspring-cloud-starter-alibaba-nacos-config2.2.1.RELEASEspring-cloud-starter-alibaba-nacos-config-2.2.1.RELEASE.jar;D:Toolsmaven_repocomalibabaspringspring-context-support1.0.6spring-context-support-1.0.6.jar;D:Toolsmaven_repocomalibaba
acos
acos-client1.2.1
acos-client-1.2.1.jar;D:Toolsmaven_repocomalibaba
acos
acos-common1.2.1
acos-common-1.2.1.jar;D:Toolsmaven_repoorgslf4jslf4j-api1.7.25slf4j-api-1.7.25.jar;D:Toolsmaven_repocommons-iocommons-io2.2commons-io-2.2.jar;D:Toolsmaven_repoorgapachecommonscommons-lang33.8.1commons-lang3-3.8.1.jar;D:Toolsmaven_repocomalibaba
acos
acos-api1.2.1
acos-api-1.2.1.jar;D:Toolsmaven_repocomgoogleguavaguava22.0guava-22.0.jar;D:Toolsmaven_repocomgooglecodefindbugsjsr3051.3.9jsr305-1.3.9.jar;D:Toolsmaven_repocomgoogleerrorproneerror_prone_annotations2.0.18error_prone_annotations-2.0.18.jar;D:Toolsmaven_repocomgooglej2objcj2objc-annotations1.1j2objc-annotations-1.1.jar;D:Toolsmaven_repoorgcodehausmojoanimal-sniffer-annotations1.14animal-sniffer-annotations-1.14.jar;D:Toolsmaven_repocommons-codeccommons-codec1.11commons-codec-1.11.jar;D:Toolsmaven_repocomfasterxmljacksoncorejackson-core2.9.7jackson-core-2.9.7.jar;D:Toolsmaven_repocomfasterxmljacksoncorejackson-databind2.9.7jackson-databind-2.9.7.jar;D:Toolsmaven_repocomfasterxmljacksoncorejackson-annotations2.9.0jackson-annotations-2.9.0.jar;D:Toolsmaven_repoioprometheussimpleclient.5.0simpleclient-0.5.0.jar;D:Toolsmaven_repoorgyamlsnakeyaml1.23snakeyaml-1.23.jar;D:Toolsmaven_repoorgspringframeworkcloudspring-cloud-commons2.2.2.RELEASEspring-cloud-commons-2.2.2.RELEASE.jar;D:Toolsmaven_repoorgspringframeworksecurityspring-security-crypto5.1.2.RELEASEspring-security-crypto-5.1.2.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkcloudspring-cloud-context2.2.2.RELEASEspring-cloud-context-2.2.2.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkootspring-boot-starter-web2.1.1.RELEASEspring-boot-starter-web-2.1.1.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkootspring-boot-starter2.1.1.RELEASEspring-boot-starter-2.1.1.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkootspring-boot2.1.1.RELEASEspring-boot-2.1.1.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkootspring-boot-autoconfigure2.1.1.RELEASEspring-boot-autoconfigure-2.1.1.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkootspring-boot-starter-logging2.1.1.RELEASEspring-boot-starter-logging-2.1.1.RELEASE.jar;D:Toolsmaven_repochqoslogbacklogback-classic1.2.3logback-classic-1.2.3.jar;D:Toolsmaven_repochqoslogbacklogback-core1.2.3logback-core-1.2.3.jar;D:Toolsmaven_repoorgapachelogginglog4jlog4j-to-slf4j2.11.1log4j-to-slf4j-2.11.1.jar;D:Toolsmaven_repoorgapachelogginglog4jlog4j-api2.11.1log4j-api-2.11.1.jar;D:Toolsmaven_repoorgslf4jjul-to-slf4j1.7.25jul-to-slf4j-1.7.25.jar;D:Toolsmaven_repojavaxannotationjavax.annotation-api1.3.2javax.annotation-api-1.3.2.jar;D:Toolsmaven_repoorgspringframeworkspring-core5.1.3.RELEASEspring-core-5.1.3.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkspring-jcl5.1.3.RELEASEspring-jcl-5.1.3.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkootspring-boot-starter-json2.1.1.RELEASEspring-boot-starter-json-2.1.1.RELEASE.jar;D:Toolsmaven_repocomfasterxmljacksondatatypejackson-datatype-jdk82.9.7jackson-datatype-jdk8-2.9.7.jar;D:Toolsmaven_repocomfasterxmljacksondatatypejackson-datatype-jsr3102.9.7jackson-datatype-jsr310-2.9.7.jar;D:Toolsmaven_repocomfasterxmljacksonmodulejackson-module-parameter-names2.9.7jackson-module-parameter-names-2.9.7.jar;D:Toolsmaven_repoorgspringframeworkootspring-boot-starter-tomcat2.1.1.RELEASEspring-boot-starter-tomcat-2.1.1.RELEASE.jar;D:Toolsmaven_repoorgapache	omcatembed	omcat-embed-core9.0.13	omcat-embed-core-9.0.13.jar;D:Toolsmaven_repoorgapache	omcatembed	omcat-embed-el9.0.13	omcat-embed-el-9.0.13.jar;D:Toolsmaven_repoorgapache	omcatembed	omcat-embed-websocket9.0.13	omcat-embed-websocket-9.0.13.jar;D:Toolsmaven_repoorghibernatevalidatorhibernate-validator6.0.13.Finalhibernate-validator-6.0.13.Final.jar;D:Toolsmaven_repojavaxvalidationvalidation-api2.0.1.Finalvalidation-api-2.0.1.Final.jar;D:Toolsmaven_repoorgjbossloggingjboss-logging3.3.2.Finaljboss-logging-3.3.2.Final.jar;D:Toolsmaven_repocomfasterxmlclassmate1.4.0classmate-1.4.0.jar;D:Toolsmaven_repoorgspringframeworkspring-web5.1.3.RELEASEspring-web-5.1.3.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkspring-beans5.1.3.RELEASEspring-beans-5.1.3.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkspring-webmvc5.1.3.RELEASEspring-webmvc-5.1.3.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkspring-aop5.1.3.RELEASEspring-aop-5.1.3.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkspring-context5.1.3.RELEASEspring-context-5.1.3.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkspring-expression5.1.3.RELEASEspring-expression-5.1.3.RELEASE.jar;D:Toolsmaven_repocomalibabafastjson1.2.58fastjson-1.2.58.jar;D:Program FilesJetBrainsIntelliJ IDEA 2020.1.1libidea_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 FilesJavajdk1.8.0_111injava.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:Usersack_AppDataLocalJetBrainsIntelliJIdea2020.1captureAgentdebugger-agent.jar -Dfile.encoding=UTF-8 -classpath "C:Program FilesJavajdk1.8.0_111jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_111jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_111jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_111jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_111jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_111jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_111jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_111jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_111jrelibext
ashorn.jar;C:Program FilesJavajdk1.8.0_111jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_111jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_111jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_111jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_111jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_111jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_111jrelibjce.jar;C:Program FilesJavajdk1.8.0_111jrelibjfr.jar;C:Program FilesJavajdk1.8.0_111jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_111jrelibjsse.jar;C:Program FilesJavajdk1.8.0_111jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_111jrelibplugin.jar;C:Program FilesJavajdk1.8.0_111jrelib
esources.jar;C:Program FilesJavajdk1.8.0_111jrelib
t.jar;D:ProjectWorkTolerance	olerance
acos-client	argetclasses;D:Toolsmaven_repocomalibabacloudspring-cloud-starter-alibaba-nacos-config2.1.2.RELEASEspring-cloud-starter-alibaba-nacos-config-2.1.2.RELEASE.jar;D:Toolsmaven_repocomalibabaspringspring-context-support1.0.6spring-context-support-1.0.6.jar;D:Toolsmaven_repocomalibaba
acos
acos-client1.2.1
acos-client-1.2.1.jar;D:Toolsmaven_repocomalibaba
acos
acos-common1.2.1
acos-common-1.2.1.jar;D:Toolsmaven_repoorgslf4jslf4j-api1.7.25slf4j-api-1.7.25.jar;D:Toolsmaven_repocommons-iocommons-io2.2commons-io-2.2.jar;D:Toolsmaven_repoorgapachecommonscommons-lang33.8.1commons-lang3-3.8.1.jar;D:Toolsmaven_repocomalibaba
acos
acos-api1.2.1
acos-api-1.2.1.jar;D:Toolsmaven_repocomgoogleguavaguava22.0guava-22.0.jar;D:Toolsmaven_repocomgooglecodefindbugsjsr3051.3.9jsr305-1.3.9.jar;D:Toolsmaven_repocomgoogleerrorproneerror_prone_annotations2.0.18error_prone_annotations-2.0.18.jar;D:Toolsmaven_repocomgooglej2objcj2objc-annotations1.1j2objc-annotations-1.1.jar;D:Toolsmaven_repoorgcodehausmojoanimal-sniffer-annotations1.14animal-sniffer-annotations-1.14.jar;D:Toolsmaven_repocommons-codeccommons-codec1.11commons-codec-1.11.jar;D:Toolsmaven_repocomfasterxmljacksoncorejackson-core2.9.7jackson-core-2.9.7.jar;D:Toolsmaven_repocomfasterxmljacksoncorejackson-databind2.9.7jackson-databind-2.9.7.jar;D:Toolsmaven_repocomfasterxmljacksoncorejackson-annotations2.9.0jackson-annotations-2.9.0.jar;D:Toolsmaven_repoioprometheussimpleclient.5.0simpleclient-0.5.0.jar;D:Toolsmaven_repoorgyamlsnakeyaml1.23snakeyaml-1.23.jar;D:Toolsmaven_repoorgspringframeworkcloudspring-cloud-commons2.1.5.RELEASEspring-cloud-commons-2.1.5.RELEASE.jar;D:Toolsmaven_repoorgspringframeworksecurityspring-security-crypto5.1.2.RELEASEspring-security-crypto-5.1.2.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkcloudspring-cloud-context2.1.5.RELEASEspring-cloud-context-2.1.5.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkootspring-boot-starter-web2.1.1.RELEASEspring-boot-starter-web-2.1.1.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkootspring-boot-starter2.1.1.RELEASEspring-boot-starter-2.1.1.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkootspring-boot2.1.1.RELEASEspring-boot-2.1.1.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkootspring-boot-autoconfigure2.1.1.RELEASEspring-boot-autoconfigure-2.1.1.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkootspring-boot-starter-logging2.1.1.RELEASEspring-boot-starter-logging-2.1.1.RELEASE.jar;D:Toolsmaven_repochqoslogbacklogback-classic1.2.3logback-classic-1.2.3.jar;D:Toolsmaven_repochqoslogbacklogback-core1.2.3logback-core-1.2.3.jar;D:Toolsmaven_repoorgapachelogginglog4jlog4j-to-slf4j2.11.1log4j-to-slf4j-2.11.1.jar;D:Toolsmaven_repoorgapachelogginglog4jlog4j-api2.11.1log4j-api-2.11.1.jar;D:Toolsmaven_repoorgslf4jjul-to-slf4j1.7.25jul-to-slf4j-1.7.25.jar;D:Toolsmaven_repojavaxannotationjavax.annotation-api1.3.2javax.annotation-api-1.3.2.jar;D:Toolsmaven_repoorgspringframeworkspring-core5.1.3.RELEASEspring-core-5.1.3.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkspring-jcl5.1.3.RELEASEspring-jcl-5.1.3.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkootspring-boot-starter-json2.1.1.RELEASEspring-boot-starter-json-2.1.1.RELEASE.jar;D:Toolsmaven_repocomfasterxmljacksondatatypejackson-datatype-jdk82.9.7jackson-datatype-jdk8-2.9.7.jar;D:Toolsmaven_repocomfasterxmljacksondatatypejackson-datatype-jsr3102.9.7jackson-datatype-jsr310-2.9.7.jar;D:Toolsmaven_repocomfasterxmljacksonmodulejackson-module-parameter-names2.9.7jackson-module-parameter-names-2.9.7.jar;D:Toolsmaven_repoorgspringframeworkootspring-boot-starter-tomcat2.1.1.RELEASEspring-boot-starter-tomcat-2.1.1.RELEASE.jar;D:Toolsmaven_repoorgapache	omcatembed	omcat-embed-core9.0.13	omcat-embed-core-9.0.13.jar;D:Toolsmaven_repoorgapache	omcatembed	omcat-embed-el9.0.13	omcat-embed-el-9.0.13.jar;D:Toolsmaven_repoorgapache	omcatembed	omcat-embed-websocket9.0.13	omcat-embed-websocket-9.0.13.jar;D:Toolsmaven_repoorghibernatevalidatorhibernate-validator6.0.13.Finalhibernate-validator-6.0.13.Final.jar;D:Toolsmaven_repojavaxvalidationvalidation-api2.0.1.Finalvalidation-api-2.0.1.Final.jar;D:Toolsmaven_repoorgjbossloggingjboss-logging3.3.2.Finaljboss-logging-3.3.2.Final.jar;D:Toolsmaven_repocomfasterxmlclassmate1.4.0classmate-1.4.0.jar;D:Toolsmaven_repoorgspringframeworkspring-web5.1.3.RELEASEspring-web-5.1.3.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkspring-beans5.1.3.RELEASEspring-beans-5.1.3.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkspring-webmvc5.1.3.RELEASEspring-webmvc-5.1.3.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkspring-aop5.1.3.RELEASEspring-aop-5.1.3.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkspring-context5.1.3.RELEASEspring-context-5.1.3.RELEASE.jar;D:Toolsmaven_repoorgspringframeworkspring-expression5.1.3.RELEASEspring-expression-5.1.3.RELEASE.jar;D:Toolsmaven_repocomalibabafastjson1.2.58fastjson-1.2.58.jar;D:Program FilesJetBrainsIntelliJ IDEA 2020.1.1libidea_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:Usersack_ acosconfig2020-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 FilesJavajdk1.8.0_111in;C:WindowsSunJavain;C:Windowssystem32;C:Windows;D:ToolsXshell 6;C:ProgramDataOracleJavajavapath;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:WindowsSystem32OpenSSH;C:Program Files (x86)NVIDIA CorporationPhysXCommon;C:Program FilesNVIDIA CorporationNVIDIA NvDLISR;C:Program FilesIntelWiFiin;C:Program FilesCommon FilesIntelWirelessCommon;D:Program Files odejs;C:Program FilesJavajdk1.8.0_111in;D:Program FilesGitcmd;D:Toolsapache-maven-3.6.3in;C:Usersack_AppDataLocalMicrosoftWindowsApps;C:Usersack_AppDataRoaming pm;C:Usersack_AppDataLocalBypassRuntm;C:Usersack_AppDataLocalGitHubDesktopin;D:Program FilesJetBrainsIntelliJ IDEA Community Edition 2019.3.3in;;D:Program FilesMicrosoft VS Codein;.]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类型文件的话,差别不大,主要是语法格式的问题,各位可以自行测试。


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


以上是关于第六篇:nacos 应用初探的主要内容,如果未能解决你的问题,请参考以下文章

第六篇Scrum冲刺博客

Python开发第六篇:模块

Redis系列-第六篇哨兵模式

python基础-第六篇-6.2模块

第六篇:汇编基础指令讲解

第六篇:循环语句 - while和for