一、tomcat简介
Tomcat服务器是一个免费的开放源代码的Web应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选。
Tomcat提供了一个Jasper编译器用以将JSP编译成对应的Servlet。TomcatServlet引擎通常与Apache或者其他Web服务器一起工作。除了用于开发过程中的调试以及那些对速度和事务处理只有很小要求的用户,很少会将Tomcat单独作为Web服务器。但随着版本的更新,正有越来越多的用户将其单独作为Web服务器用以那些对速度和可靠性有较高要求的环境中。由于Tomcat是使用Java开发的,所以它可以运行在任何一个装有JVM的不同操作系统之上。
二、实验环境
系统环境:centos6.4-i386
jdk:jdk-7u40-linux-i586.rpm
tomcat:apache-tomcat-7.0.42
nginx:tengine-1.5.1
Apache:httpd-2.4.2
三、搭建实验环境
1.安装jdk
#rpm -ivh jdk-7u40-linux-i586.rpm #vim /etc/profile.d/java.sh export JAVA_HOME=/usr/java/latest export PATH=$JAVA_HOME/bin:$PATH # . /etc/profile.d/java.sh # java -version
2.安装tomcat
# tar xf apache-tomcat-7.0.33.tar.gz -C /usr/local/ # cd /usr/local/ # ln -s apache-tomcat-7.0.33 tomcat # vim /etc/profile.d/tomcat.sh export CATALINA_HOME=/usr/local/tomcat export PATH=$CATALINA_HOME/bin:$PATH # . /etc/profile.d/tomcat.sh
添加服务脚本
[root@stu_yxm18 ~]# vim /etc/rc.d/init.d/tomcat #!/bin/sh # Tomcat init script for Linux. # # chkconfig: 2345 96 14 JAVA_OPTS='-Xms64m -Xmx128m' JAVA_HOME=/usr/java/latest CATALINA_HOME=/usr/local/tomcat export JAVA_HOME CATALINA_HOME exec $CATALINA_HOME/bin/catalina.sh $*
启动tomcat
[root@stu_yxm18 ~]# service tomcat start Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/java/latest Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
注:apache、nginx的安装在此不做演示,强烈建议编译安装!!!
四、配置tomcat运行一个简单的jsp程序
# cd /usr/local/tomcat/conf/ *****备份主配置文件***** # cp server.xml server.xml.bak # vim server.xml ******修改主配置文件如下***** ******添加一个虚拟主机***** <Host name="www.yxm.com" appBase="/usr/local/tomcat/yxmtctest" unpaclWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="www.yxm_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> <Context path="" docBase="/usr/local/tomcat/yxmtctest" /> </Host> ******修改服务端口为80***** <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> ******更改默认虚拟主机:注意前后对应***** <Engine name="Catalina" defaultHost="www.yxm.com">
提供一个jsp程序
# cd /usr/local/tomcat/ #mkdir yxmtctest #[root@stu_yxm18 tomcat]# cd yxmtctest/ #mkdir -p WEB-INF/{class,lib} #vim index.jsp <%@ page language="java" %> <html> <head><title>TomcatA</title></head> <body> <h1><font color="red">TomcatA </font></h1> <table align="centre" border="1"> <tr> <td>Session ID</td> <% session.setAttribute("abc","abc"); %> <td><%= session.getId() %></td> </tr> <tr> <td>Created on</td> <td><%= session.getCreationTime() %></td> </tr> </table> </body> </html> #service tomcat stop #注:tomcat不支持restart #service tomcat start
五、Apache做反向代理,代理后端tomcat
编译安装httpd-2.4.2
#tar xf httpd-2.4.2.tar.bz2 #ln -sv httpd-2.4.2 httpd #cd httpd #./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-proxy --enable-proxy-http --enable-proxy-ajp #make && make install
修改主配置文件
# vim /etc/httpd/httpd.conf Include /etc/httpd/extra/mod_proxy.conf # vim /etc/httpd/extra/mod_proxy.conf ProxyVia On ProxyRequests Off ProxyPreserveHost Off <Proxy *> Order allow,deny Allow from all </Proxy> ProxyPass / ajp://172.16.15.23:8009/ ProxyPassReverse / ajp://172.16.15.23:8009/
注释:
<1>ProxyVia{On|Off|Full|Block}:用于控制在http首部是否使用Via;On表示每个请求和响应报文均添加Via。
<2>ProxyRequests{On|Off}:是否开启apache正向代理的功能。
<3>ProxyPreserveHost{On|Off}:如果启用此功能,代理会将用户请求报文中的Host:行发送给后端的服务器,而不再使用ProxyPass指定的服务器地址。如果想在反向代理中支持虚拟主机,则需要开启此项。
<4>ProxyPass/ajp://172.16.15.23:8009/#####使用的是ajp协议
<5>ProxyPassReverse/ajp://172.16.15.23:8009/
六、Apache+tomcat基于mod_proxy模块构建tomcat负载均衡集群
# vim /etc/httpd/extra/mod_proxy.conf ProxyVia Off ProxyRequests Off ProxyPreserveHost Off <Proxy balancer://yxm> BalancerMember ajp://172.16.15.18:8009 loadfactor=1 BalancerMember ajp://172.16.15.23:8009 loadfactor=1 ProxySet lbmethod=bytraffic </Proxy> ProxyPass / balancer://yxm/ stickysession=JSESSIONID ProxyPassReverse / balancer://yxm/
loadfactor=1#权重
ProxySetlbmethod=bytraffic#调度算法
在另一个tomcat节点上添加服务:主配置文件完全一样,为验证负载均衡效果jsp程序不一样。
七、tengine+tomcat实现web动静分离
注:tengine的安装在此不演示(编译安装)!!!
修改nginx主配置文件
[root@nginx ~]# vim /etc/nginx/nginx.conf user nginx nginx; worker_processes 2; worker_rlimit_nofile 51200; #error_log logs/error.log; #pid logs/nginx.pid; events { use epoll; worker_connections 51200; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; client_max_body_size 20m; client_header_buffer_size 16k; large_client_header_buffers 4 16k; sendfile on; tcp_nopush on; keepalive_timeout 65; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_proxied any; gzip_http_version 1.1; gzip_comp_level 3; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; proxy_temp_path /tmp/proxy_temp; proxy_cache_path /tmp/proxy_cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=3g; proxy_connect_timeout 50; proxy_read_timeout 600; proxy_send_timeout 600; proxy_buffer_size 128k; proxy_buffers 16 256k; proxy_busy_buffers_size 512k; proxy_temp_file_write_size 1024m; proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 http_502 http_504; server { listen 80; server_name www.yxm.com ; access_log logs/host.access.log main; location / { proxy_pass http://172.16.15.14; } location ~* \.(|html|htm|jpg|jpeg|png|gif|bmp|swf|ico)$ { proxy_pass http://172.16.15.18; } location ~* \.(jsp|css|do|php)$ { proxy_pass http://172.16.15.23; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
重启nginx服务
八、Apache+tomcat实现session共享
配置后端tomcat服务器IP:172.16.15.18
# vim /usr/local/tomcat/conf/server.xml <Engine name="Catalina" defaultHost="www.yxm.com"> #注意添加的位置 <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.15.15.15" #组播地址,两个节点一定要保持一致 port="45564" frequency="500" dropTime="3000"/> <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="172.16.15.18" #IP地址,可以使用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.JvmRouteSessionIDBinderListener"/> <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/> </Cluster>
在/usr/local/tomcat/yxmtctest/WEB-INF/创建web.xml文件
vim/usr/local/tomcat/yxmtctest/WEB-INF/web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <distributable/> </web-app> ~
将以上配置复制到172.16.15.23tomcat节点上注意修改IP地址
#scp /usr/local/tomcat/conf/server.xml 172.16.15.23:/usr/local/tomcat/conf #scp /usr/local/tomcat/yxmtctest/WEB-INF/web.xml 172.16.15.23:/usr/local/tomcat/yxmtctest/WEB-INF/ #server tomcat stop #server tomcat start
看以看到两个SessionID完全一样!!!Session共享成功