【解决】使用SpringCloud遭遇Load balancer does not have available server for client

java.lang.RuntimeException: 
com.netflix.client.ClientException: 
Load balancer does not have available server for client: answer

场景:我将Eureka服务放于服务器上,两个客户端使用本机启动,一顿瞎操作后,使用 Feign 调用 answer 服务上接口,报以上错误

当时application.properties配置如下

server.port=8022
eureka.instance.hostname=damionew.top
spring.application.name=answer
##这两个 true 确实不可缺失
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.serviceUrl.defaultZone=https://${eureka.instance.hostname}/eureka/eureka
spring.profiles.active=test

解决方式就是将hostname改为当前ip

eureka.instance.hostname=localhost

排除1:首先本地是已经启动了 answer 这个服务的,同时在 eureka 页面列表上可以查看到,说明服务代码没问题

排除2:将 maven 中 springboot 依赖全部替换一遍,报错依旧,说明与依赖版本无关

猜测1:是否 Feign 代码问题,例如启动类未加注解等

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
@EnableDiscoveryClient
public class AnswerApplication {

    public static void main(String[] args) {
        SpringApplication.run(AnswerApplication.class, args);
    }

}

对应处理接口的请求使用浏览器主动调用,畅通。

猜测2:调用 answer 服务的接口有问题

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(value = "answer")
public interface AnswerFeign {
	
	@RequestMapping(value = "/answerSayHello",method=RequestMethod.GET)
	public String answerFeignTest();
}

与以前的代码比较,没有差异

猜测3:application.properties 中配置问题

网上有说加两个配置就解决的,使用后还是无效,但是未删除

eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true

瞥一眼 hostname ,猜测 hostname 未必一定用于 defaultzone ,将 hostname 改为本实例IP即可

所以猜测 之前的报错是因为 调用接口方式为:https://damionew.top:8022/feignTest

这样当然是没有的,正确的应该是 http://localhost:8022/feignTest

原创文章 88 获赞 41 访问量 16万+

猜你喜欢

转载自blog.csdn.net/Damionew/article/details/90678804
今日推荐