1. 簡介
Spring Cloud Config 是用來為分布式系統(tǒng)中為微服務應用提供集中化的外部配置支持,主要分為Spring Cloud Config Server(服務器端)和Spring Cloud Config Client(客戶端)。
2. Spring Cloud Config Server
Spring Cloud Config Server為服務器端,它是一個單獨的微服務應用,用來連接配置倉庫(本文使用的是git倉庫)并為客戶端獲取配置信息。
1. 首先,創(chuàng)建config server工程
打開http://start.spring.io/
填寫好Group、Artifact。選擇依賴的包有Config Server。
對應的pom.xml為
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2. 將下載下來的項目導入Eclipse。
目錄結構如下,我這里面新增了bootstrap.yml
3. 修改配置
在application.yml中添加
server:
port: 8080
在bootstrap.yml中添加
spring:
cloud:
config:
server:
git:
uri: https://github.com/DevinXin/config-repo
注意:ConfigServerApplication,Spring Boot 啟動類上需要添加@EnableConfigServer注解
4. 啟動configServer
通過訪問http://localhost:8080/master/foobar-dev.properties可以讀到git上的配置文件。
3. Spring Cloud Config Client
Spring Cloud Config Client為客戶端,客戶端通過配置連接服務器端,從服務器端加載配置信息。
1. 創(chuàng)建config client工程
同config server工程創(chuàng)建一樣,依賴需要web,Config Client
對應的pom.xml為:
<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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2. 修改配置
application.yml配置為
server:
port: 8081
bootstrap.yml配置為
spring:
cloud:
config:
uri: http://localhost:8080/
profile: dev
label: master
application:
name: foobar
3. 寫一個Controller
4. 啟動config Client。
訪問http://localhost:8081/configServer
可以從config Server中獲取到配置文件中的值。
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
名稱欄目:SpringCloudConfig入門-創(chuàng)新互聯(lián)
URL鏈接:http://aaarwkj.com/article20/dpeijo.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、定制開發(fā)、做網(wǎng)站、網(wǎng)站維護、響應式網(wǎng)站、網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容