feign是啥?

很多人可能对于feign 不是很熟悉,可以看一下其他网友的实例分享:spring cloud feign简介

如果觉得上面这个比较难的话,还有一个简单入门的:spring cploud feign使用详解

今天的主题是记录遇到的坑,关于feign的资料网友分享已经很详细,我就不班门弄斧啦啦

feign 坑之@RequestParam参数名

这次在做一个中台项目(使用 spring boot),需要和市场上很多同行公司结合的那种,这就涉及到同个服务需要被很多外部服务调用的场景,之前通常都会用代理去实现对其他外部服务的访问,但代理模块也需要写很多代码,对于懒惰的程序员而言,这当然是要想办法简化的(是谁说的,不会偷懒的程序员都不是好程序员来着?)

然后有大神推荐我们使用了feign,Feign 是 Spring Cloud的组件,这里就需要讨论spring boot 和spring cloud 整合的问题了,但是不展开,可以移步扩展阅读。

在依赖了 spring cloud之后,外部服务就可以愉快的开始使用 feign来调用我们的接口了

@FeignClient(name = "${test-service.application.name}", configuration = TestServiceFeignConfig.class)
public interface TestServiceInterfaceClient extends TestServiceInterfaceApi{
}
TestServiceInterfaceApi 是我们中台内部实现的接口,接口定义如下:
/**
* 版权所有(C),xxxx公司,2019,所有权利保留。
*
* 项目名: test-client-api
* 文件名: TestServiceApi.java
* 模块说明:
* 修改历史:
* 2019年6月20日 -ln- 创建。
*/
package com.xxxx.test.client.api.test; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*; import java.util.List; /**
* xxxxx服务
*
* @author ln
*
*/
@Api(tags = "xxxxx服务")
@RequestMapping(value = "service/test",
produces = "application/json;charset=utf-8")
public interface TestServiceInterfaceApi { /**
* 查询
*
* @param id1
* id1
* @param filter
* 查询条件
* @return 查询结果
*/
@ApiOperation(value = "查询")
@RequestMapping(value = "/query", method = RequestMethod.POST)
@ResponseBody
Response<List<TestEntity>> query( //
@ApiParam(required = true) @PathVariable("id1") String id1,
@ApiParam(required = true) @RequestBody TestFilter filter); /**
* 批量移除
*
* @param id1
* id1
* @param modifition
* 处理内容
* @return 处理结果
*/
@ApiOperation(value = " 批量移除")
@RequestMapping(value = "/batchremove", method = RequestMethod.POST)
@ResponseBody
Response batchRemove( //
@ApiParam(required = true) @PathVariable("id1") String id1,
@ApiParam(required = true) @RequestBody TestBatchRemove batchRemove,
@ApiParam(required = true) @RequestParam String operator);
}

这时候,在外部系统服务里 @Autowired 一下 TestServiceInterfaceClient 就可以调用到我们的接口了,按道理讲这个流程应该顺畅无比,毕竟定义 feignClient 才这么几行代码,不可能出错,but 程序员不可能是不写bug 的~~

外部服务启动时,报错:

这个报错是说我们中台服务的某个接口的第2 个参数为空,但是我们本地的服务是正常启用的,内部调用无比顺畅,那,问题在哪?

经过排查,发现是我们接口定义用到了 @RequestParam 注解,但是Feign 对 @RequestParam 声明的参数未识别到,加上参数的名字之后,一切OK

/**
* 批量移除
*
* @param id1
* id1
* @param modifition
* 处理内容
* @return 处理结果
*/
@ApiOperation(value = " 批量移除")
@RequestMapping(value = "/batchremove", method = RequestMethod.POST)
@ResponseBody
Response batchRemove( //
@ApiParam(required = true) @PathVariable("id1") String id1,
@ApiParam(required = true) @RequestBody TestBatchRemove batchRemove,
@ApiParam(required = true) @RequestParam("operator") String operator);
}

后面找了找,发现前面也有很多人遇到了相似的问题,还有网友系统的整理了坑,具体可异步扩展阅读。

虽然这是一个很小的点,小菜依旧很开心(毕竟不用背锅啊)。

我们的中台功能在不断地发展全面,so这篇帖子将会不断更新....

扩展阅读

spring cloud feign 坑的更多相关文章

  1. RestTemplate OR Spring Cloud Feign 上传文件

    SpringBoot,通过RestTemplate 或者 Spring Cloud Feign,上传文件(支持多文件上传),服务端接口是MultipartFile接收. 将文件的字节流,放入ByteA ...

  2. Spring cloud Feign 深度学习与应用

    简介 Spring Cloud Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单.Feign提供了HTTP请求的模板,通过编写简单的接口和插入注解 ...

  3. Spring Cloud Feign 如何使用对象参数

    概述 Spring Cloud Feign 用于微服务的封装,通过接口代理的实现方式让微服务调用变得简单,让微服务的使用上如同本地服务.但是它在传参方面不是很完美.在使用 Feign 代理 GET 请 ...

  4. 笔记:Spring Cloud Feign Ribbon 配置

    由于 Spring Cloud Feign 的客户端负载均衡是通过 Spring Cloud Ribbon 实现的,所以我们可以直接通过配置 Ribbon 的客户端的方式来自定义各个服务客户端调用的参 ...

  5. 笔记:Spring Cloud Feign Hystrix 配置

    在 Spring Cloud Feign 中,除了引入了用户客户端负载均衡的 Spring Cloud Ribbon 之外,还引入了服务保护与容错的工具 Hystrix,默认情况下,Spring Cl ...

  6. 笔记:Spring Cloud Feign 其他配置

    请求压缩 Spring Cloud Feign 支持对请求与响应进行GZIP压缩,以减少通信过程中的性能损耗,我们只需要通过下面二个参数设置,就能开启请求与响应的压缩功能,yml配置格式如下: fei ...

  7. 笔记:Spring Cloud Feign 声明式服务调用

    在实际开发中,对于服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以我们通常会针对各个微服务自行封装一些客户端类来包装这些依赖服务的调用,Spring Cloud Feign 在此基础上做了进 ...

  8. 第六章:声明式服务调用:Spring Cloud Feign

    Spring Cloud Feign 是基于 Netflix Feign 实现的,整合了 Spring Cloud Ribbon 和 Spring Cloud Hystrix,除了提供这两者的强大功能 ...

  9. Spring Cloud Feign Ribbon 配置

    由于 Spring Cloud Feign 的客户端负载均衡是通过 Spring Cloud Ribbon 实现的,所以我们可以直接通过配置 Ribbon 的客户端的方式来自定义各个服务客户端调用的参 ...

随机推荐

  1. bzoj 3534: [Sdoi2014]重建【矩阵树定理】

    啊啊啊无脑背过果然不可取 比如这道题就不会写 参考:https://blog.csdn.net/iamzky/article/details/41317333 #include<iostream ...

  2. VBScript+SCR+NetApi+Accoreconsole 批处理dwg文件

    继上次powershell运行accoreconsole(https://www.cnblogs.com/NanShengBlogs/p/10981687.html)的研究之后又觉得不是很合适,毕竟p ...

  3. java entity

    对java实体类的众多理解: A .就是属性类,通常定义在model层里面 B. 一般的实体类对应一个数据表,其中的属性对应数据表中的字段.好处:1.对对象实体的封装,体现OO思想.2.属性可以对字段 ...

  4. 构造+暴力 Codeforces Round #283 (Div. 2) B. Secret Combination

    题目传送门 /* 构造+暴力:按照题目意思,只要10次加1就变回原来的数字,暴力枚举所有数字,string大法好! */ /************************************** ...

  5. win7任务计划提示”该任务映像已损坏或已篡改“怎么处理

    https://jingyan.baidu.com/article/e75057f2038e2febc91a8915.html 在命令行窗口(cmd)执行命令:schtasks /query /v   ...

  6. Windows 7上安装Microsoft Loopback Adapter(微软环回网卡)

    Oracle 安装过程中,先决条件检查遇到如下错误: 正在检查网络配置要求...  检查完成.此次检查的总体结果为: 失败 <<<<  问题: 安装检测到系统的主 IP 地址是 ...

  7. 274 H-Index H指数

    给定一位研究者的论文被引用次数的数组(被引用次数是非负整数).写一个方法计算出研究者的H指数.H-index定义: “一位科学家有指数 h 是指他(她)的 N 篇论文中至多有 h 篇论文,分别被引用了 ...

  8. C# 代码笔记_文件

           string Route = @"D:\ksy\ksy\WebSite1\";//文件地址         string File_name = "user ...

  9. springboot与dubbo整合遇到的坑

    整合环境: dubbo 2.6.2 springboot 2.1.5 遇到的问题:服务一直无法注册到zookeeper注册中心 项目结构: 使用application.properties文件: 配置 ...

  10. LN : leetcode 5 Longest Palindromic Substring

    lc 5 Longest Palindromic Substring 5 Longest Palindromic Substring Given a string s, find the longes ...