nacos动态刷新配置
pom依赖
<groupId>com.sandulgroupId>
<artifactId>refresh-of-nacosartifactId>
<version>1.0-SNAPSHOTversion>
<description>nacos动态刷新练习description>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.11.RELEASEversion>
<relativePath/>
parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
<version>2.2.11.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
<version>2.1.2.RELEASEversion>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
<version>2.1.2.RELEASEversion>
dependency>
dependencies>
dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
dependencies>
application.yml
server:
port: 11024
bootstrap.yml
spring:
application:
name: nacosRefresh
cloud:
nacos:
config:
server-addr: http://192.168.10.161:8848
file-extension: yml
namespace: nacos_refresh_namespace
refresh-enabled: true
discovery:
server-addr: http://192.168.10.161:8848
register-enabled: true
profiles:
active: dev
nacos上的配置信息



启动类获取nacos上的配置
@SpringBootApplication
public class NacosRefreshApplication {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(NacosRefreshApplication.class, args);
ConfigurableEnvironment environment = run.getEnvironment();
System.out.println(environment.getProperty("nacostest"));
}
}
启动测试:
Connected to the target VM, address: '127.0.0.1:53080', transport: 'socket'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.11.RELEASE)
2021-04-29 14:04:32.451 INFO 12984 --- [ main] c.a.n.c.c.impl.LocalConfigInfoProcessor : LOCAL_SNAPSHOT_PATH:C:\Users\Administrator\nacos\config
2021-04-29 14:04:32.519 INFO 12984 --- [ main] c.a.nacos.client.config.impl.Limiter : limitTime:5.0
2021-04-29 14:04:32.539 WARN 12984 --- [ main] c.a.c.n.c.NacosPropertySourceBuilder : Ignore the empty nacos configuration and get it based on dataId[nacosRefresh] & group[DEFAULT_GROUP]
2021-04-29 14:04:32.539 WARN 12984 --- [ main] c.a.c.n.c.NacosPropertySourceBuilder : Ignore the empty nacos configuration and get it based on dataId[nacosRefresh.yml] & group[DEFAULT_GROUP]
2021-04-29 14:04:32.550 INFO 12984 --- [ main] c.a.nacos.client.config.utils.JVMUtil : isMultiInstance:false
2021-04-29 14:04:32.550 INFO 12984 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-nacosRefresh-dev.yml,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-nacosRefresh.yml,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-nacosRefresh,DEFAULT_GROUP'}]
2021-04-29 14:04:32.559 INFO 12984 --- [ main] com.sandul.NacosRefreshApplication : The following profiles are active: dev
2021-04-29 14:04:32.826 INFO 12984 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=8a536b2e-d1ea-3216-bc70-c313e598536b
2021-04-29 14:04:32.844 WARN 12984 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2021-04-29 14:04:32.844 INFO 12984 --- [ 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.
2021-04-29 14:04:32.848 WARN 12984 --- [ main] c.n.c.sources.URLConfigurationSource : No URLs will be polled as dynamic configuration sources.
2021-04-29 14:04:32.848 INFO 12984 --- [ 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.
2021-04-29 14:04:33.489 INFO 12984 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'Nacso-Watch-Task-Scheduler'
2021-04-29 14:04:33.497 INFO 12984 --- [ main] com.alibaba.nacos.client.naming : initializer namespace from System Property :null
2021-04-29 14:04:33.498 INFO 12984 --- [ main] com.alibaba.nacos.client.naming : initializer namespace from System Environment :null
2021-04-29 14:04:33.498 INFO 12984 --- [ main] com.alibaba.nacos.client.naming : initializer namespace from System Property :null
2021-04-29 14:04:34.186 INFO 12984 --- [ main] com.sandul.NacosRefreshApplication : Started NacosRefreshApplication in 4.094 seconds (JVM running for 5.515)
2021-04-29 14:04:34.190 INFO 12984 --- [ main] c.a.n.client.config.impl.ClientWorker : [fixed-192.168.10.161_8848-nacos_refresh_namespace] [subscribe] nacosRefresh-dev.yml+DEFAULT_GROUP+nacos_refresh_namespace
2021-04-29 14:04:34.192 INFO 12984 --- [ main] c.a.nacos.client.config.impl.CacheData : [fixed-192.168.10.161_8848-nacos_refresh_namespace] [add-listener] ok, tenant=nacos_refresh_namespace, dataId=nacosRefresh-dev.yml, group=DEFAULT_GROUP, cnt=1
2021-04-29 14:04:34.192 INFO 12984 --- [ main] c.a.n.client.config.impl.ClientWorker : [fixed-192.168.10.161_8848-nacos_refresh_namespace] [subscribe] nacosRefresh.yml+DEFAULT_GROUP+nacos_refresh_namespace
2021-04-29 14:04:34.192 INFO 12984 --- [ main] c.a.nacos.client.config.impl.CacheData : [fixed-192.168.10.161_8848-nacos_refresh_namespace] [add-listener] ok, tenant=nacos_refresh_namespace, dataId=nacosRefresh.yml, group=DEFAULT_GROUP, cnt=1
2021-04-29 14:04:34.192 INFO 12984 --- [ main] c.a.n.client.config.impl.ClientWorker : [fixed-192.168.10.161_8848-nacos_refresh_namespace] [subscribe] nacosRefresh+DEFAULT_GROUP+nacos_refresh_namespace
2021-04-29 14:04:34.193 INFO 12984 --- [ main] c.a.nacos.client.config.impl.CacheData : [fixed-192.168.10.161_8848-nacos_refresh_namespace] [add-listener] ok, tenant=nacos_refresh_namespace, dataId=nacosRefresh, group=DEFAULT_GROUP, cnt=1
nacos_test
2021-04-29 14:04:34.200 INFO 12984 --- [fresh_namespace] c.a.n.client.config.impl.ClientWorker : get changedGroupKeys:[]
2021-04-29 14:05:03.901 INFO 12984 --- [fresh_namespace] c.a.n.client.config.impl.ClientWorker : get changedGroupKeys:[]
2021-04-29 14:05:33.407 INFO 12984 --- [fresh_namespace] c.a.n.client.config.impl.ClientWorker : get changedGroupKeys:[]
修改nacos上的配置控制台实时刷新打印
nacostest: "nacos_test01"
2021-04-29 14:09:29.443 INFO 12984 --- [fresh_namespace] c.a.n.client.config.impl.ClientWorker : get changedGroupKeys:[]
2021-04-29 14:09:58.964 INFO 12984 --- [fresh_namespace] c.a.n.client.config.impl.ClientWorker : get changedGroupKeys:[]
2021-04-29 14:10:28.094 INFO 12984 --- [fresh_namespace] c.a.n.client.config.impl.ClientWorker : [fixed-192.168.10.161_8848-nacos_refresh_namespace] [polling-resp] config changed. dataId=nacosRefresh-dev.yml, group=DEFAULT_GROUP, tenant=nacos_refresh_namespace
2021-04-29 14:10:28.094 INFO 12984 --- [fresh_namespace] c.a.n.client.config.impl.ClientWorker : get changedGroupKeys:[nacosRefresh-dev.yml+DEFAULT_GROUP+nacos_refresh_namespace]
2021-04-29 14:10:28.110 INFO 12984 --- [fresh_namespace] c.a.n.client.config.impl.ClientWorker : [fixed-192.168.10.161_8848-nacos_refresh_namespace] [data-received] dataId=nacosRefresh-dev.yml, group=DEFAULT_GROUP, tenant=nacos_refresh_namespace, md5=90ca04dd09cd89da15836f6756212172, content=nacostest: "nacos_test01", type=yaml
2021-04-29 14:10:28.111 INFO 12984 --- [fresh_namespace] c.a.nacos.client.config.impl.CacheData : [fixed-192.168.10.161_8848-nacos_refresh_namespace] [notify-context] dataId=nacosRefresh-dev.yml, group=DEFAULT_GROUP, md5=90ca04dd09cd89da15836f6756212172
2021-04-29 14:10:29.387 WARN 12984 --- [fresh_namespace] c.a.c.n.c.NacosPropertySourceBuilder : Ignore the empty nacos configuration and get it based on dataId[nacosRefresh] & group[DEFAULT_GROUP]
2021-04-29 14:10:29.390 WARN 12984 --- [fresh_namespace] c.a.c.n.c.NacosPropertySourceBuilder : Ignore the empty nacos configuration and get it based on dataId[nacosRefresh.yml] & group[DEFAULT_GROUP]
2021-04-29 14:10:29.393 INFO 12984 --- [fresh_namespace] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-nacosRefresh-dev.yml,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-nacosRefresh.yml,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-nacosRefresh,DEFAULT_GROUP'}]
2021-04-29 14:10:29.394 INFO 12984 --- [fresh_namespace] o.s.boot.SpringApplication : The following profiles are active: dev
2021-04-29 14:10:29.406 INFO 12984 --- [fresh_namespace] o.s.boot.SpringApplication : Started application in 1.287 seconds (JVM running for 360.735)
2021-04-29 14:10:29.422 INFO 12984 --- [fresh_namespace] o.s.c.e.event.RefreshEventListener : Refresh keys changed: [nacostest]
2021-04-29 14:10:29.422 INFO 12984 --- [fresh_namespace] c.a.nacos.client.config.impl.CacheData : [fixed-192.168.10.161_8848-nacos_refresh_namespace] [notify-ok] dataId=nacosRefresh-dev.yml, group=DEFAULT_GROUP, md5=90ca04dd09cd89da15836f6756212172, listener=com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1@612cd197
2021-04-29 14:10:29.422 INFO 12984 --- [fresh_namespace] c.a.nacos.client.config.impl.CacheData : [fixed-192.168.10.161_8848-nacos_refresh_namespace] [notify-listener] time cost=1311ms in ClientWorker, dataId=nacosRefresh-dev.yml, group=DEFAULT_GROUP, md5=90ca04dd09cd89da15836f6756212172, listener=com.alibaba.cloud.nacos.refresh.NacosContextRefresher$1@612cd197
2021-04-29 14:10:58.927 INFO 12984 --- [fresh_namespace] c.a.n.client.config.impl.ClientWorker : get changedGroupKeys:[]