Spring Cloud 学习(9) --- Feign(四) 文件上传、首次调用失败问题

Feign 在远程调用时,除了 GET 方式传递 POJO 外,还有几个很重要的功能:文件上传调用返回图片流传递 Token

文件上传

源码:https://gitee.com/laiyy0728/spring-cloud/tree/master/spring-cloud-feign/spring-cloud-feign-file

Feign 的子项目 feign-form(https://github.com/OpenFeign/feign-form) 支持文件上传,其中实现了上传所需要的 Encoder

模拟文件上传:spring-cloud-feign-file-serverspring-cloud-feign-file-client,其中 server 模拟文件服务器,作为服务提供者;client 模拟文件上传,通过 FeignClient 发送文件到文件服务器

FileClient

pom 依赖



    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client




    org.springframework.cloud
    spring-cloud-starter-openfeign




    io.github.openfeign.form
    feign-form
    3.0.3


    io.github.openfeign.form
    feign-form-spring
    3.0.3

配置文件

spring:
  application:
    name: spring-cloud-feign-file-client

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    instance-id: ${spring.application.name}:${server.port}
    prefer-ip-address: true
server:
  port: 8888

启动类、FeignClient、配置、Controller

// 启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class SpringCloudFeignFileClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudFeignFileClientApplication.class, args);
    }
}

// FeignClient
@FeignClient(value = "spring-cloud-feign-file-server", configuration = FeignMultipartConfiguration.class)
public interface FileUploadFeignClient {

    /**
     * feign 上传图片
     *
     * produces、consumes 必填
     * 不要将 @RequestPart 写成 @RequestParam
     *
     * @param file 上传的文件
     * @return 上传的文件名
     */
    @RequestMapping(value = "/upload-file", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    String fileUpload(@RequestPart(value = "file")MultipartFile file);

}

// configuration
@Configuration
public class FeignMultipartConfiguration {

    /**
     * Feign Spring 表单编码器
     * @return 表单编码器
     */
    @Bean
    @Primary
    @Scope("prototype")
    public Encoder multipartEncoder(){
        return new SpringFormEncoder();
    }

}

// Controller
@RestController
public class FileUploadController {

    private final FileUploadFeignClient feignClient;

    @Autowired
    public FileUploadController(FileUploadFeignClient feignClient) {
        this.feignClient = feignClient;
    }

    @PostMapping(value = "upload")
    public String upload(MultipartFile file){
        return feignClient.fileUpload(file);
    }

}

FileServer

pom


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client

配置文件

spring:
  application:
    name: spring-cloud-feign-file-server


eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${server.port}
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8889

启动类、Controller

@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudFeignFileServerApplication {

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

}

// Controller 模拟文件上传的处理
@RestController
public class FileUploadController {

    @PostMapping(value = "/upload-file")
    public String fileUpload(MultipartFile file) {
        return file.getOriginalFilename();
    }

}

验证文件上传

POST MAN 调用 client 上传接口

Spring Cloud 学习(9) --- Feign(四) 文件上传、首次调用失败问题_第1张图片
file upload

图片流

源码:https://gitee.com/laiyy0728/spring-cloud/tree/master/spring-cloud-feign/spring-cloud-feign-file

通过 Feign 返回图片,一般是字节数组

文件上传代码的基础上,再加上图片获取

FeignClient

/**
  * 获取图片
  * @return 图片
  */
@RequestMapping(value = "/get-img")
ResponseEntity getImage();


@GetMapping(value = "/get-img")
public ResponseEntity getImage(){
    return feignClient.getImage();
}

FeignServer

@GetMapping(value = "/get-img")
public ResponseEntity getImages() throws IOException {
    FileSystemResource resource = new FileSystemResource(getClass().getResource("/").getPath() + "Spring-Cloud.png");
    HttpHeaders headers = new HttpHeaders();
    headers.add("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE);
    headers.add("Content-Disposition", "attachment; filename=Spring-Cloud.png");
    return  ResponseEntity.status(HttpStatus.OK).headers(headers).body(FileCopyUtils.copyToByteArray(resource.getInputStream()));
}

验证

在浏览器访问:http://localhost:8888/get-img ,实现图片流下载


Feign 传递 Headers

源码:https://gitee.com/laiyy0728/spring-cloud/tree/master/spring-cloud-feign/spring-cloud-feign-multi-params

在认证、鉴权中,无论是哪种权限控制框架,都需要传递 header,但在使用 Feign 的时候,会发现外部请求 ServiceA 时,可以获取到 header,但是在 ServiceA 调用 ServiceB 时,ServiceB 无法获取到 Header,导致 Header 丢失。

spring-cloud-feign-multi-params 基础上,实现传递 Header。

验证 Header 无法传递问题

Spring Cloud 学习(9) --- Feign(四) 文件上传、首次调用失败问题_第2张图片
post man 传递 header1

consumer 打印 header


Consumer 打印 header

provider 打印 header


Spring Cloud 学习(9) --- Feign(四) 文件上传、首次调用失败问题_第3张图片
Provider 打印 header

HeaderInterceptor

在 Consumer 增加 HeaderInterceptor,做 header 传递

@Component
public class FeignHeaderInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate template) {
        if (null == getRequest()){
            return;
        }
        template.header("oauth-token", getHeaders(getRequest()).get("oauth-token"));
    }

    private HttpServletRequest getRequest(){
        try {
            return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        }catch (Exception e){
            return null;
        }
    }

    private Map getHeaders(HttpServletRequest request) {
        Map headers = new HashMap<>();
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = headerNames.nextElement();
            String value = request.getHeader(key);
            headers.put(key, value);
        }
        return headers;
    }

}

验证 header

用 postman 重新请求一遍,查看 provider 控制台打印:

provider header

你可能感兴趣的:(Spring Cloud 学习(9) --- Feign(四) 文件上传、首次调用失败问题)