Feign pass File
- File Micro Service is responsible for managing files, the specific file upload download logic is in this module.
- OPENAPI is responsible for providing app users with an interface for Avatar uploads to implement specific business logic.
- Zuul is the gateway that is responsible for routing forwarding. The user accesses the gateway directly.
Avatar file = = "zuul==" openapi== "file Micro Service
<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>
@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);
- The name in the Requestpart is identical to the upload, otherwise the call fails.
- 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