当前位置:网站首页>nacos实践记录
nacos实践记录
2022-06-24 23:59:00 【快点到周五】
依赖版本如下:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2021.0.1.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2021.0.1.0</version>
<exclusions>
<exclusion>
<groupId>springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</exclusion>
</exclusions>
</dependency>
配置管理
配置文件
使用nacos配置管理,需要新建一个bootstrap.properties(yaml)配置文件,如果@Value还是取不到值,可能是因为项目没有识别到bootstrap.properties文件,再加上依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.1.0</version>
</dependency>
取值方式
@Value(“${minutes:61}”) 冒号后面的值可以当作默认值,也就是当取不到配置值的时候就会使用。我是用@NacosValue就一直取不到值的,但是用@Value就可以,不知道是不是版本问题。
配置参数的时候,不要将参数属性设置为静态的,然后在set方法上加上@Value注解,虽然这样是可以取到值的,但是在配置值刷新的时候会有问题。就还是采用 @Value注解放在属性上,通过使用get方法来获取的方式来获取配置值。
在bootstrap.properties中,如果没有指定spring.application.name,也就是对应${prefix}(默认是spring.application.name的值);我原本以为在applicaiton.properties中指定了spring.application.name,而且启动的时候也确实识别出来了,那在bootstrap.properties中不写应该是可以的,但是启动之后是可以取到配置值的,但是在配置值刷新的时候,后台就不会接收到配置,所以也不能不写。
现象如下,我在控制台中已经修改了两个参数的值,但是就是没有拉取到:
命名空间如果是使用public的话可以用不写,或者写public,但是如果是使用新建的命名空间,在配置文件中就必须使用创建之后生成的随机字符串,不然识别不出来。
配置动态刷新
在两个打印方法的上面分别加上@PostConstruct @PreDestrtoy注解。
@PostConstruct是在构造函数之后执行的;
@PreDestrtoy则是在Bean销毁之前执行的
在启动项目的时候,可以看见执行了init方法,这个时候day的值是day2217,week的值是week2217;
然后在控制台修改一下值;这个时候后台会有信息打印出来:
可以看到调用了destory方法,但是这个时候打印出来的值还是修改以前的,并且也可以知道是哪些配置值做了修改;
接着在页面调用访问配置值的方法,后台会继续打印信息:
有调用了一遍init方法,这个时候打印出来的值才是修改过后的值。
在使用@RefreshScope注解监听到配置发生了变化之后,会先销毁对象,然后在要使用到配置属性的时候再去初始化对象。
也就是在配置发生变化的时候是会去调用对象的生命周期的方法。
在项目中加入一段代码:
@Bean
public ApplicationRunner runner() {
return args -> {
String dataId = "one-provider.properties";
String group = "testGroup1";
manager.getConfigService().addListener(dataId, group, new AbstractListener() {
@Override
public void receiveConfigInfo(String s) {
log.info("监听到的内容是:{}", s);
}
});
};
}
然后重新启动之后,再修改一下配置值,后台打印如下:
自定义的监听器在配置发生变化之后监听到的是一个字符串,这个字符串包含的是我在这个dataId和group里配置的所有配置。获取到这个配置之后就可以将这个字符串的内容转换为对象,@RefreshScope的作用应该就和这个过程是类似的。
在nacos中可以直接用spring.cloud.nacos.config.name来指定dataId,也就说在bootstrap.properties中写spring.application.name和spring.cloud.nacos.config.file-extension,以及spring.profiles.active。直接用spring.cloud.nacos.config.name就行。
因为在不同的namespace中,dataId是可以相同的,所以可以用namespace来区分不同的环境,开发环境,测试环境,生产环境。
服务发现
spring.cloud.n acos.discover y.service配置是用来配置服务名称的,默认值是spring.application.name配置的值,
spring.cloud.nacos.discovery.weight是配置权重的,数字越大,权重越大。
边栏推荐
- VSCode中如何实现点击DOM自动定位到相应代码行
- E - Average and Median(二分)
- How to click DOM to automatically locate the corresponding code line in vscode
- Computer wechat user picture decoded into picture in DAT format (TK version)
- Is it safe to open an account by fraud
- Planification du réseau | [quatre couches de réseau] points de connaissance et exemples
- 李宏毅《机器学习》丨6. Convolutional Neural Network(卷积神经网络)
- F - spices (linear basis)
- UnityShader入门精要——表面着色器
- [i.mx6ul] u-boot migration (VI) network driver modification lan8720a
猜你喜欢
When they are in private, they have a sense of propriety
Once beego failed to find bee after passing the go get command Exe's pit
14 bs对象.节点名称.name attrs string 获取节点名称 属性 内容
Unity存档系统——Json格式的文件
20 years ICPC Macau station L - random permutation
Leetcode 210: curriculum II (topological sorting)
計網 | 【四 網絡層】知識點及例題
记一次beego通过go get命令后找不到bee.exe的坑
left join on和 join on的区别
[STL source code analysis] configurator (to be supplemented)
随机推荐
把 Oracle 数据库从 Windows 系统迁移到 Linux Oracle Rac 集群环境(2)——将数据库转换为集群模式
20年ICPC澳门站L - Random Permutation
ERROR日志格式与注意点
How to open an account to open a new bond is it safe to open an account
[I.MX6UL] U-Boot移植(六) 网络驱动修改 LAN8720A
I've been doing software testing for two years. I'd like to give some advice to girls who are still hesitating
DSPACE的性能渲染问题
Detailed explanation of cache (for the postgraduate entrance examination of XD)
好用的字典-defaultdict
数组-一口气冲完快慢指针
分布式事务解决方案和代码落地
打新债是不是骗局 开户是安全的吗
CUDA编程入门极简教程
计算机三级(数据库)备考题目知识点总结
LeetCode 210:课程表 II (拓扑排序)
DSPACE设置斑马线和道路箭头
用向量表示两个坐标系的变换
请问polarDB数据库可以通过mysql进行数据源连接吗
Is it safe to open an account in the way of winning 100% of the new bonds
Can automate - 10k, can automate - 20K, do you understand automated testing?