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.

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

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 pom.xml introduce the following dependencies in (omitting 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 @SpringBootApplication public class application { public static void main(string[] args) { /c14>new Springapplicationbuilder (Application.class). Web (True). Run (args); }
    • application.ymlAdd basic information about the configuration service in and information about the GIT repository, 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.

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, 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", Span class= "attr" > "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.

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 pom.xml following dependencies in:
<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:
@SpringBootApplication public class application { public static void main(string[] args) { new Springapplicationbuilder (Application.class). Web (True). Run (args); }
    • Create bootstrap.yml a configuration to specify where to get the configuration file config-server-git , for example:
Spring: Application: name:config-client Cloud: Config: uri:http://localhost:1201/ Profile:default Label:master Server: Port: 2001

The above configuration parameters correspond to the various parts of the configuration file stored in Git, as follows:

    • Spring.application.name: Part of the corresponding profile rule {application}
    • Spring.cloud.config.profile: Part of the corresponding profile rule {profile}
    • Spring.cloud.config.label: Part of the corresponding profile rule {label}
    • Spring.cloud.config.uri: config-server Address of the configuration center

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. SOURCE Source

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.