Copyright notice: This article is the blogger’s original article, welcome to reprint, reprint please indicate the author, the original hyperlink, the blogger’s address: blog.csdn.net/forezp. https://blog.csdn.net/forezp/article/details/70037291

Reprint please indicate the source: blog.csdn.net/forezp/arti… This article is from Fang Zhipeng’s blog

  • Latest version:
  • In the history of the most simple SpringCloud tutorial | article 6: the distributed configuration center (Spring Cloud Config) (Finchley version)

In the last article on Zuul, I mentioned the use of configuration services to save configuration files for individual services. It is Spring Cloud Config.

A list,

In a distributed system, due to a large number of services, a distributed configuration center is required to facilitate unified management and real-time update of service configuration files. In Spring Cloud, there is a distributed configuration center component, Spring Cloud Config, which enables configuration services to be placed in the memory of the configuration service (that is, locally) as well as in a remote Git repository. In the Spring Cloud Config component, there are two roles: Config Server and Config client.

Build the Config Server

Create a spring-boot project named config-server with 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.01.-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.52..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-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 the code

Add @enableconFigServer to the Application class to enable the configuration of the server.

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 the code

The following needs to be configured in the application. Properties file in the application’s 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 passwordCopy the code
  • Spring. Cloud. Config. Server. Git. Uri: configure git warehouse address
  • Spring. Cloud. Config. Server. Git. SearchPaths: configuration path to the warehouse
  • Spring. Cloud. Config. Label: configuration branch warehouse
  • Spring. Cloud. Config. Server. Git. Username: access to the git repository user name
  • Spring. Cloud. Config. Server. Git. Password: access to the git repository user password

If the Git repository is a public repository, you can leave the username and password blank. If the Git repository is a private repository, you need to fill in the username and password.

Remote warehouse https://gitee.com/tangzhengfeng/SpringCloud/ has a file application. The properties file has an attribute:

spring.application.name=eureka-server

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

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

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

HTTP request addresses and resource files are mapped as follows:

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

Build a Config client

Create a new Springboot project named config-client with 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.01.-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.52..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 the code

Properties configuration file :(if port 8888 is not used, the name of the configuration file should be changed to bootstrap.properties)

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

  • spring.cloud.config.profile

    • Dev development environment configuration file
    • Test Test environment
    • Pro Formal environment
  • Spring.cloud.config. uri= http://localhost:8888/ Specifies the web address of the configuration service center.

/ hi (foo); / hi (foo); / hi (foo);

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(a){
		returnfoo; }}Copy the code

Open the website to visit: http://localhost:8099/hi, and the page will display:

config-client

Config-client gets foo’s properties from config-server, which reads from git repository.

This article source download: gitee.com/tangzhengfe…

Iv. Reference materials

spring_cloud_config

Excellent articles recommended: