当前位置:网站首页>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是配置权重的,数字越大,权重越大。
边栏推荐
- [STL source code analysis] configurator (to be supplemented)
- Advanced usage of groovy
- AOSP ~ default attribute value
- Software testing weekly (issue 77): giving up once will breed the habit of giving up, and the problems that could have been solved will become insoluble.
- It is said that Yijia will soon update the product line of TWS earplugs, smart watches and bracelets
- 20年ICPC澳门站L - Random Permutation
- 爱
- Is it safe to open an account in the way of winning 100% of the new bonds
- I've been doing software testing for two years. I'd like to give some advice to girls who are still hesitating
- Centos7.3 modifying MySQL default password_ Explain centos7 modifying the password of the specified user in MySQL
猜你喜欢

分布式事务解决方案和代码落地

Once beego failed to find bee after passing the go get command Exe's pit

How transformers Roberta adds tokens

random list随机生成不重复数

Copilot免费时代结束!学生党和热门开源项目维护者可白嫖

Getting started with unityshader Essentials - PBS physics based rendering

UnityShader入门精要——表面着色器

After reciting the eight part essay, I won the hemp in June

automated testing

leecode学习笔记-机器人走到终点的最短路径
随机推荐
AOSP ~ default attribute value
高速缓存Cache详解(西电考研向)
Introduction to CUDA Programming minimalist tutorial
Expressing the transformation of two coordinate systems with vectors
Difference between left join on and join on
使用ShaderGraph制作边缘融合粒子Shader的启示
ERROR日志格式与注意点
[STL source code analysis] configurator (to be supplemented)
MATLAB主窗口与编辑器窗口分开为两个界面的解决办法
Refresh mechanism of vie
对进程内存的实践和思考
Migrate Oracle database from windows system to Linux Oracle RAC cluster environment (2) -- convert database to cluster mode
It is said that Yijia will soon update the product line of TWS earplugs, smart watches and bracelets
Array - fast and slow pointer in one breath
DSPACE的性能渲染问题
Insurance can also be bought together? Four risks that individuals can pool enough people to buy Medical Insurance in groups
计算机三级(数据库)备考题目知识点总结
[live review] battle code pioneer phase 7: how third-party application developers contribute to open source
3 years of testing experience. I don't even understand what I really need on my resume. I need 20K to open my mouth?
Is it safe to open an account in the way of winning 100% of the new bonds