資料
-
springcloud-config配置中心的使用 : https://blog.csdn.net/maoyeqiu/article/details/78543948
-
SpringCloud 配置中心Config和Eureka結合 : https://blog.csdn.net/Keith003/article/details/82382245
-
Config配置中心與客戶端的使用與詳細: https://www.cnblogs.com/hellxz/p/9306507.html
-
SpringCloudConfig配置中心: https://www.bilibili.com/video/BV1Li4y147fT?p=2
爲什麼要用springcloud配置中心
拆分粒度比較小的話,那麼這個系統將會出現大量的服務,而我們在運行服務前,要給每個服務都進行必要的信息
的配置,服務才能正常運行,比如採用eureka註冊中心的,需要給子服務添加eureka的配置,爲了方便管理這些
子服務的配置文件,所以需要一個集中的,動態的管理工具。 所以springcloud爲我們提供了ConfigServer配置中心。
幾個概念
圖解
========================倉庫配置文件=====================
-
在碼雲上創建一個名爲SpringCloudConfig的倉庫
-
將微服務的配置文件拷貝一份
-
在建好的倉庫中新建一個文件
-
將複製好的配置,粘貼進去,然後給文件起一個名字,名字的規則就是如下的配置
-
/ { 應用名 } / { 環境名 } [ / { 分支名 } ]
-
/ { 應用名 } - { 環境名 }.yml
-
/ { 應用名 } - { 環境名 }.properties
-
/ { 分支名 } / { 應用名 } - { 環境名 }.yml
-
/ { 分支名 } / { 應用名 } - { 環境名 }.properties
配置規則詳解
-
/ { 應用名 } / { 環境名 } [ / { 分支名 } ]
-
/ { 應用名 } - { 環境名 }.yml
-
/ { 應用名 } - { 環境名 }.properties
-
/ { 分支名 } / { 應用名 } - { 環境名 }.yml
-
/ { 分支名 } / { 應用名 } - { 環境名 }.properties
注意:
第一個規則的分支名是可以省略的,默認是master分支 無論你的配置文件是properties,還是yml,只要是應用名+環境名能匹配到這個配置文件,那麼就能取到 如果是想直接定位到沒有寫環境名的默認配置,那麼就可以使用default去匹配沒有環境名的配置文件 使用第一個規則會匹配到默認配置 如果直接使用應用名來匹配,會出現404錯誤,此時可以加上分支名匹配到默認配置文件 如果配置文件的命名很由多個-分隔,此時直接使用這個文件名去匹配的話,會出現直接將內容以源配置文件內容直接返回,內容前可能會有默認配置文件的內容(已測試)
========================config服務端========================
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
2、啓用configserver
server:
port: 9999
spring:
application:
name: config-server #微服務應用名
cloud:
config:
server:
git:
uri: https://gitee.com/ming_5257/SpringCloudConfig.git #git倉庫的地址
search-paths: SpringCloudConfig/eureka-dev.yml #配置倉庫路徑
username: *************
password: ********
========================config客戶端========================
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
2、添加bootstrap.yml配置文件
spring:
application:
name: search-service #服務的名稱(倉庫設置的配置文件的應用名相同,具體的看上面的配置規則詳解)
cloud: #配置中心相關
config:
profile: dev #環境配置(開發,測試,生產)
label: master #分支配置
uri: http://********** #配置中心的網址
discovery:
enabled: true #從配置中心讀取文件
==========================配置中心與註冊中心的整合======================
-
改寫配置中心的配置文件,主要是添加eureka的配置
server:
port: 9999
spring:
application:
name: config-server #微服務應用名
cloud:
config:
server:
git:
uri: https://gitee.com/ming_5257/SpringCloudConfig.git #git倉庫的地址
search-paths: SpringCloudConfig/eureka-dev.yml #配置倉庫路徑
username: *************
password: ********
# eureka
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
registry-fetch-interval-seconds: 10
instance:
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 15
- ServerClient的配置
spring:
application:
name: search-service #服務的名稱(倉庫設置的配置文件的應用名相同,具體的看上面的配置規則詳解)
cloud: #配置中心相關
config:
profile: dev #環境配置(開發,測試,生產)
label: master #分支配置
uri: http://********** #配置中心的網址
discovery:
enabled: true #從配置中心讀取文件
service-id: config-server #配置中心在eureka的服務名稱
#Eureka的配置
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
registry-fetch-interval-seconds: 5