最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年11月20日 09:17:20   作者:自律給我自由  
這篇文章主要介紹了基于Spring Cloud Zookeeper實(shí)現(xiàn)服務(wù)注冊(cè)與發(fā)現(xiàn),幫助大家更好的理解和學(xué)習(xí)spring框架,感興趣的朋友可以了解下

服務(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ù):

服務(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;

@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個(gè)蘋果";
  }
}

2.服務(wù)調(diào)用

以上就是基于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)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Struts2數(shù)據(jù)輸入驗(yàn)證教程詳解

    Struts2數(shù)據(jù)輸入驗(yàn)證教程詳解

    這篇文章主要介紹了Struts2數(shù)據(jù)輸入驗(yàn)證教程詳解的相關(guān)資料,輸入數(shù)據(jù)驗(yàn)證的方法有兩種,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2016-10-10
  • 詳細(xì)介紹Java函數(shù)式接口

    詳細(xì)介紹Java函數(shù)式接口

    函數(shù)式接口在Java中是指有且僅有一個(gè)抽象方法的接口。當(dāng)然接口中可以包含其他的方法默認(rèn)、靜態(tài)、私有,具體內(nèi)容請(qǐng)參考下面文章內(nèi)容
    2021-09-09
  • java生成隨機(jī)數(shù)的方法

    java生成隨機(jī)數(shù)的方法

    這篇文章主要介紹了java生成隨機(jī)數(shù)的方法,涉及java隨機(jī)數(shù)及字符串操作的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • Java實(shí)現(xiàn)蘿卜勇者游戲的示例代碼

    Java實(shí)現(xiàn)蘿卜勇者游戲的示例代碼

    《蘿卜勇者》是由國(guó)內(nèi)玩家自制的一款獨(dú)立游戲,玩家扮演蘿卜勇士闖關(guān),打敗各種邪惡的敵人,獲得最后的勝利。本文將利用Java實(shí)現(xiàn)這一游戲,感興趣的可以了解一下
    2022-02-02
  • Spring+Quartz實(shí)現(xiàn)動(dòng)態(tài)任務(wù)調(diào)度詳解

    Spring+Quartz實(shí)現(xiàn)動(dòng)態(tài)任務(wù)調(diào)度詳解

    這篇文章主要介紹了Spring+Quartz實(shí)現(xiàn)動(dòng)態(tài)任務(wù)調(diào)度詳解,最近經(jīng)?;趕pring?boot寫定時(shí)任務(wù),并且是使用注解的方式進(jìn)行實(shí)現(xiàn),分成的方便將自己的類注入spring容器,需要的朋友可以參考下
    2024-01-01
  • SpringBoot緩存Ehcache的使用詳解

    SpringBoot緩存Ehcache的使用詳解

    EhCache、Redis比較常用,使用Redis的時(shí)候需要先安裝Redis服務(wù)器,本文給大家介紹SpringBoot緩存Ehcache的使用詳解,感興趣的朋友跟隨小編一起看看吧
    2022-03-03
  • Android 應(yīng)用按返回鍵退向后臺(tái)運(yùn)行實(shí)例代碼

    Android 應(yīng)用按返回鍵退向后臺(tái)運(yùn)行實(shí)例代碼

    這篇文章主要介紹了Android 應(yīng)用按返回鍵退向后臺(tái)運(yùn)行實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • request.getRequestURL()等方法得到路徑的區(qū)別及說明

    request.getRequestURL()等方法得到路徑的區(qū)別及說明

    這篇文章主要介紹了request.getRequestURL()等方法得到路徑的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Java 如何使用JDBC連接數(shù)據(jù)庫(kù)

    Java 如何使用JDBC連接數(shù)據(jù)庫(kù)

    這篇文章主要介紹了Java 如何使用JDBC連接數(shù)據(jù)庫(kù),幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-02-02
  • 源碼分析Java中ThreadPoolExecutor的底層原理

    源碼分析Java中ThreadPoolExecutor的底層原理

    這篇文章主要帶大家從源碼分析一下Java中ThreadPoolExecutor的底層原理,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,需要的可以參考一下
    2023-05-05

最新評(píng)論

大余县| 宝丰县| 托克逊县| 德州市| 嵊州市| 义乌市| 普兰县| 阳泉市| 长宁区| 德钦县| 祁阳县| 九江市| 常宁市| 察雅县| 鹤峰县| 巴南区| 额尔古纳市| 正镶白旗| 平山县| 牟定县| 郑州市| 长顺县| 宣武区| 万州区| 桃源县| 阜南县| 平舆县| 涿州市| 斗六市| 瑞丽市| 伊宁县| 泽州县| 西平县| 伊宁市| 灵山县| 大庆市| 新安县| 湖口县| 垣曲县| 门源| 前郭尔|