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

詳解SpringCloud Ribbon 負(fù)載均衡通過(guò)服務(wù)器名無(wú)法連接的神坑

 更新時(shí)間:2019年06月16日 09:03:18   作者:ejiyuan  
這篇文章主要介紹了詳解SpringCloud Ribbon 負(fù)載均衡通過(guò)服務(wù)器名無(wú)法連接的神坑,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

一,問題

采取eureka集群、客戶端通過(guò)Ribbon調(diào)用服務(wù),Ribbon端報(bào)下列異常

java.net.UnknownHostException: SERVICE-HI

java.lang.IllegalStateException: No instances available for SERVICE-HI

java.lang.IllegalStateException: Request URI does not contain a valid hostname: http://SERVICE-HI

com.netfix.discovery.shared.taransport.TransportException: Cannot execute request on any known server

Spring Cloud版本比較亂,版本關(guān)聯(lián)引用更是亂,最終我切換到 <spring-cloud.version> Greenwich.SR1 </spring-cloud.version> 異常為: No instances available for SERVICE-HI

二、尋找答案 

網(wǎng)上答案千奇百怪

1,Spring Cloud 官網(wǎng),RestTemplate bean配置中添加負(fù)載均衡注解@LoadBalanced,我添加了

@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
  return new RestTemplate();
}

結(jié)果無(wú)效仍然一樣報(bào)錯(cuò)

2,訪問的服務(wù)名名稱不能有下劃線:

我的名稱是“SERVICE-HI”本身就不存在下劃線,所以不考慮這條。

3,主機(jī)名稱沒在系統(tǒng)文件hosts中配置,ping不通你服務(wù)名:

很扯的答案,為什么要配host,負(fù)載多臺(tái)機(jī)器讓主機(jī)名指向誰(shuí)?不考慮此答案

三,分析問題

百度不到,自己分析原因,發(fā)現(xiàn)ribbon服務(wù)器沒有注冊(cè)到 eureka server中

分析原理:我的客戶端服務(wù)“SERVICE-HI”已經(jīng)成功注冊(cè)到eureka server中了,如果ribbon服務(wù)器不在eureka server中注冊(cè),是不會(huì)知道客戶端服務(wù)“SERVICE-HI”的存在以及它存在的位置,那么結(jié)論就是,因?yàn)閞ibbon服務(wù)器沒有在eureka server中注冊(cè)成功,所以不能識(shí)別主機(jī)名稱。

四,解決問題

配置文件

eureka:
 client:
  serviceUrl:
   defaultZone: http://localhost:8761/eureka/
server:
 port: 8764
spring:
 application:
  name: service-ribbon

依賴導(dǎo)入

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  </dependency>
</dependencies>

主程序注釋

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication {
  
  public static void main(String[] args) {    
    SpringApplication.run( ServiceRibbonApplication.class, args );
  }

}

有問題,最終發(fā)現(xiàn)@EnableDiscoveryClient標(biāo)簽無(wú)法注冊(cè)到注冊(cè)中心,百度@EnableDiscoveryClient,得到的結(jié)論是

@EnableDiscoveryClient和@EnableEurekaClient一樣,能夠讓注冊(cè)中心能夠發(fā)現(xiàn),掃描到改服務(wù),不同點(diǎn):@EnableEurekaClient只適用于Eureka作為注冊(cè)中心,@EnableDiscoveryClient 可以是Eureka或其他(consul、zookeeper等)注冊(cè)中心 。

具體原因不去分析,這里先直接切換為@EnableEurekaClient注釋

@EnableEurekaClient在哪個(gè)包里簡(jiǎn)直是迷一樣的存在,不同版本的spring cloud 中位置不同,我使用Greenwich.SR1,需要引入下面的包

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

修改主程序注釋

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;


@SpringBootApplication
@EnableEurekaClient
@EnableHystrix //我開啟了段容器
public class ServiceRibbonApplication {

  public static void main(String[] args) {
    SpringApplication.run( ServiceRibbonApplication.class, args );
  }

}

這里提一句在 Greenwich.SR1中段容器在下面包中

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>

重新啟動(dòng)ribbon,發(fā)現(xiàn)控制臺(tái)輸入

