eureka中显示有服务但是通过ribbon调用显示No instances available for service-hello的问题
一,问题
采取eureka集群、客户端通过Ribbon调用服务,Ribbon端报下列异常
1
2
3
4
5
6
7
|
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,我添加了
1
2
3
4
5
|
@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中注册成功,所以不能识别主机名称。
四,解决问题
配置文件
1
2
3
4
5
6
7
8
9
|
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8764 spring: application: name: service-ribbon |
依赖导入
1
2
3
4
5
6
7
8
9
10
|
< 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 > |
主程序注释
1
2
3
4
5
6
7
8
9
|
@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,需要引入下面的包
1
2
3
4
|
< dependency > < groupId >org.springframework.cloud</ groupId > < artifactId >spring-cloud-starter-netflix-eureka-client</ artifactId > </ dependency > |
修改主程序注释
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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中段容器在下面包中
1
2
3
4
|
< dependency > < groupId >org.springframework.cloud</ groupId > < artifactId >spring-cloud-starter-netflix-hystrix</ artifactId > </ dependency > |
重新启动ribbon,发现控制台输入
1
2
3
4
5
6
7
8
9
10
11
12
|
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
浏览器测试访问成功!!!
五,附件:Greenwich.SR1 版中常用依赖
有好多问题都是因为 不同版本中引入不正确的依赖导致,这里列出 Greenwich.SR1 版中常用依赖,这里都不需要指定版本号
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
< 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 > |
转自https://www.jb51.net/article/163154.htm
eureka中显示有服务但是通过ribbon调用显示No instances available for service-hello的问题的更多相关文章
- Spring Cloud学习笔记【二】Eureka 服务提供者/服务消费者(ribbon)
Ribbon 是 Netflix 发布的开源项目,主要功能是为 REST 客户端实现负载均衡.它主要包括六个组件: ServerList,负载均衡使用的服务器列表.这个列表会缓存在负载均衡器中,并定期 ...
- 玩转Spring Cloud之服务注册发现(eureka)及负载均衡消费(ribbon、feign)
如果说用Spring Boot+Spring MVC是开发单体应用(或单体服务)的利器,那么Spring Boot+Spring MVC+Spring Cloud将是开发分布式应用(快速构建微服务)的 ...
- Eureka 中服务下线的几种方式
原文:https://blog.csdn.net/qq_15071263/article/details/85276486#1_6 Eureka 中服务下线的几种方式1.直接停掉服务根据默认的策略,如 ...
- springboot创建一个服务,向eureka中注册,使用swagger2进行服务管理
首先pom.xml文件,spring boot.springcloud版本很麻烦,容易出问题 <?xml version="1.0" encoding="UTF-8 ...
- SpringCloud系列五:Ribbon 负载均衡(Ribbon 基本使用、Ribbon 负载均衡、自定义 Ribbon 配置、禁用 Eureka 实现 Ribbon 调用)
1.概念:Ribbon 负载均衡 2.具体内容 现在所有的服务已经通过了 Eureka 进行了注册,那么使用 Eureka 注册的目的是希望所有的服务都统一归属到 Eureka 之中进 行处理,但是现 ...
- Spring Cloud Eureka 分布式开发之服务注册中心、负载均衡、声明式服务调用实现
介绍 本示例主要介绍 Spring Cloud 系列中的 Eureka,使你能快速上手负载均衡.声明式服务.服务注册中心等 Eureka Server Eureka 是 Netflix 的子模块,它是 ...
- Spring Cloud(二):Web服务客户端之Ribbon
上文介绍了服务如何通过Eureka实现注册,以及如何从Eureka获取已经注册的服务列表.那么拿到注册服务列表后, 如何进行服务调用?一个简单的实现是可以从被调用服务的实例列表中选择一个服务实例,通过 ...
- SpringCloud 服务负载均衡和调用 Ribbon、OpenFeign
1.Ribbon Spring Cloud Ribbon是基于Netflix Ribbon实现的-套客户端―负载均衡的工具. 简单的说,Ribbon是Netlix发布的开源项目,主要功能是提供客户端的 ...
- springcloud干货之服务消费者(ribbon)
本章介绍springcloud中的服务消费者 springcloud服务调用方式有两种实现方式: 1,restTemplate+ribbon, 2,feign 本来想一篇讲完,发现篇幅有点长,所以本章 ...
随机推荐
- Python基础之面向对象编程
面向对象编程 —— Object Oriented Programming 简写 OOP 01. 面向对象基本概念 我们之前学习的编程方式就是 面向过程 的 面向过程 和 面向对象,是两种不同的 编程 ...
- drf--ModelSerializers序列化
目录 drf--ModelSerializers序列化 项目准备 配置 settings.py 路由 多表设计 表关系分析 创建models 模型序列化 自定义模型序列化 api/serializer ...
- YUV详解
YUV格式解析2 又确认了一下H264的视频格式——H264支持4:2:0的连续或隔行视频的编码和解码 YUV(亦称YCrCb)是被欧洲电视系统所采用的一种颜色编码方法(属于PAL).YUV主要用 ...
- idea/借阅系统的APP开发
(一) 今天去还书时,偶尔想到的idea. 看着在自主借还书机器面前的长龙,我突然想到为什么不将这部机器的功能弄成APP? 通过用手机扫条形码借书不是很方便吗? 哦,不行.借书可以,怎么确保用户将书还 ...
- 自制微擎AI面相识别算术阈值
有时在朋友圈或其他地方会看到一些AI面相的分享链接或小程序,不是面相算命的有多吸引人,而是前面有"AI"两个字母.于是我就上网找了一下相关代码,发现了一个微擎系统的面相模块.下载下 ...
- django应用之corsheaders[跨域设置]
安装 pip install django-cors-headers 注册应用 INSTALLED_APPS = ( ... 'corsheaders', ... ) 中间层设置 MIDDLEWARE ...
- Odoo Qweb语法
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826202.html 一:简介 QWeb是一个基于xml的模板引擎,用于生成HTML片段和页面. 模板指令 ...
- Backbone——数据驱动UI的js开发模式
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826074.html 一:Backbone是什么——JS的MVC框架 Backbone基于undersco ...
- win黑窗口命令
rd 删除 文件夹, 语法: rd [/S] [/Q] [drive:]path 参数: drive 盘符 path 文件路径 /S 递归删除文件夹( 删除前, 要确认是否进行删除) /Q 关闭 ...
- 多态典型用例之virtual
多态典型用例之virtual 参考:https://www.cnblogs.com/dormant/p/5223215.html 1.虚函数(virtual) (1)在某基类中声明为 virtual ...