Distributed Configuration Center
Why should I use the Distributed Configuration Center thing? Now this micro-service army has covered a variety of large and small enterprises, each service granularity is relatively small, so there will be a large number of services in the system, each service must have their own configuration information, or the same configuration information, there may be different environments each service also has a separate set of configurations, this situation is a large number of configuration files, It's quite laborious to maintain, for a chestnut:
In the process of development, the general database is the development environment database, all services DB IP configuration is: 92.168.0.1, suddenly boss said, development environment changed, DB IP to modify, this is not easy, all modules change the configuration of the DB, asked you uncomfortable not uncomfortable?
This time the distributed configuration Center has played a great advantage, only need to modify the configuration center configuration, all services can be automatically effective, cool uncomfortable!
Spring Cloud Config
Website address: http://cloud.spring.io/spring-cloud-config/
Brief introduction
Spring Cloud Config
Provides external configuration support for distributed systems for both the server and the client. The configuration server provides a centralized external configuration for all environments for each application. It implements mapping of server and client pairs Spring Environment
and PropertySource
abstractions, so it can be used in applications that run in any other language, except for applications that are built by spring. As an application that can be tested or put into production by deploying pipelines, we can create configurations for these environments separately, and get the configuration of the corresponding environment to run when the environment needs to be migrated.
By default, the server uses GIT to store configuration information, which facilitates versioning of the environment configuration and provides easy management and access to configuration content through the GIT client tool. Of course he also provides a way to store localized file systems.
Use spring Cloud for centralized configuration management, remove previous configuration files from your project and put them centrally managed in Git or SVN, and notify applications when changes are needed, without restarting the application refresh configuration.
Implementation principle
In fact, the implementation of the principle is relatively simple, based on Git interactive operation.
- We put the config file on git.
- Spring Cloud Config Hub service connects to Git
- The client needs to configure configuration information from the Configuration Center service to obtain
- When the client starts, it gets the configuration information from the configuration Center on Git
Configure the Hub service side
Pom.xml adding dependencies
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency></dependencies><dependencyManagement> <dependencies> <!-- spring cloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies></dependencyManagement>
Application Start class Add annotations
@EnableConfigServer
to add annotations, enable the configuration center:
package com.qianxunclub;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;/*** @author chihiro.zhang*/@SpringBootApplication@EnableConfigServerpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
Configuration file
In application.yml
or application.properties
add configuration information:
spring: cloud: config: server: git: uri: https://gitee.com/qianxunclub/spring-boot-config-repo default-label: master search-paths: /** basedir: target/config
- Spring.cloud.config.server.git.uri: Configuring the Git repository address
- Spring.cloud.config.server.git.search-paths: Repository folder directory, if yes
/**
, all files in all directories
- Spring.cloud.config.server.git.default-label: Configuring a branch of a warehouse
- Spring.cloud.config.server.git.basedir: Configuration file pulled to local directory location
Start test
First, add a application-dev.yml
configuration file to git with the following:
test: 我是配置中心配置信息
has been configured to complete, start a wave test, see how it works, normally it can start normally, and then get the configuration file try
Access Address: Http://localhost:8888/test/dev
If you return the following, it is successful:
{ "name":"test", "profiles":[ "dev" ], "label":null, "version":"64e7882a8f280641724e454a2db5a3da7b44d3d4", "state":null, "propertySources":[ { "name":"https://gitee.com/qianxunclub/spring-boot-config-repo/application-dev.yml", "source":{ "test":"配置中心的配置信息" } } ]}
The HTTP request address and resource file mappings are as follows:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
Configure the Hub client to use
Pom.xml adding dependencies
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency></dependencies><dependencyManagement> <dependencies> <!-- spring cloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies></dependencyManagement>
Configuration file
Create a bootstrap.yml
file, remember, is a bootstrap.yml
file bootstrap.yml
file, I wrote to application.yml
this inside, a variety of problems Ah, add the following configuration:
spring: cloud: config: name: application profile: dev label: master uri: http://localhost:8888/
- Spring.cloud.config.label: Indicates the branch of the remote repository
- Spring.cloud.config.profile: Specify different environment profiles, and the corresponding git repositories
application-dev.yml
- Spring.cloud.config.name: Configuration name, General and git repository
application-dev.yml
correspondence
- Spring.cloud.config.uri: The Configuration Center service address above
Start test
First, add a class that gets the configuration information:
/** * @author chihiro.zhang */@Configuration@EnableAutoConfigurationpublic class DemoConfiguration { @Value("${test}") private String test;}
Find a place to call, output this test, will print the above git configuration information, cool not!
Tell me about the hole in the middle.
- Server-side git configuration can't get git repository configuration file anyway
spring: cloud: config: server: git: uri: https://gitee.com/qianxunclub/spring-boot-config-repo default-label: master search-paths: /** basedir: target/config
At that time this uri
configuration is the company's Git repository, the company's Git repository access is required to open an agent to have access to the agent has opened, but has been error:
Whitelabel Error PageThis application has no explicit mapping for /error, so you are seeing this as a fallback.Wed Jun 06 11:10:56 CST 2018There was an unexpected error (type=Not Found, status=404).Cannot clone or checkout repository: http://xxx.com:5080/framework/config-repo
Very depressed, do not know why, but just, just, write blog time, have tested a bit, pass the ... Day the dog, do not know what reason, and so on the study came out to add.
- Client configuration must be configured on the
bootstrap.yml
inside
uri
The address that the port is called by default 8888
http://localhost:8888/
When started, it will be loaded label
and uri
, profile
configuration, can be added in the profile
startup parameters, can profile
also be added in application.yml
the
name
You can also application.yml
add a
Demo
Configuration Center service side: Https://gitee.com/qianxunclub/qianxunclub-springboot-config
Configure Git repository: https://gitee.com/qianxunclub/qianxunclub-springboot-config
Configure client use: Https://gitee.com/qianxunclub/qianxunclub-starter-demo
Client main configuration in: Https://gitee.com/qianxunclub/qianxunclub-starter-parent/tree/master/qianxunclub-starter-config
Use of the Spring Cloud Config Distributed Configuration Center and the pits encountered