SpringCloud高可用配置中心Config詳解
前言
SpringCloud 是微服務(wù)中的翹楚,最佳的落地方案。
Spring Cloud Config 是一個解決分布式系統(tǒng)的配置管理方案,它包含了 server 和 client 兩個部分。
server 用來獲取遠(yuǎn)程的配置信息(默認(rèn)為 Git 倉庫),并且以接口的形式提供出去;
client 根據(jù) server 提供的接口讀取配置文件,以便于初始化自己的應(yīng)用。
如果配置中心出現(xiàn)了問題,將導(dǎo)致災(zāi)難性的后果,因此在生產(chǎn)環(huán)境下配置中心都會做集群,來保證高可用。
此處配置高可用實際就是把多個配置中心(指定同一個 Git 遠(yuǎn)程倉庫)注冊到注冊中心。
源碼
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下引用的時候用 ${} 就可以引入該版本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 就此處來找版本聲明。 --> <!-- 如果有,就會繼承它;如果沒有就會報錯,告訴你沒有版本信息 --> <!-- 優(yōu)先級:如果 dependencies 里的 dependency 已經(jīng)聲明了版本信息,就不會生效此處的版本信息了 --> <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 工程 - 項目結(jié)構(gòu)
配置文件
創(chuàng)建一個 Git 庫,里面存放配置文件,文件夾名稱為:config-repo;這里模擬不同的環(huán)境,所以分別構(gòu)建了
dev(開發(fā))、uat(測試)以及online(線上)三種不同的配置文件;此文件夾存在此項目的根目錄中
- springcloud-config-eureka ? - config-repo ? ? -?system-dev.properties ? ? -?system-online.properties ? ? -?system-uat.properties + springcloud-config-eureka-commons + springcloud-config-eureka-service
service 工程
① 此工程下有四個模塊:一個注冊中心,二個 server 以及一個 client
② 二個 server 除端口不一致以外,其他代碼基本一致
registry-service(注冊中心)
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>注冊中心</description> <!-- 打包方式 --> <packaging>jar</packaging> <!-- 在 properties下聲明相應(yīng)的版本信息,然后在dependency下引用的時候用 ${} 就可以引入該版本jar包了 --> <properties> </properties> <!-- 加入依賴 --> <dependencies> <!-- commons工程 依賴 --> <dependency> <groupId>com.zwc</groupId> <artifactId>springcloud-config-eureka-commons</artifactId> <version>1.0</version> </dependency> <!-- 服務(wù)注冊中心 --> <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 代替實例名 prefer-ip-address: true # 實例的主機(jī)名 hostname: ${spring.cloud.client.ip-address} # 實例的 ID 規(guī)則 instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port} client: # 是否向注冊中心注冊自己 registerWithEureka: false # 是否向注冊中心獲取注冊信息 fetchRegistry: false serviceUrl: # 注冊中心地址 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
這里使用了默認(rèn)的 8761 端口,當(dāng)然也可以更改,不過在發(fā)現(xiàn)調(diào)用服務(wù)端的注冊中心地址端口要與它一致
registry-service - 啟動類
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); } }
在啟動類中添加 @EnableEurekaServer 注解表示此工程是注冊中心
registry-server - 啟動項目
1. 項目啟動成功后訪問http://localhost:8761/即可看到eureka-server 主頁面
server(獲取遠(yuǎn)程的配置信息)
server- POM 文件
注:一共有兩個 server,但是除端口以外的代碼基本上一致,所有這里就列舉其中一個
<?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下引用的時候用 ${} 就可以引入該版本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> <!-- 提供者消費者 --> <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依賴:提供和注冊服務(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 # 對應(yīng) {label} 部分,即 Git 的分支 label: master # 倉庫文件夾名稱,多個以逗號分隔 search-paths: springcloud-config-eureka/config-repo # git 倉庫用戶名(公開庫可以不用填寫) username: # git 倉庫密碼(公開庫可以不用填寫) password: eureka: instance: # 使用 ip 代替實例名 prefer-ip-address: true # 實例的主機(jī)名 hostname: ${spring.cloud.client.ip-address} # 實例的 ID 規(guī)則 instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port} client: serviceUrl: # 注冊中心地址 defaultZone: http://${eureka.instance.hostname}:8761/eureka/
- 注意這里 uri 只能寫到倉庫名
- 如果配置文件在倉庫中多層目錄下,那么search-paths 就從根目錄開始寫起
- 注意此處配置注冊中心地址的端口為 8761 也就是上面注冊中心工程配置的端口
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 注解表示此工程可以向注冊中心提供服務(wù)
server- 啟動項目
1.項目啟動成功后訪問:http://localhost:8761/(注冊中心)可以看到服務(wù)已經(jīng)被注冊進(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. 啟動另一個 server(springcloud-config-eureka-serversecond-service)
9.項目啟動成功后訪問:http://localhost:8761/(注冊中心)可以看到服務(wù)已經(jīng)被注冊進(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. 證明兩個 server 都已經(jīng)成功從遠(yuǎn)程倉庫中獲取到了配置信息
注:接口訪問有如下規(guī)則:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
client(讀取注冊中心的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下引用的時候用 ${} 就可以引入該版本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> <!-- 提供者消費者 --> <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依賴:提供和注冊服務(wù)
client- application.yml配置文件
# 端口 server: port: 8002 spring: application: # 應(yīng)用名稱 name: config-eureka-client
client- bootstrap.yml 配置文件(注意)
spring: cloud: config: # 對應(yīng) {label} 部分,即 Git 的分支 label: master # 對應(yīng) {application} 部分 name: system # 對應(yīng) {profile} 部分 profile: dev discovery: # 開啟 Config 服務(wù)發(fā)現(xiàn)與注冊 enabled: true # 指定 server service-id: config-eureka-server eureka: instance: # 使用 ip 代替實例名 prefer-ip-address: true # 實例的主機(jī)名 hostname: ${spring.cloud.client.ip-address} # 實例的 ID 規(guī)則 instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port} client: serviceUrl: # 注冊中心地址 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)
- 從注冊中心的 server 中讀取 dev(開發(fā))環(huán)境的配置文件信息
- 注意此處配置注冊中心地址的端口為 8761 也就是上面注冊中心工程配置的端口
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 注解就可以從注冊中心的server 中讀取到對應(yīng)的配置信息
client- 啟動類
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 注解表示此工程可以向注冊中心提供服務(wù)
client- 啟動項目
1.項目啟動成功后訪問:http://localhost:8761/(注冊中心)可以看到服務(wù)已經(jīng)被注冊進(jìn)來了
2.訪問地址:http://localhost:8002/hello
3. 輸出內(nèi)容:‘I’m dev.’
4. 證明 client 已經(jīng)成功從注冊中心的 server 中讀取到了配置信息
5. 此時當(dāng) client 已經(jīng)啟動完畢后,配置文件就已經(jīng)全部讀取到了,所以即使停止其中一個 server 或者停止
全部 server,client 依然可以讀取到配置文件。此處高可用應(yīng)該是指在 client 啟動時能保證有 server 可用
service 工程 -項目結(jié)構(gòu)
把多工程項目使用 IntelliJ IDEA 打開
- 把項目從 GitHub 中下載到你的本地
- 打開IntelliJ IDEA
- 點擊 File -> Open
- 打開你下載到本地的項目目錄
- springcloud-config-eureka ->springcloud-config-eureka-service(選擇打開此工程)
- 打開 service 工程后
- 再次點擊 File -> Project Structrue
- 選擇 Modules,點擊 ‘+’ 符號
- 點擊 ImportModule
- 還是打開你下載到本地的項目目錄
- springcloud-config-eureka ->springcloud-config-eureka-commons -> pom.xml
- 點擊 OK
- 點擊 Next,F(xiàn)inish
- 點擊 Apply,OK
希望能夠幫助到你
到此這篇關(guān)于SpringCloud之配置中心Config(高可用)的文章就介紹到這了,更多相關(guān)SpringCloud配置中心Config內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot整合RestTemplate用法的實現(xiàn)
本篇主要介紹了RestTemplate中的GET,POST,PUT,DELETE、文件上傳和文件下載6大常用的功能,具有一定的參考價值,感興趣的可以了解一下2023-08-08spring?boot整合mongo查詢converter異常排查記錄
這篇文章主要為大家介紹了spring?boot整合mongo查詢時拋出converter異常的排查解決記錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-03-03springboot?@PostConstruct無效的解決
這篇文章主要介紹了springboot?@PostConstruct無效的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11SpringMVC響應(yīng)視圖和結(jié)果視圖詳解
這篇文章主要介紹了SpringMVC響應(yīng)視圖和結(jié)果視圖,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09Spring整合Quartz實現(xiàn)動態(tài)定時器的示例代碼
本篇文章主要介紹了Spring整合Quartz實現(xiàn)動態(tài)定時器的示例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-01-01