前言
最近在做私人项目,使用了微服务spring cloud,其中有一个所有请求的总入口,即api网关,使用了zuul做路由,不同的请求路由到不同的微服务中。
问题
在开发的时候遇到一个有关文件上传的问题,文件上传方法在其他微服务中,在通过网关转发的时候,微服务中对应的方法出现了如下的错误:
[23:01:11:087] [ERROR] - org.apache.juli.logging.DirectJDKLog.log(DirectJDKLog.java:182) - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: Unexpected EOF read on the socket] with root cause java.io.EOFException: Unexpected EOF read on the socket at org.apache.coyote.http11.Http11InputBuffer.fill(Http11InputBuffer.java:716) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.coyote.http11.Http11InputBuffer.access$300(Http11InputBuffer.java:40) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.coyote.http11.Http11InputBuffer$SocketInputBuffer.doRead(Http11InputBuffer.java:1043) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.coyote.http11.filters.IdentityInputFilter.doRead(IdentityInputFilter.java:102) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.coyote.http11.Http11InputBuffer.doRead(Http11InputBuffer.java:243) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.coyote.Request.doRead(Request.java:551) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.catalina.connector.InputBuffer.realReadBytes(InputBuffer.java:318) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.catalina.connector.InputBuffer.checkByteBufferEof(InputBuffer.java:611) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.catalina.connector.InputBuffer.read(InputBuffer.java:341) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.catalina.connector.CoyoteInputStream.read(CoyoteInputStream.java:132) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at java.io.FilterInputStream.read(FilterInputStream.java:133) ~
[?:1.8.0_131] at org.apache.tomcat.util.http.fileupload.util.LimitedInputStream.read(LimitedInputStream.java:132) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.tomcat.util.http.fileupload.MultipartStream$ItemInputStream.makeAvailable(MultipartStream.java:977) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.tomcat.util.http.fileupload.MultipartStream$ItemInputStream.read(MultipartStream.java:881) ~[tomcat-embed-core-9.0.7.jar:9.0.7] at java.io.InputStream.read(InputStream.java:101) ~
[?:1.8.0_131] at org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:98) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.tomcat.util.http.fileupload.util.Streams.copy(Streams.java:68) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.tomcat.util.http.fileupload.MultipartStream.readBodyData(MultipartStream.java:572) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.tomcat.util.http.fileupload.MultipartStream.discardBodyData(MultipartStream.java:596) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.tomcat.util.http.fileupload.MultipartStream.skipPreamble(MultipartStream.java:614) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.findNextItem(FileUploadBase.java:865) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.(FileUploadBase.java:845) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:256) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:280) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.catalina.connector.Request.parseParts(Request.java:2804) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.catalina.connector.Request.parseParameters(Request.java:3148) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.catalina.connector.Request.getParameter(Request.java:1109) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:381) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:75) ~
[spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~
[spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~
[spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~
[spring-web-5.0.4.RELEASE.jar:5.0.4.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:407)
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
[tomcat-embed-core-9.0.7.jar:9.0.7] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
[tomcat-embed-core-9.0.7.jar:9.0.7] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
[?:1.8.0_131] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_131] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.7.jar:9.0.7] at java.lang.Thread.run(Thread.java:748) [?:1.8.0_131]
后又直接调用微服务中的方法,可以正常执行,所以基本可以定位问题出在网关这里,经过查询和测试,应该是网关 这里的超时时间太短导致的错误。
解决
在网关的配置文件中将超时时间设置长点:
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=600000
ribbon.ConnectTimeout=10000
ribbon.ReadTimeout=10000
注意
使用zuul的时候,有一点需要注意,当转发的接口是post方式,并且传参方式为form-data
或者x-www-form-urlencode
的时候,则zuul的配置中不要添加以下配置,否则参数会传递不过去
zuul.FormBodyWrapperFilter.pre.disable=true