[Spring Cloud] Spring Cloud Config Implements Distributed Configuration Center

Keywords: Java Spring git Maven Apache

Spring Cloud Config Implements Distributed Configuration Center

1. Distributed Configuration Center

In distributed systems, there are a large number of service applications, and each application needs a corresponding configuration file to help complete the initialization and operation of the service environment.As a result, a large number of service profiles have been produced, and Spring Cloud Config implements the unified management of configuration files. It supports placing configuration services in server-side memory (i.e., local memory on the server side) and it also supports git by default, so we can also place configuration files in the GIT repository so that we canAccess and development of.

2. Start Spring Cloud Config

There are many ways to manage configurations, such as using Eureka or Spring Cloud Config, and the file system, this time using the Spring Cloud Config + Git repository.

In this Spring Cloud Config component, there are three roles: Config Server, Config Client, and Git Remote Warehouse.

Build Config Server for Configuration Center Server

Create a new Spring Boot project

Create pom.xml 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.jojo</groupId>
    <artifactId>configurationserver</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Config Server</name>
    <description>Config Server demo for demo project</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <version>2.0.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.jojo.configuration.server.ConfigServerApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Create Spring Cloud Config boot class

package com.jojo.configuration.server;

import ch.qos.logback.classic.joran.action.ConfigurationAction;
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(ConfigurationAction.class, args);
    }

}

Create the application.yml configuration file in the resources directory and configure it as follows

spring:
  application:
    name: Config Server
  cloud:
    config:
      label: master
      server:
        git:
          uri: http://ip:port/path
          search-paths: repos
          username: username
          password: password

server:
  port: 8888

Profile description:

  • spring.cloud.config.label: Branch of the configured remote warehouse
  • spring.cloud.config.server.git.uri: Configure Git repository addresses (Github, GitLab, Code Cloud...)
  • spring.cloud.config.server.git.search-paths: Directory where configuration files are placed in the configuration Git repository
  • spring.cloud.config.server.git.username: Configure the user name to access the Git repository
  • spring.cloud.config.server.git.password: Configure the password to access the Git repository

Notice the implementation:

  • If GitLab is used as the warehouse, git.uri needs to be appended with.Git at the end, not GitHub.

Create Distributed Configuration Center Client

This client can be easily exemplified by a Spring Boot Admin program. Local configuration files only do simple docking configuration, placing configuration related to Spring Boot Admin in a remote warehouse.

Create a pom.xml file in the project directory

<?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.jojo</groupId>
    <artifactId>configurationclient</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Config Client</name>
    <description>Config Client demo for demo project</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

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

        <dependency>
            <groupId>org.jolokia</groupId>
            <artifactId>jolokia-core</artifactId>
            <version>1.6.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.0.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.jojo.configuration.client.ConfigClientApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Configuration boot class

package com.jojo.configuration.client;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

Create the application.yml configuration file in the resources directory and configure it as follows

spring:
  cloud:
    config:
      uri: uri
      label: master
      name: config-client
      profile: dev

Next, the configuration file is described:

  • spring.cloud.config.uri: Configure the web address for the Distributed Configuration Center
  • spring.cloud.config.label: Configure branches of a remote Git repository
  • spring.cloud.config.name: Configure the prefix of the remote profile name corresponding to this service
  • spring.cloud.config.profile: The environment identity of the configuration file

Matters needing attention:

  • The default port for servers in distributed configurations is 8888. If the default port is modified, client projects cannot configure spring.cloud.config.uri in application.yml or application.properties and must be configured in bootstrap.yml or bootstrap.properites because the configuration that starts with bootstrap will beLoad and configure first.

Configuring a remote Git repository

Create a new project in remote Git, create a repos directory in the project, create a config-client-dev.yml configuration file in the directory, and configure the following in the file:

spring:
  application:
    name: Config Client

server:
  port: 8081

management:
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: health,info

test

Open your browser and visit http://localhost:8081. If you see the following interface, it means the configuration is successful!

3. HTTP Request Address and Resource File Mapping

  • http://ip:port/{application}/{profile}[/{label}]
  • http://ip:port/{application}-{profile}.yml
  • http://ip:port/{label}/{application}-{profile}.yml
  • http://ip:port/{application}-{profile}.properties
  • http://ip:port/{label}/{application}-{profile}.properties

Posted by POGRAN on Sun, 08 Sep 2019 19:44:13 -0700