Spring Cloud Config

Source: Internet
Author: User

One, Spring Cloud Config Server

Create a new spring boot project named Configserver

1.pom.xml adding dependencies

<?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.dzpykj</groupId> <artifactid>configserver</artifactid > <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>configserve R</name> <description>demo Projectfor   Spring boot</description> <parent> <groupId>org.springframework.boot</groupId> &        Lt;artifactid>spring-boot-starter-parent</artifactid> <version>1.5.9.RELEASE</version> <relativepath/> <!--lookup parent from repository--</parent> <properties> &LT;PR Oject.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding >UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties&    Gt <dependencyManagement> <dependencies> <dependency> <groupid>org.s                Pringframework.cloud</groupid> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.RELEASE</version> <type>pom</type> <scop E>import  </scope> </dependency> </dependencies> </dependencyManagement> <depende ncies> <dependency> <groupId>org.springframework.boot</groupId> <arti factid>spring-boot-starter-web</artifactid> </dependency> <dependency> <g Roupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid&gt            ; <scope>test</scope> </dependency> <dependency> &LT;GROUPID&GT;ORG.SPRINGF Ramework.cloud</groupid> <artifactId>spring-cloud-starter-eureka</artifactId> &LT;/DEP endency> <dependency> <groupId>org.springframework.cloud</groupId> <a rtifactid>spring-cloud-config-server</artifactid> </dependency> </dependencies> <buil      D>  <plugins> <plugin> <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins>            </build> <repositories> <repository> <id>spring-milestones</id> <name>spring milestones</name> <url>https://repo.spring.io/milestone</url><snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories></project>

2.application.properties Adding a configuration

server.port=8768spring.application.name=Configservereureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/spring.cloud.config.server.git.uri=https:// github.com/pinbo1991/springcloud/Spring.cloud.config.label=master
Spring.cloud.config.server.git.uri is the GitHub address (the configuration file address in this demo is Https://github.com/PinBo1991/springcloud)
Spring.cloud.config.label for GitHub Branch

3. Start the class plus annotations @enableconfigserver to open the service side of Spring Cloud config

 Packagecom.dzpykj;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;ImportOrg.springframework.cloud.config.server.EnableConfigServer;Importorg.springframework.cloud.netflix.eureka.enableeurekaclient;@ Springbootapplication@enableeurekaclient@enableconfigserver Public classconfigserverapplication { Public Static voidMain (string[] args) {Springapplication.run (configserverapplication.class, args); }}

Second, Spring Cloud Config Client

New spring Boot project, named Configclient

1.pom.xml adding dependencies

<?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.dzpykj</groupId> <artifactid>configclient</artifactid > <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>configclien T</name> <description>demo Project for Spring boot</description> <parent> <groupId>org.springframework.boot</groupId> &        Lt;artifactid>spring-boot-starter-parent</artifactid> <version>1.5.9.RELEASE</version> <relativepath/> <!--lookup parent from repository--</parent> <properties> &LT;PR Oject.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding >UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties&    Gt <dependencyManagement> <dependencies> <dependency> <groupid>org.s                Pringframework.cloud</groupid> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.RELEASE</version> <type>pom</type> <scop E>import  </scope> </dependency> </dependencies> </dependencyManagement> <depende ncies> <dependency> <groupId>org.springframework.boot</groupId> <arti factid>spring-boot-starter-web</artifactid> </dependency> <dependency> <g Roupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid&gt            ; <scope>test</scope> </dependency> <dependency> &LT;GROUPID&GT;ORG.SPRINGF Ramework.cloud</groupid> <artifactId>spring-cloud-starter-eureka</artifactId> &LT;/DEP endency> <dependency> <groupId>org.springframework.cloud</groupId> <a rtifactid>spring-cloud-starter-config</artifactid> </dependency> </dependencies> <bui     Ld>   <plugins> <plugin> <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins>            </build> <repositories> <repository> <id>spring-milestones</id> <name>spring milestones</name> <url>https://repo.spring.io/milestone</url><snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories></project>

2. Rename the application.properties to Bootstrap.properties and add the configuration

server.port=8769spring.application.name=Configclienteureka.client.serviceUrl.defaultZone= http://localhost:8761/eureka/spring.cloud.config.label=  Masterspring.cloud.config.profile=Testspring.cloud.config.uri= http://localhost:8768 /spring.cloud.config.discovery.enabled=true

Configuration Item Description:

Spring.application.name: The {application} section in the corresponding pre-configuration file
Spring.cloud.config.profile: The {profile} section in the corresponding pre-configuration file
Spring.cloud.config.label: Git branch that corresponds to the pre-configuration file
Spring.cloud.config.uri: Address of the configuration center

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

The configuration of this configuration file means: in Configserver spring.cloud.config.server.git.uri (ie, https://github.com/PinBo1991/springcloud/), look for

Configuration file for Configclient profile/spring.cloud.config.label/spring.application.name-spring.cloud.config.profile.properties (ie configclient-test.properties)

3. Startup class

 Packagecom.dzpykj;ImportOrg.springframework.beans.factory.annotation.Value;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;Importorg.springframework.web.bind.annotation.RequestMapping;Importorg.springframework.web.bind.annotation.restcontroller;@ Springbootapplication@enableeurekaclient@restcontroller Public classconfigclientapplication {@Value ("${configclientkey}") String Configclientkey; @RequestMapping (Value= "/getconf")     PublicString getconf () {returnConfigclientkey; }         Public Static voidMain (string[] args) {Springapplication.run (configclientapplication.class, args); }}

Configclient-test.properties file with CONFIGCLIENTKEY=ZXCV configuration, read it out

Third, test, visit Configclient project: http://127.0.0.1:8769/getConf

Iv. Source Address Https://github.com/PinBo1991/springcloud

Spring Cloud 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.