Spring Cloud (9): Config local configuration center

I. Overview

What is a configuration center? In a distributed system based on microservices, each business module can be split into independent services, and multiple requests are used to assist in fulfilling a certain requirement. In a specific business scenario, A certain request needs to call multiple services to complete, so there is a problem. There are many configuration items corresponding to multiple services. Each service has its own configuration file. If a certain microservice configuration is modified, Then other service consumers also need to make corresponding adjustments. It is very troublesome to modify in the configuration file corresponding to each service consumer, and each service needs to be redeployed after the modification. At this time, it is necessary to manage the configuration of each service in a unified manner to facilitate deployment and maintenance, so Spring Cloud proposes a solution, which is Spring Cloud Config.

Spring Cloud Config can provide configuration services for multiple clients through the server. Spring Cloud Config can store configuration files locally or in remote warehouses. It is possible to modify the configuration of the microservice without restarting it. The specific operation is to create a Config Server and manage all configuration files through it.

Two, actual combat! local configuration center

  1. Create a Maven project, the pom.xml configuration is as follows:
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-config-server</artifactId>
	<version>2.0.2.RELEASE</version>
</dependency>
  1. Create application.yml, the configuration details are as follows:
server:
  port: 8762
spring:
  application:
    name: nativeconfigserver
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/shared

Notes

  • profiles.active: the way to obtain configuration files, native means to obtain from the local

  • cloud.config.server.native.search-locations: the path where the local configuration file is stored, classpath indicates the directory where the current file is located

3. Create a shared folder under the resources path, and create configclient-dev.yml under this directory. The content of the configuration file is as follows:

server:
  port: 8070
foo: foo version 1
  1. Create a startup class with the following code:
package com.zing;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class NativeConfigServerApplication {
    
    
	public static void main(String[] args) throws Exception {
    
    
		SpringApplication.run(NativeConfigServerApplication.class, args);
	}

}

Notes

  • @EnableConfigServer: declare a configuration center
  1. Create a client to read the configuration file of the local configuration center, first create a Maven project, the pom.xml configuration is as follows:
    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-config</artifactId>
    	<version>2.0.2.RELEASE</version>
    </dependency>
6. 创建一个 bootstrap.yml,文件名必须为这个,不能是 application.yml 这个默认配置文件的名称,因为我们的配置要从配置中心拿,我们只需要写一些其他的配置项信息,如下:
spring:
  application:
    name: configclient
  profiles:
    active: dev
  cloud:
    config:
      uri: http://localhost:8762
      fail-fast: true
注解说明
  • cloud.config.uri: the access path of the local Config Server

  • cloud.config.fail-fase: Set the client to give priority to judging whether the Config Server acquisition is normal

  • Get the target configuration file name configclient-dev.yml by splicing spring.application.name and spring.profiles.active, and go to the Config Server to find the file.

  1. Create a client startup class with the following code:
package com.zing;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

}
  1. Create the controller code as follows:
package com.zing.handler;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/native")
public class NativeConfigHandler {
    
    
	
	@Value("${server.port}")
	private String port;
	
	@Value("${foo}")
	private String foo;
	
	@GetMapping("/index")
	public String index() {
    
    
		return this.port+"-"+this.foo;
	}
	
}
  1. Start the configuration center service nativeconfigserver and the configuration center client nativeconfigclient in turn, but start the registration center differently, because the registration center address is not configured. After the two services are started, we can visit http://localhost:8070/native/index to see the following page: The
    insert image description here
    result is the same as we expected, the client nativeconfigclient reads the configuration file information of the nativeconfigserver in the configuration center, and the verification is successful The local configuration center function of Spring Cloud Config is implemented.

3. Summary

Spring Cloud Config can modify its configuration by modifying the configuration file in the configuration center without restarting the server, which facilitates our centralized management of cluster configuration. In addition to supporting local storage, Git and Subversion are also supported. In addition to the Spring Cloud Config configuration center, there is another component that has to be said, that is Spring Cloud Zipkin service tracking, please look forward to the next article of my Spring Cloud (10): Zipkin service tracking

One development engineer is also in the continuous learning stage, and the usual small experiences are shared from time to time. I hope that those who read the text I wrote can avoid detours and wish you success in work and study.
Bloggers have limited experience, if there are any shortcomings, welcome to communicate and improve together~ I hope to make progress together with you who are also in CSDN.

Author | Sweet Little Sweet Potato
Produced | Little Sweet Potato

Guess you like

Origin blog.csdn.net/qq_36836370/article/details/130901372