1. 依赖
< dependency>
< groupId> org.springframework.cloud</ groupId>
< artifactId> spring-cloud-starter-openfeign</ artifactId>
</ dependency>
< dependency>
< groupId> io.github.openfeign</ groupId>
< artifactId> feign-httpclient</ artifactId>
</ dependency>
< dependency>
< groupId> io.github.openfeign.form</ groupId>
< artifactId> feign-form-spring</ artifactId>
< version> 3.8.0</ version>
</ dependency>
< dependency>
< groupId> io.github.openfeign.form</ groupId>
< artifactId> feign-form</ artifactId>
< version> 3.8.0</ version>
</ dependency>
2 配置文件
import feign. codec. Encoder ;
import feign. form. spring. SpringFormEncoder ;
import org. apache. commons. fileupload. FileItem ;
import org. apache. commons. fileupload. disk. DiskFileItemFactory ;
import org. apache. commons. io. IOUtils ;
import org. springframework. beans. factory. ObjectFactory ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. boot. autoconfigure. http. HttpMessageConverters ;
import org. springframework. cloud. openfeign. support. SpringEncoder ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
import org. springframework. context. annotation. Primary ;
import org. springframework. context. annotation. Scope ;
import org. springframework. http. MediaType ;
import org. springframework. web. multipart. MultipartFile ;
import org. springframework. web. multipart. commons. CommonsMultipartFile ;
import java. io. File ;
import java. io. FileInputStream ;
import java. io. OutputStream ;
@Configuration
public class MultipartSupportConfig {
@Autowired
private ObjectFactory < HttpMessageConverters > messageConverters;
@Bean
@Primary
@Scope ( "prototype" )
public Encoder feignEncoder ( ) {
return new SpringFormEncoder ( new SpringEncoder ( messageConverters) ) ;
}
public static MultipartFile getMultipartFile ( File file) {
FileItem item = new DiskFileItemFactory ( ) . createItem ( "file" , MediaType . MULTIPART_FORM_DATA_VALUE , true , file. getName ( ) ) ;
try ( FileInputStream inputStream = new FileInputStream ( file) ;
OutputStream outputStream = item. getOutputStream ( ) ; ) {
IOUtils . copy ( inputStream, outputStream) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
return new CommonsMultipartFile ( item) ;
}
}
3 fegin 远程调用
import com. king. content. config. MultipartSupportConfig ;
import org. springframework. cloud. openfeign. FeignClient ;
import org. springframework. http. MediaType ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RequestParam ;
import org. springframework. web. bind. annotation. RequestPart ;
import org. springframework. web. multipart. MultipartFile ;
@FeignClient ( value = "media-api" , configuration = { MultipartSupportConfig . class } , fallbackFactory = MediaServiceClientFallbackFactory . class )
public interface MediaServiceClient {
@RequestMapping ( value = "/media/upload/coursefile" , consumes = MediaType . MULTIPART_FORM_DATA_VALUE )
String uploadFile ( @RequestPart ( "filedata" ) MultipartFile upload, @RequestParam ( value = "objectName" , required= false ) String objectName) ;
}
4 熔断降级
package com. xuecheng. content. feignclient ;
import feign. hystrix. FallbackFactory ;
import lombok. extern. slf4j. Slf4j ;
import org. springframework. stereotype. Component ;
import org. springframework. web. multipart. MultipartFile ;
@Component
@Slf4j
public class MediaServiceClientFallbackFactory implements FallbackFactory < MediaServiceClient > {
@Override
public MediaServiceClient create ( Throwable throwable) {
return new MediaServiceClient ( ) {
@Override
public String uploadFile ( MultipartFile upload, String objectName) {
log. debug ( "远程调用上传文件的接口发生了熔断:{}" , throwable. toString ( ) , throwable) ;
return null ;
}
} ;
}
}