基于Spring Cloud Zookeeper實現(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.啟動zookeeper服務(wù)器和服務(wù)(我這邊是啟動了兩個服務(wù),分別是provider和consumer),然后在zookeeper客戶端中可以查看已經(jīng)注冊到zookeeper中的服務(wù):

服務(wù)發(fā)現(xiàn)
1.創(chuàng)建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;
@RestController
public 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
@LoadBalanced
public 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;
@RestController
public class ZkProviderController {
@GetMapping("/zk/provider")
public String zkProvider(){
return "10個蘋果";
}
}
2.服務(wù)調(diào)用

以上就是基于Spring Cloud Zookeeper實現(xiàn)服務(wù)注冊與發(fā)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Spring Cloud Zookeeper服務(wù)注冊與發(fā)現(xiàn)的資料請關(guān)注腳本之家其它相關(guān)文章!
- SpringCloud用Zookeeper搭建配置中心的方法
- springcloud集成zookeeper的方法示例
- SpringBoot+Dubbo+Zookeeper實現(xiàn)簡單分布式開發(fā)的應(yīng)用詳解
- 關(guān)于idea+centos7+zookeeper報錯connectionloss,timeout問題
- 如何用python 操作zookeeper
- Zookeeper全局唯一ID生成方案解析
- SpringBoot中dubbo+zookeeper實現(xiàn)分布式開發(fā)的應(yīng)用詳解
- 2020最新IDEA SpringBoot整合Dubbo的實現(xiàn)(zookeeper版)
- SpringBoot系列教程之dubbo和Zookeeper集成方法
- Python通過zookeeper實現(xiàn)分布式服務(wù)代碼解析
- 在Java中操作Zookeeper的示例代碼詳解
- SpringCloud使用Zookeeper作為注冊中心
相關(guān)文章
mybatis?like模糊查詢特殊字符報錯轉(zhuǎn)義處理方式
這篇文章主要介紹了mybatis?like模糊查詢特殊字符報錯轉(zhuǎn)義處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
Java實現(xiàn)excel表格轉(zhuǎn)成json的方法
本篇文章主要介紹了Java實現(xiàn)excel表格轉(zhuǎn)成json的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
Java 日期與時間API相關(guān)用法總結(jié)
這篇文章主要介紹了Java 日期與時間API相關(guān)用法總結(jié),幫助大家更好的理解和使用Java,感興趣的朋友可以了解下2021-02-02
Spring AOP與AspectJ的對比及應(yīng)用詳解
這篇文章主要為大家介紹了Spring AOP與AspectJ的對比及應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
java創(chuàng)建線程池的7種實現(xiàn)方法
在Java中線程池是一種管理線程的機制,它可以創(chuàng)建一組線程并重復(fù)使用它們,避免了創(chuàng)建和銷毀線程的開銷,這篇文章主要給大家介紹了關(guān)于java創(chuàng)建線程池的7種實現(xiàn)方法,需要的朋友可以參考下2023-10-10
Spring Boot @Conditional注解用法示例介紹
這篇文章主要給大家介紹了關(guān)于Spring Boot @Conditional注解用法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

