Zuul feign micro-service file upload

Source: Internet
Author: User
Tags openid

Feign pass File
    • Demand
    1. File Micro Service is responsible for managing files, the specific file upload download logic is in this module.
    2. OPENAPI is responsible for providing app users with an interface for Avatar uploads to implement specific business logic.
    3. Zuul is the gateway that is responsible for routing forwarding. The user accesses the gateway directly.

Avatar file = = "zuul==" openapi== "file Micro Service

    • Add a reference Package
        <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>
    • Add Configuration Class
@Configurationpublic 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();    }}

Exception Handling Classes

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);    }}

Annotate @import (Feignconfiguration.class) on the Startup class, import the configuration class

@Import(FeignConfiguration.class)

This allows for file delivery in 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. The name in the Requestpart is identical to the upload, otherwise the call fails.
    2. When NULL is passed in feign @requestheader, it is automatically converted to {filelength} and needs to be handled manually.
Zuul

After the micro-service uploads the file successfully, through the Zuul gateway uploads the problem, the file cannot pass to the micro-service.

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

Workaround:
Add/zuul (/zuul/v1/user/avatar) before calling the address, bypassing spring MVC and using the Zuul servlet to upload the file.

Reference: https://my.oschina.net/kmnztech/blog/1618636

or set the Servlet-path property in the application.properties of the gateway

zuul.servlet-path=/

Official documents:

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

The Zuul ServletZuul is implemented as a Servlet. For the general cases, Zuul is embeddedinto the Spring Dispatch mechanism. This allows Spring MVC to be in controlof the routing. In this case, Zuul is configured to buffer requests. Ifthere is a need to go through Zuul without buffering requests (e.g. forlarge file uploads), the Servlet is also installed outside of the SpringDispatcher. By default, this is located at /zuul. This path can be changedwith the zuul.servlet-path property.

Zuul feign micro-service file upload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.