Spring Cloud Distributed Configuration Center (Spring Cloud Config) (6)

Spring Cloud Distributed Configuration Center (Spring Cloud Config) (6)

Copyright statement: This article is the original article of the blogger, welcome to reprint, please indicate the author, the hyperlink of the original text, and the blogger address: blog.csdn.net/forezp. https://blog.csdn.net/forezp/article/details/70037291

Please indicate the source for reprinting: 
blog.csdn.net/forezp/arti This 
article comes from Fang Zhipeng s blog

When talking about zuul in the previous article, it was mentioned that the configuration service is used to save the configuration files of each service. It is Spring Cloud Config.

1. Introduction

In a distributed system, due to the huge number of services, in order to facilitate the unified management and real-time update of service configuration files, a distributed configuration center component is required. In Spring Cloud, there is a distributed configuration center component spring cloud config, which supports the configuration service in the memory of the configuration service (that is, local), and it also supports the remote Git warehouse. In the spring cloud config component, there are two roles, one is config server, and the other is config client.

2. build Config Server

Create a spring-boot project, named config-server, and its pom.xml:

<?xml version= "1.0" encoding= "UTF-8" ?> <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> <groupId>com.forezp</groupId> <artifactId>config-server</artifactId> <version> 0.0 .1 -SNAPSHOT</version> <packaging>jar</packaging> <name>config-server</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 1.5 .2 .RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF- 8 </project.build.sourceEncoding> <project.reporting.outputEncoding>UTF- 8 </project.reporting.outputEncoding> <java.version> 1.8 java.version> </java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https: //repo.spring.io/milestone</url> <snapshots> <enabled> false </enabled> </snapshots> </repository> </repositories> </project> Copy code

Add the @EnableConfigServer annotation to the Application class at the entrance of the program to enable the configuration server function. The code is as follows:

package com.forezp.configserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main (String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } Copy code

The following needs to be configured in the application.properties file of the program configuration file:

spring.application.name=config-server server.port = 8888 spring.cloud.config.server.git.uri=https: //gitee.com/tangzhengfeng/SpringCloud/ spring.cloud.config.server.git.searchPaths=tree/master/eureka-server/src/main/resources spring.cloud.config.label=master spring.cloud.config.server.git.username=your username spring.cloud.config.server.git.password=your password Copy code
  • spring.cloud.config.server.git.uri: configure the git warehouse address
  • spring.cloud.config.server.git.searchPaths: configuration repository path
  • spring.cloud.config.label: configure the branch of the warehouse
  • spring.cloud.config.server.git.username: the username for accessing the git repository
  • spring.cloud.config.server.git.password: user password for accessing git warehouse

If the Git warehouse is a public warehouse, you don t need to fill in the user name and password. If it s a private warehouse, you need to fill it in. This example is a public warehouse, so feel free to use it.

  There is a file in the remote warehouse https://gitee.com/tangzhengfeng/SpringCloud/ There is a property in the application.properties file:

spring.application.name=eureka-server

Start the program: visit http://localhost:8888/spring.application.name/dev

{ "name" : "spring.application.name" , "profiles" :[ "dev" ], "label" : "master" , "version" : "a39c55c0679235734647894d170145e08fe0e0bd" , "state" : null , "propertySources" :[ ]} Copy code

Prove that the configuration service center can obtain configuration information from a remote program.

The http request address and resource file are mapped as follows:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

3. build a config client

Re-create a springboot project, name it config-client, and its pom file:

<?xml version= "1.0" encoding= "UTF-8" ?> <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> <groupId>com.forezp</groupId> <artifactId>config-client</artifactId> <version> 0.0 .1 -SNAPSHOT</version> <packaging>jar</packaging> <name>config-client</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 1.5 .2 .RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF- 8 </project.build.sourceEncoding> <project.reporting.outputEncoding>UTF- 8 </project.reporting.outputEncoding> < java.version> 1.8 </java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope> import </scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https: //repo.spring.io/milestone</url> <snapshots> <enabled> false </enabled> </snapshots> </repository> </repositories> </project> Copy code

The configuration file application.properties: (If port 8888 is not used, the configuration file name needs to be changed to bootstrap.properties )

spring.application.name=config-client spring.cloud.config.label=master spring.cloud.config.profile=dev = HTTP spring.cloud.config.uri: //localhost: 8888/ server.port = 8099 Copy the code
  • spring.cloud.config.label indicates the branch of the remote warehouse

  • spring.cloud.config.profile

    • dev development environment configuration file
    • test test environment
    • pro formal environment
  • spring.cloud.config.uri=  http://localhost:8888/  Specify the URL of the configuration service center.

The entry class of the program, write an API interface "/hi", return the value of the foo variable read from the configuration center, the code is as follows:

package com.forezp.configclient; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework .web.bind.annotation.RestController; @SpringBootApplication @RestController public class ConfigClientApplication { public static void main (String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } @Value("${spring.application.name}") String foo; @RequestMapping(value = "/hi") public String hi () { return foo; } } Copy code

Open the website to visit: http://localhost:8099/hi , the web page displays:

config-client

This means that config-client gets the attributes of foo from config-server, and config-server is read from git repository, as shown in the figure:

Download the source code of this article:  gitee.com/tangzhengfe...

4. reference materials

spring_cloud_config

Excellent article recommendation: