簡述:前面一篇文章我們有講到註冊中心的搭建,現在我們來講統一的配置中心,我們可以將所有的配置文件發佈到遠程管理,如github或者gitlab管理,這樣我們可以讓所有的配置文件統一管理,且切換各種環境方便。
一.準備材料
gitlab服務器且開通了http訪問下載的項目權限或者GitHub賬號。
二.新建module用作統一配置中心,怎麼新建module這裏就不做詳細介紹了。
在新的module的pom裏面引入config包
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> <version>${config.version}</version> </dependency>
三.啓動類除了原有的
@SpringBootApplication @EnableDiscoveryClient註解,增加@EnableConfigServer註解
四.統一配置中心配置文件,
server: port: 9527 servlet: context-path: /serviceConfig spring: application: name: fool-cloud-config profiles: active: git cloud: config: server: git: #uri是在GitHub或者gitlab上面的倉庫地址,我這裏是默認公共的不需要賬號密碼驗證 uri: https://github.com/Owen199433/fool-cloud.git # uri: https://github.com/jmxct520/hsp-prod.git # 標籤(這裏筆者配置命名採用的是prfile+label的方式) default-label: master #如果非公共的倉庫,可以在下面配置賬號密碼 # username: # password:
eureka: instance: prefer-ip-address: true client: serviceUrl: #這裏需要的地址是默認的空間+context-path defaultZone: http://admin:admin@localhost:9526/eureka/eureka/ enabled: true
五.啓動統一配置中心端口號發佈,且註冊到註冊中心則啓動成功,現在我們再來驗證下配置中心是否生效
新建一個module這裏我命名爲example-master.yml,內容如下
spring:
application:
name: fool-cloud-example
eureka:
instance:
prefer-ip-address: true
client:
serviceUrl:
defaultZone: http://admin:[email protected]:9526/eureka/eureka/
enabled: true
server:
port: 9528
servlet:
context-path: /example
我們將它放到第四步的遠程的倉庫,然後我們通過訪問配置中心的地址
http://localhost:9527/serviceConfig/example-master.yml 當我們看到如下我們上傳的配置內容,則統一配置中心啓動成功。
然後示範客戶端項目的配置文件bootstrap.yml內容爲
spring: application: #這裏的name和配置中心的applicationname保持一致,不然將不讀取配置文件 name: serviceConfig profiles: active: git cloud: config: label: master #配置文件的名字 profile: example-master #統一配置中心的地址 uri: http://localhost:9527/serviceConfig discovery: enabled: true service-id: fool-cloud-config eureka: instance: prefer-ip-address: true client: serviceUrl: defaultZone: http://admin:[email protected]:9526/eureka/eureka enabled: true
六.如上配置完成以後啓動example項目,配置中心可以看到從GitHub拉取了example的配置文件,並且example的端口號啓動爲遠程配置的端口號
這樣我們的統一配置中心就搭建完了,下一篇文章我們將講解搭建lcnTx事務管理中心。
更多問題交流請加入QQ羣:260292706 交流,也可私信我交流問題
Owen