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

詳解使用Spring Cloud Consul實(shí)現(xiàn)服務(wù)的注冊(cè)和發(fā)現(xiàn)

 更新時(shí)間:2018年06月11日 09:42:20   作者:西夏一品堂  
這篇文章主要介紹了詳解使用Spring Cloud Consul實(shí)現(xiàn)服務(wù)的注冊(cè)和發(fā)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

首先安裝consul環(huán)境,參照之前的文章:http://m.fzitv.net/article/141789.htm

項(xiàng)目規(guī)劃,2個(gè)服務(wù)端,1個(gè)客戶端

首先來看服務(wù)端,

一:服務(wù)端1:

項(xiàng)目依賴

<dependencyManagement> 
  <dependencies> 
    <dependency> 
      <groupId>org.springframework.cloud</groupId> 
      <artifactId>spring-cloud-consul-dependencies</artifactId> 
      <version>1.0.1.RELEASE</version> 
      <type>pom</type> 
      <scope>import</scope> 
    </dependency> 
  </dependencies> 
</dependencyManagement> 
 
<dependencies> 
  <dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-actuator</artifactId> 
    <version>1.3.5.RELEASE</version> 
  </dependency> 
  <dependency> 
    <groupId>org.springframework.cloud</groupId> 
    <artifactId>spring-cloud-starter-consul-discovery</artifactId> 
  </dependency> 
</dependencies> 

注意,增加spring-boot-actuator是為了項(xiàng)目可以訪問/health 路徑來判斷服務(wù)是否健康

package com.pp.consul1; 
 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 
 
@SpringBootApplication 
@EnableDiscoveryClient 
@RestController 
public class ConsulApp { 
   
  @RequestMapping("/home") 
  public Object home() { 
    System.out.println("1111111111111"); 
    return "OK11"; 
  } 
   
  public static void main( String[] args ) { 
    SpringApplication.run(ConsulApp.class, args); 
  } 
} 

application.properties 配置內(nèi)容

server.port=9955  
spring.application.name=Consul-Server-1 
spring.cloud.consul.host=192.168.1.100 
spring.cloud.consul.port=8500 
spring.cloud.consul.enabled=true 
spring.cloud.consul.discovery.enabled=true 
spring.cloud.consul.discovery.instanceId=tomcat1 
spring.cloud.consul.discovery.serviceName=tomcat 
spring.cloud.consul.discovery.hostname=192.168.2.95 
spring.cloud.consul.discovery.port=${server.port} 
spring.cloud.consul.discovery.healthCheckUrl=http://192.168.2.95:9955/health 
spring.cloud.consul.discovery.healthCheckInterval=10s 
spring.cloud.consul.discovery.tags=dev 

看過我之前文章的,應(yīng)該對(duì)這些配置很清楚了。這樣,一個(gè)服務(wù)端就配置寫好了。

由于我們?cè)黾恿薂EnableDiscoveryClient注解,所以,系統(tǒng)啟動(dòng)的時(shí)候,就會(huì)向consul注冊(cè)一個(gè)服務(wù),服務(wù)的名字為tomcat, ID為tomcat1

訪問consul的HTTP API /v1/catalog/service/tomcat 輸出如下:

{ 
  "Node":"192.168.1.100", 
  "Address":"192.168.1.100", 
  "ServiceID":"tomcat1", 
  "ServiceName":"tomcat", 
  "ServiceTags":["dev"], 
  "ServiceAddress":"192.168.2.95", 
  "ServicePort":9955, 
  "ServiceEnableTagOverride":false, 
  "CreateIndex":993, 
  "ModifyIndex":1057 
} 

二:服務(wù)端2

項(xiàng)目依賴和上面一樣 

package com.pp.consul2;  
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 
 
@SpringBootApplication 
@EnableDiscoveryClient 
@RestController 
public class ConsulApp { 
   
  @RequestMapping("/home") 
  public Object home() { 
    System.out.println("2222222222222222"); 
    return "OK22"; 
  } 
   
  public static void main( String[] args ) { 
    SpringApplication.run(ConsulApp.class, args); 
  } 
} 

application.properties 配置內(nèi)容:

server.port=9966 
 
spring.application.name=Consul-Server-2 
spring.cloud.consul.host=192.168.1.100 
spring.cloud.consul.port=8500 
spring.cloud.consul.enabled=true 
spring.cloud.consul.discovery.enabled=true 
spring.cloud.consul.discovery.instanceId=tomcat2 
spring.cloud.consul.discovery.serviceName=tomcat 
spring.cloud.consul.discovery.hostname=192.168.2.95 
spring.cloud.consul.discovery.port=${server.port} 
spring.cloud.consul.discovery.healthCheckUrl=http://192.168.2.95:9966/health 
spring.cloud.consul.discovery.healthCheckInterval=10s 
spring.cloud.consul.discovery.tags=test 

三:客戶端

項(xiàng)目依賴,只需要spring-cloud-starter-consul-discovery

application.properties 配置內(nèi)容:

server.port=9977 
 
spring.application.name=Consul-Client 
spring.cloud.consul.host=192.168.1.100 
spring.cloud.consul.port=8500 
spring.cloud.consul.discovery.register=false 

注意,這里的spring.cloud.consul.discovery.register需要配置成false,否則系統(tǒng)啟動(dòng)的時(shí)候,會(huì)向consul注冊(cè)一個(gè)服務(wù)

package com.pp.client; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.cloud.client.discovery.DiscoveryClient; 
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 
 
@SpringBootApplication 
@EnableDiscoveryClient 
@RestController 
public class ConsulClient { 
   
  @Autowired 
  private LoadBalancerClient loadBalancer; 
   
