Article Directory
Unified configuration management
Configured CURD
Nacos Config offers four operation for configuration management, configuration methods exist in the com.alibaba.nacos.api.config.ConfigService
middle.
- Get configuration: read configuration from Nacos Config Server
String getConfig(String dataId, String group, long timeoutMs) throws NacosException;
- Monitoring configuration: subscribe to the configuration of monitoring, and you will receive event notifications when the configuration changes
void addListener(String dataId, String group, Listener listener) throws NacosException;
- Publish configuration: save the configuration to Nacos Config Server
boolean publishConfig(String dataId, String group, String content) throws NacosException;
- Delete configuration: delete the specified configuration in the configuration center
boolean removeConfig(String dataId, String group) throws NacosException;
Dynamic monitoring
When the configuration on the Nacos Config Server changes, the related service or application needs to be notified of the change. Usually there are two ways of data exchange between client and server: pull
and push
. The former means that the client actively pulls data from the server, and the latter means that the server actively pushes data to the client.
For the pull
client, the client needs to pull data from the server regularly. Since the timing task will have a certain time interval, the real-time nature of the data cannot be guaranteed. Moreover, when the server configuration is not updated for a long time, the client's timing tasks will do some useless pull operations, which is a waste of resources.
For push
, the server needs to maintain a long connection with the client. If the number of clients is large, the server needs to consume a lot of memory resources to save each connection, and in order to check the validity of the connection, it also needs a heartbeat mechanism to maintain each connection. status.
Nacos uses a pull
pattern, a length in rotation mechanism , combining the advantages of both modes. The client in rotation with a long way to regularly send pull
a request to check whether the configuration of the server is changed, if a change occurs, the client will be configured according to the latest data changes. If there is no change, the server will hold the request first , that is , the server will (长连接会话超时时间默认为30S)
not return the result within the specified time period after receiving the request , until the configuration changes during this period, the server will carry out the original Hold request return.
After receiving the request, the Nacos server first checks whether the configuration has changed. If not, then set a timed task, delay 29.5S execution, and add the current client long-round training connection to the AllSubs
queue. At this time, there are two ways to trigger The connection result is returned.
- After waiting for 29.5S (long connection retention time), the automatic check mechanism is triggered, and the result is returned to the client regardless of the configuration change.
- At any time during the 29.5S long connection retention time, if the configuration is modified through Nacos DashBoard or API, it will trigger the update of the configuration event. The task that listens to this time will traverse the
AllSubs
queue and find the corresponding configuration item that has changed. TheClientLongPolling
task of returning the changed data through the connection of the task to complete a push operation.