Feign客户端无法与尤里卡服务器内部的方法通信



我配置了一个eureka服务器,在该服务器中我编写了一个rest api。现在我有了一个eureka客户端服务,我正试图使用来自客户端服务的foreign调用eureka服务的方法之一。但我得到了一个错误"负载均衡器没有可用的服务器为客户端:尤里卡服务"。

但如果我使用foreign将api从一个客户端服务调用到另一个客户端,那么它会给出成功的结果。只是无法从eureka服务调用API。

尤里卡服务是我的尤里卡服务器的应用程序名称。

@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
@RestController
@RequestMapping("test")
public class TestController {
@GetMapping
public String test(){
return "test success";
}
}

尤里卡服务的bootstrap.yml

eureka:
client:
registerWithEureka: false
fetchRegistry: false 
eureka-server-read-timeout-seconds: 60
eureka-server-connect-timeout-seconds: 60
serviceUrl:
defaultZone: http://localhost:8763/eureka/ 
dashboard:
enabled: true
spring:
application:
name: eureka-service

客户服务是:

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}

@FeignClient("eureka-service")
public interface TestFeign {
@GetMapping("test")
String test();
}

客户端服务的bootstrap.yml

eureka:
client:
registerWithEureka: true 
fetchRegistry: true
eureka-server-read-timeout-seconds: 60
eureka-server-connect-timeout-seconds: 60
serviceUrl:
defaultZone: http://localhost:8763/eureka/ 
spring:
application:
name: client-service
feign:
hystrix:
enabled: true

错误日志:路径为[]的上下文中Servlet[dispatcherServlet]的Servlet.service((引发异常[请求处理失败;嵌套异常为com.netflix.hystrix.exception.HystrixRuntimeException:TestFeign#test((失败,没有可用的回退。]com.netflix.client.ClientException:负载平衡器没有可用于客户端的服务器:eureka服务。

我们怎样才能解决这个问题。感谢您提前提供帮助。

您需要扫描在主类中使用@EnableFeignClients注释声明为FeignClients的接口,并添加feign.hystrix.enabled=true属性。

最新更新