跨服务上传文件,嘿嘿,来一篇实用性高的,本篇将主要完成在Feign中上传文件到另一个微服务中。步骤如下:

我们需要在服务提供者和服务消费者的项目中添加相应的依赖:

对于服务提供者的项目,你需要添加Spring Boot的Web依赖和Spring Cloud的Feign依赖。在pom.xml文件中添加以下依赖:

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-openfeign

对于服务消费者的项目,除了上述的依赖外,还需要添加Feign的文件上传支持依赖。在pom.xml文件中添加以下依赖:

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-openfeign

io.github.openfeign.form

feign-form

3.8.0

注意,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 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的

元素和元素来创建一个文件上传表单。然后,通过JavaScript和AJAX将文件发送到服务端。

文件上传

文件上传

要通过localhost:端口号直接访问HTML页面,需要在Spring Boot应用程序中添加一个控制器,将HTML页面映射到一个URL路径上。

我的HTML页面名为upload.html,并且位于src/main/resources/static目录下。

@Controller

public class UploadController {

@GetMapping("/")

public String index() {

return "upload.html";

}

}

在上述代码中,我们创建了一个名为UploadController的控制器,并使用@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文件吧

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

feign

com.miaow

0.0.1-SNAPSHOT

4.0.0

eureka-feign-upload-server

eureka-feign-upload-server

http://www.example.com

UTF-8

1.8

1.8

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.cloud

spring-cloud-starter-eureka

1.4.2.RELEASE

org.springframework.cloud

spring-cloud-starter-openfeign

org.springframework.cloud

spring-cloud-dependencies

Hoxton.SR8

pom

import

org.springframework.boot

spring-boot-maven-plugin

配置好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文件配置:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

feign

com.miaow

0.0.1-SNAPSHOT

4.0.0

eureka-feign-upload-client

eureka-feign-upload-client

http://www.example.com

UTF-8

1.8

1.8

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.cloud

spring-cloud-starter-eureka

1.4.2.RELEASE

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.cloud

spring-cloud-starter-openfeign

io.github.openfeign.form

feign-form

3.8.0

io.github.openfeign.form

feign-form-spring

3.8.0

commons-fileupload

commons-fileupload

1.4

org.projectlombok

lombok

test

org.springframework.cloud

spring-cloud-dependencies

Hoxton.SR8

pom

import

org.springframework.boot

spring-boot-maven-plugin

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 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至此,我们完成这个功能了。算是把坑填完了。

好文链接

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。
大家都在看: