feign 是一个让rest服务调用更简洁的开源项目,很多介绍文章或书也称它为声明式REST调用。传统的web service中,通过引用wsdl来自动生成一些client的代理类(或stub代码),feign跟这个有点类似,但是更灵活。
先回顾一下,上节中service-consumer对服务的调用代码:
1 @GetMapping("/order/{userId}/{orderNo}") 2 public String findOrder(@PathVariable Integer userId, @PathVariable String orderNo) { 3 UserDTO user = restTemplate.getForEntity("http://SERVICE-PROVIDER-DEMO/user/" + userId, UserDTO.class).getBody(); 4 if (user != null) { 5 return user.getUserName() + " 的订单" + orderNo + " 找到啦!"; 6 } 7 8 return "用户不存在!"; 9 }