一. APR
APR(Apache Portable Runtime)是Apache Http Server2.0的核心組件,最為Java程序員我們最常用的方式就是將其集成到tomcat中,能夠提高tomcat的性能。那么他是如何提高性能的呢?APR能夠訪問高級IO特性(例如sendfile, epoll, OpenSSL),OS級別功能(生成隨機數,操作系統狀態管理), 本地進程操作(共享內存, Unix Sockets)等功能,而我們使用一個叫做 Tomcat Native的中間件,來作為APR和Tomcat的通道,就可以使得Tomcat使用到APR的這些高級特性,例如多路復用技術。
二. 軟件下載
軟件名 | 下載地址 |
---|---|
apr-1.7.0.tar.gz | https://mirrors.tuna.tsinghua.edu.cn/apache/apr/ |
apr-util-1.6.1.tar.gz | https://mirrors.tuna.tsinghua.edu.cn/apache/apr/ |
tomcat9 | https://tomcat.apache.org/download-90.cgi |
三. 安裝APR與Tomcat Native
在所有的安裝步驟之前需要保證JDK已經安裝完成
3.1 APR的安裝
A. 解壓apr-1.7.0.tar.gz
和apr-util-1.6.1.tar.gz
tar -zxf apr-1.7.0.tar.gz
tar -zxf apr-util-1.6.1.tar.gz

B. 進入到解壓目錄apr-1.7.0
下,執行如下操作:
./configure --prefix=/usr/local/apr
出現了如下錯誤:
root@ubuntu:/datas/apr-1.7.0# ./configure --prefix=/usr/local/apr
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
Configuring APR library
Platform: x86_64-pc-linux-gnu
checking for working mkdir -p... yes
APR Version: 1.7.0
checking for chosen layout... apr
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/datas/apr-1.7.0':
configure: error: no acceptable C compiler found in $PATH
通過安裝 gcc
可以解決該問題,命令如下:
apt-get install gcc

C. 接着安裝apr-1.7.0.tar.gz
,執行如下命令:
make && make install
出現如下錯誤:
root@ubuntu:/datas/apr-1.7.0# make && make install
Command 'make' not found, but can be installed with:
apt install make
apt install make-guile
解決方式,執行如下命令:
apt install automake autoconf libtool make

D. 安裝apr-util-1.6.1.tar.gz
,執行如下命令:
./configure --with-apr=/usr/local/apr --prefix=/usr/local/apr-utils --with-java-home=/datas/jdk1.8.0_60 && make && make install
安裝過程中會出現如下圖所示的錯誤:
解決方式,執行如下命令:
apt install libexpat1-dev
3.2 安裝tomcat-native
tomcat7以后的版本中都自帶了tomcat-native,在tomcat的解壓目錄下的bin目錄下。
A. 解壓
tar -zxf tomcat-native.tar.gz
B. 進入到tomcat-native的解壓目錄tomcat-native-1.2.23-src/native下,執行如下命令:
./configure --with-apr=/usr/local/apr --with-java-home=/datas/jdk1.8.0_60 --with-ssl=/usr/bin/openssl --with-ssl=yes && make && make install
安裝的過程會出現如下錯誤:
checking for gcc option to accept ISO C89... none needed
checking for OpenSSL library... not found
configure: error: OpenSSL was not found in any of /usr /usr/local /usr/local/ssl /usr/pkg /usr/sfw; use --with-ssl=/path
解決方式,執行如下命令即可解決問題:
apt-get install libssl-dev
解決問題參考資料:https://stackoverflow.com/questions/3016956/how-do-i-install-the-openssl-libraries-on-ubuntu
四. Tomcat配置與啟動
4.1 配置環境變量
在/etc/profile文件末尾加上如下代碼:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib
執行 source /etc/profile 重新加載環境變量
4.2 tomcat配置
A. 在tomcat解壓目錄下的bin目錄下,創建名為setenv.sh
的文件,在文件中寫入如下內容:
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CATALINA_HOME/lib
export LD_LIBRARY_PATH
參考文檔:http://tomcat.apache.org/native-doc/
B. 修改tomcat的配置文件server.xml(apache-tomcat-9.0.22/conf目錄下),修改之前的內容為:
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!-- 中間內容省略 -->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
修改之后的內容為:
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="off" />
<!-- 中間內容省略 -->
<Connector port="8080" protocol="org.apache.coyote.http11.Http11AprProtocol"
connectionTimeout="20000"
redirectPort="8443" />
C. 啟動Tomcat,在日志中如果出現以下內容,表示tomcat的APR模式配置成功