设为首页 收藏本站
查看: 1103|回复: 0

[经验分享] linux 下tomcat安装apr运行模式优化并发性能

[复制链接]

尚未签到

发表于 2018-12-2 07:11:35 | 显示全部楼层 |阅读模式
  (一)简单介绍
  Tomcat支持三种接收请求的处理方式:BIO、NIO、APR 。
  BIO由于每个请求都要创建一个线程来处理,线程开销比较大,不能再高并发的场景,性能也是最低的。
  NIO是一个基于缓冲区、并能提供非阻塞I/O操作的Java API,比传统的bio更好的并发性能。
  APR(Apache Portable Run-time libraries)简单理解,就是从操作系统级别解决异步IO问题,大幅度的提高服务器的处理和响应性能, 也是Tomcat运行高并发应用的首选模式。 启用这种模式稍微麻烦一些,需要安装一些依赖库,以下就是安装所需的条件:
  1,最新的apr
  2,最新的apr-util
  3,tomcat-native.tar.gz(在tomcat/bin/下有相应的安装tar包
  

  (二)安装步骤

  1,安装相应的依赖库
[root@ittestserver1 opt]# yum install -y apr-devel openssl-devel gcc make
Loaded plugins: fastestmirror
Determining fastest mirrors
* base: mirrors.163.com
* epel: mirror01.idc.hinet.net
* extras: mirrors.163.com
* updates: mirrors.cn99.com
base                                                                                                          | 3.7 kB     00:00     
epel                                                                                                          | 4.3 kB     00:00     
epel/primary_db                                                                                               | 5.9 MB     00:01     
extras                                                                                                        | 3.4 kB     00:00     
jenkins                                                                                                       | 2.9 kB     00:00     
jenkins/primary_db                                                                                            |  19 kB     00:01     
updates                                                                                                       | 3.4 kB     00:00     
updates/primary_db                                                                                            | 5.4 MB     00:02     
Setting up Install Process
Package apr-devel-1.3.9-5.el6_2.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package gcc.x86_64 0:4.4.7-16.el6 will be updated
--> Processing Dependency: gcc = 4.4.7-16.el6 for package: gcc-c++-4.4.7-16.el6.x86_64
---> Package gcc.x86_64 0:4.4.7-17.el6 will be an update
--> Processing Dependency: libgomp = 4.4.7-17.el6 for package: gcc-4.4.7-17.el6.x86_64
--> Processing Dependency: cpp = 4.4.7-17.el6 for package: gcc-4.4.7-17.el6.x86_64
--> Processing Dependency: libgcc >= 4.4.7-17.el6 for package: gcc-4.4.7-17.el6.x86_64
  gcc.x86_64 0:4.4.7-17.el6              make.x86_64 1:3.81-23.el6              openssl-devel.x86_64 0:1.0.1e-48.el6_8.4            
Dependency Updated:
  cpp.x86_64 0:4.4.7-17.el6                   gcc-c++.x86_64 0:4.4.7-17.el6            libgcc.x86_64 0:4.4.7-17.el6                  
  libgomp.x86_64 0:4.4.7-17.el6               libstdc++.x86_64 0:4.4.7-17.el6          libstdc++-devel.x86_64 0:4.4.7-17.el6         
  openssl.x86_64 0:1.0.1e-48.el6_8.4         
Complete!  2,安装apr动态库,将最新版本的apr下载到目录下(http://apr.apache.org)  目前最新的是apr-1.5.2.tar.gz
[root@Monitor install]# wget http://mirrors.cnnic.cn/apache//apr/apr-1.5.2.tar.gz
--2017-03-02 17:17:32--  http://mirrors.cnnic.cn/apache//apr/apr-1.5.2.tar.gz
正在解析主机 mirrors.cnnic.cn... 218.241.113.17
正在连接 mirrors.cnnic.cn|218.241.113.17|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1031613 (1007K) [application/octet-stream]
正在保存至: “apr-1.5.2.tar.gz”
100%[===========================================================================================>] 1,031,613   1.69M/s   in 0.6s   
2017-03-02 17:17:33 (1.69 MB/s) - 已保存 “apr-1.5.2.tar.gz” [1031613/1031613])
[root@Monitor install]# tar xf apr-1.5.2.tar.gz
[root@Monitor install]# cd apr-1.5.2
[root@Monitor apr-1.5.2]# ./configure --prefix=/usr/local/apr
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
Configuring APR library
Platform: x86_64-unknown-linux-gnu
checking for working mkdir -p... yes
APR Version: 1.5.2
checking for chosen layout... apr
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... no
Restore user-defined environment settings...
  restoring CPPFLAGS to ""
  setting EXTRA_CPPFLAGS to "-DLINUX -D_REENTRANT -D_GNU_SOURCE"
  restoring CFLAGS to ""
  setting EXTRA_CFLAGS to "-g -O2 -pthread"
  restoring LDFLAGS to ""
  setting EXTRA_LDFLAGS to ""
  restoring LIBS to ""
  setting EXTRA_LIBS to "-lrt -lcrypt  -lpthread"
  restoring INCLUDES to ""
  setting EXTRA_INCLUDES to ""
configure: creating ./config.status
config.status: creating Makefile
config.status: creating include/apr.h
config.status: creating build/apr_rules.mk
config.status: creating build/pkg/pkginfo
config.status: creating apr-1-config
config.status: creating apr.pc
config.status: creating test/Makefile
config.status: creating test/internal/Makefile
config.status: creating include/arch/unix/apr_private.h
config.status: executing libtool commands
rm: cannot remove `libtoolT': No such file or directory
config.status: executing default commands
[root@Monitor apr-1.5.2]# make && make install
make[1]: Entering directory `/tmp/install/apr-1.5.2'
/tmp/install/apr-1.5.2/build/mkdir.sh tools
/bin/sh /tmp/install/apr-1.5.2/libtool --silent --mode=compile gcc -g -O2 -pthread   -DHAVE_CONFIG_H  -DLINUX -D_REENTRANT -D_GNU_SOURCE   -I./include -I/tmp/install/apr-1.5.2/include/arch/unix -I./include/arch/unix -I/tmp/install/apr-1.5.2/include/arch/unix -I/tmp/install/apr-1.5.2/include -I/tmp/install/apr-1.5.2/include/private -I/tmp/install/apr-1.5.2/include/private  -o tools/gen_test_char.lo -c tools/gen_test_char.c && touch tools/gen_test_char.lo
/bin/sh /tmp/install/apr-1.5.2/libtool --silent --mode=link gcc -g -O2 -pthread   -DHAVE_CONFIG_H  -DLINUX -D_REENTRANT -D_GNU_SOURCE   -I./include -I/tmp/install/apr-1.5.2/include/arch/unix -I./include/arch/unix -I/tmp/install/apr-1.5.2/include/arch/unix -I/tmp/install/apr-1.5.2/include -I/tmp/install/apr-1.5.2/include/private -I/tmp/install/apr-1.5.2/include/private   -no-install    -o tools/gen_test_char tools/gen_test_char.lo    -lrt -lcrypt  -lpthread
/tmp/install/apr-1.5.2/build/mkdir.sh include/private  

  3,下载安装apr-util(http://apr.apache.org/download.cgi)
[root@Monitor install]# wget http://mirrors.cnnic.cn/apache//apr/apr-util-1.5.4.tar.gz
--2017-03-02 17:23:09--  http://mirrors.cnnic.cn/apache//apr/apr-util-1.5.4.tar.gz
正在解析主机 mirrors.cnnic.cn... 218.241.113.17
正在连接 mirrors.cnnic.cn|218.241.113.17|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:874044 (854K) [application/octet-stream]
正在保存至: “apr-util-1.5.4.tar.gz”
100%[===========================================================================================>] 874,044     1.68M/s   in 0.5s   
2017-03-02 17:23:10 (1.68 MB/s) - 已保存 “apr-util-1.5.4.tar.gz” [874044/874044])
[root@Monitor install]# tar xf apr-util-1.5.4.tar.gz
[root@Monitor install]# cd apr-util-1.5.4
[root@Monitor apr-util-1.5.4]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
[root@Monitor apr-util-1.5.4]# make && make install
make[1]: Entering directory `/tmp/install/apr-util-1.5.4'
/bin/sh /usr/local/apr/build-1/libtool --silent --mode=compile gcc -g -O2 -pthread   -DHAVE_CONFIG_H  -DLINUX -D_REENTRANT -D_GNU_SOURCE   -I/tmp/install/apr-util-1.5.4/include -I/tmp/install/apr-util-1.5.4/include/private  -I/usr/local/apr/include/apr-1    -o buckets/apr_brigade.lo -c buckets/apr_brigade.c && touch buckets/apr_brigade.lo
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.
----------------------------------------------------------------------
/usr/bin/install -c -m 644 aprutil.exp /usr/local/apr-util/lib
/usr/bin/install -c -m 755 apu-config.out /usr/local/apr-util/bin/apu-1-config
[root@Monitor apr-util-1.5.4]# ll /usr/local/apr-util/
总用量 12
drwxr-xr-x 2 root root 4096 3月   2 17:24 bin
drwxr-xr-x 3 root root 4096 3月   2 17:24 include
drwxr-xr-x 3 root root 4096 3月   2 17:24 lib  

  3,安装tomcat-native。
  方法一:源码安装,不过安装过程中会有一些依赖,特别麻烦,我上次在这个问题上一直有报错,浪费掉一两天的时间才解决。一般tomcat的/bin/下都会有这个安装文件。
[root@Monitor bin]# cd /usr/local/apache-tomcat-7.0.63/bin
[root@Monitor bin]# ls
bootstrap.jar       commons-daemon.jar            daemon.sh         setclasspath.sh  startup.sh                tool-wrapper.bat
catalina.bat        commons-daemon-native.tar.gz  digest.bat        shutdown.bat     tomcat-juli.jar           tool-wrapper.sh
catalina.sh         configtest.bat                digest.sh         shutdown.sh      tomcat-native-1.1.33-src  version.bat
catalina-tasks.xml  configtest.sh                 setclasspath.bat  startup.bat      tomcat-native.tar.gz      version.sh
[root@Monitor bin]# tar xf tomcat-native.tar.gz
[root@Monitor bin]# cd tomcat-native-1.1.33-src/jni/native/
[root@Monitor native]# ./configure --with-apr=/usr/local/apr/bin/apr-1-config --with-java-home=/usr/java/jdk1.7.0_79/
[root@Monitor native]# make && make install  最后添加环境变量
[root@ittestserver1 native]# vim /etc/profile
export LD_LIBRARY_PATH=/usr/local/apr/lib
[root@ittestserver1 native]# source /etc/profile  

  方法二:使用yum安装,方便快捷省时省力比较推荐。

[root@ittestserver1]# yum list|grep tomcat-native
[root@ittestserver1 lib]# yum install tomcat-native -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
* epel: mirror01.idc.hinet.net
* extras: mirrors.163.com
* updates: mirrors.cn99.com
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package tomcat-native.x86_64 0:1.1.34-1.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=====================================================================================================================================
Package                            Arch                        Version                              Repository                 Size
=====================================================================================================================================
Installing:
tomcat-native                      x86_64                      1.1.34-1.el6                         epel                       60 k
Transaction Summary
=====================================================================================================================================
Install       1 Package(s)
Total download size: 60 k
Installed size: 165 k
Downloading Packages:
tomcat-native-1.1.34-1.el6.x86_64.rpm                                                                         |  60 kB     00:00     
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : tomcat-native-1.1.34-1.el6.x86_64                                                                                 1/1
/sbin/ldconfig: /usr/lib/libtcnative-1.so.0 不是符号连接
  Verifying  : tomcat-native-1.1.34-1.el6.x86_64                                                                                 1/1
Installed:
  tomcat-native.x86_64 0:1.1.34-1.el6                                                                                                
Complete!
[root@ittestserver1 lib]# rpm -qa|grep tomcat-native
tomcat-native-1.1.34-1.el6.x86_64
unset i
unset -f pathmunge
配置相关的全局变量:
方法一:修改/etc/profie文件
[root@ittestserver1 lib]# vim /etc/profile
export LD_LIBRARY_PATH=/usr/local/apr/lib
[root@ittestserver1 lib]# source /etc/profile
方法二:编辑$TOMCAT_HOME/bin/catalina.sh文件,在虚拟机启动参数JAVA_OPTS中添加
java.library.path参数,指定apr库的路径
[root@Monitor xn1]# vim /usr/local/tomcat_thirdmanage/bin/catalina.sh   
JAVA_OPTS="$JAVA_OPTS -Djava.library.path=/usr/local/apr/lib"

tomcat8以下版本,需要指定运行模式  最后启动下tomcat,查看日志为启用了apr模式将protocol从HTTP/1.1改成org.apache.coyote.http11.Http11AprProtocol
  

  查看启动日志,即为完成。

  

  (三)tomcat优化

  (1)优化连接池

  [root@Monitor local]# vim /usr/local/apache-tomcat-7.0.63/conf/server.xml
放开连接池:
   
优化连接端口:
  (2)修改catalina.sh防止内存溢出。
[root@ittestserver1 opt]# vim /usr/local/tomcat/bin/catalina.sh
# -----------------------------------------------------------------------------
JAVA_OPTS="-server -Xms8192m  -Xmx8192m -Xss512k -XX:PermSize=1024m -XX:MaxNewSize=2048m -XX:MaxNewSize=2048m -XX:MaxPermSize=1024m -XX:+DisableExplicitGC -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:+UseFastAccessorMethods -Djava.awt.headless=true  -Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8"  

  





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-642130-1-1.html 上篇帖子: Tomcat启动失败原因积累 下篇帖子: tomcat自动关闭 报 X connection to localhost:10.0 broken 错误
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表