Spring Cloud學習筆記 【篇一:分布式配置中心 Spring Colud Config】
一、簡介
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
Spring Cloud學習筆記 【篇一:分布式配置中心 Spring Colud Config】