当前位置:网站首页>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 .
边栏推荐
- Modifying universal render data at runtime
- We media do not know how to realize it? Sharing of 7 major realization methods
- 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.
- Cloud native database vs traditional database
- MATLAB主窗口与编辑器窗口分开为两个界面的解决办法
- 20年ICPC澳门站L - Random Permutation
- DSPACE的性能渲染问题
- AOSP ~ default attribute value
- Is it safe for tonghuashun securities to open an account
- 支付宝被风控7天怎么办?付解决方案
猜你喜欢

AI自己写代码让智能体进化!OpenAI的大模型有“人类思想”那味了

Getting started with unityshader Essentials - PBS physics based rendering

Lihongyi, machine learning 6 Convolutional neural network

高数 | 精通中值定理 解题套路汇总

支付宝被风控7天怎么办?付解决方案

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

Overview of AOSP ~ WiFi architecture

好用的字典-defaultdict

Xiaomi routing R4A Gigabit version installation feed+openwrt tutorial (the full script does not need to be hard modified)

AI writes its own code to let agents evolve! The big model of openai has the flavor of "human thought"
随机推荐
Difference between left join on and join on
Advanced usage of groovy
指南针在上面开户安全吗?靠谱吗?
Summary of knowledge points of computer level III (database) test preparation topics
C语言数组与结构体指针
PyTorch学习笔记(七)------------------ Vision Transformer
Is it safe for tonghuashun securities to open an account
ACL access control of squid proxy server
支付宝被风控7天怎么办?付解决方案
怎么开户打新债 开户是安全的吗
高数 | 精通中值定理 解题套路汇总
How to open an account to open a new bond is it safe to open an account
在Microsoft Exchange Server 2007中安装SSL证书的教程
MySql安裝教程
Please check the list of commonly used software testing tools.
left join on和 join on的区别
XML建模
Overview of AOSP ~ WiFi architecture
Error log format and precautions
Expressing the transformation of two coordinate systems with vectors