Spring Boot 微服务之间通过FeignClient进行大文件下载:

来源:互联网 发布:崩坏学园2官方淘宝 编辑:程序博客网 时间:2024/06/09 20:43

使用FeignClient作为中间件进行一个微服务之间的调用的时候,一般的服务请求是没有什么问题,但是,当下载大文件,会出现:java heap space

也就是堆溢出问题。


具体解决方案如下:


1、首先是service层返回ResponseEntity<Resource>

2、@FeignClient的remote接口返回Response对象(FeignClient提供的Response对象)

3、前端层获取Response对象之后,可以获取headers和body信息.代码如下:

Response response = exploreServiceRemote.downPackSensorDataFile(recordCode, uniqueCode);Map<String, Collection<String>> headers = response.headers();HttpHeaders httpHeaders = new HttpHeaders();headers.forEach((key, values) -> {    List<String> headerValues = new LinkedList<>();    headerValues.addAll(values);    httpHeaders.put(key, headerValues);});Response.Body body = response.body();try {    InputStream inputStream = body.asInputStream();//HttpURLInputStream    InputStreamResource resource = new InputStreamResource(inputStream);    return ResponseEntity        .ok()        .contentType(MediaType.APPLICATION_OCTET_STREAM)        .headers(httpHeaders)        .body(resource);} catch (IOException e) {    throw new SdsResourceFileNotFoundException(        MessageFormat.format("Can not download resource recordCode [{0}] and uniqueCode [{1}]",            recordCode, uniqueCode));}

阅读全文
1 0