Spring Cloud - Chapter 7 - Profile Management Center (Spring Cloud Config)

I. Brief Introduction

Distributed systems often have multiple services and different configuration files. Different environment profiles have different contents: development environment, test environment and production environment.

To facilitate the management of a large number of configuration files, spring cloud has a corresponding configuration file management: spring cloud config.

There are two main roles in this component: config server configuration file management center and config client to read the real configuration information from config server.

Configuration file support is localized, as well as in remote Git repositories.

2. Reuse spring cloud-base, the parent pom project of this series of projects.

Add dependencies to its pom:

        <!--Configuration file management-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>

Create a new folder config and set the corresponding configuration content:

config-client-pro.properties :

version = pro -1.1.1.11- FZW -1.1.1

config-client-test.properties :

version =  test-2.2.2.22

config-client-dev.properties :

version = dev-0.0.0.DEV-0.0.0

3. New config-server project

1. file - new - module 

2. spring Initializr - module SDK chooses its own JDK, the rest can be filled out without filling in, next.

3. Fill in the relevant information of the project: the name of the package, the name of the project, etc., next.

4. spring cloud config - Check config server, next.

5. Project name, code location, finish.

 

6. The structure of the project is as follows:

 

7. 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.config</groupId>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-server</name>
    <description>Configuration file management server</description>

    <parent>
        <groupId>com.base</groupId>
        <artifactId>base-config</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>

</project>

8. Add a comment on the project startup class: @EnableConfigServer, to turn on the configuration file management service function.

package com.config.configserver;


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

@SpringBootApplication

// Configuration file management
@EnableConfigServer
public class ConfigServerApplication {

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

}

9. Set in the configuration file:

Read the configuration file information under the new config folder in the spring cloud-base project.

# project name
spring.application.name=config-server

# port
server.port=3333

# git warehouse address
spring.cloud.config.server.git.uri=https://gitee.com/FJ_WoMenDeShiJie/springcloud-base.git

# Warehouse Path
spring.cloud.config.server.git.searchPaths=config

# Warehouse Branch
spring.cloud.config.label=master

# git Warehouse User Name (Open Library - Not Filled)
spring.cloud.config.server.git.username=

# git warehouse password (open Library - not filled)
spring.cloud.config.server.git.password=

10. Start engineering visits: http://localhost:3333/ Write here casually/dev

Represents the success of accessing the configuration file in the remote warehouse. The last dev of the access path can also be written arbitrarily during this test.

4. New config-client project

1. file - new - module 

2. spring Initializr - module SDK chooses its own JDK, the rest can be filled out without filling in, next.

3. Fill in the relevant information of the project: the name of the package, the name of the project, etc., next.

4. spring cloud config - Check config client, next.

 

5. Project name, code location, finish.

6. The structure of the project is as follows:

7. 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.config</groupId>
    <artifactId>config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-client</name>
    <description>Configuration file management client</description>

    <parent>
        <groupId>com.base</groupId>
        <artifactId>base-config</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <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>
    </dependencies>

</project>

8. Note on the project startup class: @RestController, write getVersion method, provide external access.

package com.config.configclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@SpringBootApplication
public class ConfigClientApplication {

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

    @Value("${version}")
    String version;

    @RequestMapping(value = "/getVersion")
    public String getVersion() {
        return version;
    }

    /**
     * Add this method to resolve the error: Can not resolve placeholder'version'in value "${version}"
     * @return
     */
    @Bean
    public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
        PropertySourcesPlaceholderConfigurer c = new PropertySourcesPlaceholderConfigurer();
        c.setIgnoreUnresolvablePlaceholders(true);
        return c;
    }

}

9. Configuration file settings: Note that the name of this configuration file is bootstrap.properties.

Reasons for calling this name: The official preference is to read this file, and bootstrap loads it before application.


# Project name
spring.application.name=config-client

# port
server.port=4444

# Warehouse Branch
spring.cloud.config.label=master

# Read files: dev development environment, test test, pro production
spring.cloud.config.profile=test

# Configuration File Management Service Cong-server Address
spring.cloud.config.uri=http://localhost:3333/

This is set to read the configuration file in spring cloud-base: config-client-test.properties.

10. Start engineering visits: http://localhost:4444/getVersion

Modify the files accessed in the configuration file:

This is set to read the configuration file in spring cloud-base: config-client-dev.properties

Restart Project Visit: http://localhost:4444/getVersion

So far, config-server has successfully read the configuration file from the remote warehouse. Cong-client also succeeded in reading the specific information values in the configuration file through config-server.

In the config-client configuration file, different configuration files can be read by modifying the spring.cloud.config.profile option.

-------------------------------------------------------------

Next article: spring Cloud - Chapter 8-

See the source code: https://gitee.com/FJ_WoMenDeShiJie/springcloud-base

https://gitee.com/FJ_WoMenDeShiJie/springcloud-config-server

https://gitee.com/FJ_WoMenDeShiJie/springcloud-config-client

-------------------------------------------------------------

PS: This series is updated from time to time, just for personal learning and sharing.

Content full reference bibliography:

Spring Cloud and Docker Microsoft Architecture

Spring Cloud Micro Services Actual Warfare,

In-depth Understanding Spring Cloud and Microservice Construction,

And Dashen Blog: https://blog.csdn.net/forezp/article/details/70148833
----------------------------------------------------------------


The following is from: https://blog.csdn.net/forezp/article/details/81041028

After config-server is started successfully, there are several ways of access path mapping:

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

 

Keywords: Spring git Maven Apache

Added by Kaizard on Tue, 30 Jul 2019 18:13:15 +0300