springcloud-Distributed Configuration Center (config)

Source: Internet
Author: User

Brief introduction

In the Distributed file system, because of the large number of services, in order to facilitate the unified management of service profiles, real-time updates, so the need for distributed configuration Center components. In spring cloud, there is a distributed Configuration center component, Spring Cloud Config, which enables configuration services to be placed in the memory of the configuration service (that is, local) and also in a remote Git repository. In the Spring Cloud Config component, there are two roles, one is config server, but the config client.

Project Building

Create a springboot project named Config-server

Pom as follows

<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0/http Maven.apache.org/xsd/maven-4.0.0.xsd "><modelversion>4.0.0</modelversion><groupid>com. springcloud</groupid><artifactid>config-server</artifactid><version>0.0.1-snapshot< /version><packaging>jar</packaging><name>config-server</name><description> Config-server-desc</description><parent><groupid>org.springframework.boot</groupid> <artifactId>spring-boot-starter-parent</artifactId><version>1.5.13.RELEASE</version> <relativePath/> <!--lookup parent from repository--></parent><properties>< Project.build.sourceencoding>utf-8</project.build.sourceencoding><project.reporting.outputencoding >utf-8</project.reporting.outputencoding><java.version>1.8</java.version><spring-cloud.version>edgware.sr3</ Spring-cloud.version></properties><dependencies><dependency><groupid> org.springframework.cloud</groupid><artifactid>spring-cloud-config-server</artifactid></ Dependency><dependency><groupid>org.springframework.cloud</groupid><artifactid> Spring-cloud-starter-eureka</artifactid></dependency><dependency><groupid> Org.springframework.boot</groupid><artifactid>spring-boot-starter-test</artifactid><scope >test</scope></dependency></dependencies><dependencyManagement><dependencies> <dependency><groupId>org.springframework.cloud</groupId><artifactId> Spring-cloud-dependencies</artifactid><version>${spring-cloud.version}</version><type> Pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId> org.springframework.boot</groupid><artifactid>spring-boot-maven-plugin</artifactid></ Plugin></plugins></build></project>

At the entrance of the program application class plus the @enableconfigserver annotation opens the function of the configuration server, the code is as follows

@EnableConfigServer @springbootapplicationpublic class Configserverapplication {public static void main (string[] args) {Springapplication.run (configserverapplication.class, args);}}

The configuration file of the program Application.yml file is configured as follows

server:port:8888spring:application:name:config-servercloud:config:server:git:uri:https://github.com/enzofeng/ Springcloudconfig.gitsearch-paths:respousername:enzofengpassword: *******label:master

Spring.cloud.config.server.git.uri: Configuring the Git repository address
Spring.cloud.config.server.git.searchPaths: Configuring the Warehouse Path
Spring.cloud.config.label: Configuring a branch of a warehouse
Spring.cloud.config.server.git.username: User name to access the GIT repository
Spring.cloud.config.server.git.password: Access passwords for Git repositories

If the GIT repository is a public repository, you can not fill in the user name and password, if it is a private warehouse need to fill

Add a new configuration file to git in the following

Foo=foo version 3

Launch program, Access Http://localhost:8888/foo/dev

{"Name": "foo", "Profiles": ["Dev"], "label": null, "Version": "7e84320106fd4750fa1079934ee6cb88d46e9eb0", "state": NULL, "Propertysources": []}

Certificate Configuration Service Center can obtain configuration information from remote Programs
The HTTP request address and resource file mappings are as follows

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

Building a config Client

Pom as follows

<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0/http Maven.apache.org/xsd/maven-4.0.0.xsd "><modelversion>4.0.0</modelversion><groupid>com. springcloud</groupid><artifactid>config-client</artifactid><version>0.0.1-snapshot< /version><packaging>jar</packaging><name>config-client</name><description> Config-client-desc</description><parent><groupid>org.springframework.boot</groupid> <artifactId>spring-boot-starter-parent</artifactId><version>1.5.13.RELEASE</version> <relativePath/> <!--lookup parent from repository--></parent><properties>< Project.build.sourceencoding>utf-8</project.build.sourceencoding><project.reporting.outputencoding >utf-8</project.reporting.outputencoding><java.version>1.8</java.version><spring-cloud.version>edgware.sr3</ Spring-cloud.version></properties><dependencies><dependency><groupid> org.springframework.cloud</groupid><artifactid>spring-cloud-starter-config</artifactid></ Dependency><dependency><groupid>org.springframework.boot</groupid><artifactid> Spring-boot-starter-web</artifactid></dependency><dependency><groupid> Org.springframework.boot</groupid><artifactid>spring-boot-starter-test</artifactid><scope >test</scope></dependency></dependencies><dependencyManagement><dependencies> <dependency><groupId>org.springframework.cloud</groupId><artifactId> Spring-cloud-dependencies</artifactid><version>${spring-cloud.version}</version><type> pom</type><scope>import</scope></dependency></dependencies></dependencymanagement><build><plugins><plugin><groupid> org.springframework.boot</groupid><artifactid>spring-boot-maven-plugin</artifactid></ Plugin></plugins></build></project>

The configuration file is as follows

Spring:application:name:config-clientcloud:config:label:masterprofile:devuri:http://localhost:8888/server:port : 8881

Spring.cloud.config.label: Indicates the branch of the remote repository
Spring.cloud.config.profile
Dev Development environment configuration file
Test environment configuration file
Pro Official Environment configuration file
spring.cloud.config.uri=http://localhost:8888/Specify the URL of the Configuration service center
The entry class of the program, write an API interface "/hi", return the value of the Foo variable read from the configuration center, the code is as follows

@RestController @springbootapplicationpublic class Configclientapplication {public static void main (string[] args) { Springapplication.run (Configclientapplication.class, args);} @Value ("${foo}") string foo; @RequestMapping ("/hi") Public String Hi () {return foo;}}

Start Project, request Http://localhost:8881/hi

Description Config-client Gets the properties of Foo from Config-server, and Config-server is read from the Git repository

Springcloud-Distributed Configuration Center (config)

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.