美文网首页
【Spring Cloud 系列 四】Feign 实现声明式 R

【Spring Cloud 系列 四】Feign 实现声明式 R

作者: 司鑫 | 来源:发表于2018-09-01 01:53 被阅读50次

    一 Feign 简介


    Feign 是 Netflix 开发的声明式、模版话的 HTTP 客户端,可以帮助我们更加便捷、优雅的调用 HTTP API。
    在 Spring Cloud 中,使用 Feign 会非常简单,只需要创建一个接口,并加上相应的注解即可。Spring Cloud 对 Feign 进行了增强,使其支持了 Spring MVC 注解,并整合了 Ribbon 和 Eureka,从而可以更加方便的使用。
    没用 Feign 时我们一般使用 RestTemplate 实现 REST API 调用:

    
    @RestController
    public class MovieController{
      @Autowired
      private RestTemplate restTemplate;
    
      public User findById (Long id){
       return this.restTemplate.getForObject("http://microservice-provider-user/"+id, User.class); 
       }
    }  
    

    在使用了 Feign 后代码会变得更加简介明了

    @FeignClient(name="microservice-provider-user")
    public interface UserFeignClient{
      @GetMapping("{id}")
      public User findById(@PathVariable Long id);
    }
    
    @RestController
    public class MovieController{
      @Autowired
      private UserFeignClient userFeignClient;
    
      @GetMapping("users/{id}")
      public User findById(@PathVariable Long id){
        return this.userFeignClient.findById(id);
      }
    }
    

    二 为消费者整合 Feign


    1 为消费者服务添加依赖
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-openfeign', version:'1.4.0.RELEASE'
    
    2 创建一个 Feign 接口,并添加 @FeignClient 注解
    @FeignClient(name="microservice-provider-user")
    public interface UserFeignClient{
      @GetMapping("{id}")
      public User findById(@PathVariable Long id);
    }
    

    其中 @FeignClient 中的值是一个客户端名称,

    • 如果我们使用了 Eureka,那么会在服务注册表中查询与客户端名称对应的服务进行调用;
    • 如果没有使用 Eureka ,使用了 Ribbon,那么会根据 Ribbon 的service.ribbon.listOfServers中配置的服务器列表进行匹配。
    • 也可以使用 url 属性来指定请求的 URL@FeignClient(name="microservice-provider-user",url="http://localhost:8080")
    3 在 controller 中调用 microservice-provider-user 服务的获取用户接口
    @RestController
    public class MovieController{
      @Autowired
      private UserFeignClient userFeignClient;
    
      @GetMapping("users/{id}")
      public User findById(@PathVariable Long id){
        return this.userFeignClient.findById(id);
      }
    }
    

    三 构造多参数请求


    1 Get 请求的多参数的 URL
    • 方法一
    @FeignClient(name="microservice-provider0=-user")
     public interface UserFeignClient {
      @GetMapping("/get")
      public User get(@RequestParam("id") Long id, @RequestParam ("username") String username)
    }
    
    • 方法二
    @FeignClient(name="microservice-provider0=-user")
     public interface UserFeignClient {
      @GetMapping("/get")
      public User get(@RequestParam Map map)
    }
    
    2 POST 请求包含多个参数
    Provider
    
    @RestController
     public interface UserController {
      @PostMapping("/post")
      public User post(@RequestBody User user){...}
    }
    
    Consumer
    
    @FeignClient(name="microservice-provider-user")
     public interface UserFeignClient {
      @GetMapping("/post")
      public User post(@RequestBody User user)
    }
    

    相关文章

      网友评论

          本文标题:【Spring Cloud 系列 四】Feign 实现声明式 R

          本文链接:https://www.haomeiwen.com/subject/jucywftx.html