微服務配置中心
Spring Cloud Config 實現(xiàn)分布式配置
創(chuàng)建配置中心
步驟一:加入依賴
創(chuàng)建一個基礎的Spring Boot工程,并在pom.xml中引入依賴
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
步驟二:在程序主類上添加@EnableConfigServer注解便瑟,開啟spring cloud config的服務端功能
@EnableConfigServer
@SpringBootApplication
public class ConfigureCenterApplication{
public static void main(String[] args){
SpringApplication.run(ConfigureCenterApplication.class,args);
}
}
步驟三:配置服務端
spring-cloud-config 服務端配置中心分布式存儲配置文件有四種方式
- native 本地文件系統(tǒng)
- git-native 本地文件系統(tǒng)背亥,git形式
- git-remote git遠程倉庫
- subversion svn倉庫
server:
port: 11000
eureka:
client:
service-url:
defaultZone: http://localhost:10009/eureka/
# 本地文件存儲方式
spring:
application:
name: @pom.artifactId@
profiles:
active: native
cloud:
config:
server:
native:
search-locations: classpath:/config
----
# git形式的本地存儲方式
git官方建議拨扶,使用本地文件系統(tǒng)進行git存儲庫僅用于測試糊余。使用服務器在生產(chǎn)環(huán)境中托管配置庫焊唬。
git本地倉庫方式,需要將存儲配置文件的目錄進行git操作,如下:
#git init
#git add *
#git commit -m '配置文件提交'
spring:
cloud:
config:
server:
git:
uri: file:///D:/Java/config(windows文件系統(tǒng)的前綴為『file:///』,其他系統(tǒng)的前綴為『file://』)
---
# git 遠程存儲方式
spring:
cloud:
config:
server:
git:
uri: https://github.com/zhengbinger/config-center
search-paths: '{application}'
username: zhengbinger
password: ******
----
# svn 倉庫存儲
需要在工程中引入豫缨,svnkit的依賴
且必須顯示聲明spring.profiles.active=subversion不然還是用的git,svn默認的lable是trunk
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
</dependency>
spring.profiles.active=subversion
spring.cloud.config.server.svn.uri=http://localhost/usvn/svn/datadev/docs/config/
spring.cloud.config.server.svn.username=zhengbing
spring.cloud.config.server.svn.password=xxxxxx
spring.cloud.config.server.svn.search-paths={application} #使用{application}占位符
spring.cloud.config.server.svn.default-label=trunk
spring.cloud.config.server.svn.basedir=/data #默認在系統(tǒng)臨時目錄下面端朵,需要調(diào)整一下避免臨時文件被系統(tǒng)自動清理
環(huán)境庫
環(huán)境庫的資源有三個變量參數(shù)化:
{application}映射到客戶端的"spring.application.name"
{profile}映射到客戶端上的"spring.profiles.active"(逗號分隔列表)
{label}這是一個服務器端功能好芭,標記"版本"分支的一組配置文件,例如: dev/master 等
查看配置文件庫中的文件
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
http://localhost:11000/server-configure/dev -- 默認使用master分支,等同于 http://localhost:11000/server-configure/dev/master
http://localhost:11000/server-configure/dev/dev -- 查看dev分支的配置文件