當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringCloud微服务(06):Config组件,实现配置统一管理
生活随笔
收集整理的這篇文章主要介紹了
SpringCloud微服务(06):Config组件,实现配置统一管理
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、Config簡介
在微服務(wù)系統(tǒng)中,服務(wù)較多,相同的配置:如數(shù)據(jù)庫信息、緩存、參數(shù)等,會出現(xiàn)在不同的服務(wù)上,如果一個配置發(fā)生變化,需要修改很多的服務(wù)配置。spring cloud提供配置中心,來解決這個場景問題。
系統(tǒng)中的通用配置存儲在相同的地址:GitHub,Gitee,本地配置服務(wù)等,然后配置中心讀取配置以restful發(fā)布出來,其它服務(wù)可以調(diào)用接口獲取配置信息。
二、配置服務(wù)端
1、項目結(jié)構(gòu)
- 核心注解:@EnableConfigServer
2、核心依賴
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId> </dependency>3、核心配置文件
這里注意讀取文件的配置
- active :native,讀取本地配置;
- active :git,讀網(wǎng)絡(luò)倉庫配置;
4、讀取配置內(nèi)容
不同的環(huán)境讀取的結(jié)果不同。
info:date: 20190814author: cicadasign: developversion: V1.0三、配置客戶端
1、核心依賴
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId> </dependency>2、核心配置文件
在上面的配置中心,配置讀取Git資源,所以這里的配置也就是讀取Git資源。
server:port: 8001 spring:application:name: config-client-8001profiles:active: devcloud:config:# 讀取本地配置 ---------------------------#uri: http://localhost:9001## 讀取策略:快速失敗#fail-fast: true## 讀取的文件名:無后綴#name: client-8001## 讀取的配置環(huán)境#profile: dev # client-8001-dev.yml# ----------------------------------------# github上的資源名稱 -----------------------name: client-8001# 讀取的配置環(huán)境profile: devlabel: master# 本微服務(wù)啟動后,通過配置中心6001服務(wù),獲取GitHub的配置文件uri: http://localhost:9001# ----------------------------------------3、測試接口
@RestController public class ClientController {@Value("${info.date}")private String date ;@Value("${info.author}")private String author ;@Value("${info.sign}")private String sign ;@Value("${info.version}")private String version ;/*** 獲取配置信息*/@RequestMapping("/getConfigInfo")public String getConfigInfo (){return date+"-"+author+"-"+sign+"-"+version ;} }四、基于Eureka配置
上面的模式,通過服務(wù)中心,直接獲取配置。下面把注冊中心Eureka加進(jìn)來。
1、項目結(jié)構(gòu)
啟動順序也是如下:
node06-eureka-7001 config-server-9001 config-client-80012、修改配置項
- 將config-server-9001添加到注冊中心;
- 配置config-client-8001讀取注冊中心;
完成后Eureka注冊中心效果圖,啟動順序如下:
3、修改客戶端配置
通過注冊中心獲取服務(wù),避免使用URI地址。
經(jīng)過測試后,正確無誤。
- 提醒:國內(nèi)如果讀取git的配置,可能經(jīng)常出去無法加載的問題,該案例使用的是Gitee的地址。
五、源代碼地址
GitHub地址:知了一笑 https://github.com/cicadasmile/spring-cloud-base 碼云地址:知了一笑 https://gitee.com/cicadasmile/spring-cloud-base總結(jié)
以上是生活随笔為你收集整理的SpringCloud微服务(06):Config组件,实现配置统一管理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux-squirrel
- 下一篇: MySQL使用Amoeba作为Proxy