from: http://nll.im/post/tomcat-apr.html
操作系統(tǒng):Centos6.3
Tomcat:7.0.42
JDK:1.6.0_45
配置:8G,4核.
最近tomcat負(fù)載比較高,默認(rèn)的配置和bio的處理方式已經(jīng)無力支撐.據(jù)說APR能提升50%~60%的性能,所以嘗試下APR優(yōu)化.
APR介紹:
Tomcat可以使用APR來提供超強(qiáng)的可伸縮性和性能,更好地集成本地服務(wù)器技術(shù)。 APR(Apache Portable Runtime)是一個高可移植庫,它是Apache HTTP Server 2.x的核心。 APR有很多用途,包括訪問高級IO功能(例如sendfile,epoll和OpenSSL),OS級別功能(隨機(jī)數(shù)生成,系統(tǒng)狀態(tài)等等),本地進(jìn)程管理(共享內(nèi)存,NT管道和UNIX sockets)。這些功能可以使Tomcat作為一個通常的前臺WEB服務(wù)器,能更好地和其它本地web技術(shù)集成,總體上讓Java更有效率作為一個高性能web服務(wù)器平臺而不是簡單作為后臺容器。 在產(chǎn)品環(huán)境中,特別是直接使用Tomcat做WEB服務(wù)器的時候,應(yīng)該使用Tomcat Native來提高其性能。
1. 服務(wù)器安裝GCC
客戶服務(wù)器連不上外網(wǎng),服務(wù)器也沒有g(shù)cc,所以先使用代理連接外網(wǎng),修改/etc/yum.conf
,加入代理配置:
proxy=http://10.103.0.46:808
如果需要驗證加入用戶名密碼:
proxy_username=代理服務(wù)器用戶名 proxy_password=代理服務(wù)器密碼
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/
(操作前請做好相應(yīng)備份)
運(yùn)行以下命令生成緩存:
yum clean all yum makecache
然后安裝gcc
:
2. 安裝APR
從http://apr.apache.org/download.cgi下載apr,apr-util,apr-iconv. 因為服務(wù)器沒有配置全局的http代理,只是yum代理,所以下載之后傳到服務(wù)器即可.
傳輸完安裝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目錄下,找到對應(yīng)的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
安裝完成之后 會出現(xiàn)如下提示信息
Libraries have been installed in: /usr/local/apr/lib
添加環(huán)境變量: vi /etc/profile
在文件末尾處添加下面的變量
export LD_LIBRARY_PATH=/usr/local/apr/lib
然后執(zhí)行下面命令,使環(huán)境變量即時生效
以下為完整安裝腳本:
#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
協(xié)議.
之后啟動tomcat即可.
遇到問題:
SEVERE: Failed to initialize the SSLEngine. org.apache.tomcat.jni.Error: 70023: This function has not been implemented on this platform
請關(guān)閉SSL偵聽,除非你有使用SSL,修改conf/server.xml
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="off" />
壓測結(jié)果:
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