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.
Suppose we read the application name of the configuration center config-client
, then we can have the default profile for that project in the Git repository config-client.yml
:
- 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
:
Building the Configuration Center
Building a distributed configuration center through Spring Cloud Config is simple and requires only three steps:
- Create a basic spring boot project, named:
config-server-git
, and pom.xml
introduce the following dependencies in (omitting the parent and dependencymanagement sections):
123456 |
<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.
123456789 |
@EnableConfigServer @SpringBootApplication public class application { public static void main(string[] args) { new Springapplicationbuilder (Application.class). Web (True). Run (args); } |
application.yml
Add basic information about the configuration service in and information about the GIT repository, such as:
12345678910 |
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.
If we need access to the GIT repository, we can configure the following two properties.
Spring.cloud.config.server.git.username: User name to access the GIT repository
Spring.cloud.config.server.git.password: Access the user password for the GIT repository
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 URL above maps the {application}-{profile}.properties
corresponding configuration file, which corresponds to a {label}
different branch on git, which defaults to master. We can try to construct different URLs to access different configuration content and get the following return:
1234567891011121314151617181920212223 |
{ "name": "Config-client", "profiles": [ ], "label": "version": null, "state": null, "propertysources": [ { Span class= "attr" > "name": " source ": { " Info.profile ": Span class= "string", "Dev" }, { "name": " 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.
From now on, I will record the process and the essence of the recently developed Springcloud micro-service cloud architecture, and help more friends who are interested in developing the Spring cloud framework, hoping to help more good scholars. Let's explore how the Spring cloud architecture is built and how it can be used in enterprise projects. SOURCE Source
Spring Cloud builds a microservices architecture distributed configuration Center