Springcloud config configuration (based on consul)

Source: Internet
Author: User
Tags git clone

One, build the configuration Center

  1. Add dependent dependencies in the Pom.xml file 

<dependencies>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-config-server</artifactId>        </dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>

  

  2. Add @enableconfigserver annotations on the main class of the Springboot program to open the service-side function of Springcloud config

@SpringBootApplication @EnableConfigServer  Public class  Application {      publicstaticvoid  main (string[] args) {        Springapplication.run (Application. class  , args);    }}

  

  3. Add the relevant configuration information in the Application.properties

spring.application.name=config-server Server.port=7001
#Git仓库位置 Spring.cloud.config.server.git.uri=https://Gitee.com/*****/config-properties.git   #指定Git仓库路径下的文件夹 spring.cloud.config.server.git.search-paths=myconfig Spring.cloud.config.server.git.username=username Spring.cloud.config.server.git.password= Password #指定Git分支 spring.cloud.config.label=dev  

  

  4.URL call to see if the configuration takes effect

(1)/{application}/{profile}[/{label}]

(2)/{application}-{profile}.yml

The above URL maps {Application}-{profile}.properties's corresponding configuration file, where {label} corresponds to a different branch of git, which defaults to master.

View the contents of the Application-myproject.properties file in a git project, call Http://localhost:7001/application/myproject or http://localhost : 7001/application/myproject/master

{    "name":"Application",    "Profiles": ["MyProject"],    "label":NULL,    "version":"",    " State":NULL,    "propertysources": [{        "name":"https://gitee.com/****/config-properties.git/master/application-myproject.properties",        "Source": {            "Server.servlet.context-path":"/myproject",            "Server.port":"7002",            "Spring.application.name":"MyProject",            "Spring.cloud.consul.host":"localhost",            "Spring.cloud.consul.port":"8500",            "Spring.cloud.consul.discovery.health-check-path":"${server.servlet.context-path}/healthcheck",            "Spring.cloud.consul.discovery.register":"true"        }    }]}

After the configuration server obtains the configuration information from Git, it stores a copy of the file system in Config-server, essentially config-server the configuration content from the git clone command to be stored locally. The content is then read and returned to the MicroServices app for loading.

Config-serveR is staged in the local repository via Git , which effectively prevents the GIT repository from failing and causing the configuration information to fail to load.

Two. Client Configuration mapping

  1. Add dependent dependencies in the Pom.xml file 

         <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>    
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>

  2. Create the Springboot application main class

@SpringBootApplication  Public class  Application {      publicstaticvoid  main (string[] args) {        Springapplication.run (Application. class  , args);    }}

  3. Create a boostrap.properties configuration to specify the Config-server location to get the configuration file

Spring.cloud.config.enabled=true  #配置中心config-The address of the server Spring.cloud.config.uri=http: // 127.0.0.1:23001/  #对应配置文件规则中的 {Application} part Spring.cloud.config.name=application #对应配置文件规则中的 {profile} section  Spring.cloud.config.profile=order #Git分支 spring.cloud.config.label=Dev Spring.cloud.config.username[email protected]163. com Spring.cloud.config.password= bananas66424

Springboot the load order of the configuration files, the configuration file loading in addition to the application jar will take precedence over the configuration content within the jar package, and the configuration of the bootstrap.properties to Config-server, This allows the application to obtain some external configuration information from the Config-server, which takes precedence over local content, allowing for an external configuration.

Three. Service Solution

(1) The hook function of the Git repository helps us to monitor the configuration changes in real time.

(2) Placeholder configuration URI

{application}, {profile}, {label} these placeholders, in addition to the rules used to identify the configuration file, can also be used to configure the URI of the Git warehouse address in Config Server.

For example, using the {application} placeholder to implement a configuration warehouse that matches several different services simultaneously

Spring.cloud.config.server.git.uri=https://gitee.com/*****/{application}-properties.git

Another example is the effect of applying a directory using the {application} placeholder

Spring.cloud.config.server.git.search-paths={application}

(3) Property overrides

Config Server has the property override feature, which allows the developer to provide configuration properties for all applications, using only the Spring.cloud.config.server.overrides property to set parameters for key-value pairs, which are Loaded into the client's configuration in the same way. Like what:

Spring.cloud.config.server.overrides.name=userstring

(4) Security

Because our microservices applications and configuration centers are built on spring Boot, it is easier to use it in conjunction with Spring Security.

When dependencies are introduced in Pom.xml, security for configuration center access can be achieved without any additional changes.

<dependency>            <groupId>org.springframework.boot</groupId>            <artifactId> Spring-boot-starter-security</artifactid>
</dependency>

Specify the user and password in the configuration file, such as:

Security.user.name=user Security.user.password=37cc5635-559b-4e6f-b633-7e932b813f73

Since we have already set up security for config-server, we need to add secure information to the client to access the server properly.

    

Spring.cloud.config.username=user Spring.cloud.config.password=37cc5635-559b-4e6f-b633-7e932b813f73

(5) Encryption and decryption

When using the cryptographic decryption feature, you need to install an unlimited version of JCE in the runtime environment of the configuration center. We can download it from Oracle's official website, which is a compressed package that you can see after extracting the following three files:

Readme. TXT Local_policy.jar Us_export_policy.jar

The Local_policy.jar and Us_export_policy.jar two files are then copied to the $java_home/jre/lib/security directory.

Then configure the encrypted data as follows:

Spring.datasource.username=user Spring.datasource.password={cipher} dba650baa81d78bd08799d8d4429de499bd4c2053c05f029e7cfbf143695f5b

By using the {cipher} prefix before the property value to label the content as an encrypted value, when the MicroServices client loads the configuration, the configuration center automatically decrypts the value with the {cipher} prefix.

      

Springcloud config configuration (based on consul)

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.