Tomcat8 security configuration and performance optimization

1. Regarding the number of Tomcat sessions, 
this can be viewed directly from Tomcat's web management interface 
or with the help of a third-party tool Lambda Probe. It has a little more functionality than Tomcat's own management, but not much 

2 .Monitor the memory usage of Tomcat Using 
the jconsole that comes with JDK, you can clearly see the memory usage, thread status, the total amount of currently loaded classes, etc. 
The jvisualvm that comes with JDK can download plug-ins (such as GC, etc.), More information can be viewed. If you are analyzing the local Tomcat, you can also perform memory sampling, etc., and check the usage of each class. 

3. Print the loading of classes and the recycling of objects. 
This can be configured by configuring the startup parameters of the JVM, and printing these information (to the screen ( The default will also go to catalina.log) or file), the specific parameters are as follows: 
-XX:+PrintGC: output format: [GC 118250K->113543K(130112K), 0.0094143 secs] [Full GC 121376K->10414K(130112K), 0.0650971 secs] 
-XX:+PrintGCDetails: Output form: [GC [DefNew: 8614K->781K(9088K), 0.0123035 secs] 118250K->113543K(130112K), 0.0124633 secs] [GC [DefNew: 8614K->8614K(9814K->8614K) ), 0.0000665 secs][Tenured: 112761K->10414K(121024K), 0.0433488 secs] 121376K->10414K(130112K), 0.0436268 secs] 
-XX:+PrintGCTimeStamps -XX:+PrintGC:PrintGCTimeStamps can be mixed with the above two, the output format: 11.851: [GC 98328K->93620K(130112K), 0.0082960 secs] 
-XX:+PrintGCApplicationConcurrentTime: print before each garbage collection , the uninterrupted execution time of the program. Can be mixed with the above. Output form: Application time: 0.5291524 seconds 
-XX:+PrintGCApplicationStoppedTime: Prints the time the program was paused during garbage collection. Can be mixed with the above. Output form: Total time for which application threads were stopped: 0.0468229 seconds 
-XX:PrintHeapAtGC: Print detailed stack information before and after GC 
-Xloggc:filename: Use in conjunction with the above to record relevant log information to a file for analysis 

-verbose: class monitors the status of loaded classes 
-verbose:gc Displays information on the output device when memory reclamation occurs in the virtual machine 
-verbose:jni Outputs information about native method calls, generally used to diagnose jni call error messages 

4. Add JMS remote monitoring 
for Tomcat deployed on other machines in the LAN can open the JMX monitoring port, and other machines in the LAN can view some commonly used parameters through this port (but some more complex functions are not supported), which can also be configured in the JVM startup parameters. , configured as follows: 
-Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false 
-Djava.rmi.server.hostname=192.168.71.38 Set the IP address of the JVM's JMS monitoring monitoring, mainly to prevent The wrong monitor is the intranet address of 127.0.0.1 - 
Dcom.sun.management.jmxremote.port=1090 Set the port for JVM JMS monitoring - 
Dcom.sun.management.jmxremote.ssl=false Set JVM JMS monitoring does not use SSL 
-Dcom.sun.management.jmxremote.authenticate=false Setting the JMS monitoring of the JVM does not require authentication 


5. Professional analysis tools include 
IBM ISA, JProfiler, etc. You can search online for specific monitoring and analysis methods. 

 

 

Cluster solution

单个Tomcat的处理性能是有限的,当并发量较大的时候,就需要有部署多套来进行负载均衡了。 

集群的关键点有以下几点: 
1.引入负载端 
软负载可以使用nginx或者apache来进行,主要是使用一个分发的功能 
参考: 
http://ajita.iteye.com/blog/1715312(nginx负载) 
http://ajita.iteye.com/blog/1717121(apache负载) 

2.共享session处理 
目前的处理方式有如下几种: 
1).使用Tomcat本身的Session复制功能 
参考http://ajita.iteye.com/blog/1715312(Session复制的配置) 
方案的有点是配置简单,缺点是当集群数量较多时,Session复制的时间会比较长,影响响应的效率 
2).使用第三方来存放共享Session 
目前用的较多的是使用memcached来管理共享Session,借助于memcached-sesson-manager来进行Tomcat的Session管理 
参考http://ajita.iteye.com/blog/1716320(使用MSM管理Tomcat集群session) 
3).使用黏性session的策略 
对于会话要求不太强(不涉及到计费,失败了允许重新请求下等)的场合,同一个用户的session可以由nginx或者apache交给同一个Tomcat来处理,这就是所谓的session sticky策略,目前应用也比较多 
参考:http://ajita.iteye.com/blog/1848665(tomcat session sticky) 
nginx默认不包含session sticky模块,需要重新编译才行(windows下我也不知道怎么重新编译) 
优点是处理效率高多了,缺点是强会话要求的场合不合适 

3.小结 
以上是实现集群的要点,其中1和2可以组合使用,具体场景具体分析吧~

 

JVM优化

Tomcat本身还是运行在JVM上的,通过对JVM参数的调整我们可以使Tomcat拥有更好的性能。针对JVM的优化目前主要在两个方面: 

1.内存调优 
内存方式的设置是在catalina.sh中,调整一下JAVA_OPTS变量即可,因为后面的启动参数会把JAVA_OPTS作为JVM的启动参数来处理。 

具体设置如下: 
JAVA_OPTS="$JAVA_OPTS -Xmx3550m -Xms3550m -Xss128k -XX:NewRatio=4 -XX:SurvivorRatio=4" 

其各项参数如下: 
-Xmx3550m:设置JVM最大可用内存为3550M。 
-Xms3550m:设置JVM促使内存为3550m。此值可以设置与-Xmx相同,以避免每次垃圾回收完成后JVM重新分配内存。 
-Xmn2g:设置年轻代大小为2G。整个堆大小=年轻代大小 + 年老代大小 + 持久代大小。持久代一般固定大小为64m,所以增大年轻代后,将会减小年老代大小。此值对系统性能影响较大,Sun官方推荐配置为整个堆的3/8。 
-Xss128k:设置每个线程的堆栈大小。JDK5.0以后每个线程堆栈大小为1M,以前每个线程堆栈大小为256K。更具应用的线程所需内存大小进行 调整。在相同物理内存下,减小这个值能生成更多的线程。但是操作系统对一个进程内的线程数还是有限制的,不能无限生成,经验值在3000~5000左 右。 

-XX:NewRatio=4:设置年轻代(包括Eden和两个Survivor区)与年老代的比值(除去持久代)。设置为4,则年轻代与年老代所占比值为1:4,年轻代占整个堆栈的1/5 
-XX:SurvivorRatio=4:设置年轻代中Eden区与Survivor区的大小比值。设置为4,则两个Survivor区与一个Eden区的比值为2:4,一个Survivor区占整个年轻代的1/6 
-XX:MaxPermSize=16m:设置持久代大小为16m。 
-XX:MaxTenuringThreshold=0:设置垃圾最大年龄。如果设置为0的话,则年轻代对象不经过Survivor区,直接进入年老代。 对于年老代比较多的应用,可以提高效率。如果将此值设置为一个较大值,则年轻代对象会在Survivor区进行多次复制,这样可以增加对象再年轻代的存活 时间,增加在年轻代即被回收的概论。 


2.垃圾回收策略调优 
垃圾回收的设置也是在catalina.sh中,调整JAVA_OPTS变量。 
具体设置如下: 
JAVA_OPTS="$JAVA_OPTS -Xmx3550m -Xms3550m -Xss128k -XX:+UseParallelGC  -XX:MaxGCPauseMillis=100" 
具体的垃圾回收策略及相应策略的各项参数如下: 

串行收集器(JDK1.5以前主要的回收方式) 
-XX:+UseSerialGC:设置串行收集器 

并行收集器(吞吐量优先) 
示例: 
java -Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:+UseParallelGC  -XX:MaxGCPauseMillis=100 

-XX:+UseParallelGC:选择垃圾收集器为并行收集器。此配置仅对年轻代有效。即上述配置下,年轻代使用并发收集,而年老代仍旧使用串行收集。 
-XX:ParallelGCThreads=20:配置并行收集器的线程数,即:同时多少个线程一起进行垃圾回收。此值最好配置与处理器数目相等。 
-XX:+UseParallelOldGC:配置年老代垃圾收集方式为并行收集。JDK6.0支持对年老代并行收集 
-XX:MaxGCPauseMillis=100:设置每次年轻代垃圾回收的最长时间,如果无法满足此时间,JVM会自动调整年轻代大小,以满足此值。 
-XX:+UseAdaptiveSizePolicy:设置此选项后,并行收集器会自动选择年轻代区大小和相应的Survivor区比例,以达到目标系统规定的最低相应时间或者收集频率等,此值建议使用并行收集器时,一直打开。 

并发收集器(响应时间优先) 
示例:java -Xmx3550m -Xms3550m -Xmn2g -Xss128k -XX:+UseConcMarkSweepGC 
-XX:+UseConcMarkSweepGC:设置年老代为并发收集。测试中配置这个以后,-XX:NewRatio=4的配置失效了,原因不明。所以,此时年轻代大小最好用-Xmn设置。 
-XX:+UseParNewGC: 设置年轻代为并行收集。可与CMS收集同时使用。JDK5.0以上,JVM会根据系统配置自行设置,所以无需再设置此值。 
-XX:CMSFullGCsBeforeCompaction:由于并发收集器不对内存空间进行压缩、整理,所以运行一段时间以后会产生“碎片”,使得运行效率降低。此值设置运行多少次GC以后对内存空间进行压缩、整理。 
-XX:+UseCMSCompactAtFullCollection:打开对年老代的压缩。可能会影响性能,但是可以消除碎片 

3.小结 
在内存设置中需要做一下权衡 
1)内存越大,一般情况下处理的效率也越高,但同时在做垃圾回收的时候所需要的时间也就越长,在这段时间内的处理效率是必然要受影响的。 
2)在大多数的网络文章中都推荐 Xmx和Xms设置为一致,说是避免频繁的回收,这个在测试的时候没有看到明显的效果,内存的占用情况基本都是锯齿状的效果,所以这个还要根据实际情况来定。

 

Server.xml的Connection优化

Tomcat的Connector是Tomcat接收HTTP请求的关键模块,我们可以配置它来指定IO模式,以及处理通过这个Connector接受到的请求的处理线程数以及其它一些常用的HTTP策略。其主要配置参数如下: 

1.指定使用NIO模型来接受HTTP请求 
protocol="org.apache.coyote.http11.Http11NioProtocol" 指定使用NIO模型来接受HTTP请求。默认是BlockingIO,配置为protocol="HTTP/1.1" 
acceptorThreadCount="2" 使用NIO模型时接收线程的数目 

2.指定使用线程池来处理HTTP请求 
首先要配置一个线程池来处理请求(与Connector是平级的,多个Connector可以使用同一个线程池来处理请求) 
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-" 
maxThreads="1000" minSpareThreads="50" maxIdleTime="600000"/> 
<Connector port="8080"
executor="tomcatThreadPool" 指定使用的线程池 

3.指定BlockingIO模式下的处理线程数目 
maxThreads="150"//Tomcat使用线程来处理接收的每个请求。这个值表示Tomcat可创建的最大的线程数。默认值200。可以根据机器的时期性能和内存大小调整,一般可以在400-500。最大可以在800左右。 
minSpareThreads="25"---Tomcat初始化时创建的线程数。默认值4。如果当前没有空闲线程,且没有超过maxThreads,一次性创建的空闲线程数量。Tomcat初始化时创建的线程数量也由此值设置。 
maxSpareThreads="75"--一旦创建的线程超过这个值,Tomcat就会关闭不再需要的socket线程。默认值50。一旦创建的线程 超过此数值,Tomcat会关闭不再需要的线程。线程数可以大致上用 “同时在线人数*每秒用户操作次数*系统平均操作时间” 来计算。 
acceptCount="100"----指定当所有可以使用的处理请求的线程数都被使用时,可以放到处理队列中的请求数,超过这个数的请求将不予处 理。默认值10。如果当前可用线程数为0,则将请求放入处理队列中。这个值限定了请求队列的大小,超过这个数值的请求将不予处理。 
connectionTimeout="20000" --网络连接超时,默认值20000,单位:毫秒。设置为0表示永不超时,这样设置有隐患的。通常可设置为30000毫秒。 

4.其它常用设置 
maxHttpHeaderSize="8192" http请求头信息的最大程度,超过此长度的部分不予处理。一般8K。 
URIEncoding="UTF-8" 指定Tomcat容器的URL编码格式。 
disableUploadTimeout="true" 上传时是否使用超时机制 
enableLookups="false"--是否反查域名,默认值为true。为了提高处理能力,应设置为false 
compression="on"   打开压缩功能 
compressionMinSize="10240" 启用压缩的输出内容大小,默认为2KB 
noCompressionUserAgents="gozilla, traviata"   对于以下的浏览器,不启用压缩 
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain" 哪些资源类型需要压缩 

5.小结 
关于Tomcat的Nio和ThreadPool,本身的引入就提高了处理的复杂性,所以对于效率的提高有多少,需要实际验证一下。 

6.配置示例 
<Connector port="8080" 
redirectPort="8443"
maxThreads="150" 
minSpareThreads="25" 
maxSpareThreads="75" 
acceptCount="100" 
connectionTimeout="20000" 
protocol="HTTP/1.1" 

maxHttpHeaderSize="8192" 
URIEncoding="UTF-8" 
disableUploadTimeout="true" 
enableLookups="false" 
compression="on" 
compressionMinSize="10240" 
noCompressionUserAgents="gozilla, traviata" 
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain"> 
... 
</Connector>

 

 

管理AJP端口

AJP是为 Tomcat 与 HTTP 服务器之间通信而定制的协议,能提供较高的通信速度和效率。如果tomcat前端放的是apache的时候,会使用到AJP这个连接器。由于我们公司前端是由nginx做的反向代理,因此不使用此连接器,因此需要注销掉该连接器。

<!-- <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> -->



默认 Tomcat 是开启了对war包的热部署的。为了防止被植入木马等恶意程序,因此我们要关闭自动部署。

修改实例:

      <Host name="localhost"  appBase=""             unpackWARs="false" autoDeploy="false">
http://blog.csdn.net/wanglei_storage/article/details/50225779

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326771161&siteId=291194637