Posted in Java/Android onJune 24, 2021
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异常!!!
成功案例:
以上为个人经验,希望能给大家一个参考,也希望大家多多支持三水点靠木。
Feign调用传输文件异常的解决
- Author -
老飞pfy声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@