zuul feign微服务间文件上传

feign传文件

  • 需求
  1. 文件微服务负责管理文件,具体的文件上传下载逻辑都在此模块。
  2. openAPI负责向app用户提供头像上传的接口,实现具体业务逻辑。
  3. zuul是网关,负责路由转发。用户直接访问网关。

头像文件==》zuul==》openAPI==》文件微服务

  • 增加引用包
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form-spring</artifactId>
            <version>2.1.0</version>
        </dependency>
  • 增加配置类
@Configuration
public class FeignConfiguration {
    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;
    @Bean
    public Retryer retryer(){
        return new Retryer.Default(1000,10000,3);
    }

    @Bean
    Request.Options feignOptions() {
        return new Request.Options(5 * 1000, 10 * 1000);
    }

    // 主要是这个地方
    @Bean
    @Primary
    @Scope("prototype")
    public Encoder multipartFormEncoder() {
        return new SpringFormEncoder(new SpringEncoder(messageConverters));
    }

    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }

    @Bean
    public ErrorDecoder errorDecoder(){
        return new FeignErrorDecoder();
    }

}

异常处理类

public class FeignErrorDecoder implements feign.codec.ErrorDecoder {
    private static final Logger logger = LoggerFactory.getLogger(FeignErrorDecoder.class);

    @Override
    public Exception decode(String methodKey, Response response) {

        if(response.status() >= 400 && response.status() <= 499){
            return new HystrixBadRequestException("微服务之间调用失败!");
        }

        return feign.FeignException.errorStatus(methodKey, response);
    }

}

在启动类上加注解@Import(FeignConfiguration.class),导入配置类

@Import(FeignConfiguration.class)

至此可实现feign中文件传递

feignclient

 @ApiOperation(value = "用户修改头像", notes = "用户修改头像")
 @RequestMapping(value = "/v1/user/avatar", method = RequestMethod.PUT)
 public CommonResponse editAvatar(@RequestHeader(value = "openId") String openId,
                                     @RequestPart(value = "file") MultipartFile avatarFile);
  1. RequestPart中的name要和上传时一致,否则调用失败。
  2. feign中@RequestHeader传递null时,会自动转为{fileLength},需要手动处理。

zuul

微服务上传文件成功后,通过zuul网关上传又出现问题,文件传不到微服务中。

org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part ‘file‘ is not present

解决方法:

在调用地址前加上/zuul(/zuul/v1/user/avatar),绕过spring mvc,使用zuul servlet去上传文件。

参考:https://my.oschina.net/kmnztech/blog/1618636

或者在网关的application.properties中设置servlet-path属性

zuul.servlet-path=/

官方文档:

http://cloud.spring.io/spring-cloud-static/Dalston.SR1/#_uploading_files_through_zuul

The Zuul Servlet
Zuul is implemented as a Servlet. For the general cases, Zuul is embedded
into the Spring Dispatch mechanism. This allows Spring MVC to be in control
of the routing. In this case, Zuul is configured to buffer requests. If
there is a need to go through Zuul without buffering requests (e.g. for
large file uploads), the Servlet is also installed outside of the Spring
Dispatcher. By default, this is located at /zuul. This path can be changed
with the zuul.servlet-path property.

原文地址:https://www.cnblogs.com/iiot/p/9567255.html

时间: 2024-07-30 03:51:57

zuul feign微服务间文件上传的相关文章

ASIHTTPRequest系列(三):文件上传

五.文件上传 1.服务端 文件上传需要服务端的配合.我们可在本机搭建tomcat测试环境.关于tomcat在MacOSX下的安装配置,参考作者另一博文<安装Tomcat到Mac OSX>. 打开Eclipse,新建web工程.在其中新建一个ServletUploadServlet: import java.io.*; import java.util.*; importjavax.servlet.ServletException; importjavax.servlet.http.HttpSe

SpringMVC高速实现文件上传功能

SpringMVC为我们封装了上传文件的功能,如今就试用一下 须要的jar包 我用的是Maven项目,就不须要到处下载Jar包了 SpringMVC的搭建 首先要在applicationContext配置文件中注冊一下文件上传的服务 如 <!-- 文件上传配置--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartRes

记录Tomcat8.5文件上传,文件权限无法访问

部署一个服务,文件上传本地可以,但是在Linux上通过docker发布到容器上,文件上传后,没有权限访问,查了好久才发现是Tomcat8.5的问题,低版本没有这个问题,现记录下. tomcat/bin/catalina.sh 打开这个文件找到下面的这段话: # Set UMASK unless it has been overridden if [ -z "$UMASK" ]; then UMASK="0027" fi umask $UMASK 修改为: # Set

Spring Cloud下使用Feign Form实现微服务之间的文件上传

背景 ? Spring Cloud现在已经被越来越多的公司采用了,微服务架构比传统意义上的单服务架构从复杂度上多了很多,出现了很多复杂的场景.比如,我们的产品是个app,支持第三方登录功能,在手机端调用第三方授权接口之后,返回了用户的相关信息,比如open_id,性别,头像等.这些信息我们需要保存在我们服务器上,当时针对头像是应该保存图片的url还是图片本身发生了歧义,在一番讨论之后,得出的结果是,我们需要通过url将图片下载到我们本地,然后调用我们自己的文件微服务中上传功能保存起来. 工具 I

SpringCloud+Feign环境下文件上传与form-data同时存在的解决办法

最近项目转型使用SpringCloud框架下的微服务架构,各微服务之间使用Feign进行调用.期间,发现若被调用方法涉及到文件上传且仅存在单个文件时,一切正常,代码片段如下: 1 @RequestMapping(value = "/if/****/add", method = RequestMethod.POST,consumes = MediaType.MULTIPART_FORM_DATA_VALUE) 2 JSONObject add(@RequestPart(value = &

spring cloud —— feign文件上传

前言 最近项目中需要用feign调用cloud微服务实现文件上传,但是FeignClient调用接口时不支持上传文件: 本项目采用Feign-form扩展库进行实现文件上传,期间也遇到了很多问题,在这里就和大家分享一下遇到的问题和解决办法. 以下是我感觉比较实用的一些博客,我虽然没有全部遇到博主们说的那些问题,但是相信这些会对大家有所帮助. 参考 http://www.mamicode.com/info-detail-2311867.html https://blog.csdn.net/ytzz

Springboot Feign文件上传

服务提供者 @SpringBootApplication public class FeignUploadFirstApplication { @RestController public class UploadController { @RequestMapping(value = "/uploadFile",method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public Str

文件上传更新服务相关

需求: 客户端向服务端发送一次请求,请求是一系列配置文件的文件名.当前客户端所持有文件的版本号.期待服务端返回的数据形式(url或二进制数据).当请求的配置文件有更新,服务端返回相应的url或二进制数据.之所以要分url和二进制数据,因为有的配置文件比较小且比较重要需要马上获取到数据:而有的配置文件稍微大一些,重要性比较低,可以在客户端拉线程慢慢下. 最终实现: 分为后台管理系统(负责文件上传)和数据更新服务. 文件上传部分是用Vue.js+KOA实现.其中关于文件上传那块,浏览器端使用Ajax

基于 lua-resty-upload 实现简单的文件上传服务

今天了解了一下 lua-resty-upload 模块,并基于 lua-resty-upload 模块简单实现了一个基本的表单文件上传服务. lua-resty-upload 在 github 上的项目地址为: https://github.com/openresty/lua-resty-upload 从实现可以看到,其实 upload 服务的实现还是比较简单的,就一个源文件 lualib/resty/upload.lua,总的代码行数也只有 300 行不到. 下面我整理了一下搭建文件上传服务的