Reading configuration Process
1, entrance in ConfigController: : getConfig () method.
2. Obtain the read lock. If the read lock is obtained successfully, go to the next step; otherwise, failure is returned.
3. Obtain basic information (isBeta, configType, etc.) from the cache according to groupKey;
4. Determine whether to read the database or file based on the standalone deployment mode and whether the database is a built-in database Derby.
Return the content information in the database (if the database is queried), or the content of the file stream.
Points to be aware of
1. If the standalone deployment is in use and Derby is a built-in database, the data in the database is queried directly, the cache (ConcurrentHashMap); Otherwise, Read nacos file system (nacos/distribution/target/nacos – server – Version/nacos/data/config – data / {version} / nacos/data/config – data/version/nacos/data/config – data / / ${GROUP_NAME} {dataId}) number According to.
2. Obtain the == read lock == before obtaining the configuration
3. Use ConcurrentHashMap to cache basic information of the configuration file, including groupKey, MD5, and 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(a) {
return EnvUtil.getStandaloneMode() && isEmbeddedStorage();
}
Copy the code
Why is nacOS offline and still available for configuration?
Since service startup accesses the registry NACOS and updates the configuration to environment variables, endpoints can be turned on
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
Copy the code
Then visit: http://localhost:8080/actuator/env, you can see the configuration data in the environment variable.
Something to learn from
Implementation of read/write locks
The synchronized keyword is used to modify the method of obtaining and releasing a lock.
The read lock can be acquired multiple times, which is achieved by incrementing the status of int.
There is only one write lock, marked status -1.
Note: 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(a) {
if (isWriteLocked()) {
return false;
} else {
status++;
return true; }}/** * Release the read lock. */
public synchronized void releaseReadLock(a) {
status--;
}
/** * Try write lock. */
public synchronized boolean tryWriteLock(a) {
if(! isFree()) {return false;
} else {
status = -1;
return true; }}public synchronized void releaseWriteLock(a) {
status = 0;
}
private boolean isWriteLocked(a) {
return status < 0;
}
private boolean isFree(a) {
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;
}
Copy the code