Spring Cloud微服务【Finchley.RELEASE版本】(四)使用feign服务间传送文件

Spring Cloud微服务【Finchley.RELEASE版本】(四)使用feign服务间传送文件

(一)服务提供方【接受文件】项目构建

依赖引入

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

启动类添加注解

添加如下两个注解 即可。
@EnableFeignClients
@EnableDiscoveryClient

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaFeignUploadServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaFeignUploadServerApplication.class, args);
    }
}

application.properties配置

spring.application.name=eureka-feign-upload-client
server.port=3005
# 连接注册中心的服务地址
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

编写controller接受文件,暴露服务接口

@RestController
public class TestController {

    @PostMapping(value = "/uploadFile" ,consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String handleFileUpload(@RequestPart(value = "TestFile") MultipartFile file){
        System.out.println("我被调用了,我用来接受文件!我收到的文件名:"+file.getName());
        return file.getName();
    }
}

(二)服务消费方【上传文件】项目构建

依赖引入

lombok依赖是后续使用了@SneakyThrows注解需要的。

    <!--feign 上传文件所需的依赖 start-->
    <dependencies>
        <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>
        <!--feign 上传文件所需的依赖 end-->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.22</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

启动类添加注解

同上

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaFeignUploadClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaFeignUploadClientApplication.class, args);
    }
}

application.properties配置

spring.application.name=eureka-feign-upload-client
server.port=3005
# 连接注册中心的服务地址
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

编写feign调用客户端UploadService

 @FeignClient(value = "eureka-feign-upload-server", configuration = UploadService.MultipartSupportConfig.class)
    public interface UploadService {
        @PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
        String handleFileUpload(@RequestPart(value = "TestFile") MultipartFile file);

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

测试类,直接指定文件调用UploadService的方法

@SneakyThrows注解用来简化写法而已,具体可以参见lombok官网。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class UploadServiceTest {

    @Autowired
    private UploadService uploadService;

    @Test
    @SneakyThrows
    public void handleFileUpload() {

        File file = new File("test.txt");
        DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem("TestFile",
                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 multi = new CommonsMultipartFile(fileItem);
        uploadService.handleFileUpload(multi);
        System.out.println("执行调用服务结束!上传文件完成");
    }

}

(三)测试

直接运行客户端的eureka-feign-upload-client里面的测试方法:
client控制台:
这里写图片描述

server日志:
这里写图片描述

这里没有对上传的文件进行保存操作,仅仅只是获取了文件名而已。

猜你喜欢

转载自blog.csdn.net/qq_29534483/article/details/81330269