Spring Cloud Starter Series Octal - Center Config configuration of the build server and client

Import Scene 1

In a distributed system, each item has a wide variety of profiles, as we continue to develop and increase service profile has also increased. Every modification of the service information, results in an update and restart. To solve this problem, we urgently need new solutions, distribution center so into our field of vision.

2 Configuration Center Spring Cloud Config

Spring Cloud Config is a configuration management solution to address the distributed system. It includes client and server in two parts, the server can provide storage profile, by way of an interface configuration file contents advertised, and the client through the interface to obtain data. Spring Cloud Config use git to store configuration files by default.

3 Construction of a simple configuration center

We started as a service center configured server, and then configure each service need to get as a client to the server to obtain the configuration.

Create your own git repository in two profiles, one for consumer-dev.properties, the other is consumer-pro.properties
Here Insert Picture Description
content file is to set a consumer.saying property, the value of the two documents were Hello World / I want to do something.

It should be noted that the file name can not indiscriminately take (Normally no one chaos to take it). consumer-dev.properties and consumer-pro.properties are different versions of the same project, which project called consumer, dev corresponds development environment, pro corresponds to a production environment.

We'll start building the distribution center server, the server relies first introduced Config

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>

Then the configuration file is necessary, meaning each new configuration I have written in the notes, and we can not understand serious look at notes.

server.port=8087
spring.application.name=config

# git仓库的地址
spring.cloud.config.server.git.uri=https://github.com/LingHaoYuan/config
# git仓库的账号                        
spring.cloud.config.server.git.username=
# git仓库的密码                                           
spring.cloud.config.server.git.password=
# 配置文件分支
spring.cloud.config.server.git.default-label=master
# 配置文件相对地址
#spring.cloud.config.server.git.search-paths=

Finally, we need to add @EnableConfigServer annotation on the class to add support for startup Config service side.

package com.example.Config;

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

@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {

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

}

Next, we first test whether the central server configuration can be read github profile on your browser to http: // localhost: 8087 / consumer / dev, results are as follows:

{“name”:“consumer”,“profiles”:[“dev”],“label”:null,“version”:“07719761462c62db3333a050d8c8a8a7be96bb21”,“state”:null,“propertySources”:[{“name”:“https://github.com/LingHaoYuan/config/consumer-dev.properties”,“source”:{“consumer.saying”:"“Hello World”"}}]}

We can see that the return information contains information of all kinds profiles that illustrate the configuration server center can acquire git repository of profile information. If we want to directly view the profile information can be entered directly in the browser http: // localhost: 8087 / consumer-dev.properties, find it does return configuration information

consumer.saying: “Hello World”

In fact, Spring Cloud Config is a set of access rules, we can directly access through access rules.

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

{Application} represents the name of the configuration file, {profile} indicates the version of the configuration file, such as the development version is dev, {label} represent git branch, the default is the master branch.

Configuration Center client combat

We created the configuration center server before, and now we continue to build a distribution center of the client, and the client attempts to obtain information about the server's configuration.

Before we use consumer items, first add clients rely Config

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

Then we add a profile bootstrap.properties

# 对应{application}部分
spring.cloud.config.name=consumer
# 对应{profile}部分
spring.cloud.config.profile=dev
# 配置中心的地址
spring.cloud.config.uri=http://localhost:8087/
# 对应git的分支
spring.cloud.config.label=master

Note that only related to the spring-cloud properties in bootstrap.properties configuration, config is part of the contents in order to be loaded correctly. Because of config configuration prior to application.properties, and bootstrap.properties Esen loaded application.properties.

Start class does not need to change, and then we rewrite controller

package com.example.EurekaConsumer.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ConfigTestController {

	@Value("${consumer.saying}")
    private String saying;
	
	@RequestMapping("/test")
	@ResponseBody
	public String test() {
		return saying;
	}
}

We start the configuration center server and the client, visit http: // localhost: 8082 / test, found that returns "Hello World", explained the client can obtain the correct configuration information from the server, our configuration center clients also achieved success .

Reference: springcloud (VI): Configuration Center git example
Spring Cloud Config for configuration center, to see which one is enough

Published 113 original articles · won praise 206 · views 10000 +

Guess you like

Origin blog.csdn.net/Geffin/article/details/102882129