  @Autowired 
  private DiscoveryClient discoveryClient; 
   
  /** 
   * 從所有服務(wù)中選擇一個(gè)服務(wù)(輪詢) 
   */ 
  @RequestMapping("/discover") 
  public Object discover() { 
    return loadBalancer.choose("tomcat").getUri().toString(); 
  } 
   
  /** 
   * 獲取所有服務(wù) 
   */ 
  @RequestMapping("/services") 
  public Object services() { 
    return discoveryClient.getInstances("tomcat"); 
  } 
   
  public static void main( String[] args ) { 
    SpringApplication.run(ConsulClient.class, args); 
  } 
} 

啟動(dòng)之后,就可以訪問/discover,/services 查看效果了。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java?Web中ServletContext對(duì)象詳解與應(yīng)用

    Java?Web中ServletContext對(duì)象詳解與應(yīng)用

    ServletContext是一個(gè)容器,可以用來存放變量,供一個(gè)web項(xiàng)目中多個(gè)Servlet共享,下面這篇文章主要給大家介紹了關(guān)于Java?Web中ServletContext對(duì)象詳解與應(yīng)用的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • SpringBoot中MapStruct實(shí)現(xiàn)優(yōu)雅的數(shù)據(jù)復(fù)制

    SpringBoot中MapStruct實(shí)現(xiàn)優(yōu)雅的數(shù)據(jù)復(fù)制

    本文主要介紹了SpringBoot中MapStruct實(shí)現(xiàn)優(yōu)雅的數(shù)據(jù)復(fù)制,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08
  • 一步步帶你入門Java中File類

    一步步帶你入門Java中File類

    java.io.File類是文件和目錄路徑名的抽象表示形式,主要用于文件和目錄的創(chuàng)建、查找和刪除等操作,下面這篇文章主要給大家介紹了關(guān)于入門Java中File類的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • SpringBoot實(shí)現(xiàn)登錄攔截的示例代碼

    SpringBoot實(shí)現(xiàn)登錄攔截的示例代碼

    如果我們不進(jìn)行登錄攔截的話,即使我們跳過登錄頁面直接去訪問任意一個(gè)頁面也能訪問成功,那么登錄功能就沒有意義,同時(shí)也會(huì)存在安全問題,本文就來介紹一下SpringBoot登錄攔截,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • 關(guān)于springboot配置文件密文解密方式

    關(guān)于springboot配置文件密文解密方式

    這篇文章主要介紹了關(guān)于springboot配置文件密文解密方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • SpringBoot3整合MyBatis出現(xiàn)異常:Property?'sqlSessionFactory'or?'sqlSessionTemplate'?are?required

    SpringBoot3整合MyBatis出現(xiàn)異常:Property?'sqlSessionFactory&a

    這篇文章主要介紹了SpringBoot3整合MyBatis報(bào)錯(cuò):Property?‘sqlSessionFactory‘?or?‘sqlSessionTemplate‘?are?required,其實(shí)不是個(gè)大問題,只是自己編碼時(shí)遇到了,然后總結(jié)總結(jié)分享一下,如果有遇到類似問題的,可以參考一下
    2022-11-11
  • 深入了解final在java中的應(yīng)用

    深入了解final在java中的應(yīng)用

    談到final關(guān)鍵字,想必很多人都不陌生,在使用匿名內(nèi)部類的時(shí)候可能會(huì)經(jīng)常用到final關(guān)鍵字。另外,Java中的String類就是一個(gè)final類,那么今天我們就來了解final這個(gè)關(guān)鍵字的用法。
    2019-06-06
  • Java16 JDK安裝并設(shè)置環(huán)境變量的方法步驟

    Java16 JDK安裝并設(shè)置環(huán)境變量的方法步驟

    突然想起自己大學(xué)剛接觸java的時(shí)候,要下載JDK和配置環(huán)境變量,那時(shí)候我上網(wǎng)找了很多教學(xué),本文就詳細(xì)的介紹一下Java16 JDK安裝并設(shè)置環(huán)境變量,感興趣的可以了解一下
    2021-09-09
  • 淺談SpringBoot 中關(guān)于自定義異常處理的套路

    淺談SpringBoot 中關(guān)于自定義異常處理的套路

    這篇文章主要介紹了淺談SpringBoot 中關(guān)于自定義異常處理的套路,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • SpringBoot實(shí)現(xiàn)單點(diǎn)登錄(SSO)的四種方案

    SpringBoot實(shí)現(xiàn)單點(diǎn)登錄(SSO)的四種方案

    單點(diǎn)登錄(Single?Sign-On,SSO)是企業(yè)應(yīng)用系統(tǒng)中常見的用戶認(rèn)證方案,它允許用戶使用一組憑證訪問多個(gè)相關(guān)但獨(dú)立的系統(tǒng),無需重復(fù)登錄,本文給大家介紹了SpringBoot實(shí)現(xiàn)單點(diǎn)登錄(SSO)的四種方案,需要的朋友可以參考下
    2025-04-04

最新評(píng)論

眉山市| 黎川县| 溆浦县| 那曲县| 漳浦县| 福鼎市| 娄烦县| 贵南县| 逊克县| 土默特右旗| 双峰县| 尚义县| 宁南县| 温州市| 榆林市| 武陟县| 格尔木市| 泾源县| 景德镇市| 翁牛特旗| 伊吾县| 清原| 商城县| 衡阳县| 盐津县| 韶山市| 莱西市| 喀喇沁旗| 吐鲁番市| 龙江县| 正安县| 拜泉县| 潮安县| 印江| 宿迁市| 福安市| 英超| 平和县| 长治市| 海原县| 于田县|