Spring Cloud Config is a new project created by the spring Cloud team to provide centralized external configuration support for infrastructure and microservices applications in distributed systems, which are divided into two parts: server and client. The server, also known as the Distributed Configuration Center, is a standalone microservices application that connects to the configuration warehouse and provides clients with access to configuration information, encryption/decryption information, and the client is a microservices application or infrastructure in the microservices architecture. They manage application resource-related configuration content through the specified configuration center and obtain and load configuration information from the configuration center at startup. Spring Cloud Config implements an abstract mapping of environment variables and property configurations in both the server and client, so it can be used in applications that run in any other language, except for applications that are built by spring. Because the configuration center implemented by Spring Cloud Config uses git to store configuration information by default, the configuration server built with Spring Cloud Config naturally supports version management of configuration information for microservices applications. The configuration content can be easily managed and accessed through the GIT client tool. Of course, it also provides support for other storage methods, such as SVN repositories, localized file systems.
In this article, we'll learn how to build a distributed configuration center based on git storage and transform the client to get configuration information from the configuration center and bind it to the entire process in the code.
Prepare to configure the warehouse
Prepare a git repository that can be created either on the code cloud or on GitHub. Examples of warehouses prepared in this article: Http://git.oschina.net/didispace/config-repo-demo
Assuming we read the application name config-client in the configuration center, we can config-client.yml the default profile for the project in the Git repository:
info: profile: default
To demonstrate loading configurations for different environments, we can create a configuration file for the dev environment in the Git repository config-client-dev.yml:
info: profile: dev
Building the Configuration Center
Building a distributed configuration center through Spring Cloud Config is simple and requires only three steps:
创建一个基础的Spring Boot工程,命名为:config-server-git,并在pom.xml中引入下面的依赖(省略了parent和dependencyManagement部分):
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency></dependencies>
Create a spring boot program main class and add @enableconfigserver annotations to open the service-side capabilities of Spring Cloud CONFIG.
@EnableConfigServer@SpringBootApplicationpublic class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); }}
Add basic information about the configuration service and information about the Git repository in application.yml, such as:
spring application: name: config-server cloud: config: server: git: uri: http://git.oschina.net/didispace/config-repo-demo/server: port: 1201
Here, a distributed configuration center that is implemented with Spring Cloud config and uses git to manage configuration content is complete. We can start the app first, make sure there are no errors, and then try the following.
如果我们的Git仓库需要权限访问,那么可以通过配置下面的两个属性来实现;spring.cloud.config.server.git.username:访问Git仓库的用户名spring.cloud.config.server.git.password:访问Git仓库的用户密码
After completing these preparations, we will be able to access our configuration directly via tools such as browser, postman or curl. The URL that accesses the configuration information is mapped to the configuration file as follows:
/{application}/{profile}[/{label}]/{application}-{profile}.yml/{label}/{application}-{profile}.yml/{application}-{profile}.properties/{label}/{application}-{profile}.properties
The above URL maps {Application}-{profile}.properties's corresponding configuration file, where {label} corresponds to a different branch on Git, which defaults to master. We can try to construct different URLs to access different configuration content, for example, to access the master branch, config-client app's dev environment, you can access this url:http://localhost:1201/config-client/ Dev/master, and get the following return:
{ "name": "config-client", "profiles": [ "dev" ], "label": "master", "version": null, "state": null, "propertySources": [ { "name": "http://git.oschina.net/didispace/config-repo-demo/config-client-dev.yml", "source": { "info.profile": "dev" } }, { "name": "http://git.oschina.net/didispace/config-repo-demo/config-client.yml", "source": { "info.profile": "default" } } ]}
We can see that the JSON returns the app name: config-client, Environment name: Dev, branch name: Master, and the configuration content of the default and dev environments.
The schema code is as follows:
Building a microservices Architecture Spring Cloud: Distributed Configuration Center