• OpenFeign


    OpenFeign—服务间的调用

    一.什么是Feign和OpenFeign?

    在使用Feign或者OpenFeign前,服务之间的调用路径在函数内部设置:

    在这里插入图片描述

    能不能像controller调用service一样,通过注入的方式设置呢。Feign和OpenFeign可以实现。

    Feign:是声明式的web service客户端,它让微服务之间的调用变得更简单了,可以帮助我们实现面向接口编程,类似controller调用service。Feign内置了Ribbon,用来做客户端负载均衡,去调用服务注册中心的服务

    OpenFeign:是Spring Cloud在Feign的基础上支持了SpringMVC的注解,如@RequestMapping等等。OpenFeign 没有内置 Ribbon,需要单独对 Ribbon 进行配置

    springboot 2.0 以上基本上使用openfeign,openfeign 如果从框架结构上看就是2019年feign停更后出现版本,也可以说大多数新项目都用openfeign ,2018年以前的项目在使用feign。

    二.OpenFeign怎么使用?

    1.入门案例

    在前面Eureka和Ribbon案例的基础之上

    1.1 添加Jar包
    
        org.springframework.cloud
        spring-cloud-starter-openfeign
    
    
    • 1
    • 2
    • 3
    • 4
    1.2 添加注解

    在这里插入图片描述

    1.3 创建客户端接口

    @FeignClient 指定服务的名称

    @RequestMapping 中的value是设置目标服务的请求路径 method设置请求方法

    在这里插入图片描述

    1.4 使用

    在这里插入图片描述

    1.5 访问

    ​ 访问服务消费者,服务消费者会调用服务提供者中的方法

    ​ http://localhost:8080/goods

    2.如何传递参数?

    2.1参数传递原则

    ​ 1.传递单个参数时,建议使用@PathVariable

    ​ 2.传递多个参数时,建议采用@RequestParam

    ​ 3.传递对象参数时,统一采用json的方式,添加@RequestBody注解。

    ​ 4. 如果传递的参数比较复杂时,默认会采用post的请求方式

    2.2 在服务提供方添加带参数的方法
    //创建接口,当只有单个参数传递时,建议使用@PathVariable
    @GetMapping("/goods/{id}")
    public ResponseResult searchGoodsById(@PathVariable Integer id)
    {
        Goods goods=new Goods(id,"手机",100*id);
    
        ResponseResult result= Response.createOkResp("单个参数",goods);
    
        return result;
    }
    
    //创建接口,当有多个参数时,建议用@RequestParam
    @GetMapping("/searchGoodsByParam")
    public ResponseResult searchGoodsByParam(@RequestParam Integer id, @RequestParam String name)
    {
        Goods goods=new Goods(id,name,100*id);
    
        ResponseResult result= Response.createOkResp("多个参数",goods);
    
        return result;
    }
    
    //创建接口,对象参数时,使用@RequestBody
    //如果传递的参数比较复杂时,默认会采用post的请求方式
    @PostMapping("/saveGoods")
    public ResponseResult saveGoods(@RequestBody Goods goods) {
    
        ResponseResult result= Response.createOkResp("对象参数",goods);
    
        return result;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    2.3 在服务消费方创建接口,用来调用服务

    注意:

    接口中不支持GetMapping 和PostMapping要用RequestMapping方式,然后指定RequestMethod为Get
    @PathVariable和@RequestParam中的value不要省

    @RequestMapping(value="/goods/{id}",method = RequestMethod.GET)
    public ResponseResult searchGoodsById(@PathVariable(value = "id") Integer id);
    
    @RequestMapping(value="/searchGoodsByParam",method = RequestMethod.GET)
    public ResponseResult searchGoodsByParam(@RequestParam(value = "id") Integer id, @RequestParam(value = "name") String name);
    
    @RequestMapping(value = "/saveGoods",method = RequestMethod.GET)
    public ResponseResult saveGoods(@RequestBody Goods goods) ;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    2.4 在服务消费方的控制层通过接口调用服务

    Author:呆萌老师 QQ:2398779723 微信:it_daimeng

    在这里插入图片描述

    在这里插入图片描述

    2.5 测试

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

    在这里插入图片描述

    2.6 错误测试
    1.如果传递的参数为对象或其它复杂类型时,默认的请求方式是Post。

    如果我们在服务提供方这里设置为Get,会报错。

    在这里插入图片描述

    在这里插入图片描述

    结论:

    为什么FeignClient发起的GetMapping会报错,是因为FeignClient最后是用HttpURLConnectiion发起的网络连接,在发起的过程中,Connection会判断其自身的body是否为空,如果不为空,则将 GET Method 转换为 POST Method。

    按照上面的GET会转POST的理论,所以我们FeignClient调用端写的是GetMapping,参数不贴注解,只要服务端的生产者是PSOT请求加@RequestBody接收,那么就能正确接收并响应数据。

    2.FeignClient接口中的@PathVariable和@RequestParam 中的value不能省,否则会报语法错误。

    在这里插入图片描述

    但服务提供者和服务消费者对应的控制层方法中可以省

    在这里插入图片描述

  • 相关阅读:
    1747. 应该被禁止的 Leetflex 账户
    并发编程模型
    【QT开发(10)】QT 进程
    nodejs中的错误类型及捕获处理
    Tlsr8258开发-b85m_module编译无法通过
    React Native自学笔记
    GCC Arm 12.2编译提示 LOAD segment with RWX permissions 警告
    目标检测:Anchor-free算法模型
    计算机丢失dll文件如何处理?教你快速处理的方法
    【基础教程】基于Matlab实现多种算法的曲线拟合
  • 原文地址:https://blog.csdn.net/daimenglaoshi/article/details/127837049