- Distributed Configuration Center: Spring Cloud Config
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. And 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, and so on.
1. Create Maven project, skeleton Select QuickStart, Name: Demo-config-server
2. Join related dependencies:
3. Create a Startup class:
4. Create the Application.yml file in the Src\main\resources directory:
- Spring.cloud.config.server.git.uri: Configure the Git repository location.
- Spring.cloud.config.server.git.search-paths: Configure the relative search location under the warehouse path to configure multiple.
- Spring.cloud.config.server.git.username: Access the user name of the git repository.
- Spring.cloud.config.server.git.password: Access the user password for the git repository.
Note: Because the GIT project is public, you can do so without providing a user name and password.
5. Log in to Git, create the Config-repo folder under the Demo-springcloud/directory, and add a new 4 configuration file:
6. The contents of the configuration file are as follows, in order to test version control, in the master branch of the Git repository, add a suffix of 1.0 to the form attribute, create a config-label-test branch, and use 2.0 as a suffix for the values in the configuration file:
7. Start the project, there are several ways to access the configuration content, where application is the prefix of the configuration file, here is application;profile as the suffix of the configuration file, here is the Dev, pro and Test;label on behalf of the GIT branch name:
- /{application}/{profile}[label]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
For example, access to Http://localhost:8001/application/dev/config-label-test gets the following:
8. At the same time, observing the console of the Demo-config-server project, the following output was found:
9. After the configuration server obtains the configuration information from Git, it stores a copy of the file system in Demo-config-server, essentially demo-config-server the configuration content through the git clone command to be stored locally, The content is then read and returned to the MicroServices app for loading. The temporary storage of git in the local repository can effectively prevent the failure of the Git repository from loading configuration information. Can be disconnected from the network, again in the browser to initiate the above request, you can see the console error message: Could not fetch the remote for config-label-test remote, but it will still return the configuration content for the request, The content is derived from the configuration that was previously accessed in the Demo-config-server local file system:
10. According to the path printed by the console, go to the specified directory and discover the downloaded configuration file:
1. Create Maven project, skeleton Select QuickStart, Name: demo-config-client
2. Join related dependencies:
3. Create a Startup class:
4. Create the bootstrap.properties file under the Src/main/resources directory (Note: These properties must be configured in bootstrap.properties , This allows the configuration information in the Demo-config-server to be loaded correctly. Spring boot Loads the configuration files in the order that the configuration files outside of the app jar package will take precedence over the configuration content within the jar package, while the bootstrap.properties The configuration of the demo-config-client allows the application to obtain some external configuration information from the Demo-config-server, which has a higher priority than local content, thus enabling an external configuration:
- Spring.application.name: The {application} section of the corresponding configuration file rule.
- Spring.cloud.config.profile: The {profile} section in the corresponding configuration file rule.
- Spring.cloud.config.label: the {label} portion of the corresponding profile rule.
- Spring.cloud.config.uri: Configures the address of the center demo-config-server.
5. Create a restful interface to return the From property of the configuration center, start the project, and test the effect:
Spring Cloud Learning Notes-010