In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the example analysis of Nacos configuration management, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
Hard coding
Configuration items exist as class fields, such as:
Public class AppConfig {private int connectTimeoutInMills = 5000; public int getConnectTimeoutInMills () {return connectTimeoutInMills;} public void setConnectTimeoutInMills (int connectTimeoutInMills) {this.connectTimeoutInMills = connectTimeoutInMills;}}
There are three main problems with this form:
If the configuration needs to be dynamically modified, the current application is required to expose the interface that manages the configuration item, up to Controller's API interface or JMX.
In addition, configuration changes occur in memory and are not persisted. Therefore, restart the application after modifying the configuration, and the configuration will change back to the default value in the code. This is a pit, and the author once fell into it and climbed ashore for a long time.
The last problem is that when you have multiple machines, to modify a configuration, each has to be operated again, the cost of operation and maintenance can be imagined, which is extremely painful.
Configuration file
Common properties, yml files, or other custom files in Spring, such as the "conf" suffix:
# application.propertiesconnectTimeoutInMills=5000
Compared to the "hard-coded" form, it solves the second problem and persists the configuration. However, the other two problems have not been solved, and the cost of operation and maintenance is still very high.
Configuration dynamic changes can be done by exposing the management interface in a way similar to "hard coding", in which case the code takes one more step to persist the logic of the new configuration to the file. Or, to be simple and rude, log in directly to the machine to modify the configuration file, and then restart the application to make the configuration take effect. Of course, you can also add a scheduled task to the code, such as reading the contents of the configuration file every 10 seconds, so that the latest configuration can take effect in the application in time, thus eliminating the "heavy" operation of restarting the application.
By adding "persistence logic" and "scheduled tasks", the form of "configuration file" is a small step forward than "hard-coding".
DB configuration table
The DB here can be either a relational database such as MySQL or a non-relational database such as Redis. Data sheets such as:
CREATE TABLE `config` (`id` bigint (20) unsigned NOT NULL AUTO_INCREMENT, `key` varchar (50) NOT NULL DEFAULT''COMMENT' configuration item', `value` varchar (50) NOT NULL DEFAULT''COMMENT' configuration content', `updated_ time`timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `created_ time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE KEY `idx_ key` (`key`) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=' configuration information' INSERT INTO `config` (`key`, `value`, `created_ time`, `created_ time`) VALUES ('connectTimeoutInMills',' 5000, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
Compared with the former two, it takes the configuration out of the application and centralized management, which can greatly reduce the cost of operation and maintenance.
So how can it solve the problem of dynamically updating the configuration? As far as I know, there are two ways.
First, as before, it is solved by exposing the management interface, and of course, you have to increase the persistence logic, except that you used to write the file, but now you write the latest configuration to the database. However, the program also needs to have the task of regularly reading the latest configuration from the database, so that only by calling the management configuration interface of one of the machines can the latest configuration be sent to all the machines in the entire application cluster, so as to achieve the goal of reducing the cost of operation and maintenance.
Second, directly modify the database, the program through the timing task to read the latest configuration content from the database.
The form of the DB configuration table solves the main problem, but it is not elegant enough and brings some "encumbrance".
Nacos configuration management
Nacos really separates the configuration from the application, manages it uniformly, and elegantly solves the problems of dynamic change, persistence, operation and maintenance cost of configuration.
The application itself does not need to add management configuration interfaces, nor does it need to implement configuration persistence, let alone introduce "scheduled tasks" to reduce operation and maintenance costs. The configuration management function provided by Nacos brings together all the logic related to the configuration, and provides an easy-to-use SDK, so that the configuration of the application can be easily managed by Nacos.
If you are using Nacos in Spring, you only need three steps:
Add dependency
Com.alibaba.nacos nacos-spring-context ${latest.version}
Add the @ EnableNacosConfig annotation to enable Nacos Spring's configuration management service. In the following example, we use @ NacosPropertySource to load the configuration source with dataId as example and turn on automatic updates:
@ Configuration@EnableNacosConfig (globalProperties = @ NacosProperties (serverAddr = "127.0.0.1 serverAddr 8848") @ NacosPropertySource (dataId = "example", autoRefreshed = true) public class NacosConfiguration {}
Set the property value through the @ Value annotation of Spring.
Note: you need to have a Setter method at the same time to update automatically when configuration changes.
Public class AppConfig {@ Value ("${connectTimeoutInMills:5000}") private int connectTimeoutInMills; public int getConnectTimeoutInMills () {return connectTimeoutInMills;} public void setConnectTimeoutInMills (int connectTimeoutInMills) {this.connectTimeoutInMills = connectTimeoutInMills }} Thank you for reading this article carefully. I hope the article "sample Analysis of Nacos configuration Management" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.