2019-06-15 13:08:06.668 INFO 14796 --- [      main] com.netflix.discovery.DiscoveryClient  : Getting all instance registry info from the eureka server
2019-06-15 13:08:06.878 INFO 14796 --- [      main] com.netflix.discovery.DiscoveryClient  : The response status is 200
2019-06-15 13:08:06.882 INFO 14796 --- [      main] com.netflix.discovery.DiscoveryClient  : Starting heartbeat executor: renew interval is: 30
2019-06-15 13:08:06.886 INFO 14796 --- [      main] c.n.discovery.InstanceInfoReplicator   : InstanceInfoReplicator onDemand update allowed rate per min is 4
2019-06-15 13:08:06.891 INFO 14796 --- [      main] com.netflix.discovery.DiscoveryClient  : Discovery Client initialized at timestamp 1560575286889 with initial instances count: 2
2019-06-15 13:08:06.894 INFO 14796 --- [      main] o.s.c.n.e.s.EurekaServiceRegistry    : Registering application SERVICE-RIBBON with eureka with status UP
2019-06-15 13:08:06.896 INFO 14796 --- [      main] com.netflix.discovery.DiscoveryClient  : Saw local status change event StatusChangeEvent [timestamp=1560575286896, current=UP, previous=STARTING]
2019-06-15 13:08:06.900 INFO 14796 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient  : DiscoveryClient_SERVICE-RIBBON/DESKTOP-FJQITE3:service-ribbon:8764: registering service...
2019-06-15 13:08:06.958 INFO 14796 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient  : DiscoveryClient_SERVICE-RIBBON/DESKTOP-FJQITE3:service-ribbon:8764 - registration status: 204
2019-06-15 13:08:06.961 INFO 14796 --- [      main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8764 (http) with context path ''
2019-06-15 13:08:06.963 INFO 14796 --- [      main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8764
2019-06-15 13:08:06.967 INFO 14796 --- [      main] cn.meylink.ServiceRibbonApplication   : Started ServiceRibbonApplication in 5.868 seconds (JVM running for 7.204)

查看Eureka

瀏覽器測(cè)試訪問成功?。?!

五,附件:Greenwich.SR1 版中常用依賴

有好多問題都是因?yàn)?不同版本中引入不正確的依賴導(dǎo)致,這里列出 Greenwich.SR1 版中常用依賴,這里都不需要指定版本號(hào)

<dependencies>
  <!-- eureka client -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>
  <!-- eureka server -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  </dependency>
  <!-- 段容器 -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  </dependency>
  <!-- ribbon -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  </dependency>
  <!-- feign -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
  </dependency>
  <!-- config server -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
  </dependency>
  <!-- config client -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>
  <!-- zuul -->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>

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

相關(guān)文章

  • Springboot升級(jí)到2.7.2結(jié)合nacos遇到的坑及解決

    Springboot升級(jí)到2.7.2結(jié)合nacos遇到的坑及解決

    這篇文章主要介紹了Springboot升級(jí)到2.7.2結(jié)合nacos遇到的坑及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • SpringBoot項(xiàng)目中處理返回json的null值(springboot項(xiàng)目為例)

    SpringBoot項(xiàng)目中處理返回json的null值(springboot項(xiàng)目為例)

    本文以spring boot項(xiàng)目為例給大家介紹SpringBoot項(xiàng)目中處理返回json的null值問題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下
    2019-10-10
  • MyBatis帶參查詢的方法詳解

    MyBatis帶參查詢的方法詳解

    這篇文章主要介紹了MyBatis帶參查詢的方法詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • Java中集合LinkedList的原理與使用方法

    Java中集合LinkedList的原理與使用方法

    這篇文章主要給大家介紹了關(guān)于Java中集合LinkedList的原理與使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • 詳解Spring Boot整合Mybatis實(shí)現(xiàn) Druid多數(shù)據(jù)源配置

    詳解Spring Boot整合Mybatis實(shí)現(xiàn) Druid多數(shù)據(jù)源配置

    本篇文章主要介紹了詳解Spring Boot整合Mybatis實(shí)現(xiàn) Druid多數(shù)據(jù)源配置,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • Java復(fù)制(拷貝)數(shù)組的4種方法:arraycopy()方法、clone() 方法、copyOf()和copyOfRan

    Java復(fù)制(拷貝)數(shù)組的4種方法:arraycopy()方法、clone() 方法、copyOf()和copyOfRa

    這篇文章主要介紹了Java復(fù)制(拷貝)數(shù)組的4種方法:arraycopy()方法、clone() 方法、copyOf()和copyOfRan,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Spring?Boot實(shí)現(xiàn)分布式系統(tǒng)中的服務(wù)發(fā)現(xiàn)和注冊(cè)(最新推薦)

    Spring?Boot實(shí)現(xiàn)分布式系統(tǒng)中的服務(wù)發(fā)現(xiàn)和注冊(cè)(最新推薦)

    在本文中,我們深入探討了Spring?Boot如何實(shí)現(xiàn)分布式系統(tǒng)中的服務(wù)發(fā)現(xiàn)和注冊(cè),我們使用Eureka作為服務(wù)注冊(cè)中心,Ribbon作為負(fù)載均衡器,Hystrix作為熔斷器,成功地實(shí)現(xiàn)了服務(wù)發(fā)現(xiàn)、服務(wù)注冊(cè)、負(fù)載均衡和服務(wù)熔斷等功能,需要的朋友參考下吧
    2023-06-06
  • java單例模式實(shí)現(xiàn)面板切換

    java單例模式實(shí)現(xiàn)面板切換

    這篇文章主要為大家詳細(xì)介紹了java單例模式實(shí)現(xiàn)面板切換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • 迅速學(xué)會(huì)@ConfigurationProperties的使用操作

    迅速學(xué)會(huì)@ConfigurationProperties的使用操作

    這篇文章主要介紹了迅速學(xué)會(huì)@ConfigurationProperties的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 詳解MyBatis特性之動(dòng)態(tài)SQL

    詳解MyBatis特性之動(dòng)態(tài)SQL

    動(dòng)態(tài) SQL 是 MyBatis 的強(qiáng)大特性之一,這篇文章我們將結(jié)合動(dòng)態(tài)SQL完成更加復(fù)雜的 SQL 操作,文章通過(guò)代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-01-01

最新評(píng)論

藁城市| 安远县| 阜康市| 昌图县| 东安县| 赤城县| 平乡县| 星子县| 和平县| 泾源县| 中超| 开化县| 江源县| 新乡县| 巴塘县| 遂宁市| 眉山市| 赣榆县| 楚雄市| 潮州市| 盖州市| 民丰县| 淅川县| 赤峰市| 淮北市| 凤冈县| 石泉县| 苏尼特右旗| 新营市| 德州市| 陇南市| 盘锦市| 霞浦县| 扶绥县| 文山县| 海口市| 望城县| 钟祥市| 孝昌县| 荔浦县| 井冈山市|