Spring Cloud Building MicroServices Architecture (iv) Distributed configuration Center

Source: Internet
Author: User
Tags git client using git

Spring Cloud Config provides external configuration support for distributed systems for both the server and the client. The configuration server provides a centralized external configuration for all environments for each application. It implements a mapping of the spring environment and propertysource abstractions to both the server side and the client, so it can be used in applications that run in any other language, except for applications that are built by spring. As an application that can be tested or put into production by deploying pipelines, we can create configurations for these environments separately, and get the configuration of the corresponding environment to run when the environment needs to be migrated.

The configuration server uses Git to store configuration information by default, which facilitates versioning of the environment configuration and provides easy management and access to configuration content through the GIT client tool. Of course he also provides a way to store localized file systems, and here are two ways to use distributed configuration to store the configuration content of a microservices application's multi-environment.

Building Config Server

Building a config Server from spring cloud is simple and requires only three steps:

    • Create a REST API to return the From property of the configuration center, as follows:
    The dependency is introduced in Pom.xml spring-cloud-config-server and the full dependency configuration is as follows:
  •     <parent> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-sta Rter-parent</artifactid> <version>1.5.2.RELEASE</version> <relativePath/> <!--Lo Okup parent from repository-</parent> <properties> <project.build.sourceencoding>utf -8</project.build.sourceencoding> <project.reporting.outputencoding>utf-8</ project.reporting.outputencoding> <java.version>1.7</java.version> </properties> &LT;DEP endencies> <dependency> <groupId>org.springframework.cloud</groupId> &lt            ;artifactid>spring-cloud-config-server</artifactid> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-test</artif Actid> <scope>test</scope&Gt </dependency> </dependencies> <dependencyManagement> <dependencies> <de Pendency> <groupId>org.springframework.cloud</groupId> <artifactid>spri Ng-cloud-dependencies</artifactid> <version>Camden.SR6</version> <type& Gt;pom</type> <scope>Import</scope> </dependency> </dependencies> </dependencyManagement> &LT;BUILD&G        T                <plugins> <plugin> <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
    • Create a spring boot program main class and add @EnableConfigServer annotations to open config Server
    • @SpringBootApplication @enableconfigserver  Public class springcloudconfigcenterapplication {    publicstaticvoid  main (string[] args) {        springapplication.run (springcloudconfigcenterapplication. class , args);}    }
      • application.propertiesConfigure the service information and git information, such as:
      •  spring.application.name=config-serverserver.port  =7002# git management configuration Spring.cloud.config.server.git.uri  =https://" Span style= "COLOR: #008000" >github.com/xxx/ spring.cloud.config.server.git.searchpaths= Springcloud-config-center/config-repospring.cloud.config.server.git.username  =spring.cloud.config.server.git.password  = # # Turn on local configuration #spring.profiles.active  =native   #Config server retrieves the configuration file by default from the application's src /main/resource directory. You can also specify the retrieval location #spring.cloud.config.server by using the following configuration.  native . SEARCHLOCATIONS=FILE:F:/PR 
          • Spring.cloud.config.server.git.uri: Configuring the Git repository location
          • Spring.cloud.config.server.git.searchPaths: Configure the relative search location under the warehouse path to configure multiple
          • 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

        Here, using a configuration center implemented with Spring Cloud config and using git to manage content, start the app and start the following content successfully.

        Spring Cloud Config also provides a way to configure the local storage. We only need to set properties spring.profiles.active=native , and config server will retrieve the configuration file from the app's directory by default src/main/resource . You can also spring.cloud.config.server.native.searchLocations=file:F:/properties/ specify the location of the configuration file through properties. Although Spring Cloud config provides such functionality, it is recommended to use GIT in order to support better functionality for managing content and versioning.

      • server-side validation

        In order to verify the configuration server completed above, a Config-repo directory was created under Http://github as the configuration repository, and the following four configuration files were built according to different environments:

        • Didispace.properties
        • didispace-dev.properties
        • didispace-test.properties
        • Didispace-prod.properties

        Where a from property is set with different values for each configuration file, such as:

        • from=git-default-1.0
        • li>from=git-dev-1.0
        • from=git-test-1.0
        • from=git-prod-1.0

        In order to test version control, in master, We all add a suffix of 1.0, create a config-label-test branch at the same time, and use 2.0 as a suffix for the values in each configuration file.

        Once these preparations have been completed, we will be able to access our configuration directly through tools such as the browser or postman. The

        URL 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 {application}-{profile}.properties for the corresponding configuration file, {label} corresponds to a different branch on Git, which defaults to master.

        We can try to construct different URLs to access different configuration contents

      • For example: To access the Config-label-test branch, configcenter application PROD environment, you can pass this url:http://localhost:7001/configcenter/prod/config-label-test
      • Micro-Server Mapping configuration

        After you have completed and verified the Configuration Service center, let's look at how we can get configuration information in the MicroServices app.

        • Create a spring boot application that introduces Spring-cloud-starter-config dependencies in Pom.xml, complete dependencies as follows:
          • <dependencies>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-config-server</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactid>spring-boot-starter-test </artifactId>            <scope>test</scope>        </dependency>    </dependencies>
            <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency>
          • Create the most basic spring boot startup main class
          • Create bootstrap.properties a configuration to specify config server, for example:
        • Spring.application.name: The {application} section in the corresponding pre-configuration file Spring.cloud.config.profile: {profile} in the corresponding pre-configuration file Section Spring.cloud.config.label: Git branch Spring.cloud.config.uri for the pre-configuration file: Address of the configuration center

          It is important to note that the above properties must be configured in the bootstrap.properties config section to be loaded correctly. Because the configuration of config will precede application.properties , and bootstrap.properties the load is before application.properties .

          • Create a REST API to return the From property of the configuration center, as follows:
          • Configure @Value("${from}") the From property in the service by binding.

            Launch the app and access: Http://localhost:7002/from, we can output the from content of the corresponding environment according to the configuration content.

Spring Cloud Building MicroServices Architecture (iv) 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.