我试图在简单的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
)注入每一服务器,所以我明明已经在这里错过了一些东西。
任何人都可以请指出我在正确的方向吗?
感谢,
罗布。
它适合我。你可以分享一个简单的示例项目吗? –
谢谢戴夫。看来我对“注释/自动装配”的要求太高了。我已通过Feign.builder()创建我的客户端来解决此问题,而不是自动装配@FeignClient实例,然后告诉构建者使用RibbonClient,即:'DataServicesClient api = Feign.builder()。contract(new SpringMvcContract())。客户端(新RibbonClient())。目标( DataServicesClient.class,“https:// ds”)' – RobP
好,但你不应该这样做。 '@ EnableFeignClients'将为你做到这一点。 –