SpringCloud Ribbon 负载均衡 通过服务器名无法连接的神坑一个
一,问题
采取eureka集群、客户端通过Ribbon调用服务,Ribbon端报下列异常
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版本比较乱,版本关联引用更是乱,最终我切换到 <spring-cloud.version>Greenwich.SR1</spring-cloud.version> 异常为:No instances available for SERVICE-HI
二、寻找答案
网上答案千奇百怪
1,Spring Cloud 官网,RestTemplate bean配置中添加负载均衡注解@LoadBalanced,我添加了
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
结果无效仍然一样报错
2,访问的服务名名称不能有下划线:
我的名称是“SERVICE-HI”本身就不存在下划线,所以不考虑这条。
3,主机名称没在系统文件hosts中配置,ping不通你服务名:
很扯的答案,为什么要配host,负载多台机器让主机名指向谁?不考虑此答案
三,分析问题
百度不到,自己分析原因,发现ribbon服务器没有注册到 eureka server中

分析原理:我的客户端服务“SERVICE-HI”已经成功注册到eureka server中了,如果ribbon服务器不在eureka server中注册,是不会知道客户端服务“SERVICE-HI”的存在以及它存在的位置,那么结论就是,因为ribbon服务器没有在eureka server中注册成功,所以不能识别主机名称。
四,解决问题
配置文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764
spring:
application:
name: service-ribbon
依赖导入
<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 );
} }
有问题,最终发现@EnableDiscoveryClient标签无法注册到注册中心,百度@EnableDiscoveryClient,得到的结论是
@EnableDiscoveryClient和@EnableEurekaClient一样,能够让注册中心能够发现,扫描到改服务,不同点:@EnableEurekaClient只适用于Eureka作为注册中心,@EnableDiscoveryClient 可以是Eureka或其他(consul、zookeeper等)注册中心。
具体原因不去分析,这里先直接切换为@EnableEurekaClient注释
@EnableEurekaClient在哪个包里简直是迷一样的存在,不同版本的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>
重新启动ribbon,发现控制台输入
-- ::06.668 INFO --- [ main] com.netflix.discovery.DiscoveryClient : Getting all instance registry info from the eureka server
-- ::06.878 INFO --- [ main] com.netflix.discovery.DiscoveryClient : The response status is 200
-- ::06.882 INFO --- [ main] com.netflix.discovery.DiscoveryClient : Starting heartbeat executor: renew interval is:
-- ::06.886 INFO --- [ main] c.n.discovery.InstanceInfoReplicator : InstanceInfoReplicator onDemand update allowed rate per min is
-- ::06.891 INFO --- [ main] com.netflix.discovery.DiscoveryClient : Discovery Client initialized at timestamp with initial instances count:
-- ::06.894 INFO --- [ main] o.s.c.n.e.s.EurekaServiceRegistry : Registering application SERVICE-RIBBON with eureka with status UP
-- ::06.896 INFO --- [ main] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=, current=UP, previous=STARTING]
-- ::06.900 INFO --- [nfoReplicator-] com.netflix.discovery.DiscoveryClient : DiscoveryClient_SERVICE-RIBBON/DESKTOP-FJQITE3:service-ribbon:: registering service...
-- ::06.958 INFO --- [nfoReplicator-] com.netflix.discovery.DiscoveryClient : DiscoveryClient_SERVICE-RIBBON/DESKTOP-FJQITE3:service-ribbon: - registration status:
-- ::06.961 INFO --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): (http) with context path ''
-- ::06.963 INFO --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to
-- ::06.967 INFO --- [ main] cn.meylink.ServiceRibbonApplication : Started ServiceRibbonApplication in 5.868 seconds (JVM running for 7.204)
查看Eureka

浏览器测试访问成功!!!
五,附件:Greenwich.SR1 版中常用依赖
有好多问题都是因为 不同版本中引入不正确的依赖导致,这里列出 Greenwich.SR1 版中常用依赖,这里都不需要指定版本号
<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>
SpringCloud Ribbon 负载均衡 通过服务器名无法连接的神坑一个的更多相关文章
- SpringCloud无废话入门02:Ribbon负载均衡
1.白话负载均衡 在上一篇的介绍中,我们创建了两个一模一样的服务提供者:Provider1和Provider2,然后它们提供的服务也一模一样,都叫Hello-Service.为什么一样的服务我们要部署 ...
- SpringCloud的入门学习之概念理解、Ribbon负载均衡入门
1.Ribbon负载均衡,Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端.负载均衡的工具. 答:简单的说,Ribbon是Netflix发布的开源项目,主要功能 ...
- SpringCloud:Ribbon负载均衡
1.概述 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端 负载均衡的工具. 简单的说,Ribbon是Netflix发布的开源项目,主要功能是提供客 ...
- SpringCloud学习笔记(五):Ribbon负载均衡
简介 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套 客户端 负载均衡的工具 .(重点:客户端) 简单的说,Ribbon是Netflix发布的开源项目,主要功能是提 ...
- SpringCloud系列——Ribbon 负载均衡
前言 Ribbon是一个客户端负载均衡器,它提供了对HTTP和TCP客户端的行为的大量控制.我们在上篇(猛戳:SpringCloud系列——Feign 服务调用)已经实现了多个服务之间的Feign调用 ...
- SpringCloud系列五:Ribbon 负载均衡(Ribbon 基本使用、Ribbon 负载均衡、自定义 Ribbon 配置、禁用 Eureka 实现 Ribbon 调用)
1.概念:Ribbon 负载均衡 2.具体内容 现在所有的服务已经通过了 Eureka 进行了注册,那么使用 Eureka 注册的目的是希望所有的服务都统一归属到 Eureka 之中进 行处理,但是现 ...
- spring-cloud: eureka之:ribbon负载均衡自定义配置(二)
spring-cloud: eureka之:ribbon负载均衡自定义配置(二) 有默认配置的话基本上就是轮询接口,现在我们改用自定义配置,同时支持:轮询,随机接口读取 准备工作: 1.eureka服 ...
- spring-cloud: eureka之:ribbon负载均衡配置(一)
spring-cloud: eureka之:ribbon负载均衡配置(一) 比如我有: 一个eureka服务:8761 两个user用户服务: 7900/7901端口 一个movie服务:8010 1 ...
- SpringCloud学习(4)——Ribbon负载均衡
Ribbon概述 SpringCloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡工具. 简单的说, Ribbon是Netflix发布的开源项目, 主要功能是提供客户端软 ...
随机推荐
- python基础之Matplotlib库的使用一(平面图)
在我们过去的几篇博客中,说到了Numpy的使用,我们可以生成一些数据了,下面我们来看看怎么让这些数据呈现在图画上,让我们更加直观的来分析数据. 安装过程我就不再说了,不会安装的,回去补补python最 ...
- 古老的txt下传和txt上载
1.下传文件 TYPES:BEGIN OF TY_DATA, A2 TYPE CHAR20, B2 TYPE I, C2 TYPE CHAR20, END OF TY_DATA. DATA:IT_DA ...
- WebApi使用Unity实现IOC
最近在学习ASP.NET MVC,使用Unity作为依赖注入容器.分别在WebAPI和MVC中使用.这篇文章介绍WebAPI,MVC的在下篇文章中介绍.下面是学习的一点经验. 一 IOC简单介绍 Io ...
- jmeter工具下载及工具功能操作介绍
本博文jmeter介绍的是在windows下使用,linux后期看情况更新,谢谢 简单介绍,想更多了解的去官方,多的很: The Apache JMeter™ application is open ...
- 谈谈JavaScript Navigator 对象属性
Navigator 对象属性 可以在Navigator对象上使用以下属性: 属性 描述 appCodeName 返回浏览器的代码名称 appName 返回浏览器的名称 appVersion 返回浏览器 ...
- linux的ftp操作
1.查看是否安装 rpm -qa |grep vsftpd 没有输出,表示没有安装 2.查看服务状态 1).service vsftpd status 输出信息 vsftpd is stopped 表 ...
- openstack Train版 “nova-status upgrade check”报错:Forbidden: Forbidden (HTTP 403)
部署openstack train版,在部署完nova项目时,进行检查,执行 nova-status upgrade check 返回报错信息如下: [root@controller ~]# nova ...
- 19、FTP服务器
FTP (File Transfer Protocol) 文件传输协议的简称.主要用跨网络.跨平台的文件 传输. FTP 支持两种工作工作模式:主动模式.被动模式. 主动模式: 客户端使用 ...
- Ubuntu下搭建Kubernetes集群(4)--部署K8S Dashboard
K8S Dashboard是官方的一个基于WEB的用户界面,专门用来管理K8S集群,并可展示集群的状态.K8S集群安装好后默认没有包含Dashboard,我们需要额外创建它. 首先我们执行命令: wg ...
- day21_7.25 面向对象之继承
一.继承 什么是继承? 继承是一种关系,就是描述两者之间什么是什么的关系. 在程序中,继承描述的是类与类之间的关系. 例如a如果继承了b,a就具备了b的所有变量与方法,可以直接调用. class B: ...