spring cloud Starter Series 7: A Git-based Distributed Configuration Center -- Spring Cloud Config

Keywords: Java Spring git Maven Apache

The spring cloud components we touched on earlier are all based on Netflix components. This time, let's look at a brand new project created by the spring cloud team itself: Spring Cloud Config.
It is used to provide centralized external configuration support for infrastructure and micro services 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 connects the configuration warehouse and provides access interfaces for clients (these interfaces return configuration information, encryption, decryption information, etc.).

Client is the micro-service application or infrastructure in the micro-service architecture. They manage the business-related configuration content of application resources through the established configuration center, and obtain and load configuration information from the configuration center at startup.
Because the configuration center uses Git to store configuration information by default, we will use Git-related content. If we have not used Git or forgotten how to use it, we can refer to Mr. Liao Xuefeng's. Git tutorial.
In addition, my own Git remote warehouse is the code cloud.
========================================= gorgeous partition line==================
Next, let's see how the code works.

I. Preparing a remote Git warehouse

  1. Create a new project on Gitee: https://gitee.com/sam-uncle/spring-cloud-learning
  2. Create a new subdirectory under the project, spring-cloud-config-file, and then create three new files
    1.   

       

    2. The contents are from=git-dev-1.0, from=git-test-1.0, and from=git-1.0, respectively.
  3. Create a new branch config-lable-test, and create three new files with the same name in the new branch, but the contents are from=git-dev-2.0, from=git-test-2.0, and from=git-2.0, respectively.

 

II. Construction of Configuration Center

First, the final code structure is given:

  

The construction process is as follows:

  1. New maven project config-server
  2. Modify POM files
    <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.sam</groupId>
      <artifactId>config-server</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      
      <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.1.RELEASE</version>
        </parent>
    
        <properties>
            <javaVersion>1.8</javaVersion>
        </properties>
        <!-- Use dependencyManagement Version management -->
        <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>
    
        <dependencies>
           <!-- Introduce config server rely on -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-server</artifactId>
            </dependency>
    
        </dependencies>
        
    </project>
  3. Create Startup Class
    /**
     * @EnableConfigServer
     * 
     * Open Spring Cloud Config's server-side functionality
     *
     */
    @SpringBootApplication
    @EnableConfigServer
    public class ConfigServerApp {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigServerApp.class, args);
        }
    }
  4. Configure the application.properties file to specify remote warehouse information
    server.port=7001
    spring.application.name=config-server
    
    #Configure the address of the Git warehouse
    spring.cloud.config.server.git.uri=https://gitee.com/sam-uncle/spring-cloud-learning/
    #Configuring relative search locations under warehouse paths can configure multiple
    spring.cloud.config.server.git.search-paths=spring-cloud-config-file
    #Here configure the username of your Git repository
    spring.cloud.config.server.git.username=User name
    #Configure your Git repository password here
    spring.cloud.config.server.git.password=Password
  5. Start and verify

The mapping relationship between URL s accessing configuration information and configuration files is as follows:

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

The url above maps the configuration file corresponding to {application}-{profile}.properties, where {label} corresponds to different branches on Git by default to master.

Access http://localhost:7001/sam/dev/config-label-test through a browser. The results are as follows:

    


3. Implementing Client

Final code structure:

  

 

The construction process is as follows:

  1. New maven project config-client
  2. Modify POM files
    <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.sam</groupId>
        <artifactId>config-client</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.1.RELEASE</version>
        </parent>
    
        <properties>
            <javaVersion>1.8</javaVersion>
        </properties>
        <!-- Use dependencyManagement Version management -->
        <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>
    
        <dependencies>
            <!-- Introduce config rely on -->
            <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>
    
        </dependencies>
    
    </project>
  3. Create Startup Class
    @SpringBootApplication
    public class ConfigClientApp {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigClientApp.class, args);
        }
    
    }
  4. Configure the bootstrap.properties file to specify the config-server location
    server.port=7002
    #{application}
    spring.application.name=sam
    #{profile}
    spring.cloud.config.profile=dev
    #{label}
    spring.cloud.config.label=master
    
    #config server uri
    spring.cloud.config.uri=http://localhost:7001/
  5. Create controller
    @RefreshScope
    @RestController
    public class TestController {
    
        
        /**
         * Write the values in the configuration file into the code by @Value
         */
        @Value("${from}")
        private String from;
    
        @RequestMapping("/from")
        public String from() {
            return from;
        }
    }
  6. Start and test

    

 

4. Working Principle
The working principle of Spring Cloud Config configuration center is as follows:

  1. When the client starts, it requests Config Server for configuration information according to the application name {application}, environment name {profile}, branch name {label} configured in bootstrap.properties.
  2. Config Server finds configuration information based on Git warehouse information maintained by itself and configuration location information passed by customers.
  3. The configuration information found is downloaded to the local (Config Server) file system through the git clone command. When accessing or starting the client through the page, we can see the following log downloaded on the server side:
    2018-05-14 22:51:58.055  INFO 3084 --- [nio-7001-exec-1] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/C:/Users/sam/AppData/Local/Temp/config-repo-8627749771720918793/spring-cloud-config-file/sam-dev.properties
    2018-05-14 22:51:58.055  INFO 3084 --- [nio-7001-exec-1] o.s.c.c.s.e.NativeEnvironmentRepository  : Adding property source: file:/C:/Users/sam/AppData/Local/Temp/config-repo-8627749771720918793/spring-cloud-config-file/sam.properties
  4. Config Server creates Spring's ApplicationContext instance, loads configuration files from the Git local repository, and finally reads these configuration contents back to the client.
  5. The client loads the application context instance of the client after obtaining the external configuration information.

Posted by hayson1991 on Mon, 17 Dec 2018 15:54:04 -0800