Spring Cloud | Chapter 9: Config Distributed Configuration Center

foreword

As online projects become larger and larger, each project is scattered with various configuration files. If a distributed development model is adopted, the required configuration files will continue to increase as the number of services increases. A change in basic service information will cause a series of updates and restarts, making operation and maintenance miserable and error-prone. The configuration center came into being to solve such problems.

Why Unified Management of Microservice Configuration

In the microservice architecture, the configuration management of microservices generally has the following requirements:

  • Centrally manage configuration. An application system using a microservice architecture may contain hundreds or thousands of microservices, so centralized management is very necessary.
  • Different environments have different configurations. There are different configuration files for development, testing, staging, and production.
  • Can be adjusted dynamically during operation.
  • The configuration can be automatically updated after modification. No reboot is required.

Introduction to Config

The Spring Cloud Config project is a configuration management solution for distributed systems. It includes two parts, Client and Server, Server 提供配置文件的存储、以接口的形式将配置文件的内容提供出去, Client通过接口获取数据、并依据此数据初始化自己的应用. Spring Cloud 使用 git 或 svn 存放配置文件uses git by default, and demonstrates it with git as the code.

quick start

Build the GIT repository

First, a folder config-server is created on github to store configuration files. In order to simulate the production environment, we create the following three configuration files:

// 开发环境
application-dev.properties
// 测试环境
application-test.properties
// 生产环境
application-pro.properties

And a key-value pair is written in each configuration file, which are: profile=dev-1.0、profile=test-1.0、profile=pro-1.0.
At the same time, create a new branch named config-server-v2.0.
insert image description here

server side

  1. Create a new project and add dependencies

    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    
  2. Modify the startup class Application.javaby @EnableConfigServerdeclaring that it is the configuration center server.

    @SpringBootApplication
    @EnableConfigServer
    public class ConfigApplication {
          
          
        public static void main(String[] args) {
          
          
            SpringApplication.run(ConfigApplication.class, args);
        }
    
    }
    
  3. Write a configuration fileapplication.yml

    server:
      port: 7376
    spring:
      application:
        name: config
      cloud:
        config:
          server:
            git:
              # 配置git仓库的地址
              uri: 
              # git仓库用户名
              username: 
              # git仓库密码
              password: 
    
  4. Test after launch. First of all, we need to test whether the server can read the configuration information on github, and directly access:http://localhost:7376/applicatiom/pro

    The returned information is as follows:

    {
          
          
    	name: "applicatiom",
    	profiles: [
    		"pro"
    	],
    	label: null,
    	version: "0dc8f44a675ab935b919962e294edcd864ed6306",
    	state: null,
    	propertySources: [
    		{
          
          
    			name: "https://github.com/Sun-xyu/config-server.git/application-pro.properties",
    			source: {
          
          
    				profile: "pro-1.0"
    			}
    		}
    	]
    }
    

    The information returned above includes the configuration file 位置、版本、配置文件的名称以及配置文件中的具体内容, indicating that the server has successfully obtained the configuration information of the git warehouse.

    If you directly view the configuration information in the configuration file, you can access: http://localhost:7376/applicatiom-dev.properties, and return:profile: dev-1.0

    If you directly view the configuration information in the configuration file of the branch, you can access: http://localhost:7376/config-server-v2.0/applicatiom-dev.properties, and return:profile: dev-2.0

    Add the configuration information in the configuration file applicatiom-dev.properties as: config: this is config server dev1.0, visit again in the browser http://localhost:7376/applicatiom-dev.properties, and return: config: this is config server dev1.0 profile: dev-1.0. Explain that the server will automatically read the latest submitted content

  5. The configuration files in the warehouse will be converted into web interfaces, and access can refer to the following rules:

    /{application}/{profile}[/{label}]
    /{application}-{profile}.yml
    /{label}/{application}-{profile}.yml
    /{application}-{profile}.properties
    /{label}/{application}-{profile}.properties
    

Asapplication-dev.properties an example, it application's application, profileis dev, labelis master. The client will choose to read the corresponding configuration according to the filled parameters.

Client

  1. Create a new project and add dependencies

    <dependencies>
    	<dependency>
    		<groupId>org.springframework.cloud</groupId>
    		<artifactId>spring-cloud-starter-config</artifactId>
    	</dependency>
    	<dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-web</artifactId>
    	</dependency>
    </dependencies>
    
  2. Create a normal startup class Application.java.

    @SpringBootApplication
    public class ConfigclientApplication {
          
          
        public static void main(String[] args) {
          
          
            SpringApplication.run(ConfigclientApplication.class, args);
        }
    }
    
  3. To write a configuration file, you need to configure two configuration files, application.propertiesand bootstrap.properties
    application.properties are as follows:

    server:
      port: 7377
    spring:
      application:
        name: config-client
    

    The bootstrap.properties are as follows:

    spring:
     application:
       # 对应config server 的配置文件的{application}
       name: client
     cloud:
       config:
         # 对应config server 地址
         uri: http://localhost:7376
         # 对应config server 所获取的配置文件的 {profile}
         profile: dev
         # 指定git仓库的分支,对应获取配置文件的{lable}
         label: master
    
    • spring.application.name : corresponds to the {application} part
    • spring.cloud.config.profile: corresponding to the {profile} part
    • spring.cloud.config.label: corresponding to the branch of git. If the configuration center uses local storage, this parameter is useless
    • spring.cloud.config.uri: the specific address of the configuration center
    • spring.cloud.config.discovery.service-id: Specifies the service-id of the configuration center, which is easy to expand to a high-availability configuration cluster.

    Special attention: The above spring-cloud-related properties must be configured in bootstrap.properties, so that the content of the config part can be loaded correctly. Because the relevant configuration of config will be prior to application.properties, and the loading of bootstrap.properties is also prior to application.properties.

  4. Write test classes. Use the @Value annotation to get the value of the server-side parameter

    @RestController
    public class ConfigClientController {
          
          
    
        @Value("${config}")
        private String config;
    
        @GetMapping("/config")
        public String helloConfig(){
          
          
            return config;
        }
    }
    
  5. Test, browser access http://localhost:7377/config, return this is config server dev1.0. Read the configuration in the git repository. It means that the parameters have been correctly obtained from the server. At this point, a complete server provides configuration services, and the example of the client obtaining configuration parameters is completed.

summary

The example about the configuration center is explained. If you manually modify application-dev.propertiesthe configuration information in: config=this is config server dev1.0 commit againSubmit to github, visit again in the browser http://localhost:7377/config, and return: this is config server dev1.0, indicating that the obtained information is still the old parameter, why is this? Because the SpringBoot project will only get the value of the configuration file when it is started, after modifying the github information, the client side does not get it again, which leads to this problem. How to solve this problem? I will introduce it in detail next time.

Guess you like

Origin blog.csdn.net/u012294515/article/details/88654433