Spring Boot Web Server设置tomcat cache size

问题描述

基于SpringBoot搭建了web服务,在服务器端启动时tomcat显示[WARN]信息,提示由于cache不足导致resource无法加载,需要增加cache的最大值。

WARN如下:

[localhost-startStop-1] [Cache] [179] [Unable to add the resource at [/WEB-INF/lib/spring-retry-1.1.3.RELEASE.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache]

解决方法

通过查询得知tomcat最大静态资源缓存大小为10240bytes(10 megabytes),需要通过修改cacheMaxSize来解决问题。

不同于直接部署tomcat,SpringBoot内嵌了Tomcat,最后在Stack Overflow找到解决方法解决了问题。

转自How does Spring Boot control Tomcat cache? - Stack Overflow

package config;

import org.apache.catalina.Context;
import org.apache.catalina.webresources.StandardRoot;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created on 2018/12/7.
 *
 * @author Marvin Yang
 */
@Configuration
public class WebConfig {

    /**
     * Create bean, and setting the maximum size of the cache
     *
     * @return a EmbeddedServletContainerFactory
     */
    @Bean
    public EmbeddedServletContainerFactory embeddedServletContainerFactory() {
        return new TomcatEmbeddedServletContainerFactory() {

            @Override
            protected void postProcessContext(Context context) {
                final int maxCacheSize = 40 * 1024;
                StandardRoot standardRoot = new StandardRoot(context);
                standardRoot.setCacheMaxSize(maxCacheSize);
                context.setResources(standardRoot);
            }
        };
    }
}

其他

  1. 查到的答案有不少回答是修改context.xml,但是未能起作用,不知道是配置的不对还是没有放置在正确的位置。
   <?xml version="1.0" encoding="UTF-8"?>
   <Context>
     <Resources cachingAllowed="true" cacheMaxSize="204800" />
   </Context> 

参考链接: tomcat resources doc

  1. 考虑是否可以通过application.properties直接配置,像port那样简单配置,但是未能找到相关配置项。
   # 端口配置
   server.port=8080

参考链接: sprintboot common application properties

转自

How does Spring Boot control Tomcat cache? - Stack Overflow

猜你喜欢

转载自blog.csdn.net/ymaini/article/details/84874006
今日推荐