具有图像响应的Spring Boot FeignClient会出现错误' stream is closed'

时间:2016-07-27 15:05:58

标签: java spring rest spring-boot

My Spring REST控制器使用FeignClient消耗另一个Image / Blob存储微服务失败,错误'流已关闭'。什么是使用blob数据服务的正确方法。

REST控制器

@RestController
@RequestMapping("/blob")
public class BlobDataRestController  extends BaseRESTController{

    @Autowired
    private IBlobService blobService;

    @CrossOrigin
    @RequestMapping(value = "/{filename:.+}", method = RequestMethod.GET)
     public ResponseEntity<InputStreamResource> getFile(@PathVariable(value = "filename") String filename){
        return blobService.getFile(filename);
    }
}

FeignClient接口

@FeignClient(name="blob-service", url="${blob-service.url}")
public interface IBlobService {

     @RequestMapping(value = "/blob/{filename:.+}", method = RequestMethod.GET)
     public ResponseEntity<InputStreamResource> getFile(@PathVariable(value = "filename") String filename);
}

调用微服务时出错 /blob/filename.jpg

2016-07-27T20:31:41.73+0530 [App/0]      OUT 2016-07-27 15:01:41.732 ERROR 29 --- [io-61779-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
2016-07-27T20:31:41.73+0530 [App/0]      OUT java.io.IOException: stream is closed
2016-07-27T20:31:41.73+0530 [App/0]      OUT    at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.ensureOpen(HttpURLConnection.java:3309) ~[na:1.8.0_91]
2016-07-27T20:31:41.73+0530 [App/0]      OUT    at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3334) ~[na:1.8.0_91]
2016-07-27T20:31:41.73+0530 [App/0]      OUT    at java.io.FilterInputStream.read(FilterInputStream.java:133) ~[na:1.8.0_91]
2016-07-27T20:31:41.73+0530 [App/0]      OUT    at java.io.PushbackInputStream.read(PushbackInputStream.java:186) ~[na:1.8.0_91]
2016-07-27T20:31:41.73+0530 [App/0]      OUT    at java.io.FilterInputStream.read(FilterInputStream.java:107) ~[na:1.8.0_91]
2016-07-27T20:31:41.73+0530 [App/0]      OUT    at org.springframework.util.StreamUtils.copy(StreamUtils.java:126) ~[spring-core-4.2.6.RELEASE.jar!/:4.2.6.RELEASE]
2016-07-27T20:31:41.73+0530 [App/0]      OUT    at org.springframework.http.converter.ResourceHttpMessageConverter.writeInternal(ResourceHttpMessageConverter.java:102) ~[spring-web-4.2.6.RELEASE.jar!/:4.2.6.RELEASE]
2016-07-27T20:31:41.73+0530 [App/0]      OUT    at org.springframework.http.converter.ResourceHttpMessageConverter.writeInternal(ResourceHttpMessageConverter.java:47) ~[spring-web-4.2.6.RELEASE.jar!/:4.2.6.RELEASE]
2016-07-27T20:31:41.73+0530 [App/0]      OUT    at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:195) ~[spring-web-4.2.6.RELEASE.jar!/:4.2.6.RELEASE]
2016-07-27T20:31:41.73+0530 [App/0]      OUT    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:238) ~[spring-webmvc-4.2.6.RELEASE.jar!/:4.2.6.RELEASE]
2016-07-27T20:31:41.73+0530 [App/0]      OUT    at org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor.handleReturnValue(HttpEntityMethodProcessor.java:183) ~[spring-webmvc-4.2.6.RELEASE.jar!/:4.2.6.RELEASE]
2016-07-27T20:31:41.73+0530 [App/0]      OUT    at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:81) ~[spring-web-4.2.6.RELEASE.jar!/:4.2.6.RELEASE]
2016-07-27T20:31:41.73+0530 [App/0]      OUT    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:126) ~[spring-webmvc-4.2.6.RELEASE.jar!/:4.2.6.RELEASE]
2016-07-27T20:31:41.73+0530 [App/0]      OUT    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:832) ~[spring-webmvc-4.2.6.RELEASE.jar!/:4.2.6.RELEASE]
2016-07-27T20:31:41.73+0530 [App/0]      OUT    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:743) ~[spring-webmvc-4.2.6.RELEASE.jar!/:4.2.6.RELEASE]
2016-07-27T20:31:41.73+0530 [App/0]      OUT    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.2.6.RELEASE.jar!/:4.2.6.RELEASE]
2016-07-27T20:31:41.73+0530 [App/0]      OUT    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961) ~[spring-webmvc-4.2.6.RELEASE.jar!/:4.2.6.RELEASE]
2016-07-27T20:31:41.73+0530 [App/0]      OUT    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895) ~[spring-webmvc-4.2.6.RELEASE.jar!/:4.2.6.RELEASE]
2016-07-27T20:31:41.73+0530 [App/0]      OUT    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967) ~[spring-webmvc-4.2.6.RELEASE.jar!/:4.2.6.RELEASE]

1 个答案:

答案 0 :(得分:9)

如果我将ResponseEntity与byte []一起用作响应,那么一切都运行良好:

@FeignClient(name="blob-service", url="${blob-service.url}")
public interface IBlobService {
     @RequestMapping(value = "/blob/{filename:.+}", method = RequestMethod.GET)
     public ResponseEntity<byte[]> getFile(@PathVariable(value = "filename") String filename);
}

但它可能导致OutOfMemoryError,所以我做了一个调查,找到了更好的解决方案:

@FeignClient(name="blob-service", url="${blob-service.url}")
public interface IBlobService {
     @RequestMapping(value = "/blob/{filename:.+}", method = RequestMethod.GET)
     public feign.Response getFile(@PathVariable(value = "filename") String filename);
}

我正在以这种方式使用blobService:

try (Response response = blobService.getFile(fileName)) {
    InputStream is = response.body().asInputStream();
  // work with is
}

我在Feign sources中发现,如果响应大小超过8192字节,response.body()。asInputStream()将返回原始sun.net.www.http.KeepAliveStream

if (response.body().length() == null ||
            response.body().length() > MAX_RESPONSE_BUFFER_SIZE) {
    shouldClose = false;
    return response;
}

否则将创建feign.Response.ByteArrayBody。如果您尝试访问InputStream it will return ByteArrayInputStream。 所有这些都解决了可能的OutOfMemoryError。