爲何要統一管理微服務配置?git
隨着微服務不斷的增多,每一個微服務都有本身對應的配置文件。在研發過程當中有測試環境、UAT環境、生產環境,所以每一個微服務又對應至少三個不一樣環境的配置文件。這麼多的配置文件,若是須要修改某個公共服務的配置信息,如:緩存、數據庫等,不免會產生混亂,這個時候就須要引入Spring Cloud另一個組件:Spring Cloud Config。 github
有哪幾種?
Apollo(阿波羅)是攜程框架部門研發的配置管理平臺,可以集中化管理應用不一樣環境、不一樣集羣的配置,配置修改後可以實時推送到應用端。spring
apollo項目基於springboot與springcloud,能夠獨立部署數據庫
Apollo GitHub地址:bootstrap
https://github.com/ctripcorp/apollo緩存
今天重點SpringCloud-config
compile 'org.springframework.cloud:spring-cloud-config-server'
客戶端Gradlespringboot
compile 'org.springframework.cloud:spring-cloud-starter-config'
啓動類引入ConfigServer表明爲配置中心服務端架構
package org.gd; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; /** * @DATA 2019-03-11 09:10 * @Author 張國偉 WeChat:17630376104 * @Description TODO */ @EnableConfigServer @SpringBootApplication public class ConfigConter { public static void main(String[] args) { SpringApplication.run(ConfigConter.class, args); } }
建立三個bootstrapapp
咱們把配置文件放在Gitlab上,在Gitlab上建立yml框架
建立yml
spring: application: name: config-center cloud: config: server: git: uri: https://gitlab.com/zgw1469039806/config-center ###git地址 clone-on-start: true #默認狀況下,配置服務會在配置文件第一次被請求時遠程的配置庫.固然也能夠配置爲在啓動時clone遠程的配置庫 search-paths: local #選擇是那個配置 username: ****** #git帳號密碼 password: ****** server: port: 9999clone
spring: application: name: config-center cloud: config: server: git: uri: https://gitlab.com/zgw1469039806/config-center.git clone-on-start: true search-paths: test username: ***** password: ***** server: port: 9999
沒錯 兩個個就是環境不同
spring:
profiles:
active: test
用主yml來負責啓動時的切換測試環境仍是生產環境,若是配置沒有錯誤的話直接訪問能夠訪問到yml
直接訪問yml的名字便可,ok到這裏配置中心已經配置完畢,那咱們再來看下客戶端怎麼配置
spring: application: name: project-shopping-mall cloud: config: uri: http://localhost:9999
客戶端很簡單,指向config服務端就能夠
咱們能夠看到客戶端直接讀取到git上面,說明配置已經成功。
項目GitHub地址:https://github.com/zgw1469039806/springcloud-project