首页 > 代码库 > Tomcat的应用
Tomcat的应用
一、简介
Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。对于一个初学者来说,可以这样认为,当在一台机器上配置好Apache 服务器,可利用它响应对HTML页面的访问请求。实际上Tomcat 部分是Apache 服务器的扩展,但它是独立运行的,所以当你运行tomcat 时,它实际上作为一个与Apache 独立的进程单独运行的,Tomcat 很受广大程序员的喜欢,因为它运行时占用的系统资源小,扩展性好,支持负载平衡与邮件服务等开发应用系统常用的功能;而且它还在不断的改进和完善中,任何一个感兴趣的程序员都可以更改它或在其中加入新的功能。
二、拓扑
三、实现过程
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 | [root@haproxy ~] # yum -y install haproxy [root@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 #--------------------------------------------------------------------- frontend haproxy *: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 if url_static
use_backend dynamic if url_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 [root@haproxy ~] # [root@haproxy ~] # service haproxy start |
2、配置varnish(172.16.70.2)
(1)安装varnish
1 2 3 4 5 6 7 | [root@varnish ~] # 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 [root@varnish ~] # [root@varnish ~] # yum -y install *.rpm |
(2)修改vanish,使用内存做为缓存
1 2 | [root@varnish ~] # 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 | [root@varnish ~] # cd /etc/varnish/ [root@varnish varnish] # [root@varnish 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
set req.http.X-Forward-For = req.http.X-Forward-For + ", " + client.ip;
} else {
set req.http.X-Forward-For = client.ip;
}
if (req.http.url ~ "\.(jpg|png|gif|jpeg)$" ) {
set req.backend = images; #如果是图片则请求图片服务器
} else {
set req.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) {
set resp.http.X-Cache = "Hit from " + server.ip;
} else {
set resp.http.X-Cache = "MISS" ;
} } |
②、使用配置好的vcl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | [root@varnish ~] # service varnish start [root@varnish ~] # [root@varnish ~] # 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 | [root@static2 ~] # yum -y install httpd [root@static2 ~] # [root@static2 ~] # vim /var/www/html/index.html <h1>Welcome to static html(172.16.70.5)< /h1 > [root@static2 ~] # service httpd start |
①、访问以html结尾的静态页面
②、测试varnish缓存命中
(2)配置static1(images):172.16.70.4
放一张1.jpg的图片到图片服务器:172.16.70.4
1 2 3 4 5 6 7 8 | [root@static2 ~] # yum -y install httpd [root@static2 ~] # [root@static2 ~] # vim /var/www/html/index.html <h1>Welcome to static image(172.16.70.4)< /h1 > [root@static1 html] # ls 1.jpg index.html [root@static1 html] # [root@static2 ~] # service httpd start |
①、访问以1.jpg结尾的静态页面
4、配置nginx+tomcat动态服务器(172.16.70.3)
(1)安装配置tomcat
①、安装配置JDK
1 2 3 4 5 6 7 8 9 10 | [root@dynamic ~] # ls anaconda-ks.cfg install .log install .log.syslog jdk-7u9-linux-x64.rpm [root@dynamic ~] # [root@dynamic ~] # yum -y install jdk-7u9-linux-x64.rpm [root@dynamic ~] # [root@dynamic ~] # vim /etc/profile.d/java export JAVA_HOME= /usr/java/latest export PATH=$JAVA_HOME /bin :$PATH [root@dynamic ~] # [root@dynamic ~] # . /etc/profile.d/java |
②、安装tomcat
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [root@dynamic ~] # ls anaconda-ks.cfg install .log jdk-7u9-linux-x64.rpm apache-tomcat-7.0.42. tar .gz install .log.syslog [root@dynamic ~] # [root@dynamic ~] # tar xf apache-tomcat-7.0.42.tar.gz -C /usr/local/ [root@dynamic ~] # [root@dynamic ~] # cd /usr/local/ [root@dynamic local ] # ln -sv apache-tomcat-7.0.42/ tomcat [root@dynamic local ] # [root@dynamic local ] # vim /etc/profile.d/tomcat export CATALINA_HOME= /usr/local/tomcat export PATH=$CATALINA_HOME /bin :$PATH [root@dynamic local ] # [root@dynamic local ] # . /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 | [root@dynamic ~] # 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 export JAVA_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
sleep 2
exec $CATALINA_HOME /bin/catalina .sh start ;; *)
echo "Usage: `basename $0` {start|stop|restart}"
exit 1
;; esac [root@dynamic ~] # [root@dynamic ~] # chmod +x /etc/rc.d/init.d/tomcat [root@dynamic ~] # chkconfig --add tomcat [root@dynamic ~] # chkconfig tomcat on |
④、配置tomcat
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | [root@dynamic conf] # pwd /usr/local/tomcat/conf [root@dynamic conf] # [root@dynamic 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########### <Host name= "172.16.7.100" appBase= "webapps"
unpackWARs= "true" autoDeploy= "true" >
<Context path= "" docBase= "dynaweb" />
<Valve className= "org.apache.catalina.valves.AccessLogValve" directory= "logs"
prefix= "dynaweb_access_log." suffix= ".txt"
pattern= "%h %l %u %t "%r" %s %b" />
< /Host > |
⑤、创建动态(jsp)网页页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [root@dynamic ~] # cd /usr/local/tomcat/webapps/ [root@dynamic webapps] # mkdir dynaweb/WEB-INF/{classes,lib} -pv [root@dynamic webapps] # cd dynaweb/ [root@dynamic dynaweb] # vim index.jsp <%@ page language= "java" %> <%@ page import = "java.util.*" %> <html>
< head >
<title>JSP test page.< /title >
< /head >
<body>
<% out.println( "Hello,world!" ); %>
< /body > < /html > [root@dynamic dynaweb] # [root@dynamic dynaweb] # service tomcat start |
⑥、测试访问动态页面
(2)安装配置nginx服务器
①、安装nginx
②、配置nginx
在监听80端口的server内添加下面这个location
|
(3)测试动态效果