Tomcat是开放源码的web应用服务器。可以处理像asp,php,cgi,jsp等Apache不能处理的动态页面,但在处理静态资源能力不如Apache服务器,所以通常与Apache在同一台服务器上集成,形成一个可以同时高效处理动静资源的web服务器。
Tomcat组件:
顶级组件:Server
服务类:Service
连接器:http,https,ajp
容器类:Engine,Host,Context
被嵌套的组件:valve(访问控制),logger(日志),realm(用户认证),loader(加载 器),manager(管理器)
集群类组件:listener(侦听器)
Tomcat安装
(1)系统自带的openjdk+tomcat
[[email protected] ~]# yum install -y java-1.7.0-openjdk.x86_64 java-1.7.0-openjdk-devel.x86_64
修改环境变量,使其被系统应用:
[[email protected] ~]# vim /etc/profile.d/java.sh
[[email protected] ~]# exec bash #重读bash,取代之前的bash
[[email protected] ~]# yum -y install tomcat.noarch tomcat-webapps.noarch tomcat-admin-webapps.noarch tomcat-lib.noarch #安装tomcat及相应组件
这时可以通过systemctl start tomcat.service开启tomcat服务
(2)使用Oracle JDK和tomcat主站提供的编译完成的程序包
[[email protected] ~]# rpm -ivh jdk-8u25-linux-x64.rpm
修改环境变量,使其被系统应用:
[[email protected] ~]# vim /etc/profile.d/java.sh
[[email protected] ~]# exec bash #重读bash,取代之前的bash [[email protected] ~]# tar xf apache-tomcat-8.0.23.tar.gz -C /usr/local [[email protected] ~]# cd /usr/local [[email protected] ~]# ln -sv apache-tomcat-8.0.23 tomcat #创建tomcat软链接 [[email protected] ~]# vim /etc/profile.d/tomcat.sh #修改环境变量
添加tomcat脚本
#!/bin/bash # JAVA_HOME=/usr/java/latest CATALINA_BASE=/usr/local/tomcat PATH=$JAVA_HOME/bin:$CATALINA_BASE/bin:$PATH export JAVA_HOME CATALINA_BASE PATH case $1 in start) exec $CATALINA_BASE/bin/catalina.sh start ;; stop) exec $CATALINA_BASE/bin/catalina.sh stop ;; restart) $CATALINA_BASE/bin/catalina.sh stop sleep 2 exec $CATALINA_BASE/bin/catalina.sh start ;; *) echo "Usage: `basename $0` (start|stop|restart)" exit 1 ;; esac
这时可以通过systemctl start tomcat.service开启tomcat服务
Tomcat目录结构
bin:存放各种平台下启动和关闭Tomcat的脚本文件
lib:存放Tomcat服务器和所有web应用需要的jar包
conf:Tomcat的各种配置文件,最重要的是server.xml
logs:存放Tomcat的日志文件
webapps:web应用的默认部署目录
temp:临时文件目录
Tomcat配置文件
server.xml:主配置文件
context.xml:每个webapp都可以专门的配置文件,这些配置文件通常位于webapp程序目录下的 WEB-INF目录中,用于定义会话管理,JDBC等;conf/context.xml是为各webapp提供 默认配置
web.xml:每个webapp只在部署之后才能被访问;此文件则用于为各webapp定义默认的部署操作方 式
tomcat-users.xml:用户认证的账号和密码配置文件
catalina.policy:当使用-security选项来启动tomcat实例时会读取此配置文件来实现基于安全 策略的运行方式
catalina。properties:java属性的定义文件,用于设定类加载器路径等,以及一些与JVM性能相 关的调优参数
logging-properties:日志系统相关的配置
手动添加一个测试应用程序
(1)创建webapp特有的目录结构
[[email protected] ~]# mkdir -pv myapp/{classes,lib,WEB-INF,META-INF}
(2)提供webapp的测试页面
[[email protected] ~]# vim myapp/index.jsp
构建LNMT
Client --> http --> Nginx --> reverse_proxy (http) --> tomcat (http connector)
location / { proxy_pass #nginx反向代理 }
location ~* \.(jsp|do)$ { proxy_pass #nginx动静分离 }
构建LAMT
Client --> http --> httpd --> reverse_proxy_module (http) --> tomcat (http connector)
Client --> http --> httpd --> reverse_proxy_module (ajp) --> tomcat (ajp connector)
第一种方式:proxy_module, proxy_http_module
第二种方式:proxy_module, proxy_ajp_module
(1) 第一种方式的配置机制,使用虚拟主机
<VirtualHost *:80> ServerName tc1.yl.com ProxyRequests Off ProxyVia On ProxyPreserveHost On <Proxy *> Require all granted </Proxy> ProxyPass / http://localhost:8080/ ProxyPassReverse / http://localhost:8080/ <Location /> Require all granted </Location> </VirtualHost>
(2)
<VirtualHost *:80> ServerName tc1.yl.com ProxyRequests Off ProxyVia On ProxyPreserveHost On <Proxy *> Require all granted </Proxy> ProxyPass / ajp://localhost:8080/ ProxyPassReverse / ajp://localhost:8080/ <Location /> Require all granted </Location> </VirtualHost>
Tomcat会话保持
(1)session sticky
①source ip
②cookie
(2)session cluster
(3)session server
①kv:memcached,redis
方法一:(1)apache通过mod_proxy,mod_proxy_http,mod_proxy_balancer;
tomcat通过http connector
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED <proxy balancer://lbcluster1> BalancerMember http://172.16.50.100:8080 loadfactor=10 route=TomcatA BalancerMember http://172.16.50.103:8080 loadfactor=10 route=TomcatB ProxySet stickysession=ROUTEID </proxy> <VirtualHost *:80> ServerName web1.magedu.com ProxyVia On ProxyRequests Off ProxyPreserveHost On <Proxy *> Require all granted </Proxy> ProxyPass / balancer://lbcluster1/ ProxyPassReverse / balancer://lbcluster1/ <Location /> Require all granted </Location> </VirtualHost>
(2)apache通过mod_proxy,mod_proxy_ajp,mod_proxy_balancer;
tomcat通过ajp connector
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED <proxy balancer://lbcluster1> BalancerMember ajp://172.16.50.100:8080 loadfactor=10 route=TomcatA BalancerMember ajp://172.16.50.103:8080 loadfactor=10 route=TomcatB ProxySet stickysession=ROUTEID </proxy> <VirtualHost *:80> ServerName web1.magedu.com ProxyVia On ProxyRequests Off ProxyPreserveHost On <Proxy *> Require all granted </Proxy> ProxyPass / balancer://lbcluster1/ ProxyPassReverse / balancer://lbcluster1/ <Location /> Require all granted </Location> </VirtualHost>
(3)apache通过mod_jk
tomcat通过ajp connector
①反向代理
模块配置文件:mod_jk.conf
LoadModule jk_module modules/mod_jk.so JkWorkersFile /etc/httpd/conf.d/workers.properties JkLogFile logs/mod_jk.log JkLogLevel debug JkMount /* TomcatA JkMount /status/ stat1
workers配置文件:worker.properties
worker.list=TomcatA,stat1 worker.TomcatA.port=8009 worker.TomcatA.host=172.16.50.100 worker.TomcatA.type=ajp13 worker.TomcatA.lbfactor=1 worker.stat1.type = status
②负载均衡
模块配置文件:mod_jk.conf
LoadModule jk_module modules/mod_jk.so JkWorkersFile /etc/httpd/conf.d/workers.properties JkLogFile logs/mod_jk.log JkLogLevel debug JkMount /* lbcluster1 JkMount /status/ stat1
workers配置文件:workers.properties
worker.list = lbcluster1,stat1 worker.TomcatA.type = ajp13 worker.TomcatA.host = 172.16.50.100 worker.TomcatA.port = 8009 worker.TomcatA.lbfactor = 1 worker.TomcatB.type = ajp13 worker.TomcatB.host = 172.16.50.103 worker.TomcatB.port = 8009 worker.TomcatB.lbfactor = 1 worker.lbcluster1.type = lb worker.lbcluster1.sticky_session = 0 worker.lbcluster1.balance_workers = TomcatA, TomcatB worker.stat1.type = status
方法二:Session Cluster
构建步骤:
(1)各节点配置使用deltamanager
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster" channelSendOptions="8"> <Manager className="org.apache.catalina.ha.session.DeltaManager" expireSessionsOnShutdown="false" notifyListenersOnReplication="true"/> <Channel className="org.apache.catalina.tribes.group.GroupChannel"> <Membership className="org.apache.catalina.tribes.membership.McastService" address="228.0.1.7" port="45564" frequency="500" dropTime="3000"/> <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="auto" port="4000" autoBind="100" selectorTimeout="5000" maxThreads="6"/> <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter"> <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/> </Sender> <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/> <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/> </Channel> <Valve className="org.apache.catalina.ha.tcp.ReplicationValve" filter=""/> <Valve className="org.apache.catalina.ha.session.JvmRouteBinderValve"/> <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer" tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/" watchDir="/tmp/war-listen/" watchEnabled="false"/> <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/> </Cluster>
(2)为需要使用session cluster的webapps开启session distribution的功能
WEB-INF/web.xml中添加:<distributable/>
方法三:构建session server(通过memcached)
下载如下jar文件至各tomcat节点的tomcat安装目录下的lib目录中,其中的${version}要换成你所 需要的版本号,tc${6,7,8}要换成与tomcat版本相同的版本号。
memcached-session-manager-${version}.jar
memcached-session-manager-tc${6,7,8}-${version}.jar
spymemcached-${version}.jar
msm-javolution-serializer-${version}.jar
javolution-${version}.jar
分别在两个tomcat上的某host上定义一个用于测试的context容器,并在其中创建一个会话管理器,如下所示
<Context path="/test" docBase="/usr/local/tomcat/webapps/test" reloadable="true"> <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:172.16.50.101:11211,n2:172.16.50.102:11211" failoverNodes="n1" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" /> </Context>
在172.16.50.104上配置反向代理的负载均衡内容,类似如下所示:
<Proxy balancer://tomcat> BalancerMember http://172.16.50.100:8080 loadfactor=1 BalancerMember http://172.16.50.103:8080 loadfactor=1 ProxySet lbmethod=byrequests </Proxy> ProxyVia Off ProxyRequests Off ProxyPass / balancer://tomcat/ ProxyPassReverse / balancer://tomcat/ <Proxy *> Request all granted </Proxy> <Location /> Request all granted </Location>
分别为两个context提供测试页面
tomcatA
<%@ page language="java" %> <html> <head><title>TomcatA</title></head> <body> <h1><font color="red">TomcatA.magedu.com</font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <% session.setAttribute("magedu.com","magedu.com"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> </html>
tomcatB
<%@ page language="java" %> <html> <head><title>TomcatB</title></head> <body> <h1><font color="blue">TomcatB.magedu.com</font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <% session.setAttribute("magedu.com","magedu.com"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> </html>
测试结果,在浏览器中访问http://172.16.50.104/test,结果如下所示,其session ID在负载均衡环境中保持不变。