Spring Cloud Config distributed configuration

Posted by biggieuk on Sun, 24 Oct 2021 19:54:20 +0200

summary

Configuration file problems faced by distributed systems

Microservice means to split the business in a single application into one sub service. The granularity of each service is relatively small, so there will be a large number of services in the system. Since each service needs the necessary configuration information to run, a set of centralized and dynamic configuration management facilities is essential. spring cloud provides configServer to solve this problem

SpringCloud config distributed configuration center

spring cloud config provides centralized external support for microservices in the microservice architecture. The configuration server provides a centralized external configuration for all links of different microservice applications.

  • spring cloud config is divided into two parts: server and client.

    • The server is also called distributed configuration center. It is an independent micro service application, which is used to connect to the configuration server and provide the client with access interfaces such as obtaining configuration information, encryption and decryption information.

    • The client manages application resources and business-related configuration content through the specified configuration center, and obtains and loads configuration information from the configuration center at startup. The configuration server uses git to store configuration information by default, which is helpful for version management of environment configuration. The GIT client tool can be used to manage and access the configuration content conveniently.

Role of spring cloud config distributed configuration center

  • Centralized management profile
  • Different environments, different configurations, dynamic configuration updates, and deployment by environment, such as / dev /test /prod /beta /release
  • The configuration is dynamically adjusted during operation. It is no longer necessary to write configuration files on each service deployed machine. The service will uniformly pull and configure its own information from the configuration center
  • When the configuration changes, the service does not need to restart, it can sense the configuration changes and apply the new configuration
  • Expose the configuration information in the form of REST interface

Integration of spring cloud config distributed configuration center and GitHub

spring cloud config uses git to store configuration files by default (there are other ways, such as self-contained SVN and local files), but Git is the most recommended and is accessed in the form of http / https.

introduction

Spring cloud config server, get the configuration

Create a new springcloud-config-server-3344 module

Add dependency

    <dependencies>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-config-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version>3.0.5</version>
        </dependency>

    </dependencies>

application.yml configuration file
Configuration get configuration resources from git

server:
  port: 3344

spring:
  application:
    name: springcloud-config-server
  # Connection code cloud remote warehouse
  cloud:
    config:
      server:
        git:
          # Note that it is https, not ssh
          uri: https://gitee.com/yanyustudy/springcloud-config.git
          # You can connect to git through config server to access its resources and configuration~

Main startup class, open annotation

@EnableConfigServer // Start the spring cloud config server service
@SpringBootApplication
public class Config_server_3344 {
    public static void main(String[] args) {
        SpringApplication.run(Config_server_3344.class,args);
    }
}

Test get configuration resources
The default strategy for locating resources is to clone a git repository (in spring.cloud.config.server.git.uri) and use it to initialize a mini spring application.

The HTTP service has resources in the following format:

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

The SpringCloud config client completes the configuration through the server

Create a new springcloud config client module

Add dependency

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-bootstrap -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
            <version>3.0.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>3.0.5</version>
        </dependency>


        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--actuator Improve monitoring information-->
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.5.5</version>
        </dependency>
    </dependencies>

configuration file
bootstrap.yml is a system level configuration file higher than application.yml to prevent remote configuration conflicts

# System level configuration
spring:
  cloud:
    config:
      name: config-client # The resource name that needs to be read from git without suffix
      profile: dev
      label: master  #branch
      uri: http://localhost:3344

application.yml

# User level configuration
spring:
  application:
    name: springcloud-config-client

Main startup class

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

test
Create controller/ConfigClientController class test

@RestController
public class ConfigClientControlle {

        @Value("${spring.application.name}")
        private String applicationName; //Get microservice name

        @Value("${eureka.client.service-url.defaultZone}")
        private String eurekaServer; //Get Eureka services

        @Value("${server.port}")
        private String port; //Get the port number of the server


        @RequestMapping("/config")
        public String getConfig(){
            return "applicationName:"+applicationName +
                    "eurekaServer:"+eurekaServer +
                    "port:"+port;
        }

}

Topics: git Spring svn Spring Cloud