今天在实现feign远程调用文件上传服务的时候踩了一些坑,特此记录下
第一个坑 多参数传递问题
Method has too many Body parameters: public abstract 。。。。
当使用Feign时,如果发送的是get请求,那么需要在请求参数前加上@RequestParam注解修饰,Controller里面可以不加该注解修饰。
post 请求
feign中你可以有多个@RequestParam,但只能有不超过一个@RequestBody。
正确写法如下:
public int save(@RequestBody final Person p,@RequestParam("userId") String userId,@RequestParam("userTel") String userTel);
由于远程参数都是 request,response 于是我直接把参数去掉,只保留一个 参数
第二个坑 Feign 默认不支持调用文件上传接口,需要自行配置来满足feign的调用方式
Feign 默认不支持调用文件上传接口,需要自行配置来满足feign的调用方式
需要在项目中增加如下依赖:
io.github.openfeign.form
feign-form-spring
3.2.2
io.github.openfeign.form
feign-form
3.2.2
然后提供调用接口:
@FeignClient(name = "upload-file-service", fallback = FileUploadServiceFallback.class, configuration = FileUploadService.UserFeignConfig.class)
public interface FileUploadService {
@RequestMapping(value = "/fdfs/upload", method = RequestMethod.POST,consumes=MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseBody
@CrossOrigin
public Map upload(@RequestPart(value = "file", required = false) MultipartFile file) throws Exception;
class UserFeignConfig {
@Bean
public Encoder multipartFormEncoder() {
return new SpringFormEncoder();
}
@Bean
public Logger.Level logger() {
return Logger.Level.FULL;
}
}
}
文件上传需要使用 RequestPart 注解(不然报错feign the request was rejected because no multipart boundary was found)
配置类需要指定 Encoder
服务调用请求超时
本来以为是程序的问题,也没进入 hystrix 断路器,调试后发现有数据返回,经过查找发现,应该是PostMan 等待超时了。
设置PostMan 超时时间如下:
华裳绕指柔, 版权所有丨如未注明 , 均为原创|转载请注明使用 feign 实现远程服务调用的坑!