As shown in the figure, it is divided into client and server
1. Introduction:
Spring Cloud Config is a new project created by the Spring Cloud team. It is used to provide centralized external configuration support for infrastructure and micro service applications in distributed systems. It is divided into two parts: server and client.
The server is also called distributed configuration center. It is an independent micro service application, which is used to connect the configuration warehouse and provide access interfaces for the client to obtain configuration information, encryption / decryption information and so on;
The client is each micro service application or infrastructure in the micro service architecture. They manage the application resources and business-related configuration content through the specified configuration center, and obtain and load the configuration information from the configuration center at startup.
Spring Cloud Config implements the abstract mapping of environment variables and attribute configurations in the server and client, so it can also be used in applications running in any other language in addition to applications built by spring.
Since the configuration center implemented by Spring Cloud Config uses Git to store configuration information by default, the configuration server built by Spring Cloud Config naturally supports version management of micro service application configuration information, and can easily manage and access configuration content through Git client tools.
Of course, it also provides support for other storage methods, such as SVN warehouse and localized file system
Construction method steps:
Building a Config Server through Spring Cloud is very simple and requires only three steps:
1 pom. The spring cloud config server dependency is introduced into XML.
2 add the @ enableconfig server annotation in the main class of the program to start the Config Server.
3 finally, configure the yml file with GIT.
Configuration center validation:
First, write four configuration files ~, and then, for example, there is a from attribute to set different values. Finally, upload it to GitHub through gitbash. According to the verification rules, it is as follows: http://localhost:4000/{application}/{profile}/{label}
2.server service provider:
pom Code:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.bfxy</groupId> <artifactId>spring-cloud-master</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>spring-cloud-06-config-server</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <!-- <version>Dalston.SR5</version> --> <version>Edgware.SR4</version> <!-- <version>Finchley.SR1</version> --> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <finalName>spring-cloud-06-config-server</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.bfxy.springcloud.Application</mainClass> </configuration> </plugin> </plugins> </build> </project>
yml files: configuring git management
spring: application: name: config-server cloud: config: server: git: uri: https://github. COM / baihezhuo / learngit ##git address server: context-path: / port: 4000
Main entry: @ enableconfigureserver / / start the configuration center service
package com.bfxy.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer //Start configuration center service @SpringBootApplication //SpringBoot core configuration public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
3. Client
pom Code:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.bfxy</groupId> <artifactId>spring-cloud-master</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>spring-cloud-06-config-client</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <!-- <version>Dalston.SR5</version> --> <version>Edgware.SR4</version> <!-- <version>Finchley.SR1</version> --> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <finalName>spring-cloud-06-config-client</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.bfxy.springcloud.Application</mainClass> </configuration> </plugin> </plugins> </build> </project>
Configuration YML: the client configuration file name is fixed bootstrap yml
spring: application: name: evn cloud: config: ## key uri: http://localhost:4000/ ##Config service provider Profile: configuration of dev ##dev environment label: master ## branch
management: security: enabled: false
##http://localhost:4000/{application}/{profile}/{label} spring: application: name: evn cloud: config: uri: http://localhost:4000/ profile: dev label: master server: context-path: / port: 7001 management: security: enabled: false
Main entrance: (no change)
package com.bfxy.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication //SpringBoot core configuration public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
Take out the configuration file data with the interface test:
package com.bfxy.springcloud.api; import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RefreshScope //Manual forced refresh by post method: http://localhost:7001/refresh @RestController public class ConfigController { @Value("${from}") private String from; @RequestMapping(value="/from") public String from(){ System.err.println("from: " + from); return this.from; } }