最近在使用最新版本的SpringCloud编写demo时发现的问题。使用Feign在进行服务间调用时,会提示异常:
ClientException:Load balancer does not have available server for xxx
使用的版本为:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
提示信息已经很明确了。在早期的F版本左右,搭建一个简单的调用实例,是不需要显示的指明Ribbon配置的,但是在最新的版本中,需要主动指明启用Ribbon或单独进行配置。现将问题记录于此以便查阅。
解决方案
早期版本可能的方案
如果在某度上搜索这个问题,其中之一的解决方案是,引入
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
同样的,这可能是早期版本的解决方案,并不适用现在的版本。事实上,如果去中央仓库上查看这个包,会发现其已经被标记为 deprecated
,并且推荐使用 spring-cloud-starter-netflix-eureka-client
替代。
最新版本的方案
显示的声明启用Ribbon
在消费端的配置文件中设置:
ribbon:
eureka:
enabled: true
手动配置Ribbon
如果项目中并没有使用Eureka,可以手动进行配置文件配置或者编写代码进行配置。
配置文件配置如下:
# 生产者服务名 micro-service-producer: ribbon: # 服务地址 listOfServers: localhost:8200,localhost:8201
代码配置如下:
@RibbonClient(name = "micro-service-producer", configuration = RibbonConfig.class) public class RibbonConfig { private static final String listOfServers = "localhost:8200,localhost:8201"; @Bean public ServerList<Server> ribbonServerList() { List<Server> list = Lists.newArrayList(); if (!Strings.isNullOrEmpty(listOfServers)) { for (String s: listOfServers.split(",")) { list.add(new Server(s.trim())); } } return new StaticServerList<>(list.toArray(new Server[0])); } }
总结
上面的问题可能并不是导致这个报错的唯一原因,但应该是最有可能的原因。结合版本、使用的技术再确定解决方案,不可盲目相信网上的答案。
原文
http://www.jptangchina.com/2019/08/25/springcloud-wei-fu-wu-clientexception-load-balancer-does-not-have-available-server-for-client-yi-chang-jie-jue-fang-an/
本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » SpringCloud微服务ClientException:Load balancer does not have available server for client异常…