2016-08-25 50 views
8

Tôi đang chuyển ứng dụng Spring MVC 5 tuổi với JSP sang Spring Boot. Do đó, theo mẫu trong http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-jsp-limitations Tôi đang sử dụng bao bì "chiến tranh".Spring Boot kiểm soát bộ đệm Tomcat như thế nào?

Khởi động tomcat được nhúng. Tuy nhiên nhật ký có đầy đủ các cảnh báo lưu trong bộ nhớ cache như trong ví dụ bên dưới

 
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. 

Tôi rất sẵn lòng tăng bộ đệm tomcat nhưng tôi không tìm cách điều khiển bộ đệm trong Khởi động mùa xuân. Vui lòng, tư vấn !!!


Tôi đã thử đề xuất từ ​​Andy Wilkinson bên dưới. Cũng đã cố gắng sử dụng nó trong 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); 
      }); 

     } 
    } 

} 

tin nhắn về việc thay đổi kích thước bộ nhớ cache là trong nhật ký, nhưng mã trên không có ảnh hưởng trên những lời cảnh báo

+0

Có một số thông tin về lỗi này: http://stackoverflow.com/questions/26893297/tomcat-8-throwing-org-apache-catalina-webresources-cache-getresource-unable-to As far Tôi hiểu để tăng kích thước bộ nhớ cache mà bạn phải xác định ['EmbeddedServletContainerCustomizer'] (http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/context/embedded/EmbeddedServletContainerCustomizer.html) –

+0

Cảm ơn bạn đã có ý tưởng. Tôi có thể truy cập vào một thể hiện của [TomcatEmbeddedServletContainerFactory] (http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainerFactory.html). Tuy nhiên không có phương pháp nào có thể giúp kiểm soát bộ nhớ đệm – Viktor

Trả lời

8

Tôi đã gặp vấn đề tương tự cho một thời gian, nhưng gợi ý Andy Wilkinson của thiết cho tôi đi đúng hướng. Điều gì làm việc cho tôi là đặt bộ nhớ cache như Andy đã làm, nhưng sau đó cũng đặt rõ ràng tài nguyên trong ngữ cảnh.

@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; 
} 

Hy vọng điều này sẽ hữu ích!

+0

Tuyệt vời, cảm ơn!Tôi thấy đã tạo 'standardRoot' không được truyền đi bất cứ đâu, nhưng tự thuyết phục bản thân nó là OK vì nó có' context' trong hàm tạo. – Viktor

+0

Cảm ơn bạn, có thể xác nhận nó hoạt động với khởi động mùa xuân 1.4.2. –

3

Bạn có thể cấu hình kích thước bộ nhớ cache sử dụng một bối cảnh tùy biến cấu hình bối cảnh với một tùy chỉnh StandardRoot:

Java 7:

@Bean 
public TomcatEmbeddedServletContainerFactory tomcatFactory() { 
    TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory(); 
    tomcatFactory.addContextCustomizers(new TomcatContextCustomizer() { 

     @Override 
     public void customize(Context context) { 
      StandardRoot standardRoot = new StandardRoot(context); 
      standardRoot.setCacheMaxSize(40 * 1024); 
     } 

    }); 
    return tomcatFactory; 
} 

Java 8:

@Bean 
public TomcatEmbeddedServletContainerFactory tomcatFactory() { 
    TomcatEmbeddedServletContainerFactory tomcatFactory = new TomcatEmbeddedServletContainerFactory(); 
    tomcatFactory.addContextCustomizers((context) -> { 
     StandardRoot standardRoot = new StandardRoot(context); 
     standardRoot.setCacheMaxSize(40 * 1024); 
    }); 
    return tomcatFactory; 
} 
+0

Cảm ơn rất nhiều! Tôi đã thử đề xuất của bạn (với 1024 * 1024) cả hai cách: tạo nhà máy và tùy chỉnh nó trong EmbeddedServletContainerCustomizer sau (mã được thêm vào câu hỏi của tôi). Tuy nhiên không có bất kỳ ảnh hưởng nào đến kết quả - các cảnh báo vẫn còn ở đó – Viktor

+0

'standardRoot.setCachingAllowed (false);' cũng không tắt cache. Có vẻ 'StandardRoot' không ảnh hưởng đến bất cứ điều gì – Viktor

+0

Có vẻ như tùy biến của bạn không được sử dụng. Bạn có chắc chắn nó nằm trong gói được bao phủ bởi quét thành phần không? –

1

Đề cập đến câu trả lời here, tôi đã tạo src/main/webapp/META-INF/context.xml và thêm vào phần sau.

<?xml version="1.0" encoding="UTF-8"?> 
<Context> 
    <Resources cachingAllowed="true" cacheMaxSize="204800" /> 
</Context> 
Các vấn đề liên quan