前言:分为4个模块,eureka注册中心,消费者,提供者,feign接口。在实现基本的feign调用前提下,实现feign进行文件处理。
主要核心在于feign接口处的方法要加一些参数,要写一个配置类,要导入一些额外的包。
1.feign接口编写
- 导入pom
<!--feign的支持 使用了feign之后,不需要再注入resttemplate了,也不需要导入ribbon了,集成了ribbon-->
<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>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
- feign接口编写(特别注意requestmapping括号中的参数)
@FeignClient(value = "HRM-FILE",configuration = MultipartSupportConfig.class) //开启注解,引入配置类
public interface FileFeingClient {
/**
* 文件上传要加上后面的 produces
* @param file
* @return
* @throws Exception
*/
@RequestMapping(value = "oss/upload", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
AjaxResult upload(@RequestPart(value = "file") MultipartFile file) throws Exception;
}
- 写一个配置类,配置编码
@Configuration
public class MultipartSupportConfig {
@Bean
public Encoder feignFormEncoder() {
return new SpringFormEncoder();
}
}
2.文件上传消费者代码
1.pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Eureka 客户端依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
**xxxxx 导入自己的feign接口模块**
</dependency>
2. controller层
@Autowired
private FileFeingClient fileFeingClient;
@RequestMapping(value = "upload",method = RequestMethod.POST)
public AjaxResult upload(@RequestPart("file") MultipartFile file) throws Exception {
AjaxResult upload = fileFeingClient.upload(file);
return upload;
}
3. 启动类
启动类上的注解
@SpringBootApplication
@EnableEurekaClient //交给eureka管理
@EnableFeignClients(value = “xxxx”) //扫码feignclient接口,否则注入的时候会报错
3.文件上传服务提供者 (没什么特别,就正常的一个controller接口)
1. controller层
@RestController
@RequestMapping("oss")
public class FileController {
@RequestMapping(value = "upload",method = RequestMethod.POST)
public AjaxResult upload(@RequestPart("file") MultipartFile file) throws Exception {
xxxxx。。。。拿到file后,该干嘛干嘛
}
}
2.pom.xml
<!-- Eureka 客户端依赖 -->
<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-web</artifactId>
</dependency>