一,问题

采取eureka集群、客户端通过Ribbon调用服务,Ribbon端报下列异常

  1. java.net.UnknownHostException: SERVICE-HI
  2. java.lang.IllegalStateException: No instances available for SERVICE-HI
  3. java.lang.IllegalStateException: Request URI does not contain a valid hostname: http://SERVICE-HI
  4. com.netfix.discovery.shared.taransport.TransportException: Cannot execute request on any known server

Spring Cloud版本比较乱,版本关联引用更是乱,最终我切换到 <spring-cloud.version>Greenwich.SR1</spring-cloud.version> 异常为:No instances available for SERVICE-HI

二、寻找答案 

网上答案千奇百怪

1,Spring Cloud 官网,RestTemplate bean配置中添加负载均衡注解@LoadBalanced,我添加了

  1. @Bean
  2. @LoadBalanced
  3. public RestTemplate getRestTemplate(){
  4. return new RestTemplate();
  5. }

结果无效仍然一样报错

2,访问的服务名名称不能有下划线:  

  1. 我的名称是“SERVICE-HI”本身就不存在下划线,所以不考虑这条。

3,主机名称没在系统文件hosts中配置,ping不通你服务名:

  1. 很扯的答案,为什么要配host,负载多台机器让主机名指向谁?不考虑此答案

三,分析问题

百度不到,自己分析原因,发现ribbon服务器没有注册到 eureka server中

分析原理:我的客户端服务“SERVICE-HI”已经成功注册到eureka server中了,如果ribbon服务器不在eureka server中注册,是不会知道客户端服务“SERVICE-HI”的存在以及它存在的位置,那么结论就是,因为ribbon服务器没有在eureka server中注册成功,所以不能识别主机名称。

四,解决问题

配置文件

  1. eureka:
  2. client:
  3. serviceUrl:
  4. defaultZone: http://localhost:8761/eureka/
  5. server:
  6. port: 8764
  7. spring:
  8. application:
  9. name: service-ribbon

依赖导入

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  9. </dependency>
  10. </dependencies>

主程序注释

  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. public class ServiceRibbonApplication {
  4.  
  5. public static void main(String[] args) {
  6. SpringApplication.run( ServiceRibbonApplication.class, args );
  7. }
  8.  
  9. }

有问题,最终发现@EnableDiscoveryClient标签无法注册到注册中心,百度@EnableDiscoveryClient,得到的结论是

@EnableDiscoveryClient和@EnableEurekaClient一样,能够让注册中心能够发现,扫描到改服务,不同点:@EnableEurekaClient只适用于Eureka作为注册中心,@EnableDiscoveryClient 可以是Eureka或其他(consul、zookeeper等)注册中心

具体原因不去分析,这里先直接切换为@EnableEurekaClient注释

@EnableEurekaClient在哪个包里简直是迷一样的存在,不同版本的spring cloud 中位置不同,我使用Greenwich.SR1,需要引入下面的包

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

修改主程序注释

  1. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  2. import org.springframework.cloud.netflix.hystrix.EnableHystrix;
  3.  
  4. @SpringBootApplication
  5. @EnableEurekaClient
  6. @EnableHystrix //我开启了段容器
  7. public class ServiceRibbonApplication {
  8.  
  9. public static void main(String[] args) {
  10. SpringApplication.run( ServiceRibbonApplication.class, args );
  11. }
  12.  
  13. }

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

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  4. </dependency>

重新启动ribbon,发现控制台输入

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

查看Eureka

浏览器测试访问成功!!!

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

有好多问题都是因为 不同版本中引入不正确的依赖导致,这里列出 Greenwich.SR1 版中常用依赖,这里都不需要指定版本号

  1. <dependencies>
  2. <!-- eureka client -->
  3. <dependency>
  4. <groupId>org.springframework.cloud</groupId>
  5. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  6. </dependency>
  7. <!-- eureka server -->
  8. <dependency>
  9. <groupId>org.springframework.cloud</groupId>
  10. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  11. </dependency>
  12. <!-- 段容器 -->
  13. <dependency>
  14. <groupId>org.springframework.cloud</groupId>
  15. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  16. </dependency>
  17. <!-- ribbon -->
  18. <dependency>
  19. <groupId>org.springframework.cloud</groupId>
  20. <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  21. </dependency>
  22. <!-- feign -->
  23. <dependency>
  24. <groupId>org.springframework.cloud</groupId>
  25. <artifactId>spring-cloud-starter-openfeign</artifactId>
  26. </dependency>
  27. <!-- config server -->
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-config-server</artifactId>
  31. </dependency>
  32. <!-- config client -->
  33. <dependency>
  34. <groupId>org.springframework.cloud</groupId>
  35. <artifactId>spring-cloud-starter-config</artifactId>
  36. </dependency>
  37. <!-- zuul -->
  38. <dependency>
  39. <groupId>org.springframework.cloud</groupId>
  40. <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-starter-test</artifactId>
  45. <scope>test</scope>
  46. </dependency>
  47. </dependencies>

SpringCloud Ribbon 负载均衡 通过服务器名无法连接的神坑一个的更多相关文章

  1. SpringCloud无废话入门02:Ribbon负载均衡

    1.白话负载均衡 在上一篇的介绍中,我们创建了两个一模一样的服务提供者:Provider1和Provider2,然后它们提供的服务也一模一样,都叫Hello-Service.为什么一样的服务我们要部署 ...

  2. SpringCloud的入门学习之概念理解、Ribbon负载均衡入门

    1.Ribbon负载均衡,Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端.负载均衡的工具. 答:简单的说,Ribbon是Netflix发布的开源项目,主要功能 ...

  3. SpringCloud:Ribbon负载均衡

    1.概述 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端       负载均衡的工具. 简单的说,Ribbon是Netflix发布的开源项目,主要功能是提供客 ...

  4. SpringCloud学习笔记(五):Ribbon负载均衡

    简介 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套 客户端 负载均衡的工具 .(重点:客户端) 简单的说,Ribbon是Netflix发布的开源项目,主要功能是提 ...

  5. SpringCloud系列——Ribbon 负载均衡

    前言 Ribbon是一个客户端负载均衡器,它提供了对HTTP和TCP客户端的行为的大量控制.我们在上篇(猛戳:SpringCloud系列——Feign 服务调用)已经实现了多个服务之间的Feign调用 ...

  6. SpringCloud系列五:Ribbon 负载均衡(Ribbon 基本使用、Ribbon 负载均衡、自定义 Ribbon 配置、禁用 Eureka 实现 Ribbon 调用)

    1.概念:Ribbon 负载均衡 2.具体内容 现在所有的服务已经通过了 Eureka 进行了注册,那么使用 Eureka 注册的目的是希望所有的服务都统一归属到 Eureka 之中进 行处理,但是现 ...

  7. spring-cloud: eureka之:ribbon负载均衡自定义配置(二)

    spring-cloud: eureka之:ribbon负载均衡自定义配置(二) 有默认配置的话基本上就是轮询接口,现在我们改用自定义配置,同时支持:轮询,随机接口读取 准备工作: 1.eureka服 ...

  8. spring-cloud: eureka之:ribbon负载均衡配置(一)

    spring-cloud: eureka之:ribbon负载均衡配置(一) 比如我有: 一个eureka服务:8761 两个user用户服务: 7900/7901端口 一个movie服务:8010 1 ...

  9. SpringCloud学习(4)——Ribbon负载均衡

    Ribbon概述 SpringCloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡工具. 简单的说, Ribbon是Netflix发布的开源项目, 主要功能是提供客户端软 ...

随机推荐

  1. Tomcat 简单容器化

    Tomcat 容器化 思考 问题1 , Tomcat 容器化,Tomcat 如何配置 APR 连接器 Tomcat 的基础镜像已经是开启了 APR. 问题2, Tomcat 是每次都需要重新构建. 一 ...

  2. 工作笔记--adb命令篇

    1.抓log方法 (bat文件) mkdir D:\logcatset /p miaoshu=请描述操作:adb logcat -v threadtime > D:\logcat\%miaosh ...

  3. Python requests库的使用(一)

    requests库官方使用手册地址:http://www.python-requests.org/en/master/:中文使用手册地址:http://cn.python-requests.org/z ...

  4. 深入浅出《设计模式》之外观模式(C++)

    前言 模式介绍 外观模式相比较之下比较简单,模式设计中定义是为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口是的这一子系统更加容易使用. 如果不理解呢,简单些说就是外观模式提 ...

  5. Outlook API

    1.Outlook简介 若要从Outlook 外控制Outlook对象,必须在编写代码的工程中建立对Outlook对象库的引用. 1.1  Outlook Application说明: 代表整个Mic ...

  6. c# 读取txt文件中文乱码解决方法

    之前做过一个项目,在程序运行目录下有个txt文件,文件内容是中文的时候会乱码, 后来用这个函数处理后,就不乱码了: private string GetPDA_Code()        {      ...

  7. 多版本切换python

    Python 安装包去官网自行下载: https://www.python.org/downloads/mac-osx/ Mac os 自带python, 但我记得是python2.7版本 在选择安装 ...

  8. vuejs兄弟通信$emit和$on

    1   vm.$on( event, callback ) 监听当前实例上的自定义事件.事件可以由vm.$emit触发.回调函数会接收所有传入事件触发函数的额外参数. 2 vm.$emit( even ...

  9. shell 之for循环几种写法

    参见博客 http://blog.csdn.net/babyfish13/article/details/52981110 ,此博客写的非常清晰明了.

  10. node端console.log输出不同颜色文字

    我们知道console.log直接输出是按着终端的默认颜色来显示的, console.log('message') 那么如何指定他们的颜色显示呢?很简单,直接再加一个参数就可以了,例如: consol ...