background
When a time of system configuration file is changed, we need to restart the service in order to make the new configuration file to take effect, spring cloud config can achieve unified management profile micro-services in all systems, but can also be achieved when when the configuration file is changed, the system will automatically update to acquire new configuration.
working principle
Spring Cloud Config of the distributed system configured to provide external support server and client, which includes Config Server and Config Client in two parts. Config Server is a laterally extended, centralized configuration server, which is used in each environment centralized configuration management application, content default storage configuration using Git (also using Subversion, local file system or storage Vault configuration), can be facilitate the realization of version control and configuration of the contents of the audit. Config Client Config Server client for operating configuration properties stored in the Config Server.
Environment to build
Start Registration Center Eureka: 7001, here only demonstrate git, the rest of the small talk
git repository configuration center
New git warehouse distribution center, address: https: //github.com/kongliuyi/config.git, add the following new configuration file in the repository:
Can be mapped to the above endpoints {application} - {profile} .properties configuration file, {application} represents the name of the micro and services, {label} Git repository corresponding to branch default master
Which config-client-dev.properties file information is as follows:
Center server configuration (Config-server)
New project config-server
1. Add dependence
<! - self-examination and monitoring of integrated functions, the role here is to do a manual refresh monitor configuration information -> < dependency > < groupId > org.springframework.boot </ groupId > < artifactId > the Spring-the Boot-Starter-Actuator </ the artifactId > </ dependency > < dependency > < the groupId > org.springframework.cloud </ the groupId > < the artifactId > Spring-config-Server-Cloud </ the artifactId > </ dependency >
2.application.yml profile
# Port number Server : Port : 7010 # ## service address registered to eureka eureka : Client : Service- url : defaultzone : HTTP: // eureka7001: 7001 / eureka the Spring : the Application : # registry application name name : config- Server Cloud : config : Server : git : # git environment address uri : https://github.com/kongliuyi/config. git # # Search directory Search-Paths : /
3. Start ConfigServerApplication Service
package net.riking.springcloud.configserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableDiscoveryClient @EnableConfigServer //开启配置中心服务端 public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
After starting the project, visit: http: // localhost: 7010 / config-client-dev.properties, see the following page:
Center client configuration (Config-client)
New project config-client, afraid you configure the client service center it wrong, so explain here. Configuration Center client is the need to use the service configuration file, any service can be called micro-client distribution center (except registry)
1. Add dependence
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency>
2.bootstrap.yml profile
In addition to the default application.yml profile, the need to add a bootstrap.yml the profile parts , as follows:
# Service start port number Server : Port : 9010 # service name (service name registered to the eureka) Spring : file application : name : config-Client # {} file application config-server corresponding to the acquired profile Cloud : config : # reading suffix config-server configuration file corresponding to the acquired profile} { profile : dev label Master: # reading git repository branch corresponds to the acquired config-server configuration file label} { # config-server registered address read Discovery : -Service -id : config- Server Enabled :to true # client registration into the list of services within eureka eureka : Client : Service-url : defaultzone : HTTP: // eureka7001: 7001 / eureka
NOTE: spring cloud has a "boot context" concept, which is the main application of the parent context. Responsible for loading the boot context configuration properties from a configuration server, and a decryption external configuration file attributes. And the main application is loaded
application. * (Yml or properties) properties in different contexts loading boot (bootstrap. *) Attributes. Disposed in the bootstrap. * Attributes have higher priority, so they can not be configured to cover the local default.
3. The service interface calls
package net.riking.springcloud.configclient.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/config/client") public class ConfigClientController { @Value("${name}") private String name; @GetMapping public String name() { return name ; } }
4. Start ConfigClientApplication Service
package net.riking.springcloud.configclient; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient ///开启对EurekaClient的支持,即:作为Eureka客户端,高版本可省略 public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }
5. Verify
After starting the project, visit: http: // localhost: 9010 // config / client, see the following page:
Dynamic refresh data
Manually refresh
Auto Refresh
Source code analysis
After finishing time