Feign调用传输文件异常的解决

1. Current request is not a multipart request

feign接口参数使用 @RequestPart 而非 @RequestParam, 同时需要指定consumes,比如这样:

@PostMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    Result<FileStorage> upload(@RequestPart(value = "file") MultipartFile file);

2. Feign failed and no fallback

这是hystrix导致,关闭feign熔断,或者延长熔断的超时时间,我简单粗暴的直接关了

3.Read timed out executing POST for “xxx”

配置了hystrix还不行,或者延长ribbon的超时时间,参考了Feign超时问题的办法,简单来说就是feign经过了ribbonn和hystrix两级调用,而且都有一个默认的超时时间,延长超时时间就好了

spring:
  servlet:
    context-path: /farm
  application:
    name: farm
  profiles:
    active: dev
  main:
    allow-bean-definition-overriding: true
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:7001/eureka
  instance:
    prefer-ip-address: true
#关闭feign熔断
feign:
  hystrix:
    enabled: false
#开启熔断,关闭熔断超时或延长调用超时时间
#hystrix:
#  command:
#    default:
#      execution:
#        timeout:
#          enabled: false
#        isolation:
#          thread:
#            timeoutInMilliseconds: 30000
#延长ribbon超时时间
ribbon:
  ReadTimeout: 30000
  ConnectTimeout: 30000

通过Feign上传文件(踩坑)

引入依赖:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

服务提供者:

@RestController
@RequestMapping("/file")
public interface FileUploadService {

    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST, consumes = MULTIPART_FORM_DATA_VALUE)
    CommonResult<String> uploadFile(@RequestPart("file") MultipartFile file,
                                    @RequestParam(value = "containerName", required = false) String containerName
}

具体实现不是重点……根据你的实际情况去完成……

服务调用者:

@RestController
@FeignClient(value = "XXXXXXXX", configuration = FileUploadServiceFeign.ClientConfiguration.class)
@RequestMapping("/file")
public interface FileUploadServiceFeign extends FileUploadService {

    /**
     * 配置类
     */
    class ClientConfiguration {
        /**
         * 此处注入的是: ObjectFactory<HttpMessageConverters>
         */
        @Autowired
        private ObjectFactory<HttpMessageConverters> messageConverters;

        @Bean
        public Encoder feignEncoder() {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
        }
    }
}

这样就行了……

需要注意的是:

在服务调用者那层的MultipartFile的value要跟服务提供者的@RequestPart中的value值一样。不然它会抛出400异常!!!

成功案例:

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 使用Feign实现微服务间文件传输

    在很多时候我们会遇到微服务之间文件传输,很多时候我们可以通过序列化等方式解决(如图片等). 最近项目中有个excel上传,以及多媒体文件上传,直接报错. 也试了2种解决方式,都不可行. 1.写一个文件Encoder解析器,会出现其他的rest请求出现encoder错误 2.springcloud feign有一个规范,不可以传输2个对象,可以是一个对象带几个参数方式. 那么我们现在需要一种方式,不配置全局的解析器,而是通过Feign Builder 去管理上传文件,这种方式管理起来也较为方便.

  • spring cloud feign实现远程调用服务传输文件的方法

    实践案例包括两个项目,服务提供者项目名:upload-service,调用服务项目名:upload-client,主要给出两个服务之间的调用过程,文件上传功能不提供 项目框架:spring-boot 2.0.1.RELEASE.spring-cloud Finchley.RELEASE 依赖: <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form<

  • Spring cloud踩坑记录之使用feignclient远程调用服务404的方法

    前言 公司项目进行微服务改造,由之前的dubbo改用SpringCloud,微服务之间通过FeignClient进行调用,今天在测试的时候,eureka注册中心有相应的服务,但feignclient就是无法调通,一直报404错误,排查过程如下: 一.问题: 服务提供方定义的接口如下: /** * 黑白名单查询接口 * * @author LiJunJun * @since 2018/10/18 */ @Component(value = "blackAndWhiteListFeignClient

  • SpringCloud Feign如何在远程调用中传输文件

    1. 文件远程传输主要涉及3点: 请求方式, 媒体类型, 序列化与反序列化, 把握住了这3点,基本上就可以搞 2. 使用Feign传输,首先搭建起Feign的架子 2.1 引入spring-cloud-starter-eureka-server依赖,用于启动一个eureka注册中心 2.2 引入spring-cloud-starter-eureka依赖,用于开启向eureka注册中心注册自己 2.3 在调用远程服务的客户端引入spring-cloud-starter-feign, 用于使用fei

  • Spring Cloud Feign文件传输的示例代码

    一.配置文件解析器 服务提供者和消费者都需要配置文件解析器,这里使用 commons-fileupload 替换原有的解析器: 依赖: <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> 注入 be

  • Feign调用传输文件异常的解决

    1. Current request is not a multipart request feign接口参数使用 @RequestPart 而非 @RequestParam, 同时需要指定consumes,比如这样: @PostMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA_VALUE) Result<FileStorage> upload(@RequestPart(value = &q

  • 解析Silverlight调用WCF/Rest异常的解决方法

    新建Rest服务接口: 复制代码 代码如下: [ServiceContract]public interface IService1{    [OperationContract]    string GetData(int value);} 接着新建一个服务实现类: 复制代码 代码如下: public class Service1 : IService1{    public string GetData(int value)    {        int i = 0;        int

  • IIS下调用证书出现异常的解决方法 (C#)

    程序发布前,跑在vs上是没问题的,当发布后,程序就报错了.通过系统日志找到了错误所在:证书调用时出现了异常.原因是:在IIS上调用证书是需要配置的,具体配置如下: 一. 确保证书已安装 1. 点击 [开始] -> [运行] -> 键入[mmc] 进入"控制台"界面 -> 选择[文件] -> [添加/删除管理单元] 2. 选择[证书] -> [计算机账户] -> [下一步] -> [完成] 3. 选择[个人] -> [证书] -> [

  • Feign调用接口解决处理内部异常的问题

    问题描述: 当使用feign调用接口,出现400-500-的接口问题时.会出错feign:FeignException.(因为是错误,只能用catch Throwable,不可使用catch Exception捕获异常)导致程序无法继续运行. 问题原因: 由于feign默认的错误处理类是FunFeignFallback会throw new AfsBaseExceptio导致外部无法捕获异常. package com.ruicar.afs.cloud.common.core.feign; impo

  • Android Beam 文件传输失败分析与解决方法

    最近在修改Android7.0原生平台的一些bug,其中有关Android Beam传输文件的一些问题还是蛮多的.所以特地找时间总结下曾经踏过的坑. 1.传输的文件名包含中文时,导致传输失败 可能是由于Google未考虑到本地化差异,导致在传输中文文件名的文件时直接提示传输失败. packages\apps\Nfc\src\com\android\nfc\beam\MimeTypeUtil.java 其实,上面忘了说了,只是从文件管理器中进入Android Beam分享才会出现上面的问题.因为当

  • Feign之Multipartfile文件传输填坑

    Multipartfile文件传输 1. 添加依赖 <dependency> <groupId>io.github.openfeign.form</groupId> <artifactId>feign-form</artifactId> <version>3.3.0</version> </dependency> <dependency> <groupId>io.github.openf

  • 解决Java 部署Tomcat时使用jni和jna调用DLL文件的问题

    我的前一篇博客提到,我们公司项目的restful框架中要用到底层的DLL C++库,最后经过测试验证结果选择采用JNA方法来调DLL库. 之后基础框架在IDEA中的jetty服务中调试成功,但是在部署到Tomcat上时出现了无法加载DLL库的状况,只能现学现卖了. 一. 把DLL文件放在Tomcat下的方法: 在Tomcat中加载dll,供其它接口调用: 在Tomcat目录根下新建一个文件夹,这里就叫DLL吧,<tomcat_home>/DLL: 把需要用到的dll放入新建的DLL目录下: 编

  • 解决微服务feign调用添加token的问题

    微服务feign调用添加token 1.一般情况是这么配置的 具体的怎么调用就不说了 如下配置,就可以在请求头中添加需要的请求头信息. package localdate; import feign.RequestInterceptor; import feign.RequestTemplate; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; imp

  • 微服务之间如何通过feign调用接口上传文件

    具体需求: 我们的项目是基于springboot框架的springcloud微服务搭建的,后端服务技术层面整体上分为business服务和core服务,business服务用于作为应用层,直接连接客户端,通常用于聚合数据,core服务用来客户端具体操作不同需求来控制数据库,文件上传是通过客户端上传接口,通过business服务,由服务端调用feign接口,也是第一次做这种文件中转,遇到各种问题,下面是我自己的解决方案,不喜勿喷,代码小白一枚; 一.core服务层接口@requestmapping

  • 使用 FFmpeg 命令拼接mp3音频文件异常问题及解决方法

    使用FFmpeg命令拼接多个mp3格式的音频文件时报错抛出异常,使用命令格式如下: ffmpeg -i 1.mp3 -i 2.mp3 -filter_complex '[0:0] [1:0] concat=n=2:v=0:a=1 [a]' -map [a] out3.mp3 异常错误提示信息: Automatic encoder selection failed for output stream #0:0. Default encoder for format mp3 (codec mp3)

随机推荐