一、首先新建 Tomcat 节点 node3
1. 安装JDK
# rpm -ivh jdk-7u5-linux-i586.rpm
定义两个环境变量:
# vi /etc/profile
- JAVA_HOME=/usr/java/jdk1.7.0_05
- PATH=$PATH:$JAVA_HOME/bin
- export JAVA_HOME PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC
退出后再重新登录:
# printenv 查看环境变量
- .....
- PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/java/jdk1.7.0_05/bin:/root/bin
- JAVA_HOME=/usr/java/jdk1.7.0_05
- .....
2. 安装 Tomcat
- # tar xf apache-tomcat-7.0.29.tar.gz -C /usr/local/
- # cd /usr/local/
- # ln -sv apache-tomcat-7.0.29/ tomcat
- create symbolic link `tomcat' to `apache-tomcat-7.0.29/'
3.修改conf/server.xml
1). 添加 jvmRoute
<Engine name="Catalina" defaultHost="localhost" jvmRoute="Tomcat_B">
2). 配置Tomcat的虚拟主机
- # vi /usr/local/tomat/conf/server.xml
"tomcat.yue.com" appBase="/www" unpackWARs="true" autoDeploy="true">"" docBase="/www/tomcat" reloadable="true"/>"org.apache.catalina.valves.AccessLogValve" directory="logs"- prefix="tomcat_access_" suffix=".log"
- pattern="%h %l %u %t "%r" %s %b" />
3). 创建虚拟主机的相关目录和文件
- # mkdir -pv /www/tomcat/WEB-INF/{lib,classes}
- # tree /www/
- /www/
- `-- tomcat
- `-- WEB-INF
- |-- classes
- `-- lib
- # cp /usr/local/tomcat/conf/web.xml /www/tomcat/WEB-INF/
4). 提供主页面
# vi /www/tomcat/index.jsp 内容如下:
- <%@ page language="java" %>
TomcatB "blue"
>TomcatB"centre" border="1">
Session ID <%= session.getId() %> Created on <%= session.getCreationTime() %>
5)启动Tomcat,并测试
二、以mod-jk的方式实现负载均衡
配置前端的Apache
1 .修改/etc/httpd/extra/httpd-jk.conf
- [root@node1 httpd]# vi extra/httpd-jk.conf
- # Load the mod_jk
- LoadModule jk_module modules/mod_jk.so
- JkWorkersFile /etc/httpd/extra/workers.properties
- JkLogFile logs/mod_jk.log
- JkLogLevel debug
- JkMount /* lbcluster-1
- JkMount /jk-status/ stat1
2. 编辑/etc/httpd/extra/workers.properties
- [root@node1 httpd]# vi extra/workers.properties
- worker.list=lbcluster-1,stat1
- worker.Tomcat_A.port=8009
- worker.Tomcat_A.host=192.168.1.12
- worker.Tomcat_A.type=ajp13
- worker.Tomcat_A.lbfactor=1
- worker.Tomcat_B.port=8009
- worker.Tomcat_B.host=192.168.1.13
- worker.Tomcat_B.type=ajp13
- worker.Tomcat_B.lbfactor=1
- worker.lbcluster-1.type = lb
- worker.lbcluster-1.sticky_session = 0 0: 表示不绑定会话 1:表示绑定会话
- worker.lbcluster-1.balance_workers = Tomcat_A,Tomcat_B
- worker.stat1.type = status
3. 编辑httpd.conf
- # Virtual hosts
- #Include /etc/httpd/extra/httpd-vhosts.conf
- Include /etc/httpd/extra/httpd-jk.conf 确保启用了 httpd-jk.conf 的配置
4. 重启Apacher 测试一下
5. 若此时我们停掉node3 上的Tomcat,看是否可以自动检测到后端的健康状况
三、 以mod_proxy 的方式实现负载均衡
可以使用http连接器,也可以使用ajp连接,注意端口不一样
1. 编辑extra/httpd-vhosts.conf
- [root@node1 httpd]# vi extra/httpd-vhosts.conf
- ProxyVia Off
- ProxyRequests Off
- ProxyPreserveHost On
- ServerName tomcat.yue.com
- ProxyPass / balancer://lbcluster-1/ stickysession=jsessionid nofailover=On
- ProxyPa***everse / balancer://lbcluster-1
//lbcluster-1> - BalancerMember http://192.168.1.12:8080 loadfactor=10 route=Tomcat_A 使用的是http 连接器
- BalancerMember http://192.168.1.13:8080 loadfactor=10 route=Tomcat_B
2. 编辑Apache的配置文件
# vi /etc/httpd/httpd.conf
- Include /etc/httpd/extra/httpd-vhosts.conf 确保启用的是 httpd-vhosts.conf 的配置
- #Include /etc/httpd/extra/httpd-jk.conf
四、实现Tomcat基于内存复制的集群
1. 编辑两个Tomcat的配置文件(注意 主机ip不能相同)
# vi conf/server.xml
以下内容定义在Engine容器中,则表示对所有主机均启动用集群功能。如果定义在某Host中,则表示仅对此主机启用集群功能。
"org.apache.catalina.ha.tcp.SimpleTcpCluster" 集群的类为SimpleTcpCluster,也是Tomcat中唯一的一种- channelSendOptions="8"> 心跳信息传输的频道
"org.apache.catalina.ha.session.DeltaManager" - expireSessionsOnShutdown="false"
- notifyListenersOnReplication="true"/>
"org.apache.catalina.tribes.group.GroupChannel" > 定义一个独立的集群,区别集群和其它节点,使用哪个"org.apache.catalina.tribes.membership.McastService" 基于多波通信的方式- address="228.50.10.1" bind="192.168.1.12" port="45564" 同一集群中多波地址要一样;多波地址、通过哪个地址所在的网卡发送信息、通过哪个端口
- frequency="500" dropTime="3000"/> 多长时间收不到对方的响应信息,就认为其故障
"org.apache.catalina.tribes.transport.nio.NioReceiver" 接收器- address="192.168.1.12" port="4000" autoBind="100" 端口、自动绑定
- selectorTimeout="5000" maxThreads="6"/>
"org.apache.catalina.tribes.transport.ReplicationTransmitter" > 发送器"org.apache.catalina.tribes.transport.nio.PooledParallelSender" /> 定义如何向外发送信息"org.apache.catalina.tribes.group.interceptors.TcpFailureDetector" /> 过滤器,定义节点间互相传递信息的时候要传递哪些信息,传递信息的时候是否要认证"org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor" />"org.apache.catalina.ha.tcp.ReplicationValve" - filter=".*\.gif;.*\.js;.*\.jpg;.*\.htm;.*\.html;.*\.txt;"/>
"org.apache.catalina.ha.session.JvmRouteBinderValve" /> JvmRoute"org.apache.catalina.ha.deploy.FarmWarDeployer" 可以实现分布式程序布署的- tempDir="/tmp/war-temp/" deployDir="/tmp/war-deploy/"
- watchDir="/tmp/war-listen/" watchEnabled="false"/>
"org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener" /> 监听器,基于事件的,当某种类型的事件发生时能够触发某种动作的"org.apache.catalina.ha.session.ClusterSessionListener" />
1.1>. 编辑 web.xml
- # vi /www/tomcat/WEB-INF/web.xml 添加如下内容:
2. 通过 mod_proxy 模块工作
2.1> 编辑http.conf
- # Virtual hosts
- Include /etc/httpd/extra/httpd-vhosts.conf 确保启用的是 httpd-vhosts.cnf 的配置
- #Include /etc/httpd/extra/httpd-jk.conf
2.2> 编辑 extra/httpd-vhosts.conf
- ProxyRequests Off
- ProxyPreserveHost On
- ServerName tomcat.yue.com
- ProxyPass / balancer://lbcluster-1/ stickysession=jsessionid nofailover=On
- ProxyPa***everse / balancer://lbcluster-1
//lbcluster-1> - BalancerMember http://192.168.1.12:8080 loadfactor=10 route=Tomcat_A
- BalancerMember http://192.168.1.13:8080 loadfactor=10 route=Tomcat_B
2.3> 重启Apache,测试一下效果:
3. 通过mod-jk 模块工作
3.1> 编辑 extra/httpd-jk.conf
- [root@node1 httpd]# vi extra/httpd-jk.conf
- # Load the mod_jk
- LoadModule jk_module modules/mod_jk.so
- JkWorkersFile /etc/httpd/extra/workers.properties
- JkLogFile logs/mod_jk.log
- JkLogLevel debug
- JkMount /* lbcluster-1
- JkMount /jk-status/ stat1
3.2> 编辑 extra/workers.properties
- [root@node1 httpd]# vi extra/workers.properties
- worker.list=lbcluster-1,stat1
- worker.Tomcat_A.port=8009
- worker.Tomcat_A.host=192.168.1.12
- worker.Tomcat_A.type=ajp13
- worker.Tomcat_A.lbfactor=1
- worker.Tomcat_B.port=8009
- worker.Tomcat_B.host=192.168.1.13
- worker.Tomcat_B.type=ajp13
- worker.Tomcat_B.lbfactor=1
- worker.lbcluster-1.type = lb
- worker.lbcluster-1.sticky_session = 0
- worker.lbcluster-1.balance_workers = Tomcat_A,Tomcat_B
- worker.stat1.type = status
3.3> 编辑 httpd.conf
- # Virtual hosts
- #Include /etc/httpd/extra/httpd-vhosts.conf
- Include /etc/httpd/extra/httpd-jk.conf 确保启用的是httpd-jk.conf 的配置
3.4> 测试一下,看看效果如何
4. 另外我们也可以将 extra/httpd-vhosts.conf 文件中内容单独定义在一文件中,如extra/httpd-proxy.conf
4.1> 编辑 extra/httpd-proxy.conf
- ProxyVia Off
- ProxyRequests Off
- ProxyPreserveHost On
- ServerName tomcat.yue.com
- # ProxyPass / balancer://lbcluster-1/ stickysession=jsessionid nofailover=On jsessionid -->java 程序 psessionid -->php 程序
- ProxyPa***everse / balancer://lbcluster-1
//lbcluster-1> - ProxySet lbmethod=bytraffic 负载均衡的调度算法,apache做代理的时候有三种
- BalancerMember http://192.168.1.12:8080 loadfactor=10 route=Tomcat_A
- BalancerMember http://192.168.1.13:8080 loadfactor=10 route=Tomcat_B
- ProxyPass /lb-manager ! 表示不做代理
- ProxyPass /status !
- ProxyStatus full
- SetHandler balancer-manager 额外的处理器
- Require all granted
- SetHandler server-status
- Require all granted
4.2> 编辑httpd.conf 将extra/httpd-proxy.conf 文件包含进去
Include /etc/httpd/extra/httpd-proxy.conf 确保启用的是httpd-proxy.conf 的配置- # Include /etc/httpd/extra/httpd-vhosts.conf
- # Include /etc/httpd/extra/httpd-jk.conf
4.3> 重启Apache,测试一下效果
4.4> 查看一下相关的状态信息
后记: 一定要注意 三台机器的时间必须要同步,否则就可能会出现一些问题
如:只显示Apache 工作了,但跳转不到后端Tomcat的虚拟主机上
由于本人能力有限,若本文中有不足或错误之处还请各位看官多多指教!