最近发现线上一个项目日志突然报错,最终找到解决方法记录一下
原因
参考 https://github.com/spring-projects/spring-boot/issues/5009
tmpwatch – removes files which haven’t been accessed for a period of time
如上所言,删除指定的目录中一段时间未访问的文件。一般对于/tmp下的文件或日志文件
意思是tomcat的临时目录会被tmpwatch删除掉,甚至可能删除掉class文件,导致错误的发生
解决方法
方法1.启动时指定新的临时目录
1
-Djava.io.tmpdir=/var/tmp
方法2.配置文件中设置新的临时目录
1
2
3
server:
tomcat:
basedir: /var/tmp/
方法3.代码中配置tomcat 临时目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Configuration
public class MultipartConfig {
@Bean
MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
String location = System.getProperty("user.dir") + "/data/tmp";
File tmpFile = new File(location);
if (!tmpFile.exists()) {
tmpFile.mkdirs();
}
factory.setLocation(location);
return factory.createMultipartConfig();
}
}
参考链接