Follow me SpringCloud | Part VI: Spring Cloud Config Github Configuration Center

SpringCloud tutorial series | Part VI: Spring Cloud Config Github Configuration Center

Springboot: 2.1.6.RELEASE

SpringCloud: Greenwich.SR1

Unless otherwise specified, the full use of this tutorial series or later

With more and more distributed projects, hard-working program ape who will begin to face a challenge, the profile will be more complicated, although the spring version offers a tasteless solution, spring.profiles.active, distributed in large type project system, better than nothing right, manually maintain painful profiles, production, UAT, test, isolated development environment, additional configuration files, such as: logback.xml log profiles, bootstrap.properties configuration file, when the system there are dozens of services, there will be hundreds of corresponding profile, is simply epic disaster blockbusters, each release on line, have to manually check the configuration file, the corresponding service will need to restart, then, is there a program, you can automatically update the configuration, and make the appropriate version control, exactly, springcloud provides a tool for us, although many respects is not perfect, the configuration is relatively weak, but also provides us with an idea .

There are many configuration center, BAT each have been out, 360 QConf, Taobao diamond, Baidu's disconf solve all these problems. There are many foreign open source distribution center Apache Commons Configuration, owner, cfg4j and so on. These open-source software and solutions are excellent, there is one way or another defect. Today we have to understand the Spring Cloud Config, and spring system can be seamlessly combined with enough high enough to facilitate simple color value.

1. Spring Cloud Config

Before introducing the Spring Cloud Config, out of thin air before we can think about the core functions of a distribution center need to provide what:

  • Provide support for client and server
  • Provide configuration of each environment
  • Profiles can be quickly modified to take effect
  • We can provide different versions of Management
  • It can support different languages ​​(java, .Net, Delphi, node, etc.)
  • A certain number of concurrent
  • HA (results in unplanned downtime prevent unusable configuration)

Spring Cloud Config project is a configuration management solution to address the distributed system. It contains two parts Client and Server, server to provide storage profile, in the form of an interface configuration file to provide the contents out, client data acquired through the interface, and based on this data to initialize their own applications. Spring cloud using git or svn to store configuration files, use the default git, git's start with an example and do another example.

First on github above creates a folder springcloud-config used to store configuration files, in order to simulate the production environment, we create the following three profiles:

// 开发环境
springcloud-config-dev.properties
// 测试环境
springcloud-config-test.properties
// 生产环境
springcloud-config-pro.properties

Each profile is written in a property springcloud.hello, property values ​​are hello dev / test / pro. Let's start configuring the server side

2. Server-side

1. 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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springcloud</groupId>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.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>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</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>

</project>

2. Profiles

server:
  port: 8080
spring:
  application:
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/meteor1993/SpringCloudLearning # git仓库的地址
          search-paths: chapter6/springcloud-config  # git仓库地址下的相对地址,可以配置多个,用,分割。
          username: #Git仓库用户名
          password: #Git仓库密码

Spring Cloud Config mode also provides local storage configuration. We only need to set the property spring.profiles.active = native, Config Server will default to retrieve the configuration file from the application of src / main / resource directory. To specify the location profile / properties / attributes: can also spring.cloud.config.server.native.searchLocations = file: E. Although Spring Cloud Config provide this functionality, but in order to support better manage content and version control features, or recommend git manner.

3. Start class ConfigServerApplication

package com.springcloud.configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }

}

Increase @EnableConfigServer notes, activate support for configuration center

4. Test

We first tested the server-side can be obtained from the information we need to github, direct access to: http: // localhost: 8080 / springcloud-config / pro, pay attention: springcloud-config prefix folder name, you can see the following results:

{
    "name":"springcloud-config",
    "profiles":[
        "pro"
    ],
    "label":null,
    "version":"4e3d1a93e869fb336254c480ed1e5b36d58124aa",
    "state":null,
    "propertySources":[
        {
            "name":"https://github.com/meteor1993/SpringCloudLearning/chapter6/springcloud-config/springcloud-config-pro.properties",
            "source":{
                "springcloud.hello":"hello pro"
            }
        }
    ]
}

The information returned includes the specific content of the above-mentioned location profile, version, configuration files and configuration file name, indicating that the server-side configuration information has been successfully acquired git repository.

If the configuration information directly view the configuration file can be accessed: http: // localhost: 8080 / springcloud-config-dev.properties, return: springcloud.hello: hello dev

Springcloud-config-dev.properties modify the configuration file in the configuration information: springcloud.hello = hello dev update, again in the browser to http: // localhost: 8080 / springcloud-config-dev.properties, return: springcloud.hello: hello dev update. Description end server will automatically read the automatic submission function.

Warehouse configuration file is converted into a web interface, access can refer to the following rules:

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

To springcloud-config-dev.properties example, its application is springcloud-config, profile is dev, label is a branch of the meaning, if there is only one main branch, you can not write, default access master branch, client based fill reads the corresponding parameter to select the configuration.

3. client end

The main show how to obtain configuration information in the server side of the business project.

1. 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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springcloud</groupId>
    <artifactId>config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <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-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</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>

</project>

2. Profiles

Here the configuration file is divided into two, application.yml and bootstrap.properties

application.yml as follows:

server:
  port: 8081
spring:
  application:
    name: spring-cloud-config-client

bootstrap.properties as follows:

spring.cloud.config.name=springcloud-config
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8080/
spring.cloud.config.label=master
  • spring.application.name: portion corresponding to {application}
  • spring.cloud.config.profile: portion corresponding to {profile}
  • spring.cloud.config.label: git of the corresponding branch. If the configuration of local storage centers, the parameter useless
  • Specific address configuration center: spring.cloud.config.uri
  • spring.cloud.config.discovery.service-id: Specifies the configuration of the central service-id, scalable cluster is highly available configuration.

NOTE: The above those associated with spring-cloud properties must be configured in bootstrap.properties, config is part of the contents in order to be loaded correctly. Because of config configuration will precede application.properties, but also prior to loading bootstrap.properties application.yml.

3. Start class

package com.springcloud.configclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }

}

Start class need only @SpringBootApplication comment on it, normal operation.

4. Access class

package com.springcloud.configclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: shiyao.wei
 * @Date: 2019/7/4 16:19
 * @Version: 1.0
 * @Desc:
 */
@RestController
public class HelloController {

    @Value("${springcloud.hello}")
    private String hello;

    @RequestMapping("/hello")
    public String from() {
        return this.hello;
    }
}

@Value using server-side annotation to obtain the value of the parameter

5. Test

Start client to access the link: http: // localhost: 8081 / hello, returns: hello dev update, explained that it had the right to obtain from the server end to the argument. This provides a complete server configuration service, the client gets the configuration parameters of the example is complete.

Can then be a little experiment, we again modify the content of springcloud-config-dev.properties is: hello dev update1, after submitting to github, again go to http: // localhost: 8081 / hello, can be found, return the content or hello dev update, this is why? Because springboot project will start only when the acquisition value of the configuration file, modify github information, client-side and did not get to go in the second, resulting in the problem. How to solve this problem? We are left to the next presentation.

Sample Code -Github

Reference: http: //www.ityouknow.com/springcloud/2017/05/22/springcloud-config-git.html

Guess you like

Origin www.cnblogs.com/babycomeon/p/11134717.html