CentOS7下配置Tomcat以APR模式+Tomcat Native运行
在慢速网络上Tomcat线程数开到300以上的水平,不配APR,基本上300个线程狠快就会用满,以后的请求就只好等待。但是配上APR之后,Tomcat将以JNI的形式调用Apache HTTP服务器的核心动态链接库来处理文件读取或网络传输操作,这时并发的线程数量明显下降,从原来的300可能会马上下降到只有几十,新的请求会毫无阻塞的进来。
Tomcat 可以使用 apr 来提供更好的伸缩性、性能和集成到本地服务器技术,用来提高 tomcat 的性能。而tomcat native在具体的运行平台上提供了一种优化技术,它本身是基于 ARP(Apache Portable(轻便) Runtime)技术。在我们应用了tomcat native后,tomcat在跟操作系统级别的交互方面可以做得更好,并且它更像apache一样,可以更好地作为一台 web server。tomcat 可以利用apache的apr接口,使用操作系统的部分本地操作,从而提升性能APR 提升的是静态页面处理能力。
(1).Tomcat的三种运行模式介绍
Tomcat 有三种 运行模式,bio、nio、apr。
bio(blocking I/O),阻塞式I/O操作,表示Tomcat使用的是传统的Java I/O操作(即java.io包及其子包),对于每一个请求都要创建一个线程来进行处理,所以开销较大不适合处理高并发的场景。Tomcat在默认情况下,就是以bio模式运行的。遗憾的是,就一般而言,bio模式是三种运行模式中性能最低的一种。我们可以通过Tomcat Manager来查看服务器的当前状态。
nio是Java SE 1.4及后续版本提供的一种新的I/O操作方式(即java.nio包及其子包),基于缓冲区、并能提供非阻塞I/O操作的Java API实现,比传统的I/O处理方式(bio)有更高的并发运行性能。是Tomcat8的默认运行模式。
apr(Apache Portable Runtime/Apache可移植运行库)是Apache HTTP服务器的支持库。从操作系统级别解决异步I/O问题,大幅度提高服务器的并发处理性能,也是Tomcat生产环境运行的首选方式。要tomcat支持apr,必须安装apr和native,这样tomcat才可以利用apache的apr接口,使用操作系统的部分本地操作,从而提升性能。
(2).安装Tomcat Native
注意:Tomcat Native已经集成到Tomcat8安装目录下的bin目录。
1)安装依赖包
注意:这里有个apr-devel,作为依赖apr也会被安装。
[root@youxi1 ~]# yum -y install apr-devel gcc gcc-c++ openssl-devel openssl
2)解压Tomcat Native包,编译安装
[root@youxi1 ~]# tar zxf /usr/local/tomcat8.5/bin/tomcat-native.tar.gz -C /usr/local/src/ [root@youxi1 ~]# cd /usr/local/src/tomcat-native-1.2.18-src/native/ [root@youxi1 native]# ./configure --with-apr=/usr/ --with-java-home=/usr/local/jdk1.8.0_191 --with-ssl [root@youxi1 native]# make && make install //编译安装 ---------------------------------------------------------------------- Libraries have been installed in: //库安装在如下地址 /usr/local/apr/lib If you ever happen to want to link against installed libraries in a given directory, LIBDIR, you must either use libtool, and specify the full pathname of the library, or use the `-LLIBDIR' flag during linking and do at least one of the following: - add LIBDIR to the `LD_LIBRARY_PATH' environment variable during execution - add LIBDIR to the `LD_RUN_PATH' environment variable during linking - use the `-Wl,-rpath -Wl,LIBDIR' linker flag - have your system administrator add LIBDIR to `/etc/ld.so.conf' See any operating system documentation about shared libraries for more information, such as the ld(1) and ld.so(8) manual pages. ---------------------------------------------------------------------- [root@youxi1 native]# echo $? 0
说明:./configure有两个参数需要注意下,--with-apr=/usr/提供的是查找apr的路径可以更详细的一点,--with-java-home后面跟随的地址要和"echo $JAVA_HOME"输出地址一致。
3)根据安装完成后的提示操作
把库添加到/etc/ld.so.conf
[root@youxi1 native]# vim /etc/ld.so.conf /usr/local/apr/lib //添加该行 [root@youxi1 native]# ldconfig //重新加载 [root@youxi1 native]# echo "ldconfig" >> /etc/rc.local //这是开机自启文件的软链接,需要源文件有执行权限 [root@youxi1 native]# ll /etc/rc.local lrwxrwxrwx. 1 root root 13 5月 14 09:12 /etc/rc.local -> rc.d/rc.local
配置环境变量
[root@youxi1 native]# vim /etc/profile.d/jdk8.sh //这是之前的JDK环境变量文件 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib //新增一行 [root@youxi1 native]# . /etc/profile.d/jdk8.sh //刷新
(3).配置APR
1)修改conf/server.xml里的协议
这个协议是<service>标签里的<Connector>标签的参数protocol,默认协议是HTTP/1.1。我们将协议修改为org.apache.coyote.http11.Http11AprProtocol
[root@youxi1 native]# vim /usr/local/tomcat8.5/conf/server.xml <Connector port="8080" protocol="org.apache.coyote.http11.Http11AprProtocol" //将HTTP/1.1修改为当前值 connectionTimeout="20000" redirectPort="8443" />
2)在bin/catalina.sh中引用apr
[root@youxi1 native]# vim /usr/local/tomcat8.5/bin/catalina.sh # Register custom URL handlers # Do this here so custom URL handles (specifically 'war:...') can be used in the security policy JAVA_OPTS="$JAVA_OPTS -Djava.protocol.handler.pkgs=org.apache.catalina.webresour ces" //这是第253行,在这下面添加一行参数 JAVA_OPTS="$JAVA_OPTS -Djava.library.path=/usr/local/apr/lib" //只要添加这一行
注意:只要添加一行。
3)重启Tomcat
[root@youxi1 native]# ps aux | grep /usr/local/tomcat8.5/ root 1113 0.6 11.9 2327404 119308 ? Sl 17:15 0:31 /usr/local/jdk1.8.0_191/bin/java -Djava.util.logging.config.file=/usr/local/tomcat8.5/conf/logging.properties
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
-Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat8.5/bin/bootstrap.jar:/usr/local/tomcat8.5/bin/tomcat-juli.jar
-Dcatalina.base=/usr/local/tomcat8.5 -Dcatalina.home=/usr/local/tomcat8.5 -Djava.io.tmpdir=/usr/local/tomcat8.5/temp org.apache.catalina.startup.Bootstrap start root 3078 0.0 0.1 112728 1004 pts/0 R+ 18:30 0:00 grep --color=auto /usr/local/tomcat8.5/ [root@youxi1 native]# kill -9 1113 [root@youxi1 native]# !ps ps aux | grep /usr/local/tomcat8.5/ root 3081 0.0 0.1 112728 1000 pts/0 R+ 18:30 0:00 grep --color=auto /usr/local/tomcat8.5/ [root@youxi1 native]# /usr/local/tomcat8.5/bin/startup.sh Using CATALINA_BASE: /usr/local/tomcat8.5 Using CATALINA_HOME: /usr/local/tomcat8.5 Using CATALINA_TMPDIR: /usr/local/tomcat8.5/temp Using JRE_HOME: /usr/local/jdk1.8.0_191 Using CLASSPATH: /usr/local/tomcat8.5/bin/bootstrap.jar:/usr/local/tomcat8.5/bin/tomcat-juli.jar Tomcat started. [root@youxi1 native]# !ps ps aux | grep /usr/local/tomcat8.5/ root 3091 94.6 7.2 2302932 72308 pts/0 Sl 18:31 0:04 /usr/local/jdk1.8.0_191/bin/java -Djava.util.logging.config.file=/usr/local/tomcat8.5/conf/logging.properties
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
-Djava.library.path=/usr/local/apr/lib -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat8.5/bin/bootstrap.jar:/usr/local/tomcat8.5/bin/tomcat-juli.jar
-Dcatalina.base=/usr/local/tomcat8.5 -Dcatalina.home=/usr/local/tomcat8.5 -Djava.io.tmpdir=/usr/local/tomcat8.5/temp org.apache.catalina.startup.Bootstrap start root 3108 0.0 0.1 112728 1004 pts/0 R+ 18:31 0:00 grep --color=auto /usr/local/tomcat8.5/
4)查看日志是否支持native
[root@youxi1 ~]# cat /usr/local/tomcat8.5/logs/catalina.out | grep Native 29-Jun-2019 17:47:25.635 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib] 29-Jun-2019 18:17:01.395 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib] 29-Jun-2019 18:24:08.267 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib] 30-Jun-2019 09:40:11.168 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib] 30-Jun-2019 11:45:02.398 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib] 30-Jun-2019 11:45:24.769 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib] 30-Jun-2019 11:54:19.711 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib] 30-Jun-2019 11:59:23.422 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib] 30-Jun-2019 12:11:33.778 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib] 30-Jun-2019 12:32:55.610 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib] 30-Jun-2019 14:33:56.449 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib] 30-Jun-2019 17:15:19.203 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib] 30-Jun-2019 18:31:10.739 信息 [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library [1.2.18] using APR version [1.4.8].
看最后一行,只有出现这一行才代表成功了。
参考:https://www.cnblogs.com/zishengY/p/7101616.html?utm_source=itdadao&utm_medium=referral