当前位置:网站首页>Nacos source code - Interface read configuration
Nacos source code - Interface read configuration
2022-06-24 11:16:00 【prepared】
Read configuration process
1、 The entrance ConfigController::getConfig() In the method ;
2、 Get read lock , If successful , Go to the next step , Otherwise return to failure ( Less than indicates writing ( Write lock ), be equal to 0 Indicates that there is no configuration data , Because the read lock can be acquired repeatedly );
3、 according to groupKey Get the basic information in the cache (isBeta,configType Etc );
4、 According to the deployment mode ( whether standalone)、 Whether the database used is a built-in database derby, To determine whether to read the database or the file ;
5、 Return to content Information ( If it is to check the database ), Or file stream content .
Something to watch out for
1、 If it is standalone Deploy , And it uses a built-in database derby, Directly query the data in the database ,cache(ConcurrentHashMap); otherwise , Read nacos File system (nacos/distribution/target/nacos-server-${version}/nacos/data/config-data/${GROUP_NAME}/${dataId}) The data of .
2、 Before getting the configuration , Need to get == Read the lock ==
3、 adopt ConcurrentHashMap Cache the basic information of the configuration file , Include :groupKey,md5,lastModifiedTs
// Determines whether to read the data directly
// if use mysql, Reduce database read pressure
// if use raft+derby, Reduce leader read pressure
public static boolean isDirectRead() {
return EnvUtil.getStandaloneMode() && isEmbeddedStorage();
}Why? nacos Get offline , You can also get the configuration ?
Because when the service starts , Will visit the registry nacos, Update the configuration to the environment variable , Can open endpoints
management.endpoints.web.exposure.include=* management.endpoint.health.show-details=always
And then visit :http://localhost:8080/actuator/env, You can view the configuration data in the environment variable .
What is worth learning
The realization of read-write lock
There is no particularly profound content , Use synchronized Keyword decorated to acquire lock 、 Method of releasing the lock .
Read locks can be acquired multiple times , adopt int Type of status Incrementally implement multiple lock reads ;
Only one write lock , Mark status by -1.
Be careful : The lock needs to be released manually .
/**
* Simplest read-write lock implementation. Requires locking and unlocking must be called in pairs.
*
* @author Nacos
*/
public class SimpleReadWriteLock {
/**
* Try read lock.
*/
public synchronized boolean tryReadLock() {
if (isWriteLocked()) {
return false;
} else {
status++;
return true;
}
}
/**
* Release the read lock.
*/
public synchronized void releaseReadLock() {
status--;
}
/**
* Try write lock.
*/
public synchronized boolean tryWriteLock() {
if (!isFree()) {
return false;
} else {
status = -1;
return true;
}
}
public synchronized void releaseWriteLock() {
status = 0;
}
private boolean isWriteLocked() {
return status < 0;
}
private boolean isFree() {
return status == 0;
}
/**
* Zero means no lock; Negative Numbers mean write locks; Positive Numbers mean read locks, and the numeric value
* represents the number of read locks.
*/
private int status = 0;
}边栏推荐
- ≥ 2012r2 configure IIS FTP
- Redis
- A fault record of misoperation dhclient script
- 【206】使用php语言去生成go语言的代码
- Investing in a good navigation framework from the beginning of the jetpack compose tutorial will help you save a lot of migration work later
- "Write once, run at all ends", Qualcomm released AI software stack!
- How to use data analysis tools to deal with emergencies in retail industry
- [深度学习][pytorch][原创]crnn在高版本pytorch上训练loss为nan解决办法
- Fais ce que tu veux.
- Maui's way of learning -- Opening
猜你喜欢

"One good programmer is worth five ordinary programmers!"

Understanding of homogeneous coordinates

Rising bubble canvas breaking animation JS special effect

Today in history: Turing's birth day; The birth of the founder of the Internet; Reddit goes online

喜欢就去行动

PHP短信通知+语音播报自动双呼

Déplacer Tencent sur le cloud a guéri leur anxiété technologique

Use the process monitor tool to monitor process operations on registries and files

Today's sleep quality record 76 points

Simple pricelist style code
随机推荐
TP-LINK 1208 router tutorial (2)
Anonymous Messenger: hidden communication of Trojan horse
H5 video conference, camera monitoring, web streaming and live broadcast integration scheme
Maui的学习之路 -- 开篇
System design: key features of distributed systems
Fais ce que tu veux.
Go basic series | 4 Environment construction (Supplement) - gomod doubts
Multi gate mixture of experts and code implementation
今日睡眠质量记录76分
为什么虚拟机ping的通主机,主机ping不通虚拟机
如何开发短信通知和语音功能医院信息系统(HIS系统)
Cloud vendor secondary virtualization restrictions
A method of generating non repeated numbers in nodejs
Example of PHP observer mode [useful in the laravel framework]
程序员大部分时间不是写代码,而是。。。
It's so difficult for me. Have you met these interview questions?
Redis
"Adobe international certification" Adobe Photoshop adjusts cropping, rotation and canvas size
Many of my friends asked me what books and online classes I recommended. This time, I contributed all the materials that I had been hiding for a long time (Part 1)
Canvas infinite scan JS special effect code