Springcloud (v): basic usage of Spring Cloud Configuration Center

Source: Internet
Author: User
Tags aop svn using git

Basic usage of Spring Cloud Configuration Center 1. Overview

This article describes the configuration center of Spring Cloud, describes how the configuration center configures the server side and configuration parameters, and also describes how the client interacts with the configuration center and configures parameter descriptions.
The Configuration Hub Server section includes: Service creation, git,svn,native backend configuration, various URL access
Configuration Center Client section includes: Access configuration, FailFast, retry

2. Service side 2.1 of Spring Cloud CONFIG. Briefly

When we are developing large systems, the same configuration (such as database information, caching, switching volume, etc.) will appear on different services due to the high number of services, and many service configurations may need to be modified if one configuration changes. To solve this problem, spring cloud provides the configuration center.
First, all public configurations are stored at the same address (where the storage can be git,svn and local files), and then the configuration center reads the configuration from these places to be restful, and other services can invoke the interface to get configuration information.

2.2. Configure the Service

Introducing the key jar

<dependency>     <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>

The Configuration Center service can be activated through @enableconfigserver. The configuration Center can be serviced separately or embedded in other services. It is recommended to use the configuration center in a separate service mode.

@SpringBootApplication@EnableConfigServer // 激活该应用为配置文件服务器:读取远程配置文件,转换为rest接口服务public class CloudGitConfigServerApplication { public static void main(String[] args) { args = new String[1]; args[0] = "--spring.profiles.active=gitsimple2"; SpringApplication.run(CloudGitConfigServerApplication.class, args); }}

Because of the diversity of configuration file storage, the following describes how each configuration form is configured. All configurations are configured in APPLICATION-*.YML

2.3. Git back end

The back-end system in Spring Cloud Configuration Center can be:

    • VCS (e.g. GIT,SVN, etc.)
    • Local file

In this section we describe the GIT configuration
Configuration parameters main configuration in Application-gitsimple2.yml

spring:  application:    name: special  cloud:    config:      server:        git:          #  配置文件只搜索url目录下的searchPaths          uri: https://github.com/hryou0922/spring_cloud.git          # 指定搜索路径,如果有多个路径则使用,分隔          searchPaths: cloud-config-git/simple2/configspecial,cloud-config-git/simple2/default # 对于使用git,svn做为后端配置,从远程库获取配置文件,需要存储到本地文件 basedir: /tmp/spring-cloud-repo # 配置中心通过git从远程git库,有时本地的拷贝被污染,这时配置中心无法从远程库更新本地配置,设置force-pull=true,则强制从远程库中更新本地库 force-pull: true

Spring.cloud.config.server.git.url: Specifies the URL address of the remote Git library where the configuration file resides

Spring.cloud.config.server.git.searchPaths: Use with the above parameter URL to locate a subdirectory of the Git library. Specifies the search path, if multiple paths are used, separating

Spring.cloud.config.server.git.basedir: For a backend configuration using GIT,SVN, getting the configuration file from the remote library needs to be stored to a local file. The default is stored in the system temp directory, and the directory name is prefixed with config-repo-, which may be/tmp/config-repo-under Linux. Because the content under/tmp may be mistakenly deleted, all for insurance purposes, it is best to modify the storage directory. If you modify the storage directory, you can modify the Spring.cloud.config.server.git.basedir

Spring.cloud.config.server.git.force-pull: When the configuration center reads data from a remote git library via git, sometimes the local copy is contaminated, and the configuration center cannot update the local configuration from the remote library. Set Force-pull=true to force the local library to be updated from the remote library

The above is some common configuration, other configuration can see their own configuration class Multiplejgitenvironmentrepository class

2.4. SVN backend

The SVN configuration method is similar to Git's, mainly using "spring.cloud.config.server.svn.*". Here slightly

2.5. File system back end

In addition to using the download configuration file from Git/svn, you can load the configuration file from the Classpath directory or from the local file system. Configure addresses with Spring.cloud.config.server.native.searchLocations. Here are two categories:

    • To load a configuration file from a local directory: start with file
      • File:///${user.home}/config-repo
        Default value: file:./, File:./config
    • Load configuration file from Classpath: Start with Classpath
      • If you do not configure a value, the default value is: classpath:/, Classpath:/config

Take Classpath as an example, the use of file is the same as the classpath usage, and here is a bit.

spring:  profiles:    # native:启动从本地读取配置文件,必须指定active的值,才可以使用本地文件配置模式    active: native # 自定义配置文件路径 cloud: config: server: native: searchLocations: classpath:/config/simple2/

Spring.profiles.active: If the local system configuration is used, this value must be native
Spring.cloud.config.server.native.searchLocations: Specify the path to the configuration file

2.6. Start and test

Services can be started via cloudgitconfigserverapplication

Enter the following URL in the browser to access the configuration file

/{application}/{profile}[/{label}]/{application}-{profile}.yml/{label}/{application}-{profile}.yml/{application}-{profile}.properties/{label}/{application}-{profile}.properties

