SpringCloud Config
- SpringCloud integrates the overall solution in microservices: distributed configuration center, distributed lock, distributed task scheduling platform, distributed transaction, and distributed log collection
- Background: If you use the traditional way to manage configuration files in microservices, the configuration file manager will be very complicated; In a production environment, when the configuration file changes, the WAR package needs to be reconfigured to re-read the configuration file information into the JVM
- SpringCloud Config Distributed Configuration Center:
- Use the same server in microservices to manage all service profile information
- If the configuration file is changed when the server is running, you can change the configuration file in real time without restarting the server
The real-time refresh of Config configuration files is not equivalent to hot deployment. The underlying implementation of hot deployment is actually a server restart, which is not suitable for production environments but only for local development testsCopy the code
The Config architecture
- When a configuration file in a system is changed, you need to restart the service for the configuration file to take effect
- SpringCloud Config enables unified management of configuration files of all systems in microservices, and automatically updates and obtains new configurations when configuration files change
Distributed configuration central framework
- Apollo: Ctrip’s distributed configuration center framework, with a graphical interface to manage configuration file information, configuration file information stored in the database
- SpringCloud Config: SpringCloud has its own distributed configuration center framework. There is no distributed configuration center that can be managed in the background. Configuration file information is stored in the version controller, such as Git and SVN
Zookeeper implements a distributed configuration center, using persistent nodes + event notificationCopy the code
Principles of distributed configuration center design
Design components for a distributed configuration center
- Web management system: Configuration files can be managed using a graphical interface in the Web background (SpringCloud Config has no graphical management component)
- Server for storing distributed configuration files:
- Use the database to store configuration files :Apollo
- Use a version controller (Git, SVN, etc.) to store configuration file information :SpringCloud Config
- ConfigServer: a server that caches configuration files (used to cache configuration file information on git servers)
- ConfigClient: Reads information about the ConfigServer configuration file
Server for storing distributed configuration files: Server for persistent storage ConfigServer: server for storing temporary cacheCopy the code
Principle of distributed configuration center
- The user submits the configuration file information to the version controller :git/SVN server
- ConfigServer caches configuration file information obtained from git server
- The ConfigClient obtains configuration file information from the ConfigServer
Create a Git environment to persist configuration file information. 1. Create a project. 2. Set up the ConfigServer environment by storing the configuration file in git folder 1. Introduce the configServer dependency on Spring-cloud-config-server 2. Eureka configuration file. The client. The service - url. DefaultZone: http://loaclhost:8100/eureka # registry service address spring. The application. The name = config - server # config - server service register alias spring. Cloud. Config. Server. Git. Url = (git folder url) # config - the address of the server reads a git project Spring. Cloud. Config. Server. Git. Search paths = - config # read configuration file directory address spring. Cloud. Config. The label = master # read branch of the environment Server. port=8888 # server port number 3 Register the main class with the @enableeurekaclient annotation to the registry and the @enableconFigServer annotation to enable the ConfigServer function module to set up the ConfigClient environment 1. Introduce the configClient dependency on Spring-Cloud-config-client 2. The spring configuration file. The application. The name = # config - client service alias (consistent with git server save configuration file name) spring. Cloud. Config. Profile = dev # read the version of the environment Spring. Cloud. Config. Discovery. Service - id = config - server # read configServer environment, configServer alias in the registry Spring. Cloud. Config. Discovery. The enable = true # open access eureka. Client. The service - url. DefaultZone = http://loaclhost:8100/eureka Server. port=8889 Create a cotroller class to read configuration file information annotated @RestController and @requestMapping 4. Create the configClient startup class with the @enableeurekaclient annotation to register with the registryCopy the code
- Git create configuration file name specification:
- Service Name – Environment. properties(ticket-dev.properties)
Config The configuration file is updated in real time
- By default, the configuration file cannot be updated in real time. You need to restart the server to refresh the configuration file, which is inconvenient
- SpringCloud Config Distributed Configuration Center supports manual and automatic refreshes:
- Manual refresh: Manually invoke the interface to read the latest configuration file information — monitoring center
SpringBoot Actuator monitoring center 1. The Actuator relies on spring-boot-starter- Actuator 2. Endpoint in the configuration file open monitoring management endpoints. Web. Exposure. Include = "*" open all endpoints 3 #. Start and run configClient. 4. Use the @refreshScope tag on the beans in the Controller class that you want to refresh to enable the refreshCopy the code
- Automatic refresh: Real-time notification by message bus — SpringBus