Config配置中心做用簡單來說:統一配置,方便管理html
開源配置中心:git
1.百度Disconfgithub
2.阿里Diamandspring
3.Spring Cloud Configbootstrap
搭建Config-Server服務器
快速上手:app
選擇依賴:Eureka和Config測試
選取Eureka緣由:保證高可用url
啓動類加入註解:spa
package org.dreamtech.configserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
配置:
Eureka-Server的搭建:http://www.javashuo.com/article/p-pxvayqiy-mx.html
使用Git服務器:我使用的是Github,新建一個測試倉庫
spring.application.name=config-server server.port=9100 eureka.client.service-url.defaultZone=http://localhost:8761/eureka spring.cloud.config.server.git.uri=https://github.com/EmYiQing/SpringCloudTest spring.cloud.config.server.git.username=EmYiQing spring.cloud.config.server.git.password=xxxxxx
在Github上新建一個配置文件作測試:
測試:
啓動項目Eureka-Server->Config-Server
查看註冊中心:成功
訪問localhost:9100:報錯
訪問http://localhost:9100/product-service.yml:拿到剛纔在Github新建的配置文件,成功
若是咱們訪問http://localhost:9100/product-service.properties?
那麼Config會直接把YML轉化爲properties文件,很強大
注意:Git上的配置文件格式必定不能出錯
還能夠作一些默認配置:好比超時時間和默認分支
spring.cloud.config.server.git.timeout=5
spring.cloud.config.server.git.default-label=master
有了Server固然也要有Client:
在服務模塊引入依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency>
將配置文件application.yml改名爲bootstrap.yml:
進行三項配置:
1.Eureka-Server-URL
2.該服務模塊名稱
3.Config配置中心在Eureka-Server的名稱和啓用
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: product-service
cloud:
config:
discovery:
service-id: CONFIG-SERVER
enabled: true
啓動項目耗時將會更長,由於讀取Git服務器配置文件是耗時的操做
Config配置中心使用的注意事項:
1.保證Git服務器的配置文件格式正確
2.Git服務器的配置文件建議採用分支進行區分,不推薦使用後綴區分
好比使用/test/product-service.yml代替/product-service-test.yml
然而到這裏仍是沒有結束,好比配置文件的動態更新等等,這些功能在之後實現