Below is a specific example to illustrate the meaning of the above URL. If our profile name is CLOUD-CONFIG-SIMPLE2.YML, then the values corresponding to each field in the URL are:

    • Application:cloud-config
    • Profile:simple2
    • label:9500e50f08c43e3e4391175c8f6d5a326b11302f

Our access to the following addresses can be accessed to the profile config-simple2.yml and the specific version under this file:
Http://127.0.0.1:10888/cloud-config/simple2
http://127.0.0.1:10888/cloud-config/simple2/9500e50f08c43e3e4391175c8f6d5a326b11302f
Http://127.0.0.1:10888/cloud-config-simple2.yml
Http://127.0.0.1:10888/9500e50f08c43e3e4391175c8f6d5a326b11302f/cloud-config-simple2.yml
Http://127.0.0.1:10888/cloud-config-simple2.properties
Http://127.0.0.1:10888/9500e50f08c43e3e4391175c8f6d5a326b11302f/cloud-config-simple2.properties

3. Client for Spring Cloud Config

After the configuration Hub server is configured successfully, other services get the configuration file from the configuration center, which is called the client.

3.1. Configure the Client

To introduce a jar:

<dependency>    <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId></dependency>

As long as the @springbootapplication annotation starts with spring boot

@SpringBootApplicationpublic class SimpleCloudServiceApplication {    public static void main(String[] args) { args = new String[1]; args[0] = "--spring.profiles.active=simple2"; SpringApplication.run(SimpleCloudServiceApplication.class, args); }}

3.2. Configuration parameters

Configure the configuration center's related configuration in bootstrap-. Yml, and do not configure appliaction-. Yml. Because the configuration is read from the bootstrap when the service starts, the configuration file is read from the remote Configuration center, and then the configuration is obtained from the appliaction, and if there is the same configuration item, the previous read value is overwritten. So if configuration center configuration is configured in Appliaction, the configuration item will have no effect.

Bootstrap-simple2.yml

Spring:cloud:# Configure the server's address config:uri:http:127.0.0.1:10888# to read the value of the configuration file read Name:cloud-config# If this value is not set, the system setting this value to Spring.profiles.active Profile:dev # can be used earlier. The default value can be git label, branch name or commit ID. Multiple labels can be used, and multiple labels can be separated by commas # Label: # true: If you fail to access the configuration center, stop the start service fail-fast: true # Configuration retry, default is retry 6 times, The initial delay is 1s retry again, and if it fails again, delay 1.1*1s, 1.1*1.1*1s 、... 。 You can use this configuration Retry:initial-interval : max # retries max-attempts: 6 # Maximum retry interval max-interval: 400 0 # Each retry time is a multiple of the previous multiplier: 1.2             

Important parameters are explained as follows:
URL of the configuration center
From where to read the configuration file, configure it with "Spring.cloud.config.url"

Which configuration files to read
Collectively determined by the following parameters, and "2.6. Start and test "combined to see deeper understanding.
-"Spring.cloud.config.name": The configuration file name, corresponding to the {applicaion} value in the Read URL above
-"Spring.cloud.config.profile": Profile of the configuration file, corresponding to the {profiles} value in the URL above
-"Spring.cloud.config.label": the previous version can be used. The default value can be git label, branch name or commit ID. You can use more than one label, and multiple labels can be separated using commas

Fast failure
Set "Spring.cloud.config.label" to true if the client is required to access the configuration center if it fails to stop the start of the service immediately

Retry
If the access configuration fails, it is automatically retried. The default is to retry 6 times, initially delay 1s retry again, if it fails again, delay 1.1*1s, 1.1*1.1*1s 、... 。 The values can be modified by using the following parameters:

    • "Spring.cloud.config.retry.initial-interval": The first failure, how long the delay retry
    • "Spring.cloud.config.retry.max-attempts": Maximum number of retries
    • "Spring.cloud.config.retry.max-interval": Maximum retry interval
    • "Spring.cloud.config.retry.multiplier": Each retry time is a multiple of the previous

      If you want to implement retry functionality, you need to introduce a new jar

<!--HTTPS://MVNREPOSITORY.COM/ARTIFACT/ORG.SPRINGFRAMEWORK.BOOT/SPRING-BOOT-STARTER-AOP--<Dependency><Groupid>org.springframework.boot</groupid> < artifactid>spring-boot-starter-aop</ artifactid> </dependency> Span class= "hljs-comment" ><!--https://mvnrepository.com/artifact/org.springframework.retry/spring-retry-- > <dependency> <groupid>org.springframework.retry</groupId > <artifactid>spring-retry</ artifactid> </dependency>  

3.3. Start and test

Start Simplecloudserviceapplication, in the browser input http://127.0.0.1:10082/simple, will return the following information, indicating success

{"age":112,"name":"git2-default-dev","randomNum":53}

转自:77870854?utm_source=tuicool&utm_medium=referral

Springcloud (v): basic usage of Spring Cloud 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.