前面的文章講解了tomcat的nio線程模型,nio也是我們?nèi)粘S玫谋容^多的io模式物独,nio對比bio的優(yōu)勢這里就不多說了袜硫,網(wǎng)上有很多的文章說的很詳細(xì),今天在文中主要介紹下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準(zhǔn)備的一套基于操作系統(tǒng)底層的類庫。
1.NIO官研、APR性能對比
這里先說下tomcat從nio切換到apr模式后的性能提升秽澳,使用的是公司的4c 8g的pc server,并發(fā)400的情況下的壓測數(shù)據(jù)戏羽,我這里的性能提升沒有很多網(wǎng)友說的那么大担神,有的網(wǎng)友甚至達(dá)到了80%的性能提升,我來回測了好幾輪大約提升20%左右始花,壓測的項目tps從原來的1500提升到了最高1800左右妄讯,對于這個結(jié)果還是比較滿意的,在不改動業(yè)務(wù)邏輯代碼的情況下能夠達(dá)到這個指標(biāo)酷宵。
2.APR原理
APR的整體模式還是非阻塞IO亥贸,實現(xiàn)的線程模型也是按照NIO的標(biāo)準(zhǔn)模型實現(xiàn)的,從官方文檔(http://apr.apache.org/docs/apr/1.6/modules.html)可以看到APR根據(jù)不同操作系統(tǒng)浇垦,分別用c重寫了大部分IO和系統(tǒng)線程操作模塊炕置,這就是為什么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中內(nèi)嵌的Tomcat默認(rèn)啟動開啟的是NIO模式朴摊,這里如果我們要在linux內(nèi)核的系統(tǒng)上使用APR模式,那么需要安裝一些lib庫,可以通過rpm -q | grep apr
來查看是否安裝了apr煌抒,如果安裝了則不再需要安裝仍劈,如果未安裝則需要安裝下列庫:
1)openssl厕倍,需要版本大于1.0.2,如果不使用https openssl也可以不安裝寡壮,就是在啟動的時候會報openssl的錯誤,直接忽視就可以了;
2)apr况既,可以去官網(wǎng)下載1.6.2最新版進(jìn)行下載 http://apr.apache.org/download.cgi
apr-util这溅,在同一個頁面進(jìn)行下載,最新版本為1.6.0版本
apr-iconv棒仍,在同一個頁面進(jìn)行下載悲靴,最新版本為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 檢查是否符合安裝條件并配置安裝參數(shù),檢查是否缺失類庫乱陡,一般來說如果安裝的不是精簡版系統(tǒng)都是能順利通過的
make & make install
如果不設(shè)置安裝路徑浇揩,那么系統(tǒng)默認(rèn)的安裝路徑為/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類
網(wǎng)上大部分講解配置tomcat apr的文章胳徽,都只是講了如何在獨立tomcat服務(wù)上如何配置apr,只需要修改server.xml中的connnector 的protocol就可以了爽彤,對于springboot會稍微復(fù)雜些养盗,需要增加一個apr配置類在啟動的時候修改Embed的tomcat connector網(wǎng)絡(luò)接入?yún)f(xié)議。
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模式了适篙,可是啟動會發(fā)現(xiàn)報錯往核,而且這個錯誤會讓你很費解,看錯誤提示報的應(yīng)該是服務(wù)啟動端口被占用匙瘪,但是實際查下來這個只是表面現(xiàn)象不是根本原因铆铆。
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后查看系統(tǒng)日志發(fā)現(xiàn)真正的原因是系統(tǒng)找不到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
看到這個錯誤提示一下子豁然開朗丹喻,趕緊在啟動參數(shù)中加上apr的路徑薄货,重新啟動。
java -Djava.library.path=/usr/apr/lib -jar xxxx-0.0.1-SNAPSHOT.jar
啟動成功后看到日志中打出了以下內(nèi)容碍论,則表示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