2016-03-07 155 views
0

我试图在简单的REST客户端测试应用程序中首次使用@FeignClient。我想利用Ribbon来实现两个服务器实例之间的负载均衡,但不使用Eureka。按照文档,我已将application.yml配置为listOfServers属性并禁用了Eureka。我的客户端名称与ribbon属性的YAML前缀名称相同。Spring Cloud:使用Ribbon和Feign客户端(但不是Eureka)丢失服务器实例

application.yml

ds: 
    ribbon: 
    listOfServers: server1:18201,server2:18201 

客户端代码:

@FeignClient("ds") 
public interface DataServicesClient { 
    @RequestMapping(method = RequestMethod.GET, value = "/context-path/customers") 
    List<Customers> getCustomers(); 
} 

当我调用应用程序,我可以看到listOfServers用丝带被拾起:

2016-03-07 12:15:17.275 INFO 39948 --- [nio-8081-exec-1] 
    c.n.l.DynamicServerListLoadBalancer  : DynamicServerListLoadBalancer for client ds 
    initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=ds,current list of 
    Servers=[server1:18201, server2:18201] 

但是客户端然后仅使用的值拨打电话没有服务器前缀的注释,显然失败。

2016-03-07 12:15:21.394 ERROR 39948 --- [nio-8081-exec-1] o.a.c.c.C.[.[.[/]. 
    [dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context 
    with path [] threw exception [Request processing failed; nested exception is 
    feign.RetryableException: Unexpected end of file from server executing GET 
    http://context-path/customers] with root cause 
java.net.SocketException: Unexpected end of file from server 
at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source) 

我期待它反过来(http:{server instance}/context-path/customers)注入每一服务器,所以我明明已经在这里错过了一些东西。

任何人都可以请指出我在正确的方向吗?

感谢,

罗布。

+0

它适合我。你可以分享一个简单的示例项目吗? –

+0

谢谢戴夫。看来我对“注释/自动装配”的要求太高了。我已通过Feign.builder()创建我的客户端来解决此问题,而不是自动装配@FeignClient实例,然后告诉构建者使用RibbonClient,即:'DataServicesClient api = Feign.builder()。contract(new SpringMvcContract())。客户端(新RibbonClient())。目标( DataServicesClient.class,“https:// ds”)' – RobP

+0

好,但你不应该这样做。 '@ EnableFeignClients'将为你做到这一点。 –

回答

1

所以它变成我们的问题是我试图访问一个安全的资源,但没有用我的feign客户端前缀https。

@FeignClient("https://ds") 

我可能有点天真,但我没有找到错误真正解释这个问题太清楚:

java.net.SocketException: Unexpected end of file from server 

我也希望所选择的服务器实例存在于错误消息,但看起来我们只是获取客户名称。这让我相信服务器简单地被省略了,但我想这只是学习曲线的一部分。

无论如何,这个问题现在已经得到解决,它的工作只与@EnableFeignClients注释,没有需要Feign.builder()

感谢支持@Dave Syer

相关问题