Tomcat-APR

 

一. 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.gzapr-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模式配置成功

 

 

 

 

 

猜你喜欢

转载自www.cnblogs.com/theworld/p/12172890.html