spring cloud config分布式配置中心的高可用問(wèn)題
在前面的文章中,我們實(shí)現(xiàn)了配置文件統(tǒng)一管理的功能,但是我們可以發(fā)現(xiàn),我們僅僅只用了一個(gè)server,如果當(dāng)這個(gè)server掛掉的話,整個(gè)配置中心就會(huì)不可用,下面,我們就來(lái)解決配置中心的高可用問(wèn)題。
下面我們通過(guò)整合Eureka來(lái)實(shí)現(xiàn)配置中心的高可用,因?yàn)樽鳛榧軜?gòu)內(nèi)的配置管理,本身其實(shí)也是可以看作架構(gòu)中的一個(gè)微服務(wù),我們可以把config server也注冊(cè)為服務(wù),這樣所有客戶端就能以服務(wù)的方式進(jìn)行訪問(wèn)。通過(guò)這種方法,只需要啟動(dòng)多個(gè)指向同一Gitlab倉(cāng)庫(kù)位置的config server端就能實(shí)現(xiàn)高可用了。
一、Config Server端加入Eureka
1、加入Eureka依賴(lài)
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
2、添加Eureka支持,將配置服務(wù)注冊(cè)到Eureka上去
@EnableEurekaClient
3、修改Server端的配置文件
加入Eureka注冊(cè)的配置,bootstrap.yml
server: port: 8889 eureka: client: service-url: defaultZone: http://localhost:8761/eureka instance: prefer-ip-address: true spring: application: name: foo cloud: config: server: git: uri: <span>https://gitlab.xxx.com/xxxxx/xxxxx.git</span> # 配置gitlab倉(cāng)庫(kù)的地址 search-paths: <span>/config-repo</span> # gitlab倉(cāng)庫(kù)地址下的相對(duì)地址,可以配置多個(gè),用,分割。 username: your name # gitlab倉(cāng)庫(kù)的賬號(hào) password: your password # gitlab倉(cāng)庫(kù)的密碼
啟動(dòng)Config Server
4、修改端口號(hào),再啟動(dòng)一個(gè)Config Server
5、查看服務(wù)是否注冊(cè)成功
二、Config 客戶端加入Eureka
1、加入Eureka的pom依賴(lài),同Config Server
2、修改Config客戶端的配置文件
bootstrap.properties經(jīng)過(guò)測(cè)試,此處客戶端只能是bootstrap.properties,否則會(huì)一直連端口8888
server.port=8890 spring.application.name=configserver spring.cloud.config.name=configserver spring.cloud.config.profile=dev spring.cloud.config.label=master # 開(kāi)啟Config服務(wù)發(fā)現(xiàn)支持 spring.cloud.config.discovery.enabled=true # 此處需要設(shè)置成Config Server在Eureka上注冊(cè)的服務(wù)名 spring.cloud.config.discovery.service-id=foo # 指定服務(wù)發(fā)現(xiàn)中心 eureka.client.service-url.defaultZone=http://localhost:8761/eureka
其中,通過(guò)eureka.client.service-url.defaultZone參數(shù)指定服務(wù)注冊(cè)中心,用于服務(wù)的注冊(cè)與發(fā)現(xiàn),再將spring.cloud.config.discovery.enabled參數(shù)設(shè)置為true,開(kāi)啟通過(guò)服務(wù)來(lái)訪問(wèn)Config Server的功能,最后利用spring.cloud.config.discovery.serviceId參數(shù)來(lái)指定Config Server注冊(cè)的服務(wù)名。這里的spring.application.name和spring.cloud.config.profile和前面博客中的意義一致。
3、在Application主類(lèi)上添加Eureka支持,同Config Server一致
三、驗(yàn)證
1、查看Eureka Server上的服務(wù)
2、查看Config Server是否正常
在瀏覽器中分別輸入:http://localhost:8888/configserver/dev/master,http://localhost:8889/configserver/dev/master,看是否有返回結(jié)果如下:
{ "name": "configserver", "profiles": [ "dev" ], "label": "master", "version": "8949024814dcb6d61f97dc49db7e9dadcfc724b1", "state": null, "propertySources": [ { "name": "https://gitlab.xxx.com/xxxxx/xxxxx/project/config-repo/configserver.properties", "source": { "name": "chhliuxyh", "hello": "i'm the king of the world!!!", "profile": "profile-default" } } ] }
3、查看客戶端是否正常
在瀏覽器中輸入http://localhost:8890/hello,看是否有如下返回
i'm the king of the world!!!
如果上面幾步正常,則說(shuō)明是ok的。
4、將8888端口對(duì)應(yīng)的Config Server停掉,然后重新輸入http://localhost:8890/hello,看是否ok!我們發(fā)現(xiàn)仍然是ok的,這樣我們就完成了配置中心的高可用了!
<pre code_snippet_id="2245130" snippet_file_name="blog_20170306_4_711400"></pre> <pre></pre> <pre></pre>
總結(jié)
以上所述是小編給大家介紹的spring cloud config分布式配置中心的高可用實(shí)現(xiàn)方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
詳解如何在Java項(xiàng)目中實(shí)現(xiàn)信號(hào)的連續(xù)接收
在Java項(xiàng)目中,信號(hào)的連續(xù)接收是一項(xiàng)重要的任務(wù),特別是在處理異步事件或者需要對(duì)外部事件做出響應(yīng)時(shí),本篇博客將介紹如何在Java項(xiàng)目中實(shí)現(xiàn)信號(hào)的連續(xù)接收,包括信號(hào)的監(jiān)聽(tīng)、處理和停止等步驟,需要的朋友可以參考下2023-11-11淺談Maven鏡像更換為阿里云中央倉(cāng)庫(kù)(精)
本篇文章主要介紹了Maven鏡像更換為阿里云中央倉(cāng)庫(kù)(精),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12解決HttpServletRequest 流數(shù)據(jù)不可重復(fù)讀的操作
這篇文章主要介紹了解決HttpServletRequest 流數(shù)據(jù)不可重復(fù)讀的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Jenkins安裝多個(gè)jdk版本并在項(xiàng)目中選擇對(duì)應(yīng)jdk版本
在使用jenkins構(gòu)建項(xiàng)目時(shí)會(huì)遇到不同的job需要配置不同版本的jdk,下面這篇文章主要給大家介紹了關(guān)于Jenkins安裝多個(gè)jdk版本并在項(xiàng)目中選擇對(duì)應(yīng)jdk版本的相關(guān)資料,需要的朋友可以參考下2024-03-03