Tomcat(三):Apache反向代理实现Tomcat负载均衡
实验拓扑:
实验环境:
OS:CentOS-6.7-x86_64
Apache:
IP:192.168.1.4
Tomcat1:
IP:192.168.1.5
Tomcat2:
IP:192.168.1.6
实验流程:
Tomcat:
两个Tomcat节点安装流程相同;
先安装Jvm:
[root@node1 ~]# yum install java-1.7.0-openjdk[root@node1 ~]# java -versionjava version "1.7.0_79"OpenJDK Runtime Environment (rhel-2.5.5.4.el6-x86_64 u79-b14)OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)
安装tomcat:
官网:
[root@node1 ~]# tar xf apache-tomcat-7.0.65.tar.gz -C /usr/local/[root@node1 ~]# cd /usr/local/[root@node1 local]# ln -sv apache-tomcat-7.0.65 tomcat"tomcat" -> "apache-tomcat-7.0.65"[root@node1 ~]# vim /etc/profile.d/tomcat.shexport CATALINA_HOME=/usr/local/tomcatexport PATH=$CATALINA_HOME/bin:$PATH[root@node1 ~]# source /etc/profile.d/tomcat.sh
提供脚本:
[root@node1 ~]# cat /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/lib/jvm/jreCATALINA_HOME=/usr/local/tomcatexport JAVA_HOME CATALINA_HOMEcase $1 instart) 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@node1 ~]# chmod +x /etc/rc.d/init.d/tomcat[root@node1 ~]# chkconfig tomcat --add
定义管理页面用户名及密码:
[root@node1 ~]# cd /usr/local/tomcat/conf[root@node1 conf]# vim tomcat-users.xml
配置文件复制到node2,并启动服务:
[root@node1 conf]# scp tomcat-users.xml node2:/usr/local/tomcat/conf/[root@node1 conf]# service tomcat start; ssh node2 'service tomcat start'
访问测试:
部署站点文件:
Node1:[root@node1 ~]# mkdir -p /tomcat/webapps/test/WEB-INF/{classess,lib}[root@node1 ~]# cd /tomcat/webapps/test[root@node1 test]# vim index.jsp<%@ page language="java" %>Tomcat Node1 node1.chencer.org
Session ID | <% session.setAttribute("chencer.org","chencer.org"); %><%= session.getId() %> |
Created on | <%= session.getCreationTime() %> |
node2.chencer.org
Session ID | <% session.setAttribute("chencer.org","chencer.org"); %><%= session.getId() %> |
Created on | <%= session.getCreationTime() %> |
定义虚拟主机,并重启tomcat:
Node1:[root@node1 ~]# cd /usr/local/tomcat[root@node1 tomcat]# vim conf/server.xml[root@node1 tomcat]# service tomcat restartNode2:[root@node2 ~]# cd /usr/local/tomcat[root@node2 tomcat]# vim conf/server.xml [root@node2 tomcat]# service tomcat restart
访问测试:
httpd可以通过2种协议连接tomcat:http,ajp;tomcat启动之后默认会监听在2个端口:8080,8009。8080用于接收http请求,8009则用于接收ajp请求。
配置apache基于mod_proxy实现Tomcat负载均衡:
安装httpd:
[root@node0 ~]# yum install httpd
确保httpd有以下模块:
[root@node0 ~]# httpd -M | grep proxy proxy_module (shared) proxy_balancer_module (shared) proxy_ftp_module (shared) proxy_http_module (shared) proxy_ajp_module (shared) proxy_connect_module (shared)
编辑httpd配置文件:
[root@node0 ~]# cd /etc/httpd/conf[root@node0 conf]# vim httpd.conf# DocumentRoot "/var/www/html" \\:关闭中心主机;在全局配置段中添加以下内容:\\:定义系群名; BalancerMember http://192.168.1.5:8080 loadfactor=1 \\:集群节点,loadfactor表示权重; BalancerMember http://192.168.1.6:8080 loadfactor=1 ProxySet lbmethod=bytraffic \\:调度算法; NameVirtualHost *:80 \\:启用虚拟主机;ServerName node0.chencer.org ProxyVia On \\:via信息; ProxyRequests off \\:关闭正向代理; ProxyPreserveHost on \\:支持虚拟合租记; ProxyPass / balancer://cluster stickysession=jsessionid \\:请求至后端主机; ProxyPassReverse / balancer://cluster stickysession=jsessionid \\:请求至后端主机; [root@node0 conf]# httpd -t[root@node0 conf]# service httpd start
访问测试:
配置apache基于mod_jk实现Tomcat负载均衡:
mod_jk模块仅支持使用ajp协议连接后端的tomcat;由于mod_jk模块出现的较早,httpd默认不包括该模块,需要通过apache的apxs函数编译该模块,安装至apache的模块目录下。若是直接使用yum安装的httpd,需要安装httpd-devel以提供apxs函数。
安装httpd-devel:
[root@node0 ~]# yum install httpd-devel
编译安装mod_jk: 下载地址:
[root@node0 ~]# tar xf tomcat-connectors-1.2.41-src.tar.gz[root@node0 ~]# cd tomcat-connectors-1.2.41-src/native/[root@node0 native]# ./configure --with-apxs="/usr/sbin/apxs"[root@node0 native]# make && make install
编辑httpd配置文件:
[root@node0 ~]# cd /etc/httpd/conf.d/[root@node0 conf.d]# vim mod_jk.confLoadModule jk_module modules/mod_jk.so \\:装载mod_jk模块;JkWorkersFile /etc/httpd/conf.d/workers.properties \\:指定tomcat实例特性配置文件位置;JkLogFile logs/mod_jk.log \\:日志文件位置;JkLogLevel debug \\:日志级别;JkMount /* cluster \\:定义集群名;JkMount /status/ status \\:状态控制页面[root@node0 conf.d]# vim workers.propertiesworker.list = cluster,status \\:工作列表;worker.node1.type = ajp13 \\:后端tomcat工作类型;worker.node1.host = 192.168.1.5 \\:后端主机地址;worker.node1.port = 8009 \\:后端主机端口;worker.node1.lbfactor = 5 \\:后端主机权重worker.node2.type = ajp13worker.node2.host = 192.168.1.6worker.node2.port = 8009worker.node2.lbfactor = 5worker.cluster.type = lb \\:定义组类型;worker.cluster.sticky_session = 0 \\:会话保持关闭;worker.cluster.balance_workers = node1, node2 \\:后端主机加入集群;worker.status.type = status \\:状态页;type有三种类型: ajb13:用于负载调度 status:tomcat自身所带的监控页面,类似于httpd的status lb:集群类型
编辑Tomcat配置文件:
Node1:[root@node1 conf]# vim server.xml[root@node1 conf]# service tomcat restartNode2:[root@node2 conf]# vim server.xml [root@node2 conf]# service tomcat restart
启动httpd:
[root@node0 conf.d]# service httpd start
访问测试:
访问状态页: