spring cloud學習筆記(一):配置伺服器搭建
阿新 • • 發佈:2019-02-07
要使用spring cloud分散式配置檔案總體上分為3個大的步驟:
1.首選你需要建立存放配置檔案的git倉庫。
2.建立一個配置檔案伺服器,該伺服器將配置檔案資訊轉化為rest介面資料對外提供。
3.建立一個demo應用專案,該專案演示使用配置檔案伺服器。
具體步驟:
1)建立配置檔案存放倉庫
Spring cloud使用git或svn存放配置檔案,預設情況下使用git,因此你需要安裝git私服或者直接使用網際網路上的github或者git.oschina,這裡推薦使用git.oschina。本文示例使用的是git.oschina,建立好git工程後,在此工程再建立兩個資料夾test和prop來存放配置檔案。然後建立兩個配置檔案。如下:
說明: 這兩個資料夾分別存放測試和正式環境的配置資訊,供demo專案使用。
2)建立配置檔案伺服器
配置檔案倉庫建立好了後,就需要建立配置管理伺服器,該伺服器只是將配置檔案轉換為rest介面對外提供服務,不做其它用途。這個伺服器的功能也是spring cloud提供的,所以我們只需要引入相關jar包,稍微設定一下即可。
1.建立該服務應用,你需要首選建立一個空的maven工程:
2.修改pom檔案的引用,新增springCloud的繼承引用和config Server服務包的引用。
<!-- 1.引入springCloud starter parent包的繼承-->
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Brixton.RELEASE</version>
<relativePath />
</parent>
<groupId>com.vx.springCloud</groupId>
<artifactId >configServer</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>configServer</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- 2. 引入configserver服務包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
3.在專案application.yml中新增配置伺服器的連結資訊
server:
#config配置服務一般設定埠8888
port: 8888
spring:
application:
name: configServer
cloud:
#git服務配置資訊
config:
server:
git:
uri: https://git.oschina.net/aofavx/conf.git
#從以下資料夾下下面查詢配置檔案
search-paths: dev*,test*
其中server.port是配置當前web應用繫結8888埠,git.uri指定配置檔案所在的git工程路徑,searchPaths表示將搜尋這些資料夾下的配置檔案。
4.修改App.java檔案,設定啟動類和congfigServer註解。
/**
* 系統配置伺服器服務專案
*/
@SpringBootApplication
//設定當前專案為配置檔案伺服器
@EnableConfigServer
public class App
{
public static void main( String[] args ){
SpringApplication.run(App.class,args);
}
}
5. 啟動服務成功,其他專案就可以通過配置伺服器拿到配置資訊了。
3)建立Demo專案使用配置服務
1.建立Demo專案,同樣先建立一個普通的maven工程:
2.引入springcloud config的clientjar包
<!-- 1.引入springCloud parent包的繼承-->
<parent>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Brixton.RELEASE</version>
<relativePath />
</parent>
<dependencies>
<!-- 2. 引入configserver服務包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
</dependencies>
3.設定指向配置伺服器的config資訊
server:
port: 8080
spring:
application:
name: configDemo
profiles:
#選擇使用下面哪個配置項
active: service1
#服務配置資訊
cloud:
config:
#此處的config.profile會根據生效的active設定對應的值
profile: ${config.profile}
name: helloword
uri: http://localhost:8888
#將不同的配置組以---分割,並通過spring.profiles定義每組的名字,方便後續引用
---
spring:
profiles: service1
config:
profile: test
---
spring:
profiles: service2
config:
profile: dev
說明:獲取git上的資源資訊遵循如下規則:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
application:表示應用名稱,在配置檔案中通過spring.config.name配置,如果不配置這使用spring.application.name值
profile:表示獲取指定環境下配置,例如開發環境、測試環境、生產環境 預設值default,實際開發中可以是 dev、test、demo、production等
label: git標籤,預設值master
4.注入需要使用的配置項
@RestController
@SpringBootApplication
public class App
{
//冒號後面代表:如果沒設定引數,就取冒號後面的值作為預設值
@Value("${message: Hello Word}")
String msg;
@ResponseBody
@RequestMapping(value = "/")
String home() {
return "獲取的引數值:"+msg;
}
public static void main( String[] args ){
SpringApplication.run(App.class,args);
}
}
5.啟動專案
此時啟動專案時,demo專案傳送獲取配置資訊的請求給configServer服務。
configServer服務會從git上根據請求的資原始檔規則下載對應檔案,儲存到本地快取,並將配置資訊返回給Demo專案。
6.測試
在瀏覽器中輸入:http://localhost:8080/ 回車。
說明配置伺服器和Demo中配置的引用都OK。
修改Demo專案埠為8081,通過maven打成Jar。執行jar命令:
java -Dspring.profiles.active=service2 -jar configDemo-1.0-SNAPSHOT.jar
此時再在瀏覽器從檢視8081埠的Demo專案。執行也OK。
配置伺服器的好處看到了吧,只需要一個profiles.active 就可以使用不同的配置了! *_*