跨服务上传文件,嘿嘿,来一篇实用性高的,本篇将主要完成在Feign中上传文件到另一个微服务中。步骤如下:
我们需要在服务提供者和服务消费者的项目中添加相应的依赖:
对于服务提供者的项目,你需要添加Spring Boot的Web依赖和Spring Cloud的Feign依赖。在pom.xml文件中添加以下依赖:
<dependencies>
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Cloud Feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
对于服务消费者的项目,除了上述的依赖外,还需要添加Feign的文件上传支持依赖。在pom.xml文件中添加以下依赖:
<dependencies>
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Cloud Feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- Feign 文件上传支持 -->
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.8.0</version>
</dependency>
</dependencies>
注意,Feign的文件上传支持依赖是feign-form,需要指定相应的版本号。
正式开始
在服务提供者的微服务中,创建一个接收文件上传的接口。可以使用@RequestParam(“file”) MultipartFile file来接收文件参数。
@PostMapping("/upload")
void uploadFile(@RequestParam("file") MultipartFile file);
在服务消费者的微服务中,创建一个Feign接口,并使用@FeignClient
注解标记它。在接口中定义上传文件的方法,使用@PostMapping
注解,并指定上传文件的路径。
@FeignClient(name = "file-service")
public interface FileServiceClient {
@PostMapping("/upload")
void uploadFile(@RequestParam("file") MultipartFile file);
}
在服务消费者的微服务中,配置Feign的文件上传支持。在配置类中添加@Configuration注解,并创建一个FeignClientConfig类,使用@Bean注解配置Encoder和Decoder。
@Configuration
public class FeignClientConfig {
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder();
}
@Bean
public Decoder feignDecoder() {
return new ResponseEntityDecoder(new SpringDecoder(feignHttpMessageConverter()));
}
private ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() {
final HttpMessageConverters httpMessageConverters = new HttpMessageConverters(new FormHttpMessageConverter());
return () -> httpMessageConverters;
}
}
在服务消费者的微服务中,使用@EnableFeignClients
注解启用Feign
客户端,并在需要上传文件的地方注入FileServiceClient
接口,调用uploadFile
方法即可完成文件上传。
@SpringBootApplication
@EnableFeignClients
public class ConsumerApplication {
@Autowired
private FileServiceClient fileServiceClient;
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
public void uploadFile(MultipartFile file) {
fileServiceClient.uploadFile(file);
}
}
这样,通过Feign客户端调用uploadFile方法时,会将文件作为参数上传到服务提供者的微服务中。注意,需要确保服务提供者和服务消费者的微服务都引入了相应的依赖,并且配置正确。
在以上代码中,我们已经定义了Feign客户端接口FileServiceClient
,并在服务消费者的代码中注入了该接口。现在,我们只需要在服务消费者的代码中调用FileServiceClient
的uploadFile
方法,即可完成文件上传。
假设我们要上传的文件是一个MultipartFile
对象,可以按照以下方式完成文件上传:
@Autowired
private FileServiceClient fileServiceClient;
public void uploadFile(MultipartFile file) {
fileServiceClient.uploadFile(file);
}
在调用uploadFile
方法时,Feign会将MultipartFile
对象转换为multipart/form-data
格式,并将其作为请求体发送到服务提供者的微服务中。服务提供者的微服务会接收到文件,并进行相应的处理。
需要注意的是,Feign默认使用的是application/json
格式进行请求和响应的序列化和反序列化。如果要使用multipart/form-data
格式进行文件上传,需要在服务消费者的代码中配置Feign的文件上传支持。
前端界面:
在网页中实现文件上传,可以使用HTML的<form>
元素和<input type="file">
元素来创建一个文件上传表单。然后,通过JavaScript
和AJAX
将文件发送到服务端。
<!DOCTYPE html>
<html>
<head>
<title>文件上传</title>
</head>
<body>
<h1>文件上传</h1>
<form id="uploadForm" enctype="multipart/form-data">
<input type="file" name="file" id="fileInput">
<button type="submit">上传</button>
</form>
<div id="message"></div>
<script>
document.getElementById("uploadForm").addEventListener("submit", function(event) {
event.preventDefault(); // 阻止表单默认提交行为
var fileInput = document.getElementById("fileInput");
var file = fileInput.files[0]; // 获取选择的文件
var formData = new FormData();
formData.append("file", file); // 将文件添加到FormData对象中
var xhr = new XMLHttpRequest();
xhr.open("POST", "/upload"); // 设置请求方法和URL
xhr.onload = function() {
if (xhr.status === 200) {
document.getElementById("message").textContent = "文件上传成功!";
} else {
document.getElementById("message").textContent = "文件上传失败!";
}
};
xhr.send(formData); // 发送请求
});
</script>
</body>
</html>
要通过localhost:端口号
直接访问HTML页面,需要在Spring Boot应用程序中添加一个控制器,将HTML页面映射到一个URL路径上。
我的HTML页面名为upload.html
,并且位于src/main/resources/static
目录下。
@Controller
public class UploadController {
@GetMapping("/")
public String index() {
return "upload.html";
}
}
在上述代码中,我们创建了一个名为UploadControlle
r的控制器,并使用@GetMapping
注解将/路径映射到upload.html
页面。当用户访问localhost:端口号时,将自动跳转到upload.html
页面。
需要注意的是,为了使Spring Boot
能够正确地处理静态资源,你需要在application.properties
文件中添加以下配置:
spring.mvc.static-path-pattern=/static/**
这将告诉Spring Boot将所有以/static/开头的URL路径映射到src/main/resources/static
目录下的静态资源。因此,你需要将upload.html文件放置在src/main/resources/static
目录下。
至此,文件上传的大概框架就搭建好了,可以去练练手了。
案例
本案例是博主在以上代码上进行的,并添加了一个单元测试上传txt文件,干货满满,请细细品鉴,光是利用上边的代码,然后汇总,期间碰到了许多错误,才成功搭建成功本案例。
首先来看看博主的项目案例目录结构:
在feign模块中搭建两个字模块,一个作为服务模块一个作为消费者模块
eureka-feign-upload-server
来瞅瞅我的pom文件吧
<?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">
<parent>
<artifactId>feign</artifactId>
<groupId>com.miaow</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-feign-upload-server</artifactId>
<name>eureka-feign-upload-server</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--spring-cloud-starter-netflix-eureka-client:这是Eureka客户端的另一个依赖库,用于将应用程序注册到Eureka服务器并从服务器中发现其他服务。它与spring-cloud-starter-eureka功能相似,但是它是基于Netflix Ribbon的,可以提供更多的负载均衡策略和配置选项。通过添加该依赖库,你可以在应用程序中实现Eureka客户端功能,并使用Netflix Ribbon进行负载均衡。-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--spring-cloud-starter-eureka是Eureka客户端的依赖库,用于将服务注册到Eureka服务器并从Eureka服务器发现其他服务。它包含了Eureka客户端的核心功能,如服务注册、服务发现、负载均衡等。-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置好pom文件,引入相关依赖之后,我们在Application.yml文件中配置我们的相关属性:
server:
port: 3401
spring:
application:
name: eureka-feign-upload-server
# eureka客户端注册到Eureka注册中心,切记需要启动eureka服务
eureka:
client:
service-url:
defaultZone: http://localhost:1000/eureka
#ribbon:
# eureka:
# enabled: true
之后创建一个FileController类
@RestController
public class FileController {
@PostMapping("/upload")
public String uploadFile(@RequestParam("file") MultipartFile file) {
return "文件上传成功";
}
}
启动类:
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class EurekaUploadServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaUploadServerApplication.class);
}
}
PS:实际上这里没啥好讲的,按照我的配置方式进行,把它当做服务器就行了。
eureka-feign-upload-client
声明:这个模块看起来简单,但是配置稍有问题就会出现错误,或者服务器找不到,以下是博主使用的,并成功实现的,可以参照一下,但还是推荐你自己玩,碰到不会的可以与我对一下,我在写本篇博文最先时候的时候就是,对照demo来的,一个一个调试,最后成功(鬼知道我踩了多少坑)。
目录结构如上:
pom.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">
<parent>
<artifactId>feign</artifactId>
<groupId>com.miaow</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-feign-upload-client</artifactId>
<name>eureka-feign-upload-client</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.8.0</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.8.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml文件配置
server:
port: 3402
spring:
application:
name: eureka-feign-upload-client
mvc:
static-path-pattern: /static/**
# 文件上传限制大小
servlet:
multipart:
enabled: true
max-file-size: 10MB
max-request-size: 10MB
# eureka客户端注册到Eureka注册中心,切记需要启动eureka服务
eureka:
client:
service-url:
defaultZone: http://localhost:1000/eureka
只要upload.html文件,在本文的上边,我只改了一个请求位置:
FileServiceClient
//@FeignClient注解来指定服务提供者的名称 configuration属性指定了一个内部静态类MultipartSupportConfig,用于配置Feign的编码器。
@FeignClient(value = "eureka-feign-upload-server",configuration = FileServiceClient.MultipartSupportConfig.class)
public interface FileServiceClient {
//SpringFormEncoder是Feign提供的一个编码器,用于处理multipart/form-data类型的请求。
@PostMapping(value = "/upload",consumes = MediaType.MULTIPART_FORM_DATA)
String uploadFile(@RequestPart( value = "file") MultipartFile file);
@Configuration //代表这个是一个配置类,告诉Spring
class MultipartSupportConfig{
@Bean
public Encoder feignFromEncoder(){
//返回一个Feign的编码器
return new SpringFormEncoder();
}
}
}
FeignClientConfig配置类
@Configuration
public class FeignClientConfig {
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder();
}
@Bean
public Decoder feignDecoder() {
return new ResponseEntityDecoder(new SpringDecoder(feignHttpMessageConverter()));
}
private ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() {
final HttpMessageConverters httpMessageConverters = new HttpMessageConverters(new FormHttpMessageConverter());
return () -> httpMessageConverters;
}
}
UploadController 控制类
@RestController
@RequestMapping("/api")
public class UploadController {
@Autowired
private FileServiceClient fileServiceClient;
@GetMapping("/")
public String index() {
return "upload.html";
}
@RequestMapping("/upload")
public void uploadFile(@RequestPart( value = "file") MultipartFile file) {
fileServiceClient.uploadFile(file);
}
}
启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class EurekaUploadClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaUploadClientApplication.class);
}
}
单元测试类:
注意单元测试的时候,需要一个upload.txt文件,你自己创建
@Slf4j
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class UploadTest {
@Autowired
FileServiceClient fileServiceClient;
@Test
public void testUpload(){
File file = new File("upload.txt");
DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem("file",
MediaType.TEXT_PLAIN_VALUE,true,file.getName());
try (InputStream input = new FileInputStream(file); OutputStream os = fileItem.getOutputStream()) {
IOUtils.copy(input, os);
} catch (Exception e) {
throw new IllegalArgumentException("Invalid file: " + e, e);
}
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
log.info(fileServiceClient.uploadFile(multipartFile));
}
}
单元测试日志:
2023-12-15 15:03:24.077 INFO 12148 --- [ main] c.n.l.DynamicServerListLoadBalancer : DynamicServerListLoadBalancer for client eureka-feign-upload-server initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=eureka-feign-upload-server,current list of Servers=[localhost:3401],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone; Instance count:1; Active connections count: 0; Circuit breaker tripped count: 0; Active connections per server: 0.0;]
},Server stats: [[Server:localhost:3401; Zone:defaultZone; Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connection made:Thu Jan 01 08:00:00 CST 1970; First connection made: Thu Jan 01 08:00:00 CST 1970; Active Connections:0; total failure count in last (1000) msecs:0; average resp time:0.0; 90 percentile resp time:0.0; 95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0; stddev resp time:0.0]
]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@4374c051
2023-12-15 15:03:24.168 INFO 12148 --- [ main] com.miaow.UploadTest : 文件上传成功
2023-12-15 15:03:24.180 INFO 12148 --- [ Thread-8] c.n.l.PollingServerListUpdater : Shutting down the Executor Pool for PollingServerListUpdater
2023-12-15 15:03:24.181 INFO 12148 --- [extShutdownHook] o.s.c.n.e.s.EurekaServiceRegistry : Unregistering application EUREKA-FEIGN-UPLOAD-CLIENT with eureka with status DOWN
2023-12-15 15:03:24.182 WARN 12148 --- [extShutdownHook] com.netflix.discovery.DiscoveryClient : Saw local status change event StatusChangeEvent [timestamp=1702623804182, current=DOWN, previous=UP]
2023-12-15 15:03:24.182 INFO 12148 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_EUREKA-FEIGN-UPLOAD-CLIENT/localhost:eureka-feign-upload-client:3402: registering service...
2023-12-15 15:03:24.185 INFO 12148 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
2023-12-15 15:03:24.188 INFO 12148 --- [extShutdownHook] c.n.u.concurrent.ShutdownEnabledTimer : Shutdown hook removed for: NFLoadBalancer-PingTimer-eureka-feign-upload-server
2023-12-15 15:03:24.194 INFO 12148 --- [extShutdownHook] c.n.u.concurrent.ShutdownEnabledTimer : Exception caught (might be ok if at shutdown)
单元测试成功了,那么我们去网页上看看吧:http://localhost:3402/static/upload.html
ok至此,我们完成这个功能了。算是把坑填完了。