文章目錄
1. 分佈式系統存在的問題—配置問題
分佈式系統意味着有好多小服務,這樣系統中存在大量的服務,但是每個服務都需要配置信息才能運行,映射到SpringBoot項目中意味中有很多application.yml配置文件,很難管理,這是SpringCloud提供了ConfigServer來解決這個問題,即使用SpringServer這一個服務實現了對所有服務的配置文件進行集中式的,動態的配置
2. Config服務是什麼
SpringCloud Config爲微服務架構中的微服務提供集中化的外部配置支持,配置服務器爲各個不同的微服務應用所有環境提供一箇中心化的外部配置
3. Config服務能幹什麼
- 集中管理配置文件
- 不同環境不同配置,動態化的配置更新
- 運行期間動態調整配置,不再需要在每個服務部署的機器上編寫配置文件,服務會向配置中心統一拉取配置自己的信息
- 當配置改動時,服務不需要重啓即可感知到配置的變化並應用新的配置
- 將配置信息已REST接口的形式暴露
4. 項目實戰
SpringCloud Config分爲服務端和客戶端,服務端稱爲分佈式配置中心,是一個獨立的服務,用來連接配置服務器併爲客戶端提供獲取配置信息,加密/解密等訪問接口;客戶端是通過制定的配置中心來管理應用資源,以及與業務相關的配置內容,並在啓動的時候從配置中心獲取和加載配置信息,可以理解爲服務提供方provider。配置文件通常放在git上
4.1 gitHub配置
將各個配置文件放置到gitHub上,文件名爲:integral-game.yml
spring:
profiles:
active: dev //@profileActive@
---
server:
port: 8090
servlet:
context-path: /game-web
spring:
profiles: dev
application:
name: integral-game-provider
main:
allow-bean-definition-overriding: true
eureka:
client:
service-url:
#客戶端註冊進eureka服務列表內
defaultZone: http://192.168.22.126:7001/eureka/
instance:
preferIpAddress: true
ipAddress: 192.168.22.126
#註冊到eureka後,status的名字(服務在eureka的唯一標誌)
instance-id: ${spring.application.name}:${random.int}
#訪問路徑可以顯示IP地址
prefer-ip-address: true
info:
app.name: provider-dept-8001
company.name: www.tfjybj.com
build.artifactId: $project.artifactId$
build.version: $project.version$
---
server:
port: 8090
servlet:
context-path: /game-web
spring:
profiles: test
application:
name: integral-game-provider
main:
allow-bean-definition-overriding: true
eureka:
client:
service-url:
#客戶端註冊進eureka服務列表內
defaultZone: http://192.168.22.227:7001/eureka/
instance:
#註冊到eureka後,status的名字(服務在eureka的唯一標誌)
instance-id: ${spring.application.name}:${random.int}
#訪問路徑可以顯示IP地址
prefer-ip-address: true
info:
app.name: provider-dept-8001
company.name: www.tfjybj.com
build.artifactId: $project.artifactId$
build.version: $project.version$
4.2 Config服務端配置
4.2.1 Config服務端連接github
在Config服務端配置如下yml文件:
server:
port: 3344
spring:
application:
name: cloud-config-service
cloud:
config:
server:
git:
uri: [email protected]:zzyybs/microservicecloud-config.git #GitHub上面的git倉庫名字
這樣服務端就和github建立了連接,可以讀取遠程git倉庫配置了
4.2.2 配置啓動項加註解@EnableConfigServer
@EnableConfigServer
@SpringBootApplication
public class CloudConfigApplication {
public static void main(String[] args) {
SpringApplication.run(CloudConfigApplication.class, args);
}
}
4.3 客戶端配置
4.3.1 application.yml配置如下:
spring:
application:
name: integral-game-provider
4.3.2 bootstrap.yml 配置如下:
spring:
cloud:
config:
name: integral-game //讀取github上的那個文件對應的文件名 integral-game.yml,不帶yml後綴名
#正常方式應該從github上更改
profile: dev //@profileActive@ ,拉取dev環境配置
#label: master // 從github的master
label: integral-config
uri: http://192.168.22.126:3344 // Config服務端地址
main:
allow-bean-definition-overriding: true
4.3.3 啓動項加註解
@SpringBootApplication
//本服務啓動後會自動註冊進eureka服務中
@EnableEurekaClient
//服務發現
@EnableDiscoveryClient
public class IntegralGameProviderApplication {
public static void main(String[] args) {
SpringApplication.run(IntegralGameProviderApplication.class, args);
}
}
這注意到,讀取外部文件的配置信息寫在了bootstrap.yml文件中,爲什麼要把配置信息寫在bootstrap.yml文件中那:application.yml是用戶級的資源配置項;而bootstrap.yml是系統級的,優先級更高。SpringCloud會創建一個Bootstrap Context作爲Spring應用的Application Context的父上下文。初始化時,Bootstrap Context負責從外部資源加載配置屬性並解析配置,這兩個上下文共享一個從外部獲取的Environment,但Bootstrap有更高優先級,默認情況下,他們不會被本地配置覆蓋。