SpringBoot + Spring Cloud Consul 服務(wù)注冊(cè)和發(fā)現(xiàn)詳細(xì)解析

什么是Consul
Consul 是 HashiCorp 公司推出的開(kāi)源工具,用于實(shí)現(xiàn)分布式系統(tǒng)的服務(wù)發(fā)現(xiàn)與配置。與其它分布式服務(wù)注冊(cè)與發(fā)現(xiàn)的方案,Consul 的方案更“一站式”,內(nèi)置了服務(wù)注冊(cè)與發(fā)現(xiàn)框架、分布一致性協(xié)議實(shí)現(xiàn)、健康檢查、Key/Value 存儲(chǔ)、多數(shù)據(jù)中心方案,不再需要依賴其它工具(比如 ZooKeeper 等)。使用起來(lái)也較為簡(jiǎn)單。Consul 使用 Go 語(yǔ)言編寫(xiě),因此具有天然可移植性(支持Linux、windows和Mac OS X);安裝包僅包含一個(gè)可執(zhí)行文件,方便部署,與 Docker 等輕量級(jí)容器可無(wú)縫配合。
Consul安裝
官網(wǎng)(consul.io)最新版本1.8.0 提供了MacOS,Windows, Linux, 如果你不知道怎么安裝,官方還提供了視頻。

我這里使用docker安裝,安裝過(guò)程總結(jié)起來(lái)為三句話:
docker search consul
docker pull consul
docker run --name consul -d -p 8600:8500 consul
沒(méi)問(wèn)題的話,本機(jī)訪問(wèn)http://localhost:8600則可以打開(kāi)consul自帶的管理系統(tǒng),默認(rèn)情況下沒(méi)有服務(wù)注冊(cè)進(jìn)來(lái)

- 準(zhǔn)備工作新建父工程, 主要約定SpringCloud, SpringBoot版本號(hào),我使用的是Hoxton.SR1, SpringBoot2.2
- 新建3個(gè)子Module,這里兩個(gè)service,一個(gè)consumer, 兩個(gè)service我用8001和8002端口來(lái)區(qū)分,主要是想在服務(wù)消費(fèi)時(shí)反應(yīng)客戶端負(fù)載均衡。
項(xiàng)目結(jié)構(gòu)如下圖

父工程pom.xml
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
服務(wù)提供者
服務(wù)注冊(cè)中心有了,那么我們來(lái)開(kāi)發(fā)兩個(gè)服務(wù)提供者,這里新建了兩個(gè)Module,端口8001和8002。兩個(gè)Module代碼相同,主要為了演示負(fù)載使用。
新建Module,添加spring-cloud-starter-consul-disconvery依賴

pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies>
添加配置(application.yml)
server:
port: 8001
spring:
application:
name: consul-student-service
cloud:
consul:
port: 8600
host: 127.0.0.1
discovery:
service-name: ${spring.application.name}
修改啟動(dòng)類,添加服務(wù)
這里我直接寫(xiě)了一個(gè)測(cè)試接口放在啟動(dòng)類里。這里我只貼了端口8001的代碼,8002代碼結(jié)構(gòu)相同,只是端口不同。
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ConsulStudentService8001 {
public static void main(String[] args) {
SpringApplication.run(ConsulStudentService8001.class,args);
}
@GetMapping("/student/version")
public String version(){
return "8001,202007222300";
}
}
OK,到這一步,啟動(dòng)兩個(gè)服務(wù),不出異常的情況下,可在注冊(cè)中心查看當(dāng)前的服務(wù)實(shí)例。



Consul消費(fèi)者
服務(wù)注冊(cè)中心有了,服務(wù)提供者也有了,我們?cè)賮?lái)開(kāi)發(fā)一個(gè)服務(wù)消費(fèi)者。
新建Module,同樣添加spring-cloud-starter-consul-disconvery依賴
pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> </dependencies>
添加配置(application.yml)
server:
port: 8080
spring:
application:
name: consul-student-consumer
cloud:
consul:
host: 127.0.0.1
port: 8600
discovery:
service-name: ${spring.application.name}
# 不需要注冊(cè)到consul中
register: false
修改啟動(dòng)類,調(diào)用服務(wù)
開(kāi)發(fā)RestTemplate配置類,調(diào)用REST接口時(shí)使用。
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
修改啟動(dòng)類
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ConsulStudentConsumer {
public static void main(String[] args) {
SpringApplication.run(ConsulStudentConsumer.class,args);
}
@Autowired
RestTemplate restTemplate;
@GetMapping("/consul/student/version")
public String version(){
//這里使用服務(wù)實(shí)例名調(diào)用REST接口
return restTemplate.getForObject("http://consul-student-service/student/version",String.class);
}
}
OK, 這一步完成之后,可以啟動(dòng)消費(fèi)者接口,刷新幾次,從返回結(jié)果上能看出來(lái)是輪訓(xùn)調(diào)用服務(wù)提供者接口實(shí)例。
到此這篇關(guān)于SpringBoot + Spring Cloud Consul 服務(wù)注冊(cè)和發(fā)現(xiàn)詳細(xì)解析的文章就介紹到這了,更多相關(guān)SpringBoot Spring Cloud Consul 服務(wù)注冊(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot 集成 ShedLock 分布式鎖的示例詳解
- SpringBoot之使用Redis實(shí)現(xiàn)分布式鎖(秒殺系統(tǒng))
- Redis分布式鎖升級(jí)版RedLock及SpringBoot實(shí)現(xiàn)方法
- SpringBoot中使用redis做分布式鎖的方法
- SpringBoot整合Redis正確的實(shí)現(xiàn)分布式鎖的示例代碼
- SpringBoot使用Redis實(shí)現(xiàn)分布式鎖
- Spring boot2X Consul如何使用Feign實(shí)現(xiàn)服務(wù)調(diào)用
- Spring boot2X Consul如何通過(guò)RestTemplate實(shí)現(xiàn)服務(wù)調(diào)用
- 利用consul在spring boot中實(shí)現(xiàn)分布式鎖場(chǎng)景分析
相關(guān)文章
Java圖片讀取ImageIO.read()報(bào)錯(cuò)問(wèn)題及解決
在使用imageio庫(kù)讀取圖片時(shí),如果路徑中包含中文,可能會(huì)導(dǎo)致讀取失敗,解決方法是將路徑中的中文字符進(jìn)行轉(zhuǎn)義處理,可以使用ImageUtil.java工具類進(jìn)行路徑轉(zhuǎn)義,從而避免錯(cuò)誤,這是一個(gè)常見(jiàn)問(wèn)題,希望本文的解決方案能幫助到遇到相同問(wèn)題的開(kāi)發(fā)者2024-10-10
詳解Spring學(xué)習(xí)之編程式事務(wù)管理
本篇文章主要介紹了詳解Spring學(xué)習(xí)之編程式事務(wù)管理,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07
Spring關(guān)于@Scheduled限制的問(wèn)題
這篇文章主要介紹了Spring關(guān)于@Scheduled限制的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
一篇文章教你用Java使用JVM工具檢測(cè)問(wèn)題
這篇文章主要介紹了深入理解Java使用JVM工具檢測(cè)問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-09-09
java線程并發(fā)控制同步工具CountDownLatch
這篇文章主要為大家介紹了java線程并發(fā)控制同步工具CountDownLatch使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
使用Java對(duì)Hbase操作總結(jié)及示例代碼
這篇文章主要介紹了使用Java對(duì)Hbase進(jìn)行操作總結(jié),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07

