问题
我们有一个Tomcat 7.0.35
运行25个webapps,它们共享相同的库,具有相同的版本(保证永远不会改变!)。我想,而不是每个项目加载相同的库,我可以将它们移动到${catalina.home}/lib
,这导致所需的内存负载减少。
然而,这需要付出代价:每个Web应用程序的部署时间从5秒增加到2分钟,这是不可行的!
我做了一些阅读(例如Class Loader HOW-TO),遗憾的是我无法找到减少启动时间的方法。有没有办法解决这个问题?
请求的分析信息
我在两个不同的场景中运行了jstack
和jvisualvm
,我将在此处提供一些调查结果(如果您需要其他信息,请与我们联系):
org.apache.catalina.loader.WebappClassLoader.findResourceInternal
"localhost-startStop-1" daemon prio=10 tid=0x00007f17f8001800 nid=0x13b8 runnable [0x00007f183c800000]
java.lang.Thread.State: RUNNABLE
at java.text.MessageFormat.subformat(MessageFormat.java:1250)
at java.text.MessageFormat.format(MessageFormat.java:819)
at org.apache.naming.StringManager.getString(StringManager.java:145)
at org.apache.naming.resources.BaseDirContext.lookup(BaseDirContext.java:500)
at org.apache.naming.resources.ProxyDirContext.lookup(ProxyDirContext.java:310)
at org.apache.catalina.loader.WebappClassLoader.findResourceInternal(WebappClassLoader.java:3011)
at org.apache.catalina.loader.WebappClassLoader.findResource(WebappClassLoader.java:1262)
at org.apache.catalina.loader.WebappClassLoader.getResourceAsStream(WebappClassLoader.java:1499)
at org.apache.catalina.startup.ContextConfig.populateJavaClassCache(ContextConfig.java:2302)
at org.apache.catalina.startup.ContextConfig.populateJavaClassCache(ContextConfig.java:2294)
at org.apache.catalina.startup.ContextConfig.populateJavaClassCache(ContextConfig.java:2308)
at org.apache.catalina.startup.ContextConfig.populateJavaClassCache(ContextConfig.java:2291)
at org.apache.catalina.startup.ContextConfig.checkHandlesTypes(ContextConfig.java:2197)
at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2154)
at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:2034)
at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1990)
at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1976)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1326)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:882)
- locked <0x00000007f52f3ec0> (a org.apache.catalina.startup.ContextConfig)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:379)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5343)
- locked <0x00000007f52f3bb8> (a org.apache.catalina.core.StandardContext)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
- locked <0x00000007f52f3bb8> (a org.apache.catalina.core.StandardContext)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:902)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:879)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:636)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1236)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1877)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Locked ownable synchronizers:
- <0x00000007846dfc70> (a java.util.concurrent.ThreadPoolExecutor$Worker)
at org.apache.catalina.loader.WebappClassLoader.getResourceAsStream
导致令人难以置信的部署时间。在项目部署期间,JVisualVM为此方法显示98%+“自我时间”
运行方法的线程的jstack输出:
"localhost-startStop-1" daemon prio=10 tid=0x00007f6280001000 nid=0x6ba3 runnable [0x00007f62c4950000]
java.lang.Thread.State: RUNNABLE
at java.lang.String.intern(Native Method)
at java.util.jar.Attributes$Name.<init>(Attributes.java:466)
at java.util.jar.Attributes.putValue(Attributes.java:168)
at java.util.jar.Attributes.read(Attributes.java:421)
at java.util.jar.Manifest.read(Manifest.java:251)
at sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVerifier.java:252)
at sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier.java:239)
at java.util.jar.JarVerifier.processEntry(JarVerifier.java:307)
at java.util.jar.JarVerifier.update(JarVerifier.java:218)
at java.util.jar.JarFile.initializeVerifier(JarFile.java:345)
at java.util.jar.JarFile.getInputStream(JarFile.java:412)
- locked <0x00000007e8e9d890> (a sun.net.www.protocol.jar.URLJarFile)
at sun.net.www.protocol.jar.JarURLConnection.getInputStream(JarURLConnection.java:162)
at java.net.URLClassLoader.getResourceAsStream(URLClassLoader.java:233)
at org.apache.catalina.loader.WebappClassLoader.getResourceAsStream(WebappClassLoader.java:1528)
at org.apache.catalina.startup.ContextConfig.populateJavaClassCache(ContextConfig.java:2302)
at org.apache.catalina.startup.ContextConfig.populateJavaClassCache(ContextConfig.java:2294)
at org.apache.catalina.startup.ContextConfig.populateJavaClassCache(ContextConfig.java:2308)
at org.apache.catalina.startup.ContextConfig.populateJavaClassCache(ContextConfig.java:2294)
at org.apache.catalina.startup.ContextConfig.checkHandlesTypes(ContextConfig.java:2197)
at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2154)
at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:2034)
at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1990)
at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1976)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1326)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:882)
- locked <0x00000007858db6e0> (a org.apache.catalina.startup.ContextConfig)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:379)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:118)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:91)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5346)
- locked <0x00000007858db318> (a org.apache.catalina.core.StandardContext)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153)
- locked <0x00000007858db318> (a org.apache.catalina.core.StandardContext)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:902)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:879)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:636)
at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1236)
at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1877)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Locked ownable synchronizers:
- <0x000000078460e618> (a java.util.concurrent.ThreadPoolExecutor$Worker)
答案 0 :(得分:2)
WebappClassLoader首先搜索自己的类路径。因此,对于不在本地类路径中的每个类,将查看所有jar文件。只有这样,它才会查看公共类加载器的委托。
假设查看所有战争的罐子需要花费x倍的时间,对于罐子,其父类装载器需要花费大量时间。
平均从正确的类加载器加载一个类将花费一半的时间来扫描它们。
从WebappClassLoader加载一个类需要x / 2次。
从父类加载器加载一个类将需要x + y / 2.
根据x和y,您可以更改使用类加载器的顺序。 如果y大于x,则应保持默认设置,如果x大于您可以尝试反转查找顺序。如果这样做,从公共类加载器加载一个类将需要y / 2次,但WebappClassLoader中的类将需要y + x / 2.
WebbappClassLoader具有此设置的委托属性,它应为true。 见Common Attributes
答案 1 :(得分:0)
简而言之:在shared.loader
conf/catalina.properties
添加值
shared.loader=shared/*.jar
更多的话:
# List of comma-separated paths defining the contents of the "shared"
# classloader. Prefixes should be used to define what is the repository type.
# Path may be relative to the CATALINA_BASE path or absolute. If left as blank,
# the "common" loader will be used as Catalina's "shared" loader.
# Examples:
# "foo": Add this folder as a class repository
# "foo/*.jar": Add all the JARs of the specified folder as class
# repositories
# "foo/bar.jar": Add bar.jar as a class repository
# Please note that for single jars, e.g. bar.jar, you need the URL form
# starting with file:.
shared.loader=shared/*.jar
默认情况下,Tomcat 6+具有层次结构http://tomcat.apache.org/tomcat-7.0-doc/class-loader-howto.html
Bootstrap
|
System
|
Common
/ \
Webapp1 Webapp2 ...
与5.5 http://tomcat.apache.org/tomcat-5.5-doc/class-loader-howto.html
中的默认值进行比较 Bootstrap
|
System
|
Common
/ \
Catalina Shared
/ \
Webapp1 Webapp2 ...
因此,通过启用只包含您的jar的共享位置,以及与已经有21个罐子的Tomcat lib文件夹分开,您可以获得至少更清晰的罐子视图。