Sping Cloud Config Distributed configuration center

1. Application scenario of distributed configuration center

Often, we use configuration files to manage some configuration information, such as application.yml

Single application architecture, configuration information management, maintenance will not be particularly troublesome, manual operation can be, because it is only a project;

Microservice architecture. Because there may be many microservices in our distributed cluster environment, it is impossible for us to modify the configuration one by one and then restart it. In certain scenarios, we also need to dynamically adjust the configuration information during operation, such as: Dynamically adjust the data source connection pool size based on the load of each microservice, and we want the microservice to update automatically when the configuration changes.

The scenario is summarized as follows:

(1) Centralized configuration management, a microservice architecture may have hundreds of microservices, so centralized configuration management is very important (one change, effect everywhere)

(2) Different configurations in different environments. For example, data source configurations are different in different environments (dev, test, proD)

(3) Dynamic adjustment during operation. For example, the data source connection pool size can be dynamically adjusted according to the load of each microservice, and the configuration can be automatically updated after modification

(4) If the configuration content changes, microservices can automatically update the configuration, so we need to carry out centralized management of the configuration file, which is also the role of the distributed configuration center.

Then we need centralized management of configuration files, which is the role of a distributed configuration center

2. Spring Cloud Config

2.1 Config Introduction

Spring Cloud Config is a distributed configuration management solution, which consists of two parts: Server and Client

  • We need to develop a project publishing service that is registered in a registry with the configServer responsible for interacting with external Git

  • Microservices A, B, and C interact directly with the Configserver

Server: Provides storage of configuration files, provides the content of configuration files in the form of interfaces, and is easily embedded in Spring Boot applications by using @enableconFigServer annotations

Client: Obtains configuration data through the interface and initializes the application

2.2 Config Distributed configuration application

Server Configuration

(1) On Github, create a repository named lagou-config-repo and upload a configuration fileContent as follows:

(2) Build the unified configuration center of Config Server, create a SpringBoot project, introduce the dependent coordinate (need to register themselves to Eureka), named lagou-Cloud-configServer-9006

(3) Configure the POM file

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

  <! --config config center server -->
  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-server</artifactId>
  </dependency>
</dependencies>
Copy the code

(4) Create a startup class and add the configuration annotation @enableconFigServer


@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer // Enable the server configuration function
public class ConfigApp {
    public static void main(String[] args) { SpringApplication.run(ConfigApp.class,args); }}Copy the code

(5) Configure the application.yml file

server:
  port: 9006
eureka:
  client:
    serviceUrl: Path to eureka Server
      Eureka server can synchronize the registry with each eureka server
      defaultZone: http://lagoucloudeurekaservera:8761/eureka/,http://lagoucloudeurekaserverb:8762/eureka/
  instance:
    Use the IP address to register, otherwise the host name will be registered.
    prefer-ip-address: true
    # customize the display format of the instance, plus the version number, convenient multi-version management, note that the address is ip-address, the earlier version is ipAddress
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}:@project.version@

spring:
  application:
  name: lagou-cloud-config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/langkemaoxin/lagou-config-repo.git
          Git server address
          username: [email protected] Git user name
          password: xxxxx Git password
          search-paths:
            - lagou-config-repo
    # read branch
    label: main

# springBoot exposes breakpoint interface such as health check
management:
  endpoints:
    web:
      exposure:
        include: "*"
  Expose health interface details
  endpoint:
    health:
      show-details: always
Copy the code

(6) Access in the browser addresshttp://localhost:9006/main/lagou-server-resume-dev.ymlGit configuration information files are directly visible

Client Configuration

Let’s reform the resume micro service 8080

(1) Add configuration client and dependency

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-config-client</artifactId>
</dependency>	
Copy the code

(2) Change application. Yml to bootstrap. yml

Yml is system-level and has a higher priority than application.yml. This configuration file is checked during application startup and the service address of the configuration center is specified in this configuration file. All application configurations are automatically pulled and enabled. (Mainly put the configuration information connected to the unified configuration center into bootstrap.yml)

To add configuration information in the configuration file, specify the service address URI, service name, service suffix, and service branch

(3) Create a controller to test the configuration. Once we use SpringCloud Config, we get the configuration information as if we were using a local configuration file

@RestController
@RequestMapping("/config")
public class ConfigController {
    @Value("${lagou.message}")
    private String lagoumessage;
    
    @Value("${mysql.url}")
    private String mysqlUrl;

    @GetMapping("/viewConfig")
    public String viewConfig(a) {
        String result = "lagouMessage=" + lagoumessage + " mysqlUrl=" + mysqlUrl;
        returnresult; }}Copy the code

(4) Visithttp://127.0.0.1:8080/config/viewConfig

3. Manually refresh the Config

In production, configuration information is often modified, and the existing code cannot support dynamic update of the configuration file. The configuration file is added to the cache at startup. If you want to obtain the latest configuration information, you have to restart the service. But SpringCloud Config already provides a set of services to do just that

The solution is: Use the REFRESH interface of the ACTUATOR with POST requests to refresh the cache

(1) Add dependencies of Springboot-starter -actuator on Client (added)

(2) Add configuration to bootstrap.yml Client (expose communication endpoint)

management:
  endpoints:
    web:
      exposure:
        include: "*"
Copy the code

(3) Configuration similar to above, add annotations@RefreshScope

(4) manual Client Client POST request, http://localhost:8080/actuator/refresh, refresh configuration information

(5) Enter the test process and update the files of the warehouse first

Let’s change the file to DB333444

Access the configuration center service and find that the configuration has changed

Access the resume micro service and find that the configuration has not changed

Use the Post method to access the Refresh interface of the 8080http://localhost:8080/actuator/refresh

Revisiting the resume microservice’s configuration interface, the configuration has been updated

4. The Config is automatically updated

Implementation of a notice everywhere effective

Pull hook inside do distributed configuration, use the zk (storage + notice), zk data changes, can notify each monitor client, the client receives the notice after can make corresponding operation (memory levels of data directly into effect, for the database connection information, connection pool, such as information changes, so will be in the notification logic processing, Such as reinitializing the connection pool)

In microservice architecture, we can combine message Bus to realize automatic update of distributed configuration (Spring Cloud Config+Spring Cloud Bus).

4.1 message Bus

The message Bus is what we use all the timeMQ message brokerTo build aA common TopicThe MQ broadcast messages are listened to and consumed by all microservice instances in the registry by connecting microservice instances through this Topic. In other words, it connects various micro-services through a theme.

4.2 Spring Cloud Config+Spring Cloud Bus automatic refresh

MQ message broker, we also chose to use RabbitMQ, ConfigServer and ConfigClient both add support for the universal message bus and connection information to RabbitMQ

(1) Add message bus support to ConfigClient client and Config Server Server

<dependency>
 <groupId>org.springframework.cloud</groupId>
 <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
Copy the code

(2) Add configurations on the ConfigClient and Config Server

spring:
  rabbitmq:
    host: 127.0. 01.
    port: 5672
    username: guest
    password: guest
Copy the code

(3) Exposed ports

It is recommended that all ports be exposed
management:
 endpoints:
 web:
 exposure:
 include: "*"
Copy the code

(4) Change the Github file suffix to 777

(5) Visit the resume micro-service-config client and find the suffix is still 666

(6) Access the configuration centerhttp://localhost:9006/main/lagou-server-resume-dev.ymlYou can see that the configuration is updated

(7) Call the Bus refresh interface of configuration center, return nothing

(8) Visit resume micro-services-config client again and find the suffix changed to 777

(9) Open RibbitMQ to look and find the log record of the call

And found that the program automatically generated a Topic for us

4.3. Update the configuration information

In broadcast mode one request is implemented, updates everywhere, what if I just want to target updates? When a refresh request to http://localhost:9006/actuator/bus-refresh/lagou-service-resume:8081

That is, the service name that follows the last instance to be flushed: the port number

Code sample