Article Source: https://www.cnblogs.com/fengzheng/p/9212155.html
This approach is only appropriate for smaller projects, such as only one or two servers, and configuration files can be modified directly. For example, Spring MVC is deployed as a war package, and you can modify the configuration files in resources directly. If this is the Spring boot project, you will want to refer to an externally editable file, such as a fixed directory, because most of the spring boot is deployed in a jar package and the configuration file in the package cannot be modified directly. If it is a larger project, it is best to use the configuration center, such as Ctrip's Apollo, Consul and so on.
Original Way
The original means is to repackage the release or restart the server each time you want to modify the configuration.
Let's say we develop with spring MVC, and after the development is complete, we deploy the war package to Tomcat, and if we modify a message interface address at this point.
We will do the following:
1, open the configuration file, modify the configuration information;
2. Compile and package;
3, stop Tomcat, delete the old project directory;
4. Put the new war package into the WebApps and start Tomcat.
Of course, you can find the configuration file for this project directly in Tomcat and then modify it, but you also need to restart Tomcat.
If you just do the development or testing, except a little waste of time, of course, can accept. So, you don't want to waste time and don't want to restart Tomcat, is there any way. This is the way this article is introduced.
Watchservice Way
Java provides the Watchservice interface, which uses the operating system's own file monitor to monitor directories and files, and when monitored objects change, there will be a signal notification, which can efficiently detect changes.
The general principle of this approach: first according to the operating system new a monitor (Watchservice), and then select the configuration file to monitor the directory or file, and then subscribe to monitor the event, such as Create, delete, edit, and finally registered to the monitored location of the monitor. Once the event that corresponds to our subscription is triggered, the appropriate logic is executed.
First on the code, this is in a spring MVC project, monitoring the resources directory.
@RepositoryPublicClass Configwatcher {PrivateStaticFinal Logger Logger = loggerfactory.GetLogger (Configwatcher.Class);PrivateStatic Watchservice Watchservice;@PostConstructPublic void Init() {logger.Info"Boot Profile Monitor");try {watchservice = filesystems.Getdefault ().Newwatchservice (); URL url = configwatcher.Class.GetResource ("/"); Path PATH = Paths.Get (URL.Touri ()); Path.Register (Watchservice, standardwatcheventkinds.Entry_modify, Standardwatcheventkinds.Entry_create); }catch (Exception E1) {E1.Printstacktrace (); }/*** Start the Monitoring thread*/Thread Watchthread =New Thread (NewWatchthread ()); Watchthread.Setdaemon (true); Watchthread.Start ();/** Register Close Hook */Thread hook =New Thread (New Runnable () {@OverridePublic void Run() {try {Watchservice.Close (); }catch (IOException e) {e.Printstacktrace (); } } }); Runtime.GetRuntime ().Addshutdownhook (Hook); }PublicClass WatchthreadImplements Runnable {@OverridePublic void Run() {while (True) {try {//try to get changes to the monitoring pool, and if not, wait for Watchkey Watchkey = Watchservice. take (); for (watchevent<?> event:watchkey.tostring (); logger. info (editfilename); /** * Reload configuration */} Watchkey.//complete monitoring requires resetting the monitor once} printstacktrace ();}}} }}
The code is very simple, it is understood at a glance, when the project starts, with Filesystems.getdefault (). Newwatchservice () creates a watchservice, which is based on the operating system. It then obtains the URL of the resources directory, obtains the path, and then calls the Register method of the Path object, registers the monitor, subscribes to edit and create events. The event is defined in the Standardwatcheventkinds class, and there are four types:
1, Standardwatcheventkinds#overflow
2, Standardwatcheventkinds#entry_create
3, Standardwatcheventkinds#entry_delete
4, Standardwatcheventkinds#entry_modify
Then a separate Watchthread thread is started to handle the change logic, and the Take () method is called in a while infinite loop until a change occurs, and once the configuration file that we monitor has changed, call our logic to reload the configuration. In addition, each time a change occurs, the Watchkey.reset () method is called to reset the monitor.
Finally, a hook is registered and the monitor can be turned off when the JVM shuts down.
In this way, when we have some configuration changes, we can modify the configuration file directly under Tomcat, without restarting it can take effect.
This article mainly introduces this way, it also said that this method is only suitable for very simple projects, for large projects, you need to use a more advanced way.
How to configure the center
When the complexity of the project becomes higher, the configuration changes in real-time, grayscale publishing, sub-environment, sub-cluster management configuration, perfect authority, audit mechanism may become the project to consider the problem, this time, simply rely on the configuration file will appear to be inadequate.
At present, compared with the more configuration center has ETCD, zookeeper, disconf, Apollo and so on. Disconf, Apollo, are all for use, perfect functionality, and a matching UI. and ETCD and zookeeper need some custom development.
A simple and dynamic way to modify configuration in a Java project takes effect immediately watchservice