Config distributed configuration center

Introduction to Config

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. 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

Spring Cloud Config is divided into two parts: server and client. The server is also known as the distributed configuration center. It is an independent micro service application, which is used to connect the configuration service 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.

What can Spring Cloud Config do?

  • Centrally manage profiles
  • Different environments have 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 sends changes, the service can sense the configuration changes and apply the new configuration without restarting
  • Expose the configuration information in the form of REST interface

Config server configuration

Create a new Config unified configuration management warehouse on Gitee

Introduce dependency

spring-cloud-config-server

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</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-actuator</artifactId>
        </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>

configuration file

server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/czshh0628/spring-cloud-config.git # warehouse address
          search-paths:
            - Spring-Cloud-Config # Warehouse name
      label: master # Which branch
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
# Exposed bus refresh configured endpoint
management:
  endpoints:
    web:
      exposure:
        include: "*"

Main startup class

Open @ enableconfig server annotation

@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {

Test access

Configure read rules

label: branch Name: service name profiles: environment (dev/test/prod)

  • /{application}-{profile}.yml http://config-3344.com:3344/application-dev.yml
  • /{application}/{profile}[/{label}] http://config-3344.com:3344/application/dev/master
  • /{label}/{application}-{profile}.yml http://config-3344.com:3344/master/application-dev.yml

Config client configuration

application.yml is a user level resource configuration item

bootstrap.yml is system level with higher priority

Spring Cloud will create a Bootstrap Context as the parent context of the Application Context of spring application. During initialization, Bootstrap Context is responsible for loading configuration properties from external sources and parsing the configuration. The two contexts share an Environment obtained from the outside

Bootstrap attributes have high priority. By default, they will not be overwritten by local configuration. Bootstrap Context and Application Context have different conventions, so a bootstrap is added YML file to ensure the separation of Bootstrap Context and Application Context configuration

To add the application. Under the Client module Change the YML file to bootstrap YML, this is critical because bootstrap YML is better than application YML is loaded first. bootstrap.yml takes precedence over application yml

Introduce dependency

spring-cloud-starter-config

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</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-actuator</artifactId>
        </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>

configuration file

bootstrap.yml

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master # Branch name
      name: config #Profile name
      profile: dev # The read suffix, which is the combination of the above three, is the configuration file of config-dev.yml on the master branch, http://config-3344.com:3344/master/config-dev.yml
      uri: http://127.0.0.1: address of 3344 # configuration center
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
# Exposed bus refresh configured endpoint
management:
  endpoints:
    web:
      exposure:
        include: "*"

Main startup class

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {

controller test

@RestController
@RefreshScope // Dynamic refresh
public class ConfigClientController {

    @Value("${spring.profiles}")
    private String configInfo;

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

visit http://localhost:3355/configInfo

Dynamic refresh

The spring boot starter actuator is introduced, the monitoring function is enabled, the endpoint exposing the bus refresh configuration is configured in the configuration file, and the @ RefreshScope dynamic refresh is enabled by the controller. Therefore, the manual dynamic refresh is realized to obtain the latest configuration, and the staff also need to manually send the POST request curl - X“ http://localhost:3355/actuator/refresh "Get the latest configuration

Keywords: Spring Cloud

Added by jackyhuphp on Fri, 18 Feb 2022 17:57:58 +0200