SpringCloud高可用配置中心Config詳解
前言
SpringCloud 是微服務(wù)中的翹楚,最佳的落地方案。
Spring Cloud Config 是一個(gè)解決分布式系統(tǒng)的配置管理方案,它包含了 server 和 client 兩個(gè)部分。
server 用來獲取遠(yuǎn)程的配置信息(默認(rèn)為 Git 倉庫),并且以接口的形式提供出去;
client 根據(jù) server 提供的接口讀取配置文件,以便于初始化自己的應(yīng)用。
如果配置中心出現(xiàn)了問題,將導(dǎo)致災(zāi)難性的后果,因此在生產(chǎn)環(huán)境下配置中心都會(huì)做集群,來保證高可用。
此處配置高可用實(shí)際就是把多個(gè)配置中心(指定同一個(gè) Git 遠(yuǎn)程倉庫)注冊(cè)到注冊(cè)中心。
源碼
GitHub地址:https://github.com/intomylife/SpringCloud
環(huán)境
- JDK 1.8.0 +
- Maven 3.0 +
- SpringBoot 2.0.3
- SpringCloud Finchley.RELEASE
開發(fā)工具
IntelliJ IDEA
正文
commons 工程
commons 工程 - POM 文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 三坐標(biāo) -->
<groupId>com.zwc</groupId>
<artifactId>springcloud-config-eureka-commons</artifactId>
<version>1.0</version>
<!-- 工程名稱和描述 -->
<name>springcloud-config-eureka-commons</name>
<description>公用工程</description>
<!-- 打包方式 -->
<packaging>jar</packaging>
<!-- 在 properties下聲明相應(yīng)的版本信息,然后在dependency下引用的時(shí)候用 ${} 就可以引入該版本jar包了 -->
<properties>
<!-- 編碼 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- jdk -->
<java.version>1.8</java.version>
<!-- SpringBoot -->
<platform-bom.version>Cairo-SR3</platform-bom.version>
<!-- SpringCloud -->
<spring-cloud-dependencies.version>Finchley.RELEASE</spring-cloud-dependencies.version>
</properties>
<!-- 加入依賴 -->
<dependencies>
</dependencies>
<!-- 依賴 jar 包版本管理的管理器 -->
<!-- 如果 dependencies 里的 dependency 自己沒有聲明 version 元素,那么 maven 就此處來找版本聲明。 -->
<!-- 如果有,就會(huì)繼承它;如果沒有就會(huì)報(bào)錯(cuò),告訴你沒有版本信息 -->
<!-- 優(yōu)先級(jí):如果 dependencies 里的 dependency 已經(jīng)聲明了版本信息,就不會(huì)生效此處的版本信息了 -->
<dependencyManagement>
<dependencies>
<!-- SpringBoot -->
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>${platform-bom.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- SpringCloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 插件依賴 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置一些共用依賴
commons 工程 - 項(xiàng)目結(jié)構(gòu)

配置文件
創(chuàng)建一個(gè) Git 庫,里面存放配置文件,文件夾名稱為:config-repo;這里模擬不同的環(huán)境,所以分別構(gòu)建了
dev(開發(fā))、uat(測試)以及online(線上)三種不同的配置文件;此文件夾存在此項(xiàng)目的根目錄中
- springcloud-config-eureka ? - config-repo ? ? -?system-dev.properties ? ? -?system-online.properties ? ? -?system-uat.properties + springcloud-config-eureka-commons + springcloud-config-eureka-service
service 工程
① 此工程下有四個(gè)模塊:一個(gè)注冊(cè)中心,二個(gè) server 以及一個(gè) client
② 二個(gè) server 除端口不一致以外,其他代碼基本一致
registry-service(注冊(cè)中心)
registry-service - POM 文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 繼承父 -->
<parent>
<groupId>com.zwc</groupId>
<artifactId>springcloud-config-eureka-service</artifactId>
<version>1.0</version>
</parent>
<!-- 三坐標(biāo) -->
<groupId>com.zwc</groupId>
<artifactId>springcloud-config-eureka-registry-service</artifactId>
<version>1.0</version>
<!-- 工程名稱描述 -->
<name>springcloud-config-eureka-registry-service</name>
<description>注冊(cè)中心</description>
<!-- 打包方式 -->
<packaging>jar</packaging>
<!-- 在 properties下聲明相應(yīng)的版本信息,然后在dependency下引用的時(shí)候用 ${} 就可以引入該版本jar包了 -->
<properties>
</properties>
<!-- 加入依賴 -->
<dependencies>
<!-- commons工程 依賴 -->
<dependency>
<groupId>com.zwc</groupId>
<artifactId>springcloud-config-eureka-commons</artifactId>
<version>1.0</version>
</dependency>
<!-- 服務(wù)注冊(cè)中心 -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependencies>
<!-- 插件依賴 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>主要加入spring-cloud-starter-netflix-eureka-server依賴
registry-service - application.yml配置文件
# 端口
server:
port: 8761
# 應(yīng)用名稱
spring:
application:
name: eureka-server
eureka:
instance:
# 使用 ip 代替實(shí)例名
prefer-ip-address: true
# 實(shí)例的主機(jī)名
hostname: ${spring.cloud.client.ip-address}
# 實(shí)例的 ID 規(guī)則
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
client:
# 是否向注冊(cè)中心注冊(cè)自己
registerWithEureka: false
# 是否向注冊(cè)中心獲取注冊(cè)信息
fetchRegistry: false
serviceUrl:
# 注冊(cè)中心地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
這里使用了默認(rèn)的 8761 端口,當(dāng)然也可以更改,不過在發(fā)現(xiàn)調(diào)用服務(wù)端的注冊(cè)中心地址端口要與它一致
registry-service - 啟動(dòng)類
package com.zwc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class SpringcloudConfigEurekaRegistryServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudConfigEurekaRegistryServiceApplication.class, args);
}
}在啟動(dòng)類中添加 @EnableEurekaServer 注解表示此工程是注冊(cè)中心
registry-server - 啟動(dòng)項(xiàng)目
1. 項(xiàng)目啟動(dòng)成功后訪問http://localhost:8761/即可看到eureka-server 主頁面

server(獲取遠(yuǎn)程的配置信息)
server- POM 文件
注:一共有兩個(gè) server,但是除端口以外的代碼基本上一致,所有這里就列舉其中一個(gè)
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 繼承父 -->
<parent>
<groupId>com.zwc</groupId>
<artifactId>springcloud-config-eureka-service</artifactId>
<version>1.0</version>
</parent>
<!-- 三坐標(biāo) -->
<groupId>com.zwc</groupId>
<artifactId>springcloud-config-eureka-serverfirst-service</artifactId>
<version>1.0</version>
<!-- 工程名稱描述 -->
<name>springcloud-config-eureka-serverfirst-service</name>
<description>config server</description>
<!-- 打包方式 -->
<packaging>jar</packaging>
<!-- 在 properties下聲明相應(yīng)的版本信息,然后在dependency下引用的時(shí)候用 ${} 就可以引入該版本jar包了 -->
<properties>
</properties>
<!-- 加入依賴 -->
<dependencies>
<!-- commons工程 依賴 -->
<dependency>
<groupId>com.zwc</groupId>
<artifactId>springcloud-config-eureka-commons</artifactId>
<version>1.0</version>
</dependency>
<!-- config server 依賴 -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<!-- 提供者消費(fèi)者 -->
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependencies>
<!-- 插件依賴 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>- 加入spring-cloud-config-server依賴:config server
- 加入spring-cloud-starter-netflix-eureka-client依賴:提供和注冊(cè)服務(wù)
server- application.yml配置文件
# 端口
server:
port: 8000
spring:
application:
# 應(yīng)用名稱
name: config-eureka-server
cloud:
config:
server:
git:
# 倉庫地址
uri: https://github.com/intomylife/SpringCloud
# 對(duì)應(yīng) {label} 部分,即 Git 的分支
label: master
# 倉庫文件夾名稱,多個(gè)以逗號(hào)分隔
search-paths: springcloud-config-eureka/config-repo
# git 倉庫用戶名(公開庫可以不用填寫)
username:
# git 倉庫密碼(公開庫可以不用填寫)
password:
eureka:
instance:
# 使用 ip 代替實(shí)例名
prefer-ip-address: true
# 實(shí)例的主機(jī)名
hostname: ${spring.cloud.client.ip-address}
# 實(shí)例的 ID 規(guī)則
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
client:
serviceUrl:
# 注冊(cè)中心地址
defaultZone: http://${eureka.instance.hostname}:8761/eureka/- 注意這里 uri 只能寫到倉庫名
- 如果配置文件在倉庫中多層目錄下,那么search-paths 就從根目錄開始寫起
- 注意此處配置注冊(cè)中心地址的端口為 8761 也就是上面注冊(cè)中心工程配置的端口
package com.zwc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class SpringcloudConfigEurekaServerfirstServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudConfigEurekaServerfirstServiceApplication.class, args);
}
}- 添加@EnableConfigServer 注解表示開啟配置中心
- 添加 @EnableEurekaClient 注解表示此工程可以向注冊(cè)中心提供服務(wù)
server- 啟動(dòng)項(xiàng)目
1.項(xiàng)目啟動(dòng)成功后訪問:http://localhost:8761/(注冊(cè)中心)可以看到服務(wù)已經(jīng)被注冊(cè)進(jìn)來了

2. 訪問地址:http://localhost:8000/config-repo/dev(獲取完整配置信息)
3.輸出內(nèi)容:
{
"name":"config-repo",
"profiles":[
"dev"
],
"label":null,
"version":"0d6480b5ba6f7f2be87b3130de8163dcb0af9f5f",
"state":null,
"propertySources":[]
}
4. 訪問地址:http://localhost:8000/system/dev(獲取完整配置信息)
5. 輸出內(nèi)容:
{
"name":"system",
"profiles":[
"dev"
],
"label":null,
"version":"0d6480b5ba6f7f2be87b3130de8163dcb0af9f5f",
"state":null,
"propertySources":[
{
"name":"https://github.com/intomylife/SpringCloud/springcloud-config-eureka/config-repo/system-dev.properties",
"source":{
"hello":"I'm dev."
}
}
]
}
6. 訪問地址:http://localhost:8000/system-dev.properties(獲取指定配置文件內(nèi)容)
7. 輸出內(nèi)容:‘hello: I’m dev.’
8. 啟動(dòng)另一個(gè) server(springcloud-config-eureka-serversecond-service)
9.項(xiàng)目啟動(dòng)成功后訪問:http://localhost:8761/(注冊(cè)中心)可以看到服務(wù)已經(jīng)被注冊(cè)進(jìn)來了

10.訪問地址:http://localhost:8001/config-repo/uat(獲取完整配置信息)
11. 輸出內(nèi)容:
{
"name":"config-repo",
"profiles":[
"uat"
],
"label":null,
"version":"0d6480b5ba6f7f2be87b3130de8163dcb0af9f5f",
"state":null,
"propertySources":[]
}
12.訪問地址:http://localhost:8001/system/uat(獲取完整配置信息)
13.輸出內(nèi)容:
{
"name":"system",
"profiles":[
"uat"
],
"label":null,
"version":"0d6480b5ba6f7f2be87b3130de8163dcb0af9f5f",
"state":null,
"propertySources":[
{
"name":"https://github.com/intomylife/SpringCloud/springcloud-config-eureka/config-repo/system-uat.properties",
"source":{
"hello":"I'm uat."
}
}
]
}
14.訪問地址:http://localhost:8001/system-uat.properties(獲取指定配置文件內(nèi)容)
15. 輸出內(nèi)容:‘hello: I’m uat.’
16. 證明兩個(gè) server 都已經(jīng)成功從遠(yuǎn)程倉庫中獲取到了配置信息
注:接口訪問有如下規(guī)則:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
client(讀取注冊(cè)中心的server 中的配置信息)
client- POM 文件
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- 繼承父 -->
<parent>
<groupId>com.zwc</groupId>
<artifactId>springcloud-config-eureka-service</artifactId>
<version>1.0</version>
</parent>
<!-- 三坐標(biāo) -->
<groupId>com.zwc</groupId>
<artifactId>springcloud-config-eureka-client-service</artifactId>
<version>1.0</version>
<!-- 工程名稱描述 -->
<name>springcloud-config-eureka-client-service</name>
<description>config client</description>
<!-- 打包方式 -->
<packaging>jar</packaging>
<!-- 在 properties下聲明相應(yīng)的版本信息,然后在dependency下引用的時(shí)候用 ${} 就可以引入該版本jar包了 -->
<properties>
</properties>
<!-- 加入依賴 -->
<dependencies>
<!-- commons工程 依賴 -->
<dependency>
<groupId>com.zwc</groupId>
<artifactId>springcloud-config-eureka-commons</artifactId>
<version>1.0</version>
</dependency>
<!-- config 依賴 -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<!-- 提供者消費(fèi)者 -->
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependencies>
<!-- 插件依賴 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>- 加入spring-cloud-starter-config依賴:config client
- 加入spring-cloud-starter-netflix-eureka-client依賴:提供和注冊(cè)服務(wù)
client- application.yml配置文件
# 端口
server:
port: 8002
spring:
application:
# 應(yīng)用名稱
name: config-eureka-clientclient- bootstrap.yml 配置文件(注意)
spring:
cloud:
config:
# 對(duì)應(yīng) {label} 部分,即 Git 的分支
label: master
# 對(duì)應(yīng) {application} 部分
name: system
# 對(duì)應(yīng) {profile} 部分
profile: dev
discovery:
# 開啟 Config 服務(wù)發(fā)現(xiàn)與注冊(cè)
enabled: true
# 指定 server
service-id: config-eureka-server
eureka:
instance:
# 使用 ip 代替實(shí)例名
prefer-ip-address: true
# 實(shí)例的主機(jī)名
hostname: ${spring.cloud.client.ip-address}
# 實(shí)例的 ID 規(guī)則
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}
client:
serviceUrl:
# 注冊(cè)中心地址
defaultZone: http://${eureka.instance.hostname}:8761/eureka/- bootstrap.yml 配置文件加載先于application.yml 配置文件
- 與 Spring Cloud Config 相關(guān)的屬性必須配置在bootstrap.yml 中
- 這里就不是直接指定 server 地址了,而是 server 的應(yīng)用名(spring.application.name)
- 從注冊(cè)中心的 server 中讀取 dev(開發(fā))環(huán)境的配置文件信息
- 注意此處配置注冊(cè)中心地址的端口為 8761 也就是上面注冊(cè)中心工程配置的端口
client- controller 前端控制器
package com.zwc.client.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @ClassName HelloController
* @Desc TODO 讀取 git 配置文件
* @Date 2019/6/2 16:54
* @Version 1.0
*/
@RestController
public class HelloController {
@Value("${hello}")
private String hello;
/*
* @ClassName HelloController
* @Desc TODO 讀取 git 配置文件
* @Date 2019/6/2 16:56
* @Version 1.0
*/
@RequestMapping("/hello")
public String hello() {
return this.hello;
}
}直接使用@Value 注解就可以從注冊(cè)中心的server 中讀取到對(duì)應(yīng)的配置信息
client- 啟動(dòng)類
package com.zwc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class SpringcloudConfigEurekaClientServiceApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudConfigEurekaClientServiceApplication.class, args);
}
}添加 @EnableEurekaClient 注解表示此工程可以向注冊(cè)中心提供服務(wù)
client- 啟動(dòng)項(xiàng)目
1.項(xiàng)目啟動(dòng)成功后訪問:http://localhost:8761/(注冊(cè)中心)可以看到服務(wù)已經(jīng)被注冊(cè)進(jìn)來了

2.訪問地址:http://localhost:8002/hello
3. 輸出內(nèi)容:‘I’m dev.’
4. 證明 client 已經(jīng)成功從注冊(cè)中心的 server 中讀取到了配置信息
5. 此時(shí)當(dāng) client 已經(jīng)啟動(dòng)完畢后,配置文件就已經(jīng)全部讀取到了,所以即使停止其中一個(gè) server 或者停止
全部 server,client 依然可以讀取到配置文件。此處高可用應(yīng)該是指在 client 啟動(dòng)時(shí)能保證有 server 可用
service 工程 -項(xiàng)目結(jié)構(gòu)

把多工程項(xiàng)目使用 IntelliJ IDEA 打開
- 把項(xiàng)目從 GitHub 中下載到你的本地
- 打開IntelliJ IDEA
- 點(diǎn)擊 File -> Open
- 打開你下載到本地的項(xiàng)目目錄
- springcloud-config-eureka ->springcloud-config-eureka-service(選擇打開此工程)
- 打開 service 工程后
- 再次點(diǎn)擊 File -> Project Structrue
- 選擇 Modules,點(diǎn)擊 ‘+’ 符號(hào)
- 點(diǎn)擊 ImportModule
- 還是打開你下載到本地的項(xiàng)目目錄
- springcloud-config-eureka ->springcloud-config-eureka-commons -> pom.xml
- 點(diǎn)擊 OK
- 點(diǎn)擊 Next,F(xiàn)inish
- 點(diǎn)擊 Apply,OK
希望能夠幫助到你
到此這篇關(guān)于SpringCloud之配置中心Config(高可用)的文章就介紹到這了,更多相關(guān)SpringCloud配置中心Config內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)將word轉(zhuǎn)換為html的方法示例【doc與docx格式】
這篇文章主要介紹了Java實(shí)現(xiàn)將word轉(zhuǎn)換為html的方法,結(jié)合實(shí)例形式分析了java針對(duì)doc與docx格式文件的相關(guān)轉(zhuǎn)換操作技巧,需要的朋友可以參考下2019-03-03
Java使用Condition控制線程通信的方法實(shí)例詳解
這篇文章主要介紹了Java使用Condition控制線程通信的方法,結(jié)合實(shí)例形式分析了使用Condition類同步檢測控制線程通信的相關(guān)操作技巧,需要的朋友可以參考下2019-09-09
使用BigDecimal進(jìn)行精確運(yùn)算(實(shí)現(xiàn)加減乘除運(yùn)算)
這篇文章主要介紹了如何使用BigDecimal進(jìn)行精確運(yùn)算,最后提供了一個(gè)工具類,該工具類提供加,減,乘,除運(yùn)算2013-11-11
Spring Cloud Stream微服務(wù)消息框架原理及實(shí)例解析
這篇文章主要介紹了Spring Cloud Stream微服務(wù)消息框架原理及實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Java中的while無限循環(huán)結(jié)構(gòu)及實(shí)例
這篇文章主要介紹了Java中的while無限循環(huán)結(jié)構(gòu)及實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
關(guān)于mybatis plus 中的查詢優(yōu)化問題
這篇文章主要介紹了關(guān)于mybatis plus 中的查詢優(yōu)化問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01

