Feign实现微服务间文件上传

在Spring Cloud 的Feign组件中并不支持文件的传输,会出现这样的错误提示:

feign.codec.EncodeException: class [Lorg.springframework.web.multipart.MultipartFile; is not a type supported by this encoder.
    at feign.codec.Encoder$Default.encode(Encoder.java:90) ~[feign-core-9.5.1.jar:na]
    at feign.form.FormEncoder.encode(FormEncoder.java:87) ~[feign-form-3.3.0.jar:3.3.0]
    at feign.form.spring.SpringFormEncoder.encode(SpringFormEncoder.java:64) ~[feign-form-spring-3.3.0.jar:3.3.0]

但是我们可以通过使用Feign的扩展包实现这个功能。

 

一. 示例介绍

服务名 端口号 角色
feign_upload_first 8100 feign服务提供者
feign_upload_second 8101 feign服务消费者

 

 

 

 

我们调用feign_upload_second的上传文件接口上传文件,feign_upload_second内部使用feign调用feign_upload_first实现文件上传。

二 、单文件上传

2.1 feign_upload_first服务提供者

文件上传的服务提供者接口比较简单,如下所示:


@SpringBootApplication
public class FeignUploadFirstApplication {
  @RestController
  public class UploadController {
    
    @RequestMapping(value = "/uploadFile",method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String handleFileUpload(@RequestPart(value = "file") MultipartFile file) {
      return file.getOriginalFilename();
    }
  }
  public static void main(String[] args) {
    SpringApplication.run(FeignUploadFirstApplication.class, args);
  }
}

2.2 feign_upload_second服务消费者

  1. 增加扩展包依赖,依赖包不能太高级,会报错
        <dependency>
          <groupId>io.github.openfeign.form</groupId>
          <artifactId>feign-form</artifactId>
          <version>3.3.0</version>
        </dependency>
        <dependency>
          <groupId>io.github.openfeign.form</groupId>
          <artifactId>feign-form-spring</artifactId>
          <version>3.3.0</version>
        </dependency>
        <dependency>
          <groupId>commons-fileupload</groupId>
          <artifactId>commons-fileupload</artifactId>
          <version>1.3.3</version>
        </dependency>
  2. 新增feign实现文件上传的配置类
    import feign.codec.Encoder;
    import feign.form.spring.SpringFormEncoder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class FeignSupportConfig {
      @Bean
      public Encoder feignFormEncoder() {
        return new SpringFormEncoder();
      }
    }
  3. feign远程调用接口
    @FeignClient(name = "file",url = "http://localhost:8100",configuration = FeignSupportConfig.class)
    public interface UploadService {
      @RequestMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
      String handleFileUpload(@RequestPart(value = "file") MultipartFile file);
    }
  4. 上传文件接口
    @RestController
    public class UploadController {
      @Autowired
      UploadService uploadService;
      
      @RequestMapping(value = "/uploadFile",method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
      public String handleFileUpload(@RequestPart(value = "file") MultipartFile file) {
        return uploadService.handleFileUpload(file);
      }
    }

    2.3 测试

    使用postman进行测试,可以正常上传文件

Feign实现微服务间文件上传

上一篇:SQLServer2016 AlwaysOn搭建实践含过程截图


下一篇:yum安装PHP7.2和rpm如何一次性删除包