av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

基于Spring Cloud Zookeeper實(shí)現(xiàn)服務(wù)注冊(cè)與發(fā)現(xiàn)

瀏覽:3日期:2023-08-01 10:06:28

服務(wù)注冊(cè)

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)注冊(cè)到zookeeper中的服務(wù):

基于Spring Cloud Zookeeper實(shí)現(xiàn)服務(wù)注冊(cè)與發(fā)現(xiàn)

服務(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ù)注冊(cè)與發(fā)現(xiàn)

以上就是基于Spring Cloud Zookeeper實(shí)現(xiàn)服務(wù)注冊(cè)與發(fā)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Spring Cloud Zookeeper服務(wù)注冊(cè)與發(fā)現(xiàn)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 黄瓜av| 91网视频 | 色视频欧美 | 中文字幕日韩欧美一区二区三区 | 成人欧美一区二区三区黑人孕妇 | 亚洲成人综合在线 | 国产视频二区 | 国产乱性 | 国产操操操| 午夜三区 | 日韩欧美不卡 | 成年人在线观看 | 欧美亚洲另类丝袜综合网动图 | 一级毛片在线播放 | 亚洲精品视频久久 | 国产一级淫片免费视频 | 成人精品毛片 | 国产91丝袜在线18 | 2018天天干天天操 | 国产日韩欧美一区 | 国产精品国产三级国产aⅴ中文 | 日韩欧美亚洲 | 精品中文在线 | 亚洲精品专区 | 国产一级视屏 | 欧美福利一区 | 日韩三极| 国产视频精品在线观看 | 久久久www成人免费精品 | 在线国产一区 | 女女百合av大片一区二区三区九县 | 精品一级毛片 | 亚洲精品日韩视频 | 国产精品日韩 | 在线观看av网站 | 国产欧美精品一区二区色综合朱莉 | 久久久激情 | 91大神在线资源观看无广告 | 久久久久久久一区 | 国产精品99久久久久久www | 色频|