spring boot /tmp 目录被清空

问题:
spring boot the temporary upload location is not valid centos

2018-12-24 15:08:36.927 ERROR 3748 --- [101-8080-exec-8] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : 
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; 
nested exception is org.springframework.web.multipart.MultipartException: 
Could not parse multipart servlet request; nested exception is java.io.IOException: 
The temporary upload location [/tmp/tomcat.5959918120490600050.8080/work/Tomcat/localhost/ROOT] is not valid] with root cause

java.io.IOException: The temporary upload location [/tmp/tomcat.5959918120490600050.8080/work/Tomcat/localhost/ROOT] is not valid
        at org.apache.catalina.connector.Request.parseParts(Request.java:2776) ~[tomcat-embed-core-8.5.11.jar!/:8.5.11]
        at org.apache.catalina.connector.Request.parseParameters(Request.java:3177) ~[tomcat-embed-core-8.5.11.jar!/:8.5.11]
        at org.apache.catalina.connector.Request.getParameter(Request.java:1110) ~[tomcat-embed-core-8.5.11.jar!/:8.5.11]

原因:
CentOS 7 会清理 10 天前的 /tmp 目录的文件。 springboot 框架启动后,创建的 /tmp/tomcat.* 目录正好在清理策略内,所以会被自动清理。

cat /usr/lib/tmpfiles.d/tmp.conf
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

# See tmpfiles.d(5) for details

# Clear tmp directories separately, to make them easier to override
v /tmp 1777 root root 10d
v /var/tmp 1777 root root 30d

# Exclude namespace mountpoints created with PrivateTmp=yes
x /tmp/systemd-private-%b-*
X /tmp/systemd-private-%b-*/tmp
x /var/tmp/systemd-private-%b-*
X /var/tmp/systemd-private-%b-*/tmp

至今无具体修复方案
https://github.com/spring-projects/spring-boot/issues/9616

绕过方法1
server.tomcat.basedir=/app/xxxx/tmp
原理: 把 tomcat 工作目录指向 jar 的当前目录,这样临时也不会被自动清理.
注意:推荐在启动脚本上,每次启动 jar 服务前,自动删除缓存文件,
类似于启动时自动清理 tomcat 的 work 和 tmp 目录。

绕过方法2
server.tomcat.basedir=/tmp/systemd-private-8080-tomcat.service-springboot
原理: /tmp/systemd-private-%b-* 这个目录是不会被自动清理的,所以把临时目录名配置成这样,可以绕过此问题

绕过方法3
启动时增加参数,指定临时目录为/app/xxx/tmp

 -Djava.io.tmpdir=/app/xxx/tmp

绕过方法4
修改系统设置,不清理 /tmp/tomcat* 目录

echo "x /tmp/tomcat*" > /usr/lib/tmpfiles.d/tomcat.conf
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。