SpringCloud Config
- Spring cloud integrates the overall solutions in microservices: distributed configuration center, distributed lock, distributed task scheduling platform, distributed transaction and distributed log collection
- Background: in microservices, if you use traditional methods to manage configuration files, the configuration file manager will be very complex; In the production environment, when the configuration file changes, you need to reconfigure the war package and re read the configuration file information into the JVM
- SpringCloud Config distributed configuration center:
- Use the same server in microservice to manage all service profile information
- During the operation of the server, if the configuration file changes, the configuration file information can be changed in real time without restarting the server
Config Real time refreshing of configuration files is not equivalent to hot deployment The underlying implementation of hot deployment is actually restarting the server,Not suitable for production environment,Only suitable for local development and testing
Config schema
- When the configuration file in a system changes, the service needs to be restarted to make the configuration file take effect
- SpringCloud Config can realize the unified management of the configuration files of all systems in the microservice. It can also realize that when the configuration file changes, the system will automatically update and obtain the new configuration
Distributed configuration center framework
- Apollo: Ctrip's distributed configuration center framework has a graphical interface to manage configuration file information, which is stored in the database
- SpringCloud Config: SpringCloud has its own distributed configuration center framework. There is no background manageable distributed configuration center. The configuration file information is stored in the version controller, such as GIT and SVN
zookeeper Implement distributed configuration center,Using persistent nodes+Event notification implementation
Design principle of distributed configuration center
Design components of distributed configuration center
- Web management system: you can use the graphical interface to manage the configuration file in the web background (SpringCloud Config has no graphical management component)
- Server for storing distributed configuration files:
- Use database to store configuration files: Apollo
- Use the version controller (git,svn, etc.) to store the configuration file information: SpringCloud Config
- ConfigServer: server that caches configuration files (used to cache configuration file information on git server)
- ConfigClient: used to read ConfigServer configuration file information
Server for storing distributed configuration files:Persistent storage server ConfigServer:Server for temporary cache storage
Principle of distributed configuration center
- The user submits the configuration file information to the version controller: git/SVN server for storage
- ConfigServer caches the configuration file information obtained from the git server
- The ConfigClient side obtains the configuration file information from the ConfigServer side
build git environment:Persistent storage profile information 1.New project 2.New folder(git Folders on are distinguished by services) 3.Save the configuration file to git Folder build ConfigServer environment 1.introduce configServer rely on spring-cloud-config-server 2.configuration file eureka.client.service-url.defaultZone:http://loaclhost:8100/eureka # Service address of Registration Center spring.application.name=config-server # Config server service registration alias spring.cloud.config.server.git.url=(git folder url address) # Config server reads the address of git project spring.cloud.config.server.git.search-paths=- config # Read the directory address of the configuration file spring.cloud.config.label=master # Read branch environment server.port=8888 # Service port number 3.Label on main class@EnableEurekaClient The annotation is registered in the registry,tagging@EnableConfigServer Annotation on ConfigServer functional module build ConfigClient environment 1.introduce configClient rely on spring-cloud-config-client 2.configuration file spring.application.name=config-client # Service alias (to be consistent with the configuration file name saved by git server) spring.cloud.config.profile=dev #Read version environment spring.cloud.config.discovery.service-id=config-server # Read the configServer environment and the alias of configServer in the registry spring.cloud.config.discovery.enable=true # Open read permission eureka.client.service-url.defaultZone=http://loaclhost:8100/eureka # Address of Registration Center server.port=8889 #Service port number 3.establish cotroller Class to read configuration file information,tagging@RestController Notes and@RequestMapping annotation 4.establish configClient Startup class,tagging@EnableEurekaClient The annotation is registered in the registry
- Create profile name specification on git:
- Service name - environment properties(ticket-dev.properties)
Config profile real-time refresh
By default, the configuration file information cannot be refreshed in real time. You need to restart the server to refresh the configuration file, which is not very convenient
SpringCloud Config distributed configuration center supports manual refresh and automatic refresh:
- Manual refresh: manually call the interface to read the latest configuration file information - monitoring center
SpringBoot Actuator monitoring center 1.introduce actuator rely on spring-boot-starter-actuator 2.Enable monitoring endpoint in configuration file management.endpoints.web.exposure.include="*" # Open all endpoints 3.Start operation configClient 4.When you need to refresh controller Class bean When dimensioning@RefreshScope Annotation make actuator Refresh effective
- Automatic refresh: real time notification via message bus -- springbus