directory
One, foreword
2. Plan summary
Three, based on the implementation of scheduled tasks
1. Based on traditional database (MySQL)
2. Distributed database (Redis)
3. Comparison of schemes
Four, practice
One, foreword
The unified configuration center was mentioned earlier in the article “How to implement Log4j to change logging levels online”. Presumably many students in Internet companies have used the unified configuration center. Large companies tend to develop their own unified configuration center, and small companies tend to use open source. At present, there are many open source unified configuration centers, such as Spring-Cloud-Config, CTRIP Apollo, Disconf and so on. However, this series of articles is not intended to introduce or compare these open source projects, but rather to discuss some ideas and solutions for unified configuration center implementation.
PS: In a small cluster (say, 10 or so machines), it is better to implement a simplified version of the unified configuration center yourself. Convenient maintenance, management, can be customized.
2. Plan summary
In fact, the unified configuration center of general industry is nothing more than the following several kinds of implementation plan.
- Implementation based on scheduled tasks (MySQL or Redis)
- Zookeeper based implementation (such as disconf)
- Implementation based on long polling (Netty or Servlet3.0)
- Implementation based on Apache Ignite (Distributed cache)
Today we will introduce the simplest of these: a scheduled task-based implementation.
Three, based on the implementation of scheduled tasks
The idea of this scheme is relatively simple, there are two implementation schemes according to different media: based on traditional database implementation (as far as possible not high concurrency), based on distributed database (support high concurrency, in fact, there is no need for scheduled tasks);
1. Based on traditional database (MySQL)
This is almost a very, very simple implementation. It does not depend on any three-party components (of course, it needs to rely on storage media such as MySQL). It directly starts a scheduled task on each machine (such as executing it every 5 seconds, which can be configured according to the actual situation), and then continuously pulls the new version data configured in the database and writes it into the local memory. At the same time, the business code only needs to pull the corresponding configuration data from local memory. The specific implementation flow chart is as follows:
To avoid pulling all configurations each time and comparing them with the local cache, you are advised to add a new version number to the configuration item and pull the latest version number each time
2. Based on distributed database (Redis)
After the introduction of Redis, the implementation is relatively simple, because Redis can support high concurrency, so we can directly put the configuration in Redis, add, delete and modify the data in Redis directly. At the same time, the machine node can read the configuration data directly from Redis every time without local cache. The realization flow chart is as follows:
This solution does not require a version number.
Of course, if you want to relieve the pressure of Redis, you can also use a scheduled task to pull the new configuration data from Redis to local memory, and then the business can read directly from local memory. The flow chart of the method is as follows:
3. Comparison of schemes
The first solution: no need to introduce other three components, directly in our Web project can be coded implementation. Deployment and maintenance are very simple, but need to implement their own Timer to get the configuration data from the database, and then update to the local cache, development is relatively complex.
The second solution: the third party component Redis needs to be introduced. The deployment and maintenance is relatively troublesome, but the code implementation is relatively simple. (If it is the Timer scheme, it is the most complex implementation.)
Four, practice
If you have any questions or comments about this article, please add an official account to discuss it. (Add an official account to get 10GB video and graphic materials on “Java Advanced Architecture”.)