First, Introduction
In distributed systems, 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 Spring Cloud Config component, there are two roles, one is config server and the other is config client.
Second, build config Server
Create a Spring-boot project, named Config-server,pom.xml, to introduce dependencies:
<Dependencies> <Dependency> <groupId>Org.springframework.cloud</groupId> <Artifactid>Spring-cloud-starter-config</Artifactid> </Dependency> <!--represented as Web engineering - <Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-web</Artifactid> </Dependency> <!--exposure to a variety of indicators seems to be necessary - <Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-actuator</Artifactid> </Dependency> <Dependency> <groupId>Org.springframework.cloud</groupId> <Artifactid>Spring-cloud-config-server</Artifactid> </Dependency> </Dependencies>
New Entry Class Bootapplication:
Import org.springframework.boot.SpringApplication; Import org.springframework.boot.autoconfigure.SpringBootApplication; Import org.springframework.cloud.config.server.EnableConfigServer, @SpringBootApplication @enableconfigserver Public class bootapplication { publicstaticvoid main (string[] args) { Springapplication.run (bootapplication. class , args);} }
Application.properties:
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 spring.cloud.config.server.git.password=xxxxoooo
There is a file in the remote repository https://github.com/shaweiwei/myspringcloudconfig/. config-client-dev.properties file has a property:
MYWW=MYWW Version 2
Startup program: Access Http://localhost:7010/myww/dev
proves that the 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
Third, build a config client
Re-create a springboot project named Config-client, whose pom file introduces 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-actuator</Artifactid> </Dependency> <Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-test</Artifactid> <Scope>Test</Scope> </Dependency>
Its configuration file bootstrap.properties:
# and Git file names correspond to spring.application.name=config-client# remote repository Branch spring.cloud.config.label=master# Dev Development Environment Profile | Test Environment | Pro Official environment # and GIT filename corresponds to spring.cloud.config.profile=dev# indicates the URL of the Configuration service center spring.cloud.config.uri= http://localhost:7010/ server.port=7020
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:
ImportOrg.springframework.beans.factory.annotation.Value;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController, @SpringBootApplication @restcontroller Public classbootapplication { Public Static voidMain (string[] args) {Springapplication.run (bootapplication.class, args); } @Value ("${myww}")//key in git config fileString Myww; @RequestMapping (Value= "/hi") PublicString hi () {returnMyww; } }
Open URL Access: Http://localhost:7020/hi, page display:
MYWW Version 2
This means that config-client gets the properties of Foo from Config-server, and Config-server is read from a git repository,
This article source: http://download.csdn.net/download/u013081610/10152869
Spring Cloud Config Distributed Configuration Center usage Tutorial