详解SpringCloud Ribbon 负载均衡通过服务器名无法连接的神坑
作者:ejiyuan 发布时间:2021-06-01 07:28:41
一,问题
采取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,发现控制台输入
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 版中常用依赖,这里都不需要指定版本号
<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.cnblogs.com/ejiyuan/p/11027229.html


猜你喜欢
- C#接口的学习,在编程中,我们经常会用到接口,那什么是接口呢?接口描述的是可属于任何类或结构的一组相关功能,所以实现接口的类或结构必须实现接
- 实践过程效果代码public partial class frmSend : Form{ public frmSe
- Mybatis-Plus是一个优秀的Mybatis增强工具,目前更新到3.1.1。Mybatis-Plus原生提供了很多单表操作的方法,极大
- Spring Security OAuth 默认提供OAuth2.0 的四大基本授权方式(authorization_code\implic
- 在工作中要求将图片上传至本地,如下代码将介绍如何将图片上传至本地准备工作:环境:eclipse4.5-x64,jdk1.7-x64,mave
- 前言在SpringIOC中,我们熟知的BeanScope有单例(singleton)、原型(prototype), Bean的Scope影响
- 在上节使用了H2之后感觉很爽,很轻便,正好有个项目要求简单,最好不适用外部数据库,于是就想着把H2数据库集成进来,这个系统已经存在了一个Or
- java通过IP解析地理位置在项目开发中,需要在登录日志或者操作日志中记录客户端ip所在的地理位置。目前根据ip定位地理位置的第三方api有
- 有项目需求需要绘制多个圆圈,并且使用连续的数字对其排列起来,也就是好多排的圆圈。首先看一下效果图:一排设置为8个,一共有53个的:一排设值为
- 1、实现这里主要用的是反射的方法。用户要传入方法名和方法参数,我们就需要先写函数返回这些信息,最后再包装一下返回给用户。获取某一程序集下所有
- 前言Java8 的新特性:Lambda表达式、强大的 Stream API、全新时间日期 API、ConcurrentHashMap、Met
- Android 8.0推出了PictureInPicture(画中画功能),目前只有在8.0以上的系统上支持。对比IOS,IOS的Pictu
- 实例如下:static bool CheckPowerOfTwo(ulong num){ return num > 0 &
- 今天谈一下C#(WinForm)如何发送带附件的电子邮件!废话少说,先截图伺候:首先C#发送邮件需要smtp服务的支持,我也不知道是不是C#
- C语言字符串大小比较#include <stdio.h>#include <string.h>int fun(cha
- 前言在工作中,很多高并发的场景中,我们会用到队列来实现大量的任务请求。当任务需要某些特殊资源的时候,我们还需要合理的分配资源,让队列中的任务
- 1、概述本文通过手动实现迭代器来了解foreach语句的本质。2、使用foreach语句遍历集合在C#中,使用foreach语句来遍历集合。
- 项目中经常会用到分享的功能,有分享链接也有分享图片,其中分享图片有的需要移动端对屏幕内容进行截取分享,说白了就是将view 转成bitmap
- Spring AOP复杂的日志记录(自定义注解)做项目中,业务逻辑要求只要对数据库数据进行改动的都需要记录日志(增删改),记录的内容有操作者
- 通过Socket + Instrumentation实现模拟键盘鼠标事件主要通过以下三个部分组成:Socket编程:实现PC和Emulato