Spring Cloud学习(五)--Feign实现文件跨服务上传

在前面的文章中,我们学习了Feign声明式服务调用,但是在Spring Cloud封装的Feign中并不直接支持传文件,但Feign官方提供了子项目Feign-form来实现跨服务的文件上传,具体如下:

服务提供方(接收文件)


服务提供方的实现比较简单,就按Spring MVC的正常实现方式即可,就在我们之前的eurekaclient项目添加,如下:

 @PostMapping(value = "/fileupload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String fileupload(@RequestPart("file") MultipartFile file){
        if(file == null){
            return "附件为空,上传文件失败";
        }
        String filename = file.getOriginalFilename();
        long fileSize = file.getSize();
        return "上传成功,文件名为:"+filename+"  文件大小:"+fileSize;
    }


服务消费方(发送文件)


在服务消费方由于会使用Feign客户端,所以在这里需要在引入feign对表单提交的依赖,在我们之前serice-feign项目中添加,具体如下:

<dependency>
   <groupId>io.github.openfeign.form</groupId>
   <artifactId>feign-form</artifactId>
   <version>3.0.3</version>
</dependency>
<dependency>
   <groupId>io.github.openfeign.form</groupId>
   <artifactId>feign-form-spring</artifactId>
   <version>3.0.3</version>
</dependency>
<dependency>
   <groupId>commons-fileupload</groupId>
   <artifactId>commons-fileupload</artifactId>
   <version>1.3.3</version>
</dependency>


定义文件上传方的应用主类和FeignClient,并且设置我们服务提供方的服务名为service-hi

@FeignClient(value = "service-hi")
public interface SchedualServiceHi {

        @PostMapping(value = "/fileupload",
                consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
        public String fileupload(@RequestPart("file") MultipartFile file);

        @Configuration
        class MultipartSupportConfig {
            @Bean
            public Encoder feignFormEncoder() {
                return new SpringFormEncoder();
            }


        }

}


服务消费端接口,同样按照Spring MVC的正常实现方式即可,如下:

@RestController
public class HiController {

    @Autowired
    SchedualServiceHi schedualServiceHi;

        @PostMapping(value = "/fileupload")
        public String fileupload(@RequestPart("file") MultipartFile file){
            return schedualServiceHi.fileupload(file);
        }

}


然后我们依次启动服务注册中心、服务提供方和服务消费方进行文件上传测试,测试结果如下:

注意点:

1.接口注解PostMapping需要加入consumes = MediaType.MULTIPART_FORM_DATA_VALUE,如下:

@PostMapping(value = "/fileupload",
        consumes = MediaType.MULTIPART_FORM_DATA_VALUE)

2.参数注解@RequestPart(“file”)不能写成@RequestParam("file")。
3.如果文件较大,最好将Hystrix的超时时间设置的长一些,否则文件还没有上传完,Hystrix就超时,导致文件上传失败。


 

`spring-cloud-starter-feign` 是早期版本中使用的依赖名称,但在 Spring Cloud 的后续版本中已经被废弃。取而代之的是 `spring-cloud-starter-openfeign`[^3]。 如果需要在 Maven 项目中引入 OpenFeign 支持,则应在项目的 `pom.xml` 文件中添加如下依赖: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 需要注意的是,OpenFeign 已经成为 Spring Cloud 的一部分,并且默认集成了与 Spring Boot 和 Spring MVC 的良好兼容性。因此无需再单独寻找 `spring-cloud-starter-feign` 这一旧版依赖[^4]。 此外,为了确保项目的正常运行,还需要确认所使用的 Spring Cloud 版本号是否匹配当前的 Spring Boot 版本。可以在 `pom.xml` 中定义 Spring Cloud BOM(Bill of Materials),以便管理所有相关依赖的版本一致性。例如: ```xml <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR12</version> <!-- 替换为适合您项目的版本 --> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ``` 最后,当使用 Feign 或 OpenFeign 时,通常会结合 Eureka 或 Consul 等务注册中心来实现动态的务发现功能。这可以通过启用相应的注解完成,比如 `@EnableDiscoveryClient` 或者 `@EnableEurekaClient`。 ### 示例代码 以下是完整的 Maven 配置示例: ```xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.5</version> <!-- 根据实际需求调整版本 --> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <java.version>11</java.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR12</version> <!-- 使用合适的 Spring Cloud 版本 --> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-httpclient</artifactId> </dependency> </dependencies> </project> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MichaelYZ111

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值