Spring cloud Part 6: Spring Cloud Config Github

Posted by michealholding on Mon, 29 Jun 2020 06:15:03 +0200

As distributed projects get bigger and bigger, hardworking programmers will begin to face a challenge, and configuration files will become more and more complicated, although spring provides a chicken rib solution, spring.profiles.active In the large-scale distributed project system, it's better than nothing. The pain of manual maintenance of configuration files, isolation of production, UAT, test, development environment, additional Configuration files, such as: logback.xml Profile of the log, bootstrap.properties Configuration files: when there are dozens of services in the system, there will be hundreds of corresponding configuration files, which is an epic disaster blockbuster. Every time you release it online, you need to manually check the configuration files, and the corresponding services need to be restarted. So, is there a scheme that can automatically update the configuration Spring cloud provides us with such a tool. Although many aspects are not perfect and the configuration ability is weak, it also provides us with a way of thinking.
(understand the source code and ask: 1791743380)

There are many configuration centers on the market, each BAT has been out, and QConf of 360, diamond of Taobao and disconf of Baidu all solve such problems. There are also many open source configuration centers abroad, such as Apache Commons Configuration, owner, cfg4j and so on. These open source software and solutions are excellent, but there are also some defects. Today, we want to learn about Spring Cloud Config, which can be seamlessly combined with the spring system. It is convenient, simple and has high appearance.

1. Spring Cloud Config

Before introducing Spring Cloud Config, we can first imagine the core functions that a configuration center needs to provide:

  • Provide client and server support
  • Provide configuration of each environment
  • Configuration files can take effect quickly after modification
  • Different versions of management can be provided
  • Can support different languages (java,. Net, Delphi, node, etc.)
  • Support a certain number of concurrency
  • High availability (prevents configuration from being unavailable due to unexpected downtime)

The Spring Cloud Config project is a configuration management solution for distributed systems. It includes two parts: client and server. Server provides the storage of configuration file and provides the content of configuration file in the form of interface. Client obtains data through the interface and initializes its own application based on the data. Spring cloud uses git or svn to store configuration files. By default, it uses GIT. Let's take git as an example.

First, a folder spring cloud config is created on github to store configuration files. To simulate the production environment, we create the following three configuration files:

// development environment 
springcloud-config-dev.properties
// testing environment
springcloud-config-test.properties
// production environment 
springcloud-config-pro.properties

Write a property in each profile springcloud.hello , the attribute values are hello dev/test/pro. Let's start to configure 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. Configuration file

server:
  port: 8080
spring:
  application:
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/meteor1993/SpringCloudLearning  #Address of GIT warehouse
          search-paths: chapter6/springcloud-config  # The relative address under git warehouse address can be configured with multiple, use and split addresses.
          username: #Git warehouse user name
          password: #Git warehouse password

Spring Cloud Config also provides a way to store configuration locally. We just need to set the properties spring.profiles.active=native , Config Server will retrieve the configuration file from the src/main/resource directory of the application by default. Or through spring.cloud.config.server.native.searchLocations=file:E:/properties / property to specify the location of the configuration file. Although Spring Cloud Config provides such functions, git is recommended to support better content management and version control.

3. Start 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);
    }

}

Add @ EnableConfigServer annotation to activate support for configuration center

4. Test

First, test whether our server can get the information we need from github, and directly access: http://localhost:8080/springcloud-config/pro , note: springcloud config is the prefix name of the folder. 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 above returned information includes the location, version, name of the configuration file and the specific content in the configuration file, indicating that the server side has successfully obtained the configuration information of git warehouse.

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

Modify the configuration file springcloud config- dev.properties The configuration information in is as follows: springcloud.hello=hello Dev update, access again in the browser http://localhost :8080/springcloud-config-dev.properties , return: springcloud.hello: hello dev update. The server will automatically read the auto submit function.

The configuration file in the warehouse will be converted into a web interface, and the following rules can be followed for access:

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

Spring cloud config- dev.properties For example, its application is springcloud config, profile is dev, and label means branch. If there is only one main branch, it can not be written. By default, the master branch will be accessed, and the client will select to read the corresponding configuration according to the parameters filled in.

3. client

It mainly shows how to obtain the configuration information of the server in 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. Configuration file

There are two configuration files, 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 : corresponding to {application} section
  • spring.cloud.config.profile: corresponding to {profile} section
  • spring.cloud.config.label: the branch corresponding to git. If the parameter is a local configuration, the
  • spring.cloud.config.uri: the specific address of the configuration center
  • spring.cloud.config.discovery.service-id: Specifies the service ID of the configuration center, which is convenient for expansion to a highly available configuration cluster.

Note: the above spring cloud related properties must be configured in the bootstrap.properties The contents of the config section can be loaded correctly. Because the relevant configuration of config will precede application.properties , and bootstrap.properties The load of the application.yml .

3. Startup 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);
    }

}

To start a class, you only need the @ SpringBootApplication annotation.

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;
    }
}

Use @ Value annotation to get the Value of server side parameter

5. Testing

Start the client and access the link: http://localhost:8081/hello , return: hello dev update, indicating that the parameters have been correctly obtained from the server side. This is a complete server to provide configuration services, and the example of the client obtaining configuration parameters is completed.

We can do another small experiment, and we can modify springcloud config again- dev.properties The content in is: hello dev update1. After submitting to github, visit again http://localhost : 8081/hello, you can find that the returned content is still hello dev update. Why? Because the springboot project only obtains the value of the configuration file when it is started. After modifying the github information, the client side does not get the value again, which leads to this problem. How to solve this problem? Save it for the next one. We'll introduce it.

Topics: Javascript Spring git Maven Apache