当前位置:网站首页>Nacos source code - Interface read configuration

Nacos source code - Interface read configuration

2022-06-24 11:16:00 prepared

Read configuration process

Read the configuration information flow chart

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;
}
原网站

版权声明
本文为[prepared]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/06/20210604151113071g.html