Tomcat 常用运行模式有3种,分别为 bio,nio,apr.生产环境建议用apr,详细请看上一篇博文《Tomcat之――运行模式》

安装APR

[root@liuyazhuang ~]# yum -y install apr apr-devel openssl-devel [root@liuyazhuang ~]# tar zxvf tomcat-native.tar.gz [root@liuyazhuang ~]# cd tomcat-native-1.1.24-src/jni/native [root@liuyazhuang native]# ./configure with-apr=/usr/bin/apr-1-config with-ssl=/usr/include/openssl/ [root@liuyazhuang native]# make && make install

安装完成之后 会出现如下提示信息

Libraries have been installed in: /usr/local/apr/lib

安装成功后还需要对tomcat设置环境变量,方法是在catalina.sh文件中增加1行:

在这段下面添加:

============ # OS specific support. $var _must_ be set to either true or false. cygwin=false darwin=false ============== CATALINA_OPTS=”-Djava.library.path=/usr/local/apr/lib”

修改8080端对应的conf/server.xml

protocol=”org.apache.coyote.http11.Http11AprProtocol”

启动tomcat之后,查看日志

more TOMCAT_HOME/logs/catalina.out Apr 07, 2017 11:49:12 AM org.apache.catalina.core.AprLifecycleListener init INFO: Loaded APR based Apache Tomcat Native library 1.1.31 using APR version 1.3.9. Apr 07, 2017 11:49:12 AM org.apache.catalina.core.AprLifecycleListener init INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. Apr 07, 2017 11:49:12 AM org.apache.catalina.core.AprLifecycleListener initializeSSL INFO: OpenSSL successfully initialized (OpenSSL 1.0.1e 11 Feb 2013) Apr 07, 2017 11:49:13 AM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler [“http-apr-8080”] Apr 07, 2017 11:49:13 AM org.apache.coyote.AbstractProtocol init INFO: Initializing ProtocolHandler [“ajp-apr-8009”] Apr 07, 2017 11:49:13 AM org.apache.catalina.startup.Catalina load INFO: Initialization processed in 1125 ms

Tomcat 优化

1.JVM 调优

在TOMCAT_HOME/bin/catalina.sh 增加如下语句,具体数值视情况而定。
添加到上面CATALINA_OPTS的后面即可:

JAVA_OPTS=-Xms512m -Xmx1024m -XX:PermSize=512M -XX:MaxNewSize=1024m -XX:MaxPermSize=1024m

参数详解

-Xms  JVM初始化堆内存大小
-Xmx  JVM堆的最大内存
-Xss   线程栈大小
-XX:PermSize JVM非堆区初始内存分配大小
-XX:MaxPermSize JVM非堆区最大内存

建议和注意事项:

-Xms和-Xmx选项设置为相同堆内存分配,以避免在每次GC 后调整堆的大小,堆内存建议占内存的60%~80%;非堆内存是不可回收内存,大小视项目而定;线程栈大小推荐256k.

32G内存配置如下:

JAVA_OPTS=-Xms20480m -Xmx20480m -Xss1024K -XX:PermSize=512m -XX:MaxPermSize=2048m

2.关闭DNS反向查询

enableLookups=”false”

3.使用APR

具体安装操作,见文章开头部分

4.优化tomcat参数