SpringBoot + Spring Cloud Consul 服務(wù)注冊和發(fā)現(xiàn)詳細(xì)解析
什么是Consul
Consul 是 HashiCorp 公司推出的開源工具,用于實現(xiàn)分布式系統(tǒng)的服務(wù)發(fā)現(xiàn)與配置。與其它分布式服務(wù)注冊與發(fā)現(xiàn)的方案,Consul 的方案更“一站式”,內(nèi)置了服務(wù)注冊與發(fā)現(xiàn)框架、分布一致性協(xié)議實現(xiàn)、健康檢查、Key/Value 存儲、多數(shù)據(jù)中心方案,不再需要依賴其它工具(比如 ZooKeeper 等)。使用起來也較為簡單。Consul 使用 Go 語言編寫,因此具有天然可移植性(支持Linux、windows和Mac OS X);安裝包僅包含一個可執(zhí)行文件,方便部署,與 Docker 等輕量級容器可無縫配合。
Consul安裝
官網(wǎng)(consul.io)最新版本1.8.0 提供了MacOS,Windows, Linux, 如果你不知道怎么安裝,官方還提供了視頻。
我這里使用docker安裝,安裝過程總結(jié)起來為三句話:
docker search consul
docker pull consul
docker run --name consul -d -p 8600:8500 consul
沒問題的話,本機訪問http://localhost:8600則可以打開consul自帶的管理系統(tǒng),默認(rèn)情況下沒有服務(wù)注冊進(jìn)來
- 準(zhǔn)備工作新建父工程, 主要約定SpringCloud, SpringBoot版本號,我使用的是Hoxton.SR1, SpringBoot2.2
- 新建3個子Module,這里兩個service,一個consumer, 兩個service我用8001和8002端口來區(qū)分,主要是想在服務(wù)消費時反應(yīng)客戶端負(fù)載均衡。
項目結(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ù)注冊中心有了,那么我們來開發(fā)兩個服務(wù)提供者,這里新建了兩個Module,端口8001和8002。兩個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}
修改啟動類,添加服務(wù)
這里我直接寫了一個測試接口放在啟動類里。這里我只貼了端口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,到這一步,啟動兩個服務(wù),不出異常的情況下,可在注冊中心查看當(dāng)前的服務(wù)實例。
Consul消費者
服務(wù)注冊中心有了,服務(wù)提供者也有了,我們再來開發(fā)一個服務(wù)消費者。
新建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} # 不需要注冊到consul中 register: false
修改啟動類,調(diào)用服務(wù)
開發(fā)RestTemplate配置類,調(diào)用REST接口時使用。
@Configuration public class ApplicationContextConfig { @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } }
修改啟動類
@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ù)實例名調(diào)用REST接口 return restTemplate.getForObject("http://consul-student-service/student/version",String.class); } }
OK, 這一步完成之后,可以啟動消費者接口,刷新幾次,從返回結(jié)果上能看出來是輪訓(xùn)調(diào)用服務(wù)提供者接口實例。
到此這篇關(guān)于SpringBoot + Spring Cloud Consul 服務(wù)注冊和發(fā)現(xiàn)詳細(xì)解析的文章就介紹到這了,更多相關(guān)SpringBoot Spring Cloud Consul 服務(wù)注冊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot 集成 ShedLock 分布式鎖的示例詳解
- SpringBoot之使用Redis實現(xiàn)分布式鎖(秒殺系統(tǒng))
- Redis分布式鎖升級版RedLock及SpringBoot實現(xiàn)方法
- SpringBoot中使用redis做分布式鎖的方法
- SpringBoot整合Redis正確的實現(xiàn)分布式鎖的示例代碼
- SpringBoot使用Redis實現(xiàn)分布式鎖
- Spring boot2X Consul如何使用Feign實現(xiàn)服務(wù)調(diào)用
- Spring boot2X Consul如何通過RestTemplate實現(xiàn)服務(wù)調(diào)用
- 利用consul在spring boot中實現(xiàn)分布式鎖場景分析
相關(guān)文章
詳解Spring學(xué)習(xí)之編程式事務(wù)管理
本篇文章主要介紹了詳解Spring學(xué)習(xí)之編程式事務(wù)管理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07java線程并發(fā)控制同步工具CountDownLatch
這篇文章主要為大家介紹了java線程并發(fā)控制同步工具CountDownLatch使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08