nacos动态刷新配置

nacos动态刷新配置

pom依赖

<groupId>com.sandul</groupId>
    <artifactId>refresh-of-nacos</artifactId>
    <version>1.0-SNAPSHOT</version>
    <description>nacos动态刷新练习</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.11.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
                <version>2.2.11.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                <version>2.1.2.RELEASE</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        </dependency>
        <!--nacos服务配置中心-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <!--nacos服务注册中心-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

application.yml

server:
  port: 11024

bootstrap.yml

spring:
  application:
    name: nacosRefresh
  cloud:
    nacos:
#      配置中心
      config:
#        ip地址
        server-addr: http://192.168.10.161:8848
#        配置文件类型
        file-extension: yml
#        nacos上配置的命名空间
        namespace: nacos_refresh_namespace
#        是否开启动态刷新
        refresh-enabled: true
#       注册中心
      discovery:
#        ip地址
        server-addr: http://192.168.10.161:8848
#        是否开启注册服务(默认true)
        register-enabled: true
#     代码版本类型:test:测试;dev:开发;pro
  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上的配置控制台实时刷新打印

# 这个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:[]
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值