spring cloud feign 坑
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 坑的更多相关文章
- RestTemplate OR Spring Cloud Feign 上传文件
SpringBoot,通过RestTemplate 或者 Spring Cloud Feign,上传文件(支持多文件上传),服务端接口是MultipartFile接收. 将文件的字节流,放入ByteA ...
- Spring cloud Feign 深度学习与应用
简介 Spring Cloud Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单.Feign提供了HTTP请求的模板,通过编写简单的接口和插入注解 ...
- Spring Cloud Feign 如何使用对象参数
概述 Spring Cloud Feign 用于微服务的封装,通过接口代理的实现方式让微服务调用变得简单,让微服务的使用上如同本地服务.但是它在传参方面不是很完美.在使用 Feign 代理 GET 请 ...
- 笔记:Spring Cloud Feign Ribbon 配置
由于 Spring Cloud Feign 的客户端负载均衡是通过 Spring Cloud Ribbon 实现的,所以我们可以直接通过配置 Ribbon 的客户端的方式来自定义各个服务客户端调用的参 ...
- 笔记:Spring Cloud Feign Hystrix 配置
在 Spring Cloud Feign 中,除了引入了用户客户端负载均衡的 Spring Cloud Ribbon 之外,还引入了服务保护与容错的工具 Hystrix,默认情况下,Spring Cl ...
- 笔记:Spring Cloud Feign 其他配置
请求压缩 Spring Cloud Feign 支持对请求与响应进行GZIP压缩,以减少通信过程中的性能损耗,我们只需要通过下面二个参数设置,就能开启请求与响应的压缩功能,yml配置格式如下: fei ...
- 笔记:Spring Cloud Feign 声明式服务调用
在实际开发中,对于服务依赖的调用可能不止一处,往往一个接口会被多处调用,所以我们通常会针对各个微服务自行封装一些客户端类来包装这些依赖服务的调用,Spring Cloud Feign 在此基础上做了进 ...
- 第六章:声明式服务调用:Spring Cloud Feign
Spring Cloud Feign 是基于 Netflix Feign 实现的,整合了 Spring Cloud Ribbon 和 Spring Cloud Hystrix,除了提供这两者的强大功能 ...
- Spring Cloud Feign Ribbon 配置
由于 Spring Cloud Feign 的客户端负载均衡是通过 Spring Cloud Ribbon 实现的,所以我们可以直接通过配置 Ribbon 的客户端的方式来自定义各个服务客户端调用的参 ...
随机推荐
- 33. Extjs中的tree节点的操作
转自:https://blog.csdn.net/masterShaw/article/details/51354351?utm_source=blogkpcl9 ext 树节点操作 tree ...
- Flask-SQLAlchemy - 不使用外键连表查询。记得常回来看我
前言 相比于 Django 的 ORM ,SQLAlchemy "不依靠外键进行跨表联查" 的解决方案就比较多. 没啥好说的,只能怪自己学艺不精.. _(:з」∠)_ 解决办法 ...
- bzoj 1630: [Usaco2007 Demo]Ant Counting【dp】
满脑子组合数学,根本没想到dp 设f[i][j]为前i只蚂蚁,选出j只的方案数,初始状态为f[0][0]=1 转移为 \[ f[i][j]=\sum_{k=0}^{a[i]}f[i-1][j-k] \ ...
- bzoj 3942: [Usaco2015 Feb]Censoring【kmp+栈】
好久没写kmp都不会写了-- 开两个栈,s存当前串,c存匹配位置 用t串在栈s上匹配,栈每次入栈一个原串字符,用t串匹配一下,如果栈s末尾匹配了t则弹栈 #include<iostream> ...
- redis在linux的安装和开机启动(二)
编译 安装 makefile已经存在 执行make 即可 make之后, 自动创建可运行的脚本文件, 不需要再执行 install了. 将脚本文件, 拷贝到指定位置, 就可以了. 手动创建目录, 需要 ...
- 整数类型c++
数据类型 定义标识符 占字节数 数值范围 数值范围 短整型 short [int] 2(16位) -32768-32767 -215-215-1 整型 [long] int 4(32位) -21474 ...
- BFS HDOJ 1728 逃离迷宫
题目传送门 /* BFS:三维BFS,加上方向.用dp[x][y][d]记录当前需要的最少转向数 */ #include <cstdio> #include <algorithm&g ...
- ASP.NET MVC 生成验证码
using System.Web.Mvc; using System.Drawing; using System; using System.Drawing.Imaging; using Models ...
- C. Coin Troubles 有依赖的背包 + 完全背包变形
http://codeforces.com/problemset/problem/283/C 一开始的时候,看着样例不懂,为什么5 * a1 + a3不行呢?也是17啊 原来是,题目要求硬币数目a3 ...
- sql 所有数据表中 插入字段
declare @tablename varchar(200)declare @sql varchar(2000)declare cur_t cursor forselect name from sy ...