為什么要使用配置中心
- 集中管理配置糊余。一個(gè)使用微服務(wù)架構(gòu)的應(yīng)用系統(tǒng)可能會(huì)包含成百上千個(gè)微服務(wù)霍殴,因此集中管理配置是非常有必要的;
- 不同環(huán)境帝簇,不同配置徘郭。例如,數(shù)據(jù)源配置在不同的環(huán)境(開發(fā)丧肴、測(cè)試残揉、預(yù)發(fā)布、生產(chǎn)等)中是不同的芋浮;
- 運(yùn)行期間可動(dòng)態(tài)調(diào)整抱环。例如,我們可根據(jù)各個(gè)微服務(wù)的負(fù)載情況纸巷,動(dòng)態(tài)調(diào)整某自定義的配置文件參數(shù)
- 配置修改后可自動(dòng)更新镇草。如配置內(nèi)容發(fā)生變化,微服務(wù)能夠自動(dòng)更新配置瘤旨。
Spring Cloud Config簡(jiǎn)介
Spring Cloud Config為分布式系統(tǒng)外部化配置提供了服務(wù)器端和客戶端的支持梯啤,它包括Config Server和Config Client兩部分。由于Config Server和Config Client都實(shí)現(xiàn)了對(duì)Spring Environment和PropertySource抽象的映射裆站,因此条辟,Spring Cloud Config非常適合Spring應(yīng)用程序黔夭,當(dāng)然也可與任何其他語(yǔ)言編寫的應(yīng)用程序配合使用宏胯。
Config Server是一個(gè)可橫向擴(kuò)展羽嫡、集中式的配置服務(wù)器,它用于集中管理應(yīng)用程序各個(gè)環(huán)境下的配置肩袍,默認(rèn)使用Git存儲(chǔ)配置內(nèi)容(也可使用Subversion杭棵、MySQL、本地文件系統(tǒng)或Vault存儲(chǔ)配置氛赐,本博客以Git為例進(jìn)行講解)魂爪,因此可以很方便地實(shí)現(xiàn)對(duì)配置的版本控制與內(nèi)容審計(jì)。
創(chuàng)建spring-cloud-config模塊:
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>spring-cloud-wsl</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>spring-cloud-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-config</name>
<description>Demo project for Spring Cloud Config</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring cloud config 服務(wù)端包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
配置文件application.yml
server:
port: 8885
spring:
application:
name: config
cloud:
config:
server:
git:
uri: 你的github地址 .git結(jié)尾的
username: 你的賬號(hào)
password: 你的密碼
# default-label: master #配置文件分支
# search-paths: config #配置文件所在根目錄
eureka:
client:
service-url:
#注冊(cè)中心地址
defaultZone: http://localhost:8761/eureka/
instance:
#將ip注冊(cè)到eureka上
prefer-ip-address: true
啟動(dòng)類添加注解:
@EnableConfigServer
@SpringBootApplication
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
spring-cloud-config模塊到此創(chuàng)建完畢
我是在碼云上存放的配置文件艰管,因?yàn)閲?guó)內(nèi)碼云會(huì)比github的訪問(wèn)速度要快一些
登錄碼云(沒(méi)有賬號(hào)的可以免費(fèi)注冊(cè)一個(gè))滓侍,首頁(yè)的右上角創(chuàng)建倉(cāng)庫(kù):
填上倉(cāng)庫(kù)名選擇公開:
然后在該倉(cāng)庫(kù)下創(chuàng)建yml文件,具體的git使用請(qǐng)自行百度牲芋,此處不進(jìn)行過(guò)多說(shuō)明
我是提交到倉(cāng)庫(kù)了兩個(gè)provider的yml文件和一個(gè)consumer-feign的yml文件撩笆,我們用provider舉例:
provider-dev1.yml:
server:
port: 8880
#eureka客戶端連接配置
eureka:
client:
service-url:
#注冊(cè)中心地址
defaultZone: http://localhost:8761/eureka/
instance:
#將ip注冊(cè)到eureka上
prefer-ip-address: true
provider-dev2.yml:
server:
port: 8881
#eureka客戶端連接配置
eureka:
client:
service-url:
#注冊(cè)中心地址
defaultZone: http://localhost:8761/eureka/
instance:
#將ip注冊(cè)到eureka上
prefer-ip-address: true
回到項(xiàng)目中,在spring-cloud-provider模塊中創(chuàng)建bootstrap.yml配置文件:
spring:
application:
name: provider
cloud:
config:
discovery:
enabled: true
serviceId: config
profile: dev1
# 指定分枝版本缸浦,默認(rèn)為master
label: master
注:這里的profile: dev1 對(duì)應(yīng)的是碼云上的配置文件名字的后綴夕冲,
具體名稱和配置的對(duì)應(yīng)關(guān)系如下:
/{name}-{profiles}.properties
/{name}-{profiles}.yml || /{name}-{profiles}.yaml
/{label}/{name}-{profiles}.properties
/{label}/{name}-{profiles}.json
/{name}/{profiles}/{label:.*}
/{name}-{profiles}.json
/{label}/{name}-{profiles}.yml || /{label}/{name}-{profiles}.yaml
/{name}/{profiles:.*[^-].*}
/{name}/{profile}/{label}/**
/{name}/{profile}/{label}/**
/{name}/{profile}/**
把a(bǔ)pplication.yml文件隨便命名個(gè)別的名:
然后pom里添加config相關(guān)依賴:
<!-- Config-Client 依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
啟動(dòng)注冊(cè)中心、配置中心裂逐、provider歹鱼,打開瀏覽器輸入http://localhost:8880/test/wangshilin
修改spring-cloud-provider模塊的配置文件bootstrap.yml中profile屬性改為dev2
重啟spring-cloud-provider:
瀏覽器輸入http://localhost:8881/test/wangshilin
后續(xù)會(huì)抽時(shí)間更新一下配置加密方面的內(nèi)容,敬請(qǐng)期待