Home  >  Article  >  Java  >  Introduction to four methods for handling parameter configuration in distributed systems

Introduction to four methods for handling parameter configuration in distributed systems

不言
不言forward
2018-10-22 14:37:052248browse

This article brings you an introduction to the four methods of processing parameter configuration in distributed systems. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

A system contains various configuration information. For example, a log file needs to configure the following information.

  • Main directory for log file generation

  • Log file name, different log levels correspond to different files

  • Current log level

There are also various other business parameters, system parameters, etc. Most single systems directly write these configurations in the configuration file. When deployed to test, In the production environment, modify the configuration file. This is easy to make mistakes and cannot be modified flexibly. Also, after the system becomes a distributed system, there are more and more subsystems, and it becomes more and more difficult for you to maintain these configurations.

I think you must be able to solve at least the following problems to be considered excellent!

1) Ability to flexibly modify configurations online

2) Ability to dynamically refresh configurations online

3) Ability to configure configurations according to different environments

4) Can manage and maintain configurations in a unified manner

So how to flexibly maintain these configurations? I have summarized several methods for you, and you can refer to them according to different application fields.

1. Database method

Store all parameters in the database and load them into memory when the system starts.

This implementation method is relatively simple, but it requires database resources. This method can be used when the system is simple and the pressure is low.

2. Packaging processing method

Use Maven's maven-resources-plugin plug-in, and then provide configuration files for different environments according to different environments (Profile), so, The configuration information for different environments is determined during the packaging stage.

This only solves the problem of configurations on different operating systems and cannot be flexibly and dynamically modified. Each update can only repackage or modify the configuration file online, and the information is also difficult to synchronize. It is okay if there are few projects, but there are many projects. Up, the configuration needs to be changed frequently, which becomes extremely cumbersome.

3. Environment variable method

You can set the property value to the environment variable, and then read it and set it to the Java system property. This can achieve the purpose of distinguishing different environments, but it still cannot dynamically update the configuration, and configuring and maintaining environment variables is quite troublesome, and it is a very troublesome problem in distributed systems.

// 读取环境变量
java.lang.System#getenv(java.lang.String)

// 设置系统属性
java.lang.System#setProperty

In this method, you can refer to some global system configurations, such as logs, caches, temporary directories, etc. Mainstream logging systems support reading configurations from system properties. Some other configurations are not recommended to be stored in environment variables.

4. Configuration center method

1) Currently, most distributed configuration centers are implemented based on Zookeeper. Spring Cloud has its own configuration center components, which all support online dynamic updates and Refresh configuration.

2) Directly store the configuration in the database. If the system concurrency is small or it is a management system, you can refer to it. For high-concurrency applications, it is not recommended to use the database as the configuration center. After all, it will bring access pressure and implement Dynamically updating the configuration is also more complicated.

Summary

These are the 4 configuration methods we currently apply. Obviously, the configuration center is the best solution and also solves the above problems, but it needs to rely on middleware and its high availability.

The above is the detailed content of Introduction to four methods for handling parameter configuration in distributed systems. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete