(一)简单介绍
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,安装相应的依赖库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
[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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
[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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
[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/下都会有这个安装文件。
1
2
3
4
5
6
7
8
9
10
|
[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
|
最后添加环境变量
1
2
3
|
[root@ittestserver1 native]
# vim /etc/profile
export
LD_LIBRARY_PATH=
/usr/local/apr/lib
[root@ittestserver1 native]
# source /etc/profile
|
方法二:使用yum安装,方便快捷省时省力比较推荐。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
[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
1
2
3
4
5
6
7
8
9
10
11
12
|
放开连接池:
<Executor name=
"tomcatThreadPool"
namePrefix=
"catalina-exec-"
maxThreads=
"150"
minSpareThreads=
"20"
maxSpareThreads=
"50"
/>
优化连接端口:
<Connector port=
"8080"
executor=
"tomcatThreadPool"
protocol=
"org.apache.coyote.http11.Http11AprProtocol"
URIEncoding=
"UTF-8"
connectionTimeout=
"20000"
enableLookups=
"false"
disableUploadTimeout=
"false"
connectionUploadTimeout=
"150000"
acceptCount=
"300"
redirectPort=
"8443"
/>
|
(2)修改catalina.sh防止内存溢出。
1
2
3
|
[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"
|