5, Service configuration

Keywords: Spring Cloud config

1, Spring Cloud Config distributed configuration center

Spring Cloud Config

1. Configuration 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. Because each service needs the necessary configuration information to run, a centralized and dynamic configuration management facility is essential.

2. What is it

Spring Cloud Config provides centralized external configuration support for microservices in the microservice architecture, and the configuration server provides a centralized external configuration for all environments of different microservice applications;

2.1 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 and encrypting / decrypting information;
  • The client manages application resources and business-related configuration contents 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, And the GIT client tool can be used to manage and access the configuration content conveniently.

3. What can I do

  1. Centralized management of configuration files;
  2. Different environments and configurations, dynamic configuration updates, and deployment by environment (such as dev, test, prod, beta, release);
  3. 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 its own configuration information from the configuration center;
  4. When the configuration changes, the service can sense the change of the configuration and apply the new configuration without restarting;
  5. Expose the configuration information in the form of REST interface (post, curl access and refresh).

4. Config Server

Implement Config Server to obtain configuration information through GitHub

4.1 create a new module cloud config center3344
4.2 POM
<?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">
    <parent>
        <artifactId>cloud-2020</artifactId>
        <groupId>com.qs.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-center3344</artifactId>

    <dependencies>
        <!--config-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!--eureka-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--Hot deployment-->
       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>
4.3 YML (modify hosts file)

Modify hosts file

# Add mapping configuration
127.0.0.1 config3344.com
server:
  port: 3344

spring:
  application:
    # Microservice name registered into Eureka server
    name:  cloud-config-center
  cloud:
    config:
      server:
        git:
          # Git address
          uri: https://gitee.com/qshome/springcloud-config.git
          # search for directory
          search-paths:
            - springcloud-config
      # Read branch
      label: master

eureka:
  client:
    service-url:
      # Address of Registration Center
      defaultZone: http://localhost:7001/eureka
4.4 main startup
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {

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

http://config3344.com:3344/master/config-dev.yml

5. Configure read rules

/{label}/{application}-{profile}.yml (label branch, name, service name, profiles environment)

  • master branch
    http://config3344.com:3344/master/config-dev.yml
    http://config3344.com:3344/master/config-test.yml
    http://config3344.com:3344/master/config-prod.yml
  • dev branch
    http://config3344.com:3344/dev/config-dev.yml
    http://config3344.com:3344/dev/config-test.yml
    http://config3344.com:3344/dev/config-prod.yml

/{application}-{profile}.yml
http://config3344.com:3344/config-dev.yml
http://config3344.com:3344/config-test.yml
http://config3344.com:3344/config-prod.yml

/{application}/{profile}[/{label}]
http://config3344.com:3344/config/dev/master
http://config3344.com:3344/config/test/master
http://config3344.com:3344/config/test/dev

6. Config Client

Enable client 3355 to access Config Server 3344 and obtain configuration information through GitHub

6.1 create a new module cloud config client3355
6.2 POM
<?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">
    <parent>
        <artifactId>cloud-2020</artifactId>
        <groupId>com.qs.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-client3355</artifactId>

    <dependencies>
        <!--config-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!--eureka-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--Hot deployment-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>
6.3 YML(bootstrap.yml)
  • bootstrap.yml is system level, with higher priority;
  • applicaiton.yml is a user level resource configuration item;
  • Spring Cloud will create a "Bootstrap Context" as the parent context of the Application Context of the spring application. During initialization, the Bootstrap Context is responsible for loading configuration properties from an external source and parsing the configuration. The two contexts share an Environment obtained from the outside.
  • Bootstrap properties have high priority. By default, they are not overwritten by local configuration. Bootstrap context and Application Context have different conventions, so a bootstrap.yml file is added to ensure the separation of bootstrap context and Application Context configuration.
server:
  port: 3355

spring:
  application:
    name: cloud-config-client
  cloud:
    # Config client configuration http://config3344.com:3344/master/config-dev.yml
    config:
      # Branch name
      label: master
      # Profile name
      name: config
      # Read suffix name
      profile: dev
      # Configuration center address
      uri: http://localhost:3344

eureka:
  client:
    service-url:
      # Address of Registration Center
      defaultZone: http://localhost:7001/eureka
6.4 main startup
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientMain3355.class, args);
    }
}
6.5 ConfigClientController
@RestController
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo() {
        return configInfo;
    }
}
6.6 testing

http://localhost:3355/configInfo

7. Config Client dynamic refresh

  • Dynamic refresh of distributed configuration
  1. O & M modifies the content of the configuration file on GitHub;
  2. Refresh 3344 and find that the Config Server configuration center responds immediately;
  3. Refresh 3355 and find no response from Config Client;
  4. 3355 does not change unless you restart or reload yourself;
7.1 modify module cloud config client3355
7.2 POM

Introducing actor monitoring

<!--actuator-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
7.3 YML(bootstrap.yml)
# Exposure monitoring endpoint
management:
  endpoints:
    web:
      exposure:
        include: "*"
7.4 modifying ConfigClientController

@RefreshScope

@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo() {
        return configInfo;
    }
}
7.5 manual refresh

curl -X POST "http://localhost:3355/actuator/refresh"

7.6 testing

http://config3344.com:3344/master/config-dev.yml ok
http://localhost:3355/configInfo ok

Posted by ahinkley on Thu, 14 Oct 2021 15:12:21 -0700