Shangyangge springcloud config

Keywords: Java Spring Spring Cloud Microservices

SpringCloud Config configuration center

summary

What is it?

SpringCloud Config provides centralized external configuration support for microservices in the microservice architecture. The configuration server provides a centralized external configuration for all environments of different microservice applications.

How do you play?

Spring cloud config is divided into 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 access interfaces for the client to obtain configuration information, encryption / decryption information and so on.

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 I do

1 centralized management profile
2. Different environments have different configurations, dynamic configuration updates and deployment by environment, such as dev/test/prod/beta/release
3. Dynamically adjust the configuration 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.
4 when the configuration changes, the service can sense the configuration changes and apply the new configuration without restarting
5 expose the configuration information in the form of REST interface
Official website
https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/

Integrated configuration of server and GitHub

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

Configuration of server Config and construction of general control center

Create a new Repository on GitHub

Create a new Repository named springcloud config on GitHub with your own account

Create a new git warehouse and clone in the local disk directory

New module

New module spring-config-center-3344

pom file

<dependencies>
   <!--Add message bus RabbitMQ support-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    </dependency>
    <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.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>

yml

server:
  port: 3344

spring:
  application:
    name:  cloud-config-center #Microservice name registered into Eureka server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/lkp19960619/springcloud-config.git #GitHub the GIT repository name above
          #search for directory
          search-paths: /**#
          #User name of git
          username: xxxxxxx
          #git password
          password: xxxxxxx
          # Specify branch
          default-label: master

#Service registration to eureka address
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

Main startup class

@SpringBootApplication
//Open configuration center annotation
@EnableConfigServer
public class ConfigCenterMain3344 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterMain3344.class,args);
    }
}

Modify the hosts file under windows and add mapping

127.0.0.1 config-3344.com

Test whether the configuration content can be obtained from GitHub through the Config microservice

  • Start ConfigCenterMain3344
  • Browser access: http://config-3344.com:3344/master/config-dev.yml
  • Page return result:

Client use

Create a new module cloud-config-client-3355

POM file

<dependencies>
        <!--Add message bus RabbitMQ support-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <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.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>

Create a new bootstrap.yml file. The application.yml configuration file is a user level resource configuration item

bootstrap.yml is a system level resource configuration item with higher priority

SpringCloud will create a Bootstrap Context as the parent context of the Application Context of the Spring application.

During initialization, BootstrapContext is responsible for loading configuration properties from external resources 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.

It is crucial to change the application.yml file under the Client module to bootstrap.yml, because bootstrap.yml is loaded before application.yml. Bootstrap.yml has higher priority than application.yml.

Main start

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

Imitation business class

@RestController
public class ConfigClientController {

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

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

Access results

It can be seen that the configuration information of the dev environment configured by the server can be obtained through the Rest interface of the client.

Attendant problems

If we want to modify the contents of a configuration file, the Config server will be refreshed after modification, and the configuration center will respond immediately, but its client will not respond unless the client restarts or reloads itself. This is the dynamic refresh of the distributed configuration in the Config configuration center.

Config dynamic refresh manual version

step1
Modify the pom file of the client module and add actor monitoring

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

step2
Modify the bootstrap.yml configuration file and add the exposure monitoring port configuration

# Exposure monitoring endpoint
management:
  endpoints:
    web:
      exposure:
        include: "*"

step3
Add @ RefreshScope annotation to business class

@RestController
//Support dynamic refresh
@RefreshScope
public class ConfigClientController {

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

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

test

At this time, modify the content of the configuration file in github - > access 3344 - > access 3355

It is found that the configuration of 3344 can be changed to the latest configuration, but the configuration of 3355 has not been updated to the latest. We need to do more work. That is, the POST request notifies the client that the configuration has been modified.

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


At this point, when you visit the client again, you will find that the configuration has been changed to the latest. Service restart is avoided.
Think about what else?

If there are multiple microservice clients 3355 / 3366 / 3377
Each microservice needs to execute a post request and refresh manually?
Can we broadcast one notice and take effect everywhere?
We want a wide range of automatic refresh to find the method

Posted by clapton on Tue, 02 Nov 2021 10:07:28 -0700