tomcat使用APR提高效能
阿新 • • 發佈:2018-12-25
作業系統: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=代理伺服器密碼
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
:
yum -y install 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
然後執行下面命令,使環境變數即時生效
source /etc/profile
以下為完整安裝指令碼:
#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.
參考: