当前位置:网站首页>Nacos practice record
Nacos practice record
2022-06-25 03:30:00 【Hurry to Friday】
The dependent versions are as follows :
<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>
Configuration Management
The configuration file
Use nacos Configuration Management , Need to create a new one bootstrap.properties(yaml) The configuration file , If @Value Still can't get the value , It may be that the project did not recognize bootstrap.properties file , Plus dependence :
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.1.0</version>
</dependency>
Value method
@Value(“${minutes:61}”) The value after the colon can be used as the default value , That is, it will be used when the configuration value cannot be obtained . I use @NacosValue You can't get the value all the time , But with @Value Can , I don't know if it's a version problem .
When configuring parameters , Do not set parameter properties to static , And then in set Method plus @Value annotation , Although it is possible to get the value , But there will be problems when the configuration value is refreshed . Just adopt @Value Annotations on attributes , By using get Method to obtain the configuration value .
stay bootstrap.properties in , If not specified spring.application.name, That's the correspondence ${prefix}( The default is spring.application.name Value ); I thought I was applicaiton.properties Specified in spring.application.name, And I did recognize it when I started it , That's in bootstrap.properties It should be possible not to write in , But the configuration value can be obtained after startup , But when the configuration value is refreshed , The background will not receive the configuration , So I can't help writing .
The phenomenon is as follows , I have modified the values of two parameters in the console , But I didn't pull it :

If the namespace is used public Can be used without writing , Or write public, But if you are using a new namespace , The random string generated after creation must be used in the configuration file , Otherwise I can't recognize .
Configure dynamic refresh
Add... To the top of the two printing methods respectively @PostConstruct @PreDestrtoy annotation .
@PostConstruct Is executed after the constructor ;
@PreDestrtoy It is in Bean Before destruction

At the start of the project , You can see the execution init Method , This is the time day The value of is day2217,week The value of is week2217;
Then change the value on the console ; At this time, information will be printed out in the background :

You can see the call destory Method , But the value printed out at this time is still the same as before , You can also know which configuration values have been modified ;
Then call the method to access the configuration value on the page , The background will continue to print information :

There was a call init Method , At this time, the printed value is the modified value .
In the use of @RefreshScope After the annotation listens to the configuration changes , The object will be destroyed first , Then initialize the object when you want to use the configuration attribute .
That is, when the configuration changes, it will call the methods of the object's life cycle .
Add a piece of code to the project :
@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(" The monitored content is :{}", s);
}
});
};
}
And then after the restart , Modify the configuration value again , Background printing is as follows :

The custom listener listens to a string after the configuration changes , This string contains my dataId and group All configurations configured in . After obtaining this configuration, you can convert the contents of this string into objects ,@RefreshScope The function of should be similar to this process .
stay nacos Can be used directly in spring.cloud.nacos.config.name To specify the dataId, That is to say bootstrap.properties Write in spring.application.name and spring.cloud.nacos.config.file-extension, as well as spring.profiles.active. Direct use spring.cloud.nacos.config.name Just go .
Because in different namespace in ,dataId It can be the same , So it can be used namespace To distinguish between different environments , development environment , Test environment , Production environment .
Service discovery
spring.cloud.n acos.discover y.service Configuration is used to configure the service name , The default value is spring.application.name The value of the configuration ,
spring.cloud.nacos.discovery.weight It is used to configure the weight , The greater the number , The greater the weight .
边栏推荐
- 在线股票开户安全吗?
- Jetson nano from introduction to practice (cases: opencv configuration, face detection, QR code detection)
- ACL access control of squid proxy server
- Use xxl-job to customize tasks and schedule them
- Is it safe to open an account by fraud
- 在Microsoft Exchange Server 2007中安装SSL证书的教程
- Doak CMS article management system recommendation
- 记一次beego通过go get命令后找不到bee.exe的坑
- UnityShader入门精要——PBS基于物理的渲染
- Expressing the transformation of two coordinate systems with vectors
猜你喜欢

好用的字典-defaultdict
![[proteus simulation] Arduino uno+ nixie tube display 4X4 keyboard matrix keys](/img/80/c97410c88856479e6be9de67936790.png)
[proteus simulation] Arduino uno+ nixie tube display 4X4 keyboard matrix keys

20年ICPC澳门站L - Random Permutation

Advanced mathematics | proficient in mean value theorem problem solving routines summary

UnityShader入门精要——PBS基于物理的渲染

使用XXL-JOB自定义任务并调度

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.

Introduction to CUDA Programming minimalist tutorial

Leecode learning notes - the shortest path for a robot to reach its destination

What if Alipay is controlled by risk for 7 days? Payment solution
随机推荐
请问polarDB数据库可以通过mysql进行数据源连接吗
股票开户,在手机上开户安全吗?
Gold medal scheme of kaggle patent matching competition post competition summary
Charles 抓包工具
DSPACE设置斑马线和道路箭头
SkyWalking 实现跨线程 Trace 传递
软件测试周刊(第77期):只要放弃一次,就会滋生放弃的习性, 原本可以解决的问题也会变得无法解决。
C#实现水晶报表绑定数据并实现打印
Solution of separating matlab main window and editor window into two interfaces
[FPGA] serial port controls temperature acquisition by command
[i.mx6ul] u-boot migration (VI) network driver modification lan8720a
MySQL根据表前缀批量修改、删除表
Leecode learning notes - the shortest path for a robot to reach its destination
@PostConstruct
Leetcode 210: curriculum II (topological sorting)
Is flush a regular platform? Is it safe for flush to open an account
打新债100%中签的方法 开户是安全的吗
C语言数组与结构体指针
自动化测试
微信小程序获取扫描二维码后携带的参数