Apache安装apr和apr-util作用

谷奕
2023-12-01

要测APR给tomcat带来的好处最好的方法是在慢速网络上(模拟Internet),将Tomcat线程数开到300以上的水平,然后模拟一大堆并发请求。如果不配APR,基本上300个线程狠快就会用满,以后的请求就只好等待。但是配上APR之后,并发的线程数量明显下降,从原来的300可能会马上下降到只有几十,新的请求会毫无阻塞的进来。APR对于Tomcat最大的作用就是socket调度。你在局域网环境测,就算是400个并发,也是一瞬间就处理/传输完毕,但是在真实的Internet环境下,页面处理时间只占0.1%都不到,绝大部分时间都用来页面传输。如果不用APR,一个线程同一时间只能处理一个用户,势必会造成阻塞。所以生产环境下用apr是非常必要的。


具体安装步骤

下载APR相关

wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-1.6.3.tar.gz
wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-iconv-1.2.2.tar.gz
wget http://mirrors.tuna.tsinghua.edu.cn/apache//apr/apr-util-1.6.1.tar.gz
  
#获取运行tomcat(无论内置还是外置,对应tomcat版本下的/bin/tomcat-native.tar.gz 目前版本为1.2.14)
  
tar zxvf apr-1.6.3.tar.gz
tar zxvf apr-util-1.6.1.tar.gz
tar zxvf apr-iconv-1.2.2.tar.gz
tar zxvf tomcat-native.tar.gz

安装APR

cd apr-1.6.3
./configure --prefix=/usr/local/apr
make && make install
  
cd apr-iconv-1.2.2
./configure --prefix=/usr/local/apr-iconv --with-apr=/usr/local/apr
make && make install
  
cd apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr --with-apr-iconv=/usr/local/apr-iconv/bin/apriconv
make && make install
  
cd tomcat-native-1.2.14-src/native
./configure --prefix=/usr/local/apr --with-java-home=/usr/local/jdk1.8.0_131/
#若报类似#include <expat.h>错误,需yum安装expat: yum install expat-devel
make && make install
  
#注意:要求系统OpenSSL library version >= 1.0.2
#若版本太低,安装native会报错。
  
openssl version
  
#以下是安装openssl操作。若版本已达到1.0.2可以不用参考。
#########################################################
下载openssl
wget https://www.openssl.org/source/openssl-1.0.2j.tar.gz
tar zxvf openssl-1.0.2j.tar.gz
cd openssl
./config --prefix=/usr/local/openssl -fPIC
make && make install
mv /usr/bin/openssl ~   //备份
ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl //替换最新openssl执行文件
openssl version  //查看openssl版本是否已替换
  
#安装成功后,可再次执行tomcat-native

配置环境变量

vi /etc/profile
#添加:export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib
  
vi /etc/bashrc
#添加:export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib
  
source /etc/profile
source /etc/bashrc

@Configuration
public class AprConfiguration {
    @Value("${server.tomcat.apr.enabled:false}")
    private boolean enabled;

    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory container = new TomcatEmbeddedServletContainerFactory();
        if (enabled) {
            AprLifecycleListener aprLifecycle = new AprLifecycleListener();
            aprLifecycle.setSSLEngine("off");
            container.setProtocol("org.apache.coyote.http11.Http11AprProtocol");
            container.addContextLifecycleListeners(aprLifecycle);
        }

        return container;
    }
}
server.tomcat.accept-count=3000
server.tomcat.max-threads=1000
server.tomcat.max-connections=1000
server.tomcat.apr.enabled=true




参考:http://blog.51cto.com/wgkgood/432272

 类似资料: