(Turn) The simplest Spring Cloud tutorial in history | Part 6: Distributed Configuration Center (Spring Cloud Config)

The latest Finchley version:
https://www.fangzhipeng.com/springcloud/2018/08/06/sc-f6-config.html

Or
http://blog.csdn.net/forezp/article/details/81041028

When talking about zuul in the previous article, it was mentioned that the configuration service is used to save the configuration files of each service. It is Spring Cloud Config.

1. Introduction

In a distributed system, due to the huge number of services, in order to facilitate the unified management and real-time update of service configuration files, a distributed configuration center component is required. In Spring Cloud, there is a distributed configuration center component spring cloud config, which supports the configuration service in the memory of the configuration service (that is, local), and also supports the remote Git warehouse. In the spring cloud config component, there are two roles, one is config server, and the other is config client.

Second, build Config Server

Create a spring-boot project (inherit the parent pom parent module in the first article), name it config-server, and its pom.xml:

<?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">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.forezp</groupId>
	<artifactId>config-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>config-server</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>com.forezp</groupId>
		<artifactId>sc-f-chapter6</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

Add the @EnableConfigServer annotation to the Application class at the entrance of the program to enable the configuration server function. The code is as follows:


@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ConfigServerApplication.class, args);
	}
}

The following needs to be configured in the application.properties file of the program configuration file:

spring.application.name=config-server
server.port=8888

spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/
spring.cloud.config.server.git.searchPaths=respo
spring.cloud.config.label=master
spring.cloud.config.server.git.username=your username
spring.cloud.config.server.git.password=your password
  • spring.cloud.config.server.git.uri: configure the git warehouse address
  • spring.cloud.config.server.git.searchPaths: configuration repository path
  • spring.cloud.config.label: configure the branch of the warehouse
  • spring.cloud.config.server.git.username: the username for accessing the git repository
  • spring.cloud.config.server.git.password: user password for accessing git warehouse
     

If the Git warehouse is a public warehouse, you don’t need to fill in the user name and password. If it is a private warehouse, you need to fill it in. This example is a public warehouse, so feel free to use it.

There is a file in the remote warehouse https://github.com/forezp/SpringcloudConfig/ There is a property in the config-client-dev.properties file:

foo = foo version 3

Start the program: visit http://localhost:8888/config-server/dev

{"name":"foo","profiles":["dev"],"label":"master",
"version":"792ffc77c03f4b138d28e89b576900ac5e01a44b","state":null,"propertySources":[]}

Prove that the configuration service center can obtain configuration information from a remote program.

The HTTP request URL and resource file path are mapped as follows:

/{application}/{profile}[/{label}]

  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

Note here: After experimentation, it should be a typesetting problem on the official website, which caused many people to misunderstand this section. The first line is the requested URL, and the following ones with .yml or .properties suffixes are obviously file paths that may be mapped to. (file name)

Three, build a config client

Re-create a springboot project, named config-client, its pom 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">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.forezp</groupId>
	<artifactId>config-client</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>config-client</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>com.forezp</groupId>
		<artifactId>sc-f-chapter6</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

Its configuration file bootstrap.properties:

spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri= http://localhost:8888/
server.port=8881

spring.cloud.config.label: Specify the branch of the remote warehouse

spring.cloud.config.profile

  • dev: development environment
  • test: test environment
  • pro: formal environment

spring.cloud.config.uri= http://localhost:8888/: Specify the URL of the configuration service center.

The entry class of the program, write an API interface "/hi", return the value of the foo variable read from the configuration center, the code is as follows:

@SpringBootApplication
@RestController
public class ConfigClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(ConfigClientApplication.class, args);
	}

	@Value("${foo}")
	String foo;
	@RequestMapping(value = "/hi")
	public String hi(){
		return foo;
	}
}

Open the website to visit: http://localhost:8881/hi, the web page displays:

foo version 3

This means that config-client gets the attributes of foo from config-server, and config-server is read from the git repository, as shown in the figure:

å¨è¿éæå ¥ å¾çæè¿ °

 

Four, follow-up 

The default strategy for locating property sources is to clone a git repository (at spring.cloud.config.server.git.uri) and use it to initialize a mini SpringApplication. The mini-application’s Environment is used to enumerate property sources and publish them via a JSON endpoint.

From the official website

The above passage is helpful to understand the role of spring-cloud-config : that is, spring-cloud-config-server pulls the property file by connecting to a media warehouse and initializes a mini Spring application. This mini Spring application takes the property file from Enumeration of the attributes obtained in the (in fact, a process of parsing file key-value pairs) and publish it as a JSON end (JSON endpoint is understood as a server that provides data in json format) 

Download the source code of this article:
https://github.com/forezp/SpringCloudLearning/tree/master/chapter6

Four, reference materials

spring_cloud_config

Read more

Summary of the simplest SpringCloud tutorials in history

SpringBoot tutorial summary

Summary of Java Interview Questions Series

 

Guess you like

Origin blog.csdn.net/u014225733/article/details/100581009