from: http://nll.im/post/tomcat-apr.html
操作系统:Centos6.3
Tomcat:7.0.42
JDK:1.6.0_45
配置:8G,4核.
最近tomcat负载比较高,默认的配置和bio的处理方式已经无力支撑.据说APR能提升50%~60%的性能,所以尝试下APR优化.
APR介绍:
Tomcat可以使用APR来提供超强的可伸缩性和性能,更好地集成本地服务器技术。 APR(Apache Portable Runtime)是一个高可移植库,它是Apache HTTP Server 2.x的核心。 APR有很多用途,包括访问高级IO功能(例如sendfile,epoll和OpenSSL),OS级别功能(随机数生成,系统状态等等),本地进程管理(共享内存,NT管道和UNIX sockets)。这些功能可以使Tomcat作为一个通常的前台WEB服务器,能更好地和其它本地web技术集成,总体上让Java更有效率作为一个高性能web服务器平台而不是简单作为后台容器。 在产品环境中,特别是直接使用Tomcat做WEB服务器的时候,应该使用Tomcat Native来提高其性能。
1. 服务器安装GCC
客户服务器连不上外网,服务器也没有gcc,所以先使用代理连接外网,修改/etc/yum.conf
,加入代理配置:
proxy=http://10.103.0.46:808
如果需要验证加入用户名密码:
proxy_username=代理服务器用户名 proxy_password=代理服务器密码
yum使用163的Centos源:参考http://mirrors.163.com/.help/centos.html
先备份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
然后下载CentOS6 放入到/etc/yum.repos.d/
(操作前请做好相应备份)
运行以下命令生成缓存:
yum clean all yum makecache
然后安装gcc
:
2. 安装APR
从http://apr.apache.org/download.cgi下载apr,apr-util,apr-iconv. 因为服务器没有配置全局的http代理,只是yum代理,所以下载之后传到服务器即可.
传输完安装apr:
tar zxvf apr-1.5.1.tar.gz cd apr-1.5.1 ./configure --prefix=/usr/local/apr make make install
安装apr-iconv:
tar zxvf apr-iconv-1.2.1.tar.gz cd apr-iconv-1.2.1 ./configure --prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr make make install
安装apr-util:
tar zxvf apr-util-1.5.3.tar.gz cd apr-util-1.5.3 ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr --with-apr-iconv=/usr/local/apr-iconv/bin/apriconv make make install
安装tomcat-native:首先到tomcat/bin目录下,找到对应的tar文件.
tar zxvf tomcat-native.tar.gz cd tomcat-native-1.1.27-src/jni/native/ ./configure --with-apr=/usr/local/apr --with-java-home=/usr/lib/jdk1.6.0_45 make make install
安装完成之后 会出现如下提示信息
Libraries have been installed in: /usr/local/apr/lib
添加环境变量: vi /etc/profile
在文件末尾处添加下面的变量
export LD_LIBRARY_PATH=/usr/local/apr/lib
然后执行下面命令,使环境变量即时生效
以下为完整安装脚本:
#setup apr tar zxvf apr-1.5.1.tar.gz cd apr-1.5.1 ./configure --prefix=/usr/local/apr make && make install cd ../ #setup apr-iconv tar zxvf apr-iconv-1.2.1.tar.gz cd apr-iconv-1.2.1/ ./configure --prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr make && make install cd ../ #setup apr-util tar zxvf apr-util-1.5.3.tar.gz cd apr-util-1.5.3 ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr --with-apr-iconv=/usr/local/apr-iconv/bin/apriconv make && make install #setup tomcat-native cd /app/apache-tomcat-7.0.42/bin tar zxvf tomcat-native.tar.gz cd tomcat-native-1.1.27-src/jni/native ./configure --with-apr=/usr/local/apr --with-java-home=/usr/lib/jdk1.6.0_45 make && make install cd / #LD_LIBRARY_PATH echo -e 'export LD_LIBRARY_PATH=/usr/local/apr/lib' >> /etc/profile export LD_LIBRARY_PATH=/usr/local/apr/lib source /etc/profile
3. 配置Tomcat
修改tomcat配置conf/server.xml
:
<!--The connectors can use a shared executor, you can define one or more named thread pools--> <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="800" minSpareThreads="400"/> <!-- A "Connector" represents an endpoint by which requests are received and responses are returned. Documentation at : Java HTTP Connector: /docs/config/http.html (blocking & non-blocking) Java AJP Connector: /docs/config/ajp.html APR (HTTP/AJP) Connector: /docs/apr.html Define a non-SSL HTTP/1.1 Connector on port 8080 --> <Connector port="80" executor="tomcatThreadPool" protocol="org.apache.coyote.http11.Http11AprProtocol" connectionTimeout="20000" redirectPort="8443" enableLookups="false" acceptCount="1000"/>
修改为Http11AprProtocol
协议.
之后启动tomcat即可.
遇到问题:
SEVERE: Failed to initialize the SSLEngine. org.apache.tomcat.jni.Error: 70023: This function has not been implemented on this platform
请关闭SSL侦听,除非你有使用SSL,修改conf/server.xml
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="off" />
压测结果:
webbench -c 4000 -t 30 http://10.103.10.140/workbench/index.jsp Webbench - Simple Web Benchmark 1.5 Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software. Benchmarking: GET http://10.103.10.140/workbench/index.jsp 4000 clients, running 30 sec. Speed=484340 pages/min, 2441573 bytes/sec. Requests: 242170 susceed, 0 failed.
参考:
http://www.chepoo.com/tomcat-performance-three-times-is-not-a-dream.html
http://rhomobi.com/topics/36
https://gitsea.com/2013/07/02/tomcat-%E5%B9%B6%E5%8F%91%E4%BC%98%E5%8C%96/
http://pengranxiang.iteye.com/blog/1128905
http://tomcat.apache.org/tomcat-7.0-doc/apr.html#Linux
http://www.cnblogs.com/wanghaosoft/archive/2013/02/04/2892099.html