Spring Boot 如何控制Tomcat缓存? [英] How does Spring Boot control Tomcat cache?

查看:77
本文介绍了Spring Boot 如何控制Tomcat缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将带有 JSP 的 5 年历史的 Spring MVC 应用程序移植到 Spring Boot.因此,根据中的示例http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-jsp-limitations 我正在使用战争"包装.

嵌入式 tomcat 启动.然而,日志中充满了缓存警告,如下例所示

<前>2016-08-25 14:59:01.442 INFO 28884 --- [main] s.b.c.e.t.TomcatEmbeddedServletContainer:Tomcat 初始化端口:8080 (http)2016-08-25 14:59:01.456 INFO 28884 --- [主要] o.apache.catalina.core.StandardService:启动服务Tomcat2016-08-25 14:59:01.458 INFO 28884 --- [main] org.apache.catalina.core.StandardEngine:启动 Servlet 引擎:Apache Tomcat/8.5.42016-08-25 14:59:01.531 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache:无法在 [/WEB-INF/lib/displaytag-1.2.jar] 到缓存,因为在驱逐过期的缓存条目后可用空间不足 - 考虑增加缓存的最大大小2016-08-25 14:59:01.531 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache:无法在 [/WEB-INF/lib/decrex-maven- 添加资源0.1.10-SNAPSHOT.jar] 到缓存,因为在驱逐过期缓存条目后可用空间不足 - 考虑增加缓存的最大大小2016-08-25 14:59:01.531 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache:无法在 [/WEB-INF/lib/spring-boot-actuator-1.4.0.RELEASE.jar] 到缓存,因为在驱逐过期缓存条目后可用空间不足 - 考虑增加缓存的最大大小2016-08-25 14:59:01.531 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache:无法在 [/WEB-INF/lib/validation-api- 添加资源1.1.0.Final.jar] 到缓存,因为在驱逐过期的缓存条目后可用空间不足 - 考虑增加缓存的最大大小2016-08-25 14:59:01.532 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache:无法在 [/WEB-INF/lib/lucene-backward-codecs-5.3.1.jar] 到缓存,因为在驱逐过期缓存条目后可用空间不足 - 考虑增加缓存的最大大小2016-08-25 14:59:01.532 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache:无法在 [/WEB-INF/lib/lucene-queries-5.3.1.jar] 到缓存,因为在驱逐过期缓存条目后可用空间不足 - 考虑增加缓存的最大大小.....2016-08-25 14:59:05.121 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache:无法在 [/WEB-INF/lib/jstl-1.2.jar] 到缓存,因为在驱逐过期的缓存条目后可用空间不足 - 考虑增加缓存的最大大小2016-08-25 14:59:05.139 WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache:无法在 [/WEB-INF/classes/commons-logging.properties] 到缓存,因为在驱逐过期的缓存条目后可用空间不足 - 考虑增加缓存的最大大小2016-08-25 14:59:05.139 INFO 28884 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : 初始化 Spring 嵌入式 WebApplicationContext2016-08-25 14:59:05.139 INFO 28884 --- [ost-startStop-1] o.s.web.context.ContextLoader:根 WebApplicationContext:初始化在 7117 毫秒内完成.....2016-08-25 15:02:03.960 INFO 28884 --- [ndardContext[]]] org.apache.catalina.webresources.Cache:后台缓存驱逐进程无法释放 [10]% 的缓存用于上下文 [] - 考虑增加缓存的最大大小.驱逐后,大约 [9,251] KB 的数据保留在缓存中.

我很乐意增加 tomcat 缓存,但我没有找到在 Spring Boot 中控制它的方法.请建议!!!


我尝试了下面安迪威尔金森的建议.还尝试在 EmbeddedServletContainerCustomizer

中使用它<前>@组件公共类 ServletContainerCustomizer 实现 EmbeddedServletContainerCustomizer {私有静态最终日志日志 = LogFactory.getLog(ServletContainerCustomizer.class);@覆盖公共无效定制(ConfigurableEmbeddedServletContainer容器){如果 (TomcatEmbeddedServletContainerFactory.class.isAssignableFrom(container.getClass())) {int cacheSize = 256 * 1024;log.info("自定义tomcat工厂.新的缓存大小(KB)为" + cacheSize);TomcatEmbeddedServletContainerFactory tomcatFactory = (TomcatEmbeddedServletContainerFactory) 容器;tomcatFactory.addContextCustomizers((上下文) -> {StandardRoot standardRoot = new StandardRoot(context);standardRoot.setCacheMaxSize(cacheSize);});}}}

有关更改缓存大小的消息在日志中,但上面的代码对警告没有影响

解决方案

我遇到同样的问题有一段时间了,但 Andy Wilkinson 的提示让我走上了正确的道路.对我有用的是像 Andy 那样设置缓存,但随后也在上下文中显式设置资源.

@Bean公共 EmbeddedServletContainerFactory servletContainer() {TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory() {@覆盖protected void postProcessContext(上下文上下文){最终 int cacheSize = 40 * 1024;StandardRoot standardRoot = new StandardRoot(context);standardRoot.setCacheMaxSize(cacheSize);context.setResources(standardRoot);//这就是它在我的情况下工作的原因.logger.info(String.format("新缓存大小 (KB): %d", context.getResources().getCacheMaxSize()));}};返回 tomcatFactory;}

希望这会有所帮助!

I'm porting 5 years old Spring MVC application with JSPs to Spring Boot. Therefore, according to sample in http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-jsp-limitations I'm using "war" packaging.

Embedded tomcat starts. However logs are full of caching warnings like in the example below

2016-08-25 14:59:01.442  INFO 28884 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2016-08-25 14:59:01.456  INFO 28884 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2016-08-25 14:59:01.458  INFO 28884 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.4
2016-08-25 14:59:01.531  WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache   : Unable to add the resource at [/WEB-INF/lib/displaytag-1.2.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
2016-08-25 14:59:01.531  WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache   : Unable to add the resource at [/WEB-INF/lib/decrex-maven-0.1.10-SNAPSHOT.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
2016-08-25 14:59:01.531  WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache   : Unable to add the resource at [/WEB-INF/lib/spring-boot-actuator-1.4.0.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
2016-08-25 14:59:01.531  WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache   : Unable to add the resource at [/WEB-INF/lib/validation-api-1.1.0.Final.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
2016-08-25 14:59:01.532  WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache   : Unable to add the resource at [/WEB-INF/lib/lucene-backward-codecs-5.3.1.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
2016-08-25 14:59:01.532  WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache   : Unable to add the resource at [/WEB-INF/lib/lucene-queries-5.3.1.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
.....
2016-08-25 14:59:05.121  WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache   : Unable to add the resource at [/WEB-INF/lib/jstl-1.2.jar] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
2016-08-25 14:59:05.139  WARN 28884 --- [ost-startStop-1] org.apache.catalina.webresources.Cache   : Unable to add the resource at [/WEB-INF/classes/commons-logging.properties] to the cache because there was insufficient free space available after evicting expired cache entries - consider increasing the maximum size of the cache
2016-08-25 14:59:05.139  INFO 28884 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2016-08-25 14:59:05.139  INFO 28884 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 7117 ms
.....
2016-08-25 15:02:03.960  INFO 28884 --- [ndardContext[]]] org.apache.catalina.webresources.Cache   : The background cache eviction process was unable to free [10] percent of the cache for Context [] - consider increasing the maximum size of the cache. After eviction approximately [9,251] KB of data remained in the cache.

I'd be happy to increase tomcat cache, but I'm failing to find a way to control it in Spring Boot. Please, advise!!!


I tried a suggestion from Andy Wilkinson below. Also tried to use it in EmbeddedServletContainerCustomizer

@Component
public class ServletContainerCustomizer implements EmbeddedServletContainerCustomizer {

    private static final Log log = LogFactory.getLog(ServletContainerCustomizer.class);

    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        if (TomcatEmbeddedServletContainerFactory.class.isAssignableFrom(container.getClass())) {

            int cacheSize = 256 * 1024;
            log.info("Customizing tomcat factory. New cache size (KB) is " + cacheSize);

            TomcatEmbeddedServletContainerFactory tomcatFactory = (TomcatEmbeddedServletContainerFactory) container;
            tomcatFactory.addContextCustomizers((context) -> {
                StandardRoot standardRoot = new StandardRoot(context);
                standardRoot.setCacheMaxSize(cacheSize);
            });

        }
    }

}

Message about changing the cache size is in the logs, but the code above has no influence on the warnings

解决方案

I've been having the same problem for a while, but Andy Wilkinson's hint set me on the right track. What worked for me was to set the cache as Andy did, but then also explicitly set the resources in context.

@Bean
public EmbeddedServletContainerFactory servletContainer() {
    TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory() {
        @Override
        protected void postProcessContext(Context context) {
            final int cacheSize = 40 * 1024;
            StandardRoot standardRoot = new StandardRoot(context);
            standardRoot.setCacheMaxSize(cacheSize);
            context.setResources(standardRoot); // This is what made it work in my case.

            logger.info(String.format("New cache size (KB): %d", context.getResources().getCacheMaxSize()));
        }
    };
    return tomcatFactory;
}

Hope this helps!

这篇关于Spring Boot 如何控制Tomcat缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