Tomcat的应用
一、简介Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。对于一个初学者来说,可以这样认为,当在一台机器上配置好Apache 服务器,可利用它响应对HTML页面的访问请求。实际上Tomcat 部分是Apache 服务器的扩展,但它是独立运行的,所以当你运行tomcat 时,它实际上作为一个与Apache 独立的进程单独运行的,Tomcat 很受广大程序员的喜欢,因为它运行时占用的系统资源小,扩展性好,支持负载平衡与邮件服务等开发应用系统常用的功能;而且它还在不断的改进和完善中,任何一个感兴趣的程序员都可以更改它或在其中加入新的功能。
二、拓扑
http://s3.运维网.com/wyfs02/M00/27/3B/wKioL1NxccKSlD-UAAEZBpebPZU262.jpg
三、实现过程
1、配置haproxy(172.16.70.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
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
# yum -y install haproxy
# vim /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 30000
user haproxy
group haproxy
daemon
# turn on stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontendhaproxy *:80
acl url_static path_beg -i /static/images/javascript/stylesheets
acl url_static path_end -i .html .jpg .gif .png .css .js
acl url_dynamic path_end -i .jsp .do
use_backend varnish ifurl_static
use_backend dynamic ifurl_dynamic
default_backend varnish
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend varnish
balance roundrobin
server node1 172.16.70.2:6081 check maxconn 10000
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend dynamic
balance roundrobin
server node2 172.16.70.3:80 check maxconn 5000
#
# service haproxy start
2、配置varnish(172.16.70.2)
(1)安装varnish
1
2
3
4
5
6
7
# ls
anaconda-ks.cfg varnish-docs-3.0.4-1.el6.x86_64.rpm
install.log varnish-libs-3.0.4-1.el6.x86_64.rpm
install.log.syslog varnish-libs-devel-3.0.4-1.el6.x86_64.rpm
varnish-3.0.4-1.el6.x86_64.rpm
#
# yum -y install *.rpm
(2)修改vanish,使用内存做为缓存
1
2
# vim /etc/sysconfig/varnish
VARNISH_STORAGE="malloc,500M"
(3)配置varnish缓存
①、不使用默认.vcl,新建一个gg.vcl使用
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
# cd /etc/varnish/
#
# vim gg.vcl
acl purgers { #定义访问控制列表
"127.0.0.1";
"172.16.0.0"/16;
}
backend images { #后端图片服务器
.host = "172.16.70.4";
.port = "80";
}
backend html { #后端网页服务器
.host = "172.16.70.5";
.port = "80";
}
sub vcl_recv {
if(req.request == "PURGE") { #不能匹配到列表的用户不允许清除缓存
if(!client.ip ~ purgers) {
error 503 "Not allowed!";
}
}
if(req.http.X-Forward-For) { #定义在后端服务器记录真正请求者的IP
setreq.http.X-Forward-For = req.http.X-Forward-For + ", "+ client.ip;
} else{
setreq.http.X-Forward-For = client.ip;
}
if(req.http.url ~ "\.(jpg|png|gif|jpeg)$") {
setreq.backend = images; #如果是图片则请求图片服务器
} else{
setreq.backend = html; #否则请求html服务器
}
return(lookup);
}
sub vcl_hit {
if(req.request == "PURGE") { #如果命中缓存,则可清理
purge;
error 200 "Purged!";
}
}
sub vcl_miss {
if(req.request == "PURGE") { #如未命中缓存,清除时则报错
purge;
error 404 "Not in cache!";
}
}
sub vcl_deliver { #通过F12可查看缓存命中状态及varnish服务器IP
if(obj.hits > 0) {
setresp.http.X-Cache = "Hit from "+ server.ip;
} else{
setresp.http.X-Cache = "MISS";
}
}
②、使用配置好的vcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# service varnish start
#
# varnishadm -S /etc/varnish/secret -T 127.0.0.1:6082 #进入varnish管理接口,启用varnish
varnish>
varnish> vcl.load cache gg.vcl
200
VCL compiled.
varnish>
varnish> vcl.list
200
active 0 boot
available 0 cache
varnish>
varnish> vcl.use cache
200
3、配置静态服务器
(1)配置static2(html):172.16.70.5
1
2
3
4
5
# yum -y install httpd
#
# vim /var/www/html/index.html
Welcome to static html(172.16.70.5)
# service httpd start
①、访问以html结尾的静态页面
http://s3.运维网.com/wyfs02/M00/26/E9/wKiom1Nt6EuhyCSLAACcYcbdnMA255.png
②、测试varnish缓存命中
http://s3.运维网.com/wyfs02/M01/26/E9/wKiom1Nt6mGDs7KNAAEjIxEEmM0310.png
(2)配置static1(images):172.16.70.4
放一张1.jpg的图片到图片服务器:172.16.70.4
1
2
3
4
5
6
7
8
# yum -y install httpd
#
# vim /var/www/html/index.html
Welcome to static image(172.16.70.4)
# ls
1.jpgindex.html
#
# service httpd start
①、访问以1.jpg结尾的静态页面
http://s3.运维网.com/wyfs02/M01/26/E9/wKiom1Nt7iSD3663ABcpXX9j8IA883.png
4、配置nginx+tomcat动态服务器(172.16.70.3)
(1)安装配置tomcat
①、安装配置JDK
1
2
3
4
5
6
7
8
9
10
# ls
anaconda-ks.cfginstall.loginstall.log.syslogjdk-7u9-linux-x64.rpm
#
# yum -y install jdk-7u9-linux-x64.rpm
#
# vim /etc/profile.d/java
exportJAVA_HOME=/usr/java/latest
exportPATH=$JAVA_HOME/bin:$PATH
#
# . /etc/profile.d/java
②、安装tomcat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# ls
anaconda-ks.cfg install.log jdk-7u9-linux-x64.rpm
apache-tomcat-7.0.42.tar.gzinstall.log.syslog
#
# tar xf apache-tomcat-7.0.42.tar.gz -C /usr/local/
#
# cd /usr/local/
# ln -sv apache-tomcat-7.0.42/ tomcat
#
# vim /etc/profile.d/tomcat
exportCATALINA_HOME=/usr/local/tomcat
exportPATH=$CATALINA_HOME/bin:$PATH
#
# . /etc/profile.d/tomcat
③、为tomcat提供服务脚本
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
# vim /etc/rc.d/init.d/tomcat
#!/bin/sh
# Tomcat init script for Linux.
#
# chkconfig: 2345 96 14
# description: The Apache Tomcat servlet/JSP container.
# JAVA_OPTS='-Xms64m -Xmx128m'
JAVA_HOME=/usr/java/latest
CATALINA_HOME=/usr/local/tomcat
exportJAVA_HOME CATALINA_HOME
case$1 in
start)
exec$CATALINA_HOME/bin/catalina.sh start ;;
stop)
exec$CATALINA_HOME/bin/catalina.sh stop;;
restart)
$CATALINA_HOME/bin/catalina.sh stop
sleep2
exec$CATALINA_HOME/bin/catalina.sh start ;;
*)
echo"Usage: `basename $0` {start|stop|restart}"
exit1
;;
esac
#
# chmod +x /etc/rc.d/init.d/tomcat
# chkconfig --add tomcat
# chkconfig tomcat on
④、配置tomcat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# pwd
/usr/local/tomcat/conf
#
# vim server.xml
#######在原来Connector下面新增加一个Connector#######
###这台主机上用nginx监听80端口,nginx反向代理至tomcat使用888端口
Connector port="888"address="172.16.7.100"maxThreads="5000"protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"/>
###########在原来Host下面新增加一个Host###########
⑤、创建动态(jsp)网页页面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# cd /usr/local/tomcat/webapps/
# mkdir dynaweb/WEB-INF/{classes,lib} -pv
# cd dynaweb/
# vim index.jsp
JSP testpage.
#
# service tomcat start
⑥、测试访问动态页面
http://s3.运维网.com/wyfs02/M00/26/EA/wKioL1NuA8mCHJSfAACisoIGpws776.png
(2)安装配置nginx服务器
①、安装nginx
②、配置nginx
在监听80端口的server内添加下面这个location
1
2
3
4
5
6
7
8
9
location / {
root html;
indexindex.html index.htm;
proxy_pass http://172.16.7.100:888;#当前端haproxy设置默认后端为dynamic时,
#默认不加.jsp结尾的访问也转到7.100
}
location ~* \.(jsp|do)$ {
proxy_pass http://172.16.7.100:888;
}
(3)测试动态效果
http://s3.运维网.com/wyfs02/M01/26/E9/wKiom1NuDqXj24N6AACEHsRqE4o183.png
页:
[1]