本文简略
使用两个子模块,从消费者调用生产者的接口
– 一个子模块为服务生产者提供文件服务,端口9090
– 一个子模块为服务消费者,端口8080
都以注册进nacos
使用
- 生产者不需要配置openfeign
- 消费者加入依赖,完成注解配置,详细如下
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
生产者接口:
@RestController
public class Controller {
@Autowired
private FileCoreService fileCoreService;
@PostMapping("/fup-")
public Result up(MultipartFile file) {
String url = fileCoreService.generate(file);
return Result.success("url", url);
}
消费者:
- 开启feign检测,@EnableFeignClients
- 新建一个接口,调用生产者提供的服务
@FeignClient("file-service")
public interface FileFeignApi {
@PostMapping(value = "/fup-", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Object usdp(MultipartFile file);
file-service 是生产者注册进入nacos的服务名
RequestMapping 必须和生产者保持一致,如果请求参数需要文件,那么需要consumes参数
- 调用服务进行文件存储
@Autowired
FileFeignApi feignClient;
@GetMapping("/up")
public Object get(MultipartFile file) {
return feignClient.usdp(file);
}