国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

基于Spring Cloud Zookeeper實現服務注冊與發現

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

服務注冊

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.在服務配置文件中添加zookeeper配置:

spring: cloud: zookeeper: connect-string: localhost:2181 #zookeeper地址

3.啟動zookeeper服務器和服務(我這邊是啟動了兩個服務,分別是provider和consumer),然后在zookeeper客戶端中可以查看已經注冊到zookeeper中的服務:

基于Spring Cloud Zookeeper實現服務注冊與發現

服務發現

1.創建controller

消費者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; /** * 調用提供者服務 * @return */ @GetMapping('/zk/consumer') public String zkConsumer(){ return '我吃了' + restTemplate.getForObject('http://' + SERVICE_NAME + SERVICE_PATH, String.class); } /** * 獲取提供者服務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個蘋果'; }}

2.服務調用

基于Spring Cloud Zookeeper實現服務注冊與發現

以上就是基于Spring Cloud Zookeeper實現服務注冊與發現的詳細內容,更多關于Spring Cloud Zookeeper服務注冊與發現的資料請關注好吧啦網其它相關文章!

標簽: Spring
相關文章:
主站蜘蛛池模板: 午夜嘿咻| 一级片a级片 | 国产黄色免费网站 | 一个人看的日本免费视频 | 另类亚洲孕妇分娩网址 | 成年人网站免费观看 | 红色记忆 | 波多野在线视频 | 久久亚洲高清观看 | 亚洲成人一区二区 | 美国一级毛片在线 | 久草com| 成人在线视频免费观看 | 国产精品久久在线 | 九九精品视频在线播放8 | 国产精品视频99 | 成人午夜亚洲影视在线观看 | 成人18免费网| 日本一区二区三区四区不卡 | 午夜国产| 久久国产精品久久精品国产 | 午夜在线精品不卡国产 | 日韩一区二区三区视频 | 亚洲成人在线免费视频 | 狠狠色狠狠色综合 | 国产一级生活片 | 国产成人亚洲精品影院 | 久草在线手机 | 国产成人亚洲综合欧美一部 | 国产精品久久网 | 日本三级日产三级国产三级 | 日本免费三级网站 | 一区二区三区中文字幕 | 生活片毛片 | 99aiav国产精品视频 | 国产91区 | 久草在线免费看 | 99久久亚洲综合精品网站 | 在线免费亚洲 | 日韩高清成人毛片不卡 | 久久综合中文字幕一区二区 |