SpringBoot + Spring Cloud Consul 服務(wù)注冊和發(fā)現(xiàn)詳細解析
什么是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 consuldocker pull consul docker run --name consul -d -p 8600:8500 consul
沒問題的話,本機訪問http://localhost:8600則可以打開consul自帶的管理系統(tǒng),默認情況下沒有服務(wù)注冊進來
項目結(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代碼相同,主要為了演示負載使用。
新建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: 8001spring: 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@RestControllerpublic class ConsulStudentService8001 { public static void main(String[] args) { SpringApplication.run(ConsulStudentService8001.class,args); } @GetMapping('/student/version') public String version(){ return '8001,202007222300'; }}
OK,到這一步,啟動兩個服務(wù),不出異常的情況下,可在注冊中心查看當前的服務(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: 8080spring: 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接口時使用。
@Configurationpublic class ApplicationContextConfig { @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); }}
修改啟動類
@SpringBootApplication@EnableDiscoveryClient@RestControllerpublic 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)詳細解析的文章就介紹到這了,更多相關(guān)SpringBoot Spring Cloud Consul 服務(wù)注冊內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP動態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗分享2. msxml3.dll 錯誤 800c0019 系統(tǒng)錯誤:-2146697191解決方法3. ASP中if語句、select 、while循環(huán)的使用方法4. xml中的空格之完全解說5. WMLScript的語法基礎(chǔ)6. 匹配模式 - XSL教程 - 47. XML入門的常見問題(四)8. ASP中解決“對象關(guān)閉時,不允許操作。”的詭異問題……9. html小技巧之td,div標簽里內(nèi)容不換行10. 解決ASP中http狀態(tài)跳轉(zhuǎn)返回錯誤頁的問題
