基于Spring Cloud Zookeeper實(shí)現(xiàn)服務(wù)注冊與發(fā)現(xiàn)
服務(wù)注冊
1.添加Spring Cloud Zookeeper依賴:
<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zookeeper-discovery</artifactId><exclusions><exclusion><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.6.2</version><exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies>
2.在服務(wù)配置文件中添加zookeeper配置:
spring: cloud: zookeeper: connect-string: localhost:2181 #zookeeper地址
3.啟動(dòng)zookeeper服務(wù)器和服務(wù)(我這邊是啟動(dòng)了兩個(gè)服務(wù),分別是provider和consumer),然后在zookeeper客戶端中可以查看已經(jīng)注冊到zookeeper中的服務(wù):
服務(wù)發(fā)現(xiàn)
1.創(chuàng)建controller
消費(fèi)者controller:
package com.buhe.zk.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cloud.client.ServiceInstance;import org.springframework.cloud.client.discovery.DiscoveryClient;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;import java.util.List;@RestControllerpublic class ZkConsumerController { private static final String SERVICE_NAME = 'provider'; private static final String SERVICE_PATH = '/zk/provider'; @Autowired private RestTemplate restTemplate; @Autowired private DiscoveryClient discoveryClient; /** * 調(diào)用提供者服務(wù) * @return */ @GetMapping('/zk/consumer') public String zkConsumer(){ return '我吃了' + restTemplate.getForObject('http://' + SERVICE_NAME + SERVICE_PATH, String.class); } /** * 獲取提供者服務(wù)URL * @return */ @GetMapping('/zk/url') public String serviceUrl() { List<ServiceInstance> list = discoveryClient.getInstances(SERVICE_NAME); if (list != null && list.size() > 0 ) { return list.get(0).getUri().toString() + SERVICE_PATH; } return null; }}
要使用RestTemplate別忘了加配置:
@Bean@LoadBalancedpublic RestTemplate restTemplate(){return new RestTemplate();}
提供者controller:
package com.buhe.zk.controller;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class ZkProviderController { @GetMapping('/zk/provider') public String zkProvider(){ return '10個(gè)蘋果'; }}
2.服務(wù)調(diào)用
以上就是基于Spring Cloud Zookeeper實(shí)現(xiàn)服務(wù)注冊與發(fā)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Spring Cloud Zookeeper服務(wù)注冊與發(fā)現(xiàn)的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究2. 三個(gè)不常見的 HTML5 實(shí)用新特性簡介3. Angular獲取ngIf渲染的Dom元素示例4. php面向?qū)ο蟪绦蛟O(shè)計(jì)介紹5. ASP調(diào)用WebService轉(zhuǎn)化成JSON數(shù)據(jù),附j(luò)son.min.asp6. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁7. 使用.net core 自帶DI框架實(shí)現(xiàn)延遲加載功能8. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報(bào)錯(cuò)問題分析9. php測試程序運(yùn)行速度和頁面執(zhí)行速度的代碼10. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執(zhí)行過程解析
