一、简介
Spring Cloud Config提供了在分布式系统的外部配置的客户端支持。通过配置服务(Config Server)来为所有的环境和应用提供外部配置的集中管理。这些概念都通过Spring的Environment
和PropertySource
来抽象,所以它可以适用于各类Spring应用,同事支持任何语言的任何应用。它也能为你支持对应用开发环境、测试环境、生产环境的配置、切换、迁移。默认的配置实现通过git实现,同事非常容易的支持其他的扩展(比如svn等)。在spring cloud config 组件中,分两个角色,一是config server,二是config client。
二、构建Config Server
新建Maven Web项目,pom.xml文件如下:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.captain.config</groupId> 5 <artifactId>server</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>server Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 <dependencyManagement> 11 <dependencies> 12 <dependency> 13 <groupId>org.springframework.cloud</groupId> 14 <artifactId>spring-cloud-config</artifactId> 15 <version>2.0.0.M7</version> 16 <type>pom</type> 17 <scope>import</scope> 18 </dependency> 19 </dependencies> 20 </dependencyManagement> 21 <dependencies> 22 <dependency> 23 <groupId>org.springframework.cloud</groupId> 24 <artifactId>spring-cloud-starter-config</artifactId> 25 </dependency> 26 <dependency> 27 <groupId>org.springframework.cloud</groupId> 28 <artifactId>spring-cloud-config-server</artifactId> 29 </dependency> 30 </dependencies> 31 <repositories> 32 <repository> 33 <id>spring-milestones</id> 34 <name>Spring Milestones</name> 35 <url>https://repo.spring.io/libs-milestone</url> 36 <snapshots> 37 <enabled>false</enabled> 38 </snapshots> 39 </repository> 40 </repositories> 41 </project>
新建class类ConfigServerApplication用作程序入口,@EnableConfigServer注解开启config server功能。
1 @SpringBootApplication 2 @EnableConfigServer 3 public class ConfigServerApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(ConfigServerApplication.class, args); 7 } 8 }
在classpath下新建文件application.yml作为服务配置文件。请注意yml语法,对此不熟悉的请百度,在此不做赘述。此例中使用了本地配置文件,如果使用远程配置文件需要对application.yml做相应调整。
1 server : 2 port : 8001 3 4 spring : 5 profiles : 6 active : native 7 cloud : 8 config : 9 server : 10 native : 11 searchLocations : classpath:/properties/
在classpaht下新建properties文件夹,并新建两个配置文件config-dev.properties config-test.properties。
config-dev.properties:
1 name=captain-dev
config-test.properties:
1 name=captain-test
启动程序,访问http://localhost:8001/config/test可以看到返回配置结果:
{ "name": "config", "profiles": [ "test" ], "label": null, "version": null, "state": null, "propertySources": [ { "name": "classpath:/properties/config-test.properties", "source": { "name": "captain-test" } } ] }
三、构建Config Client
Maven新建config client项目,pom.xml文件如下:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.captain.config</groupId> 5 <artifactId>client</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>client Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 <dependencyManagement> 11 <dependencies> 12 <dependency> 13 <groupId>org.springframework.cloud</groupId> 14 <artifactId>spring-cloud-config</artifactId> 15 <version>1.1.1.RELEASE</version> 16 <type>pom</type> 17 <scope>import</scope> 18 </dependency> 19 </dependencies> 20 </dependencyManagement> 21 <dependencies> 22 <dependency> 23 <groupId>org.springframework.boot</groupId> 24 <artifactId>spring-boot-starter-web</artifactId> 25 </dependency> 26 <dependency> 27 <groupId>org.springframework.cloud</groupId> 28 <artifactId>spring-cloud-config-client</artifactId> 29 </dependency> 30 </dependencies> 31 </project>
新建程序入口,为了测试方便将测试接口代码也放在了该类中:
1 @SpringBootApplication 2 @RestController 3 public class ConfigClientApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(ConfigClientApplication.class, args); 7 } 8 9 @Value("${name}") //取配置中心中key为name的值 10 String name; 11 12 @RequestMapping("/test") 13 String test() { 14 return "hello " + name; 15 } 16 }
在classpath下新建
application.yml:
server : port : 8002
bootstrap.yml:
spring : cloud : config : name : config profile : test label : master uri : http://localhost:8001
启动程序,访问http://localhost:8002/test输出hello captain-test
参考资料:http://blog.csdn.net/fang_sh_lianjia/article/details/52356197
原文地址:https://www.cnblogs.com/captainx/p/8482791.html