Spring Cloud builds a microservices architecture distributed configuration Center

Source: Internet
Author: User

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.

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:

Create a basic spring boot project, named: Config-server-git, and introduce the following dependencies in Pom.xml (omit the parent and dependencymanagement sections):

<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:

springapplication:name: config-servercloud: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 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.

Building the Client

After completing the above verification, make sure that the Configuration service center is working, and below we try to get the configuration information described above in the MicroServices app.

Create a spring boot application named Config-client and introduce the following dependencies in the Pom.xml:

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

Create a Spring boot application main class, as follows:

@SpringBootApplicationpublic class Application {public static void main(String[] args) {new SpringApplicationBuilder(Application.class).web(true).run(args);}}

Create a BOOTSTRAP.YML configuration to specify the Config-server-git location to get the configuration file, for example:

spring:application:name: config-clientcloud:config:uri: http://localhost:1201/profile: defaultlabel: masterserver:port: 2001

The above configuration parameters correspond to the various parts of the configuration file stored in Git, as follows:
Spring.application.name: The {application} section of the corresponding profile rule
Spring.cloud.config.profile: The {profile} section in the corresponding configuration file rule
Spring.cloud.config.label: the {label} portion of the corresponding configuration file rule
Spring.cloud.config.uri: Address of the configuration center Config-server
It is important to note that the above properties must be configured in bootstrap.properties so that the configuration information in the Config-server is loaded correctly.

After you have finished writing your code above, the reader can start Config-server-git and config-client.

{
"Profile": "Default"
}
Alternatively, we can modify the Config-client profile to Dev to see the changes in the loading configuration.
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.

Spring Cloud builds a microservices architecture distributed configuration Center

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.