前面的文章講解了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:
*/
@Configuration
public 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)
源碼demo git下載地址:https://github.com/feiweiwei/springcloud-sample.git