Teach you how to build a SpringCloud project (14) Integrate Config Distributed Configuration Center

What are microservices? A series will be seen at a glance!

1. Teach you how to build a SpringCloud project (1) Detailed explanation with pictures and texts, fool-like operation

2. Teach you how to build a SpringCloud project (2) Producers and consumers

3. Teach you how to build a SpringCloud project (3) Integrate the Eureka service registration center

4. Teach you how to build the SpringCloud project (4) Eureka cluster version construction

5. Teach you how to build the SpringCloud project (5) Build the producer cluster version

6. Teach you how to build a SpringCloud project (6) Eureka realizes service discovery

7. Teach you how to build a SpringCloud project (7) Integrate the Consul service registration center

8. Teach you how to build a SpringCloud project (8) Integrated Ribbon load balancer

9. Teach you how to build a SpringCloud project (9) Integrate OpenFeign service interface calls

10. Teach you how to build a SpringCloud project (10) Integrate Hystrix service downgrade

11. Teach you to build a SpringCloud project (11) Integrating Hystrix's service fuse

12. Teach you how to build a SpringCloud project (12) Integrate Hystrix's graphical Dashboard real-time monitoring

13. Teach you how to build a SpringCloud project (13) Integrate a new generation of Gateway

14. Teach you how to build a SpringCloud project (14) Integrated Config Distributed Configuration Center

15. Teach you how to build a SpringCloud project (15) Integrated Bus message bus

16. Teach you how to build a SpringCloud project (16) Integrated Stream message driver

17. Teach you how to build a SpringCloud project (17) Integrating Sleuth distributed link tracking

Continue to update, welcome to like and follow!

Why is there a Spring Cloud Config distributed configuration center?

Microservices means splitting the business in a single application into sub-services. The granularity of each service is relatively small, so there will be a large number of services in the system. Since each service requires the necessary configuration information to run, So a set of centralized and dynamic configuration management facilities is essential. Each of our services has its own configuration file. If there are hundreds of microservices, there will be hundreds of configuration files. If you want to change a certain configuration parameter, you may need to modify the configuration files of several microservices. Isn't it time-consuming and laborious, Spring Cloud provides the ConfigServer configuration center to solve this problem and realize it 一处修改,处处运行.

What is Spring Cloud Config Distributed Configuration Center?

Spring Cloud Config provides centralized external configuration support for microservices in the microservice architecture, and the configuration server provides centralized external configuration for all environments of different microservice applications. As shown below:
insert image description here

Spring Cloud Config is divided into two parts, the server and the client. The server also becomes a distributed configuration center. It is an independent microservice application used to connect to the configuration server and provide the client with access to configuration information, encryption and decryption information, etc. access interface.

The client manages application resources and business-related configuration content through the designated 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 Version management is performed on the environment configuration, and the configuration content can be easily managed and accessed through the git client tool.

What does Spring Cloud Config do?

  1. Centralized management of configuration files for easy management
  2. Different environments have different configurations, dynamic configuration updates, sub-environments such as: dev/test/prod/bata/release
  3. The configuration is dynamically adjusted during operation, and it is no longer necessary to write configuration files on the machine where each service is deployed. The service will statistically pull configuration information from the configuration center.
  4. When the configuration changes, the service can sense the configuration change and apply the new configuration without restarting
  5. Expose the configuration information with the Rest interface, and post/curi access can be refreshed

Spring Cloud Config Getting Started Example

1. Spring Cloud Config, which uses GitHub as the configuration file storage by default. In addition to git, you can also use databases, svn, local files, etc. as storage. And it is in the form of http/https access. Use our own account (if you don't go to github.com to register an account) to create a new Repository named springcloud-config on GitHub.

2. Create a new warehouse on github, as shown below:
insert image description here

3. Copy the git address of the newly created warehouse

insert image description here

4. Create a new git repository locally
insert image description here

5. Open the command line operation of git (right click on the desktop and select Git Base Here), enter the address of the newly created git warehouse and execute the git clone GitHub - wang-so/springcloud-config command

insert image description here

6. Create new development, production, and test configuration files and submit them to github. I only create a new test file config-dev.yml for submission here, as shown in the figure below:
insert image description here
insert image description hereinsert image description here

After the submission is successful, we can see that the file already exists on github, as shown below

insert image description here

7. Create a new configuration center module cloud-config-center with port 3344, which is the same as the previous method of creating a new module (see the first article in this series if you don’t know how to do it). The following picture shows the current project structure

insert image description here

If we create a new one successfully, cloud-config-center is automatically added to the modules tag in the total POM.XML file.

insert image description here

8. Next, let's configure first. First, we configure the POM.XML file.

<?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>mcroservice</artifactId>
        <groupId>com.study.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
 
    <artifactId>cloud-config-center</artifactId>
    <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>
 
</project>

9. Create a new application.yml configuration file under the resources file, the configuration is as follows

server:
  port: 3344
spring:
  application:
     name: cloud-config-center #注册进Eureka服务器的服务名称
  cloud:
     config:
       server:
         git:
           skipSslValidation: true # 跳过ssl认证
           uri: https://github.com/wang-so/springcloud-config.git  #GitHub上复制的项目地址
           search-paths:  #搜索目录
             - springcloud-config
       label: master   #读取分支
#服务注册到​eureka地址
eureka:
  client:
     service-url:
       defaultZone: http://eureka7001.com:7001/eureka

10. Create a new startup class

package com.buba.springcloud;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
 
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(ConfigServerApplication.class,args);
    }
}

Careful friends should be able to find that if we create multiple module sub-projects in the Springboot project, our newly created application.yml file will not become a small green leaf. This situation does not actually affect our use. But being an OCD one has to make it consistent. The solution is as follows:

Because I have already added it, there is no Spring option in the list that appears after clicking Add. Usually we choose Spring.
insert image description here

Continue to select and find the yml file to add.
insert image description here

Another possible pitfall is that the color of the newly created module sub-project is different from other sub-projects, and the color will be slightly gray. The reason for this may be that idea ignores the maven module. You can try the following solutions: Select in the menu file setting, search for maven, then select Ignored Files, and see if the grayed-out maven module in the right panel is checked. Checking means that the pom file of this module is ignored. Uncheck it to solve it. Portal

insert image description here

11. Modify the hosts file and add a mapping to make the local machine simulate the URL of the website service configuration center. The address of the host file is C:\Windows\System32\drivers\etc. The hosts file opens three configurations, which are the Eureka service registration center cluster and now Configured service configuration center:
insert image description here

12. The server side of the configuration center has been set up. Let’s start eureka first, and then start cloud-config-center. First visit http://eureka7001.com:7001/, you can see that the configuration center has been registered for /eureka Among them, as shown below:

insert image description here

Visit http://config-3344.com:3344/master/config-dev.yml again, we can see that the 3344 microservice can successfully read the remote configuration file. As shown below:

insert image description here

Here, after the git version is upgraded, our main branch is changed from master to main, so we need to use main for access. If you use master, you cannot access it. You can read the branch configuration label in the yml configuration file and use master or main.

13. Up to now, the part of our configuration center server to read the configuration file from the remote Git warehouse has been completed, and then the client of the client above will be built.
insert image description here

14. Create our configuration center module cloud-config-client, the port is 3355, the same way as before, create a new module (see the first article in this series if you don’t know), configure the pom.xml file, mainly to add The jar package of spring-cloud-starter-config, as shown below:

<?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>mcroservice</artifactId>
        <groupId>com.study.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
 
    <artifactId>cloud-config-client</artifactId>
    <dependencies>
    <!--spring-cloud客户端-->
    <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>
 
</project>

15. Then we create a new configuration file bootstrap.yml, why not use application.yml here? ? ?

application.yml is a user-level resource configuration item, while bootstrap.yml is a system-level resource configuration item. Bootstrap.yml has a higher priority. SpringCloud will create a "Bootstrap Context" as the "Application Context" of Spring applications. parent context.

When initializing, "Bootstrap Context" is responsible for loading configuration properties from external sources and parsing configurations. These two contexts share an "Environment" obtained from the outside. "Bootstrap" properties have high priority, by default, they will not be overridden by local configuration.

The two contexts "Bootstrap Context" and "Application Context" have different conventions, so a new bootstrap.yml file is added to ensure that the configurations of these two contexts are separated. The bootstrap.yml configuration file is as follows:

server:
  port: 3355
 
spring:
  application:
    name: config-client
  cloud:
    #Config客户端配置
    config:
      label: master #分支名称
      name: config #配置文件名称
      profile: dev #读取后缀名称   上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
      uri: http://localhost:3344 #配置中心地址
 
#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka

16. Create a new startup class and service class, as shown in the figure below:

package com.buba.springcloud;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
 
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(ConfigClientApplication.class,args);
    }
}
package com.buba.springcloud.controller;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class ConfigClientController {
    
    
    @Value("${config.info}")
    private String configInfo;
 
    @GetMapping("/config")
    public String getConfigInfo() {
    
    
        return configInfo;
    }
}

17. The construction of the configuration center is completed. Next, we will test whether the client can obtain configuration information by accessing the configuration center. First start the Eureka service registration center and the Config service configuration center, and then start the service configuration center client 3355. Visit localhost:3355/config.

We can obtain the configuration file obtained from the remote end through the server. As shown below:

insert image description here

18. But now there is such a problem. When we modify the content of the configuration file on GitHub and refresh the server server of the configuration center, we find that the server configuration center of the S server immediately responds and refreshes the configuration information. However, we refresh the client client, It is found that there is no response, and the configuration information is still the original configuration information. It can’t be that every time the configuration file is modified remotely, the client needs to be restarted to reload the configuration information, right? To solve this problem, we need to use dynamic refresh. We only need to add actuator monitoring on the client client, and we need to add this dependency to the pom file of the client client, as shown in the following figure:

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

19. Modify the bootstrap.yml configuration file and add the following configuration to expose monitoring breakpoints:

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

20. Add the @RefreshScope annotation to the business class ConfigClientController to enable the client service to have a refresh function, as shown in the figure below:

@RestController
@RefreshScope
public class ConfigClientController {
    
    
 
    @Value("${config.info}")
    private String configInfo;
 
    @GetMapping("/config")
    public String getConfigInfo() {
    
    
        return configInfo;
    }
 
}

21. Send a POST request to refresh the client Client. The refresh request must be sent before the client can obtain the refreshed information. The request to refresh the client must be a POST request. As shown below:

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

When the following information appears, it means that the client has been successfully activated and refreshed. If you visit again, you can get the refreshed configuration information.
insert image description here

We have finished learning Spring Cloud Config here, so easy!

But what if we have multiple microservice clients? Does every microservice need to perform a POST request for manual refresh? We can make a notification through broadcasting, which will take effect everywhere. Here is the knowledge message bus to be learned in the next section-Spring Cloud Bus.

In the next article, we will study the Spring Cloud Bus distributed configuration center, continue to learn and update, and the next section will be more exciting! Friends are welcome to like and follow! grateful!
insert image description here

おすすめ

転載: blog.csdn.net/weixin_39570655/article/details/131831433
おすすめ