Springboot Tomcat APR模式详解和实践
前面的文章讲解了tomcat的nio线程模型,nio也是我们日常用的比较多的io模式,nio对比bio的优势这里就不多说了,网上有很多的文章说的很详细,今天在文中主要介绍下tomcat的apr模式,apr全称为apache portable runtime,这里套用下wiki对apr的解释:
The Apache Portable Runtime (APR) is a supporting library for the Apache web server. It provides a set of APIs that map to the underlying operating system (OS).[2] Where the OS does not support a particular function, APR will provide an emulation. Thus programmers can use the APR to make a program truly portable across platforms. APR originally formed a part of Apache HTTP Server, but the Apache Software Foundation spun it off into a separate project. Other applications can use it to achieve platform independence.
很好理解就是为apache server准备的一套基于操作系统底层的类库。
1.NIO、APR性能对比
这里先说下tomcat从nio切换到apr模式后的性能提升,使用的是公司的4c 8g的pc server,并发400的情况下的压测数据,我这里的性能提升没有很多网友说的那么大,有的网友甚至达到了80%的性能提升,我来回测了好几轮大约提升20%左右,压测的项目tps从原来的1500提升到了最高1800左右,对于这个结果还是比较满意的,在不改动业务逻辑代码的情况下能够达到这个指标。
2.APR原理
APR的整体模式还是非阻塞IO,实现的线程模型也是按照NIO的标准模型实现的,从官方文档(http://apr.apache.org/docs/apr/1.6/modules.html)可以看到APR根据不同操作系统,分别用c重写了大部分IO和系统线程操作模块,这就是为什么APR在不改动代码的情况下能够提升,具体原理可以参考下我写的Tomcat NIO线程模式这篇文章。
下面这些就是APR重写的模块:
Memory allocation and memory pool functionality
Atomic operations
Dynamic library handling
File I/O
Command-argument parsing
Locking
Hash tables and arrays
Mmap functionality
Network sockets and protocols
Thread, process and mutex functionality
Shared memory functionality
Time routines
User and group ID services
3.Springboot如何开启APR模式
在Springboot中内嵌的Tomcat默认启动开启的是NIO模式,这里如果我们要在linux内核的系统上使用APR模式,那么需要安装一些lib库,可以通过rpm -q | grep apr
来查看是否安装了apr,如果安装了则不再需要安装,如果未安装则需要安装下列库:
1)openssl,需要版本大于1.0.2,如果不使用https openssl也可以不安装,就是在启动的时候会报openssl的错误,直接忽视就可以了;
2)apr,可以去官网下载1.6.2最新版进行下载 http://apr.apache.org/download.cgi
apr-util,在同一个页面进行下载,最新版本为1.6.0版本
apr-iconv,在同一个页面进行下载,最新版本为1.2.1版本
tomcat-native,在tomcat中自带了安装包,可以在tomcat的bin目录下找到tomcat-native.tar;
安装apr
下载apr安装包apr-1.6.2.tar.gz
tar -xvf apr-1.6.2.tar.gz
cd apr-1.6.2
./configure 检查是否符合安装条件并配置安装参数,检查是否缺失类库,一般来说如果安装的不是精简版系统都是能顺利通过的
make & make install
如果不设置安装路径,那么系统默认的安装路径为/usr/local/apr/lib
安装apr-util
下载apr-util安装包apr-util-1.6.0.tar.gz
tar -xvf apr-util-1.6.0.tar.gz
cd apr-util-1.6.0
./configure --with-apr=/usr/local/apr/lib --with-java-home=/usr/lib/jvm/jdk-8u144-linux-x64/jdk1.8.0_144 安装apr-util需要配置apr路径和jvm路径,否则会报错找不到apr
make & make install
安装apr-iconv
下载apr-iconv.tar.gz
tar -xvf apr-iconv.tar.gz
cd apr-iconv
./configure --with-apr=/usr/local/apr/lib --with-java-home=/usr/lib/jvm/jdk-8u144-linux-x64/jdk1.8.0_144
make & make install
安装tomcat-native
cd tomcat/bin
tar -xvf tomcat-native
cd tomcat-native
./configure --with-apr=/usr/local/apr/lib --with-java-home=/usr/lib/jvm/jdk-8u144-linux-x64/jdk1.8.0_144
make & make install
到此安装工作就全部完成了,如果要查看本机安装的java路径可以通过which java查看
配置apr
vi /etc/profile
在profile最前面加上 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr-1.6.2/lib
命令行输入 source /etc/profile
使之生效
新增APRConfig类
网上大部分讲解配置tomcat apr的文章,都只是讲了如何在独立tomcat服务上如何配置apr,只需要修改server.xml中的connnector 的protocol就可以了,对于springboot会稍微复杂些,需要增加一个apr配置类在启动的时候修改Embed的tomcat connector网络接入协议。
import org.apache.catalina.LifecycleListener;import org.apache.catalina.core.AprLifecycleListener;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * @Author: feiweiwei * @Description: APR配置 * @Created Date: 09:23 17/9/7. * @Modify by: */@Configurationpublic class APRConfig { @Value("${tomcat.apr:false}") private boolean enabled; @Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory container = new TomcatEmbeddedServletContainerFactory(); if (enabled) { LifecycleListener arpLifecycle = new AprLifecycleListener(); container.setProtocol("org.apache.coyote.http11.Http11AprProtocol"); container.addContextLifecycleListeners(arpLifecycle); } return container; } }
启动springboot
本以为这样做完后可以直接启动springboot打开apr模式了,可是启动会发现报错,而且这个错误会让你很费解,看错误提示报的应该是服务启动端口被占用,但是实际查下来这个只是表面现象不是根本原因。
org.springframework.boot.context.embedded.tomcat.ConnectorStartFailedException: Connector configured to listen on port 8081 failed to start... ***************************APPLICATION FAILED TO START***************************Description:The Tomcat connector configured to listen on port 8081 failed to start. The port may already be in use or the connector may be misconfigured.
打开debug后查看系统日志发现真正的原因是系统找不到apr的lib库。
Caused by: org.apache.catalina.LifecycleException: The configured protocol [org.apache.coyote.http11.Http11AprProtocol] requires the APR/native library which is not available
看到这个错误提示一下子豁然开朗,赶紧在启动参数中加上apr的路径,重新启动。
java -Djava.library.path=/usr/apr/lib -jar xxxx-0.0.1-SNAPSHOT.jar
启动成功后看到日志中打出了以下内容,则表示apr模式启动成功,开始享受APR带来的飞速感受吧。
2017-10-12 15:31:19,032 - Initializing ProtocolHandler ["http-apr-8081"]2017-10-12 15:31:19,051 - Starting ProtocolHandler ["http-apr-8081"]2017-10-12 15:31:19,080 - Tomcat started on port(s): 8081 (http)
作者:monkey01
链接:https://www.jianshu.com/p/f716726ba340
共同学习,写下你的评论
评论加载中...
作者其他优质文章