Explain spring cloud config in detail, integrate gitlab to build a distributed configuration center
In the previous blogs, we all put the configuration file in their respective services, but there is a disadvantage. Once the configuration is modified, we must stop the machine, and then modify the configuration file before going online. If there are few services, this is understandable, but if there are hundreds of services, at this time, Distributed configuration management is needed. Spring cloud config is used to solve this problem. Next, we will combine gitlab to build a distributed configuration center. The spring cloud config configuration center consists of the server side and the client side,
Premise: create a new configuration file configserver-dev.properties under the project in gitlab
1、 Configure server
1. Add dependency
2. Enable support in the application main class
3. Configure application YML file
Note: if the configuration file is placed in the root directory of the GIT repository, you do not need to use the searchpaths parameter. The configuration file in this example is in the config repo directory, so use the searchpaths parameter to prompt the config server to search the config repo subdirectory
4. Start the server and enter in the browser http://localhost:8888/configserver/dev/master
You can see that the server side can read the configuration file from gitlab. You can access the resources on gitlab in the following form
For example, enter in the browser: http://localhost:8888/configserver -Dev.yml, the results are as follows:
2、 Configure client
1. Add POM dependency
2. Configure bootstrap YML file
Note: the configuration file here needs to be placed in bootstrap Properties or bootstrap In the YML file, because the related configuration of config will precede that of application Properties, and bootstrap Properties are also loaded before application properties
3. Verify client
Add a controller on the client
Access in browser: http://localhost:8889/hello , the results are as follows:
i'm the king of the world!!!
This indicates that the client can get the value from the server.
3、 Dynamic refresh
The configuration managed by spring cloud config can be updated without restarting the client
1. Update the property value corresponding to hello in the configserver-dev.properties configuration file in gitlab warehouse
2. Visit http://localhost:8888/configserver/dev/master , it is found that the server-side content has been updated
3. Send a post request to the conf client http://localhost:8889/refresh , return 200 OK. Visit again http://localhost:8889/hello It can be seen that the property value read has been dynamically updated without restarting the client service
PS: to achieve dynamic refresh, you need to add the following starters to the POM file
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.