拓扑图
准备环境:
由于电脑配置烂,虚拟机有限,故一个虚拟机同时作多种服务器类型使用。
两个tomcat节点:172.16.19.1(tomcatA.luo.com),172.16.19.21(tomcatB.luo.com)
两个memcached节点:172.16.19.21, 172.16.19.22
一个负载均衡节点(httpd):172.16.19.1
两边都进行基本的jdk与tomcat安装
安装jdk
下载安装包并安装:
# rpm -ivh jdk-7u9-linux-x64.rpm Preparing... # ########################################## [100%] 1 :jdk # ########################################## [100%] Unpacking JAR files... rt.jar... Error: Could not open input file: / usr / java / jdk1. 7 .0_09 / jre / lib / rt.pack jsse.jar... Error: Could not open input file: / usr / java / jdk1. 7 .0_09 / jre / lib / jsse.pack charsets.jar... Error: Could not open input file: / usr / java / jdk1. 7 .0_09 / jre / lib / charsets.pack tools.jar... Error: Could not open input file: / usr / java / jdk1. 7 .0_09 / lib / tools.pack localedata.jar... Error: Could not open input file: / usr / java / jdk1. 7 .0_09 / jre / lib / ext / localedata.pack
说明:这些错误暂时可以忽略,不影响使用
查看java的安装目录
# ll /usr/java/
total 4
lrwxrwxrwx 1 root root 16 Sep 26 06:03 default -> /usr/java/latest
drwxr-xr-x 10 root root 4096 Sep 26 06:03 jdk1.7.0_09
lrwxrwxrwx 1 root root 21 Sep 26 06:03 latest -> /usr/java/jdk1.7.0_09
latest链接指定最新版本
修改环境变量
# 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
java version "1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)
安装Tomcat
解压并创建链接
# tar -xf apache-tomcat-7.0.42.tar.gz -C /usr/local
# cd /usr/local
# ln -sv apache-tomcat-7.0.42 tomcat
`tomcat' -> `apache-tomcat-7.0.42'
修改环境变量
# vim /etc/profile.d/tomcat.sh
export CATALINA_HOME=/usr/local/tomcat
export PATH=$CATALINA_HOME/bin:$PATH
# . /etc/profile.d/tomcat.sh
启动tomcat
# catalina.sh start # 或者使用命令startup.sh,默认使用8080端口,先确定端口没有被占用 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
查看启动的端口
# netstat -tnlp |grep java
tcp 0 0 :::8080 :::* LISTEN 4357/java
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 4357/java
tcp 0 0 :::8009 :::* LISTEN 4357/java
查看版本
# cd /usr/local/tomcat # catalina.sh version 或者version.sh 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 Server version: Apache Tomcat / 7.0 . 42 Server built: Jul 2 2013 08 : 57 : 41 Server number: 7.0 . 42.0 OS Name: Linux OS Version: 2.6 . 32 - 431 .el6.x86_64 Architecture: amd64 JVM Version: 1.7 .0_09 - b05 JVM Vendor: Oracle Corporation
测试tomcat成功运行:
二、配置代理、负载均衡、memcached
memcached-session-manager项目地址,http://code.google.com/p/memcached-session-manager/
下载如下jar文件至各tomcat节点的tomcat安装目录下的lib目录中,其中软件的版本号要和tomcat是相同的版本号。
node、node1节点:
memcached-session-manager-1.8.2.jar memcached-session-manager-tc7-1.8.2.jar msm-javolution-serializer-1.8.2.jar spymemcached-2.10.2.jar javolution-5.5.1.jar
分别在两个tomcat上的某host上定义一个用于测试的context容器,并在其中创建一个会话管理器,如下所示(node、node1节点):
<Context path = " /test " docBase = " /usr/local/tomcat/webapps/test " reloadable = " true " > <Manager className = " de.javakaffee.web.msm.MemcachedBackupSessionManager " memcachedNodes = " n1:172.16.19.21:11211,n2:172.16.19.22:11211 " failoverNodes = " n1 " requestUriIgnorePattern = " .*\.(ico|png|gif|jpg|css|js)$ " transcoderFactoryClass = " de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory " /> < / Context >
node(tomcatA):
# mkdir -pv /usr/local/tomcat/webapps/test/WEB-INF/{classes,lib}
# vim /usr/local/tomcat/webapps/test/index.jsp
添加如下内容:
< %@ page language = " java " %> <html > <head > <title > TomcatA< / title > < / head > <body > <h1 > <font color = " red " > TomcatA.luo.com< / font > < / h1 > <table align = " centre " border = " 1 " > <tr > <td > Session ID< / td > < % session.setAttribute( " luo.com " , " luo.com " ); %> <td > < %= session.getId() %> < / td > < / tr > <tr > <td > Created on< / td > <td > < %= session.getCreationTime() %> < / td > < / tr > < / table > < / body > < / html >
node1(tomcatB):
# mkdir -pv /usr/local/tomcat/webapps/test/WEB-INF/{classes,lib}
# vim /usr/local/tomcat/webapps/test/index.jsp
添加如下内容:
< %@ page language = " java " %> <html > <head > <title > TomcatB< / title > < / head > <body > <h1 > <font color = " red " > TomcatB.luo.com< / font > < / h1 > <table align = " centre " border = " 1 " > <tr > <td > Session ID< / td > < % session.setAttribute( " luo.com " , " luo.com " ); %> <td > < %= session.getId() %> < / td > < / tr > <tr > <td > Created on< / td > <td > < %= session.getCreationTime() %> < / td > < / tr > < / table > < / body > < / html >
在node节点(172.16.19.1)上配置反向代理的负载均衡内容
可以新建个文件mod_http.con,类似如下所示:
配置apache通过mod_proxy模块与Tomcat连接
查看相关模块是否启用
[root@node ~]# httpd -M |grep proxy
Syntax OK
proxy_module (shared)
proxy_balancer_module (shared)
proxy_ftp_module (shared)
proxy_http_module (shared)
proxy_ajp_module (shared)
proxy_connect_module (shared)
# vim /etc/httpd/conf.d/mod_http.conf
<Proxy balancer: // tomcat > BalancerMember http: // 172.16 . 19.1 : 8080 loadfactor = 1 BalancerMember http: // 172.16 . 19.21 : 8080 loadfactor = 1 # 如果使用ajp则使用下面现行替换上面现行即可 # BalancerMember ajp://172.16.19.1:8009 loadfactor=1 # BalancerMember ajp://172.16.19.21:8009 loadfactor=1 ProxySet lbmethod = byrequests < / Proxy > <Location / status > SetHandler balancer - manager ProxyPass ! Order allow,deny Allow from all < / Location > ProxyPass / balancer: // tomcat / ProxyPassReverse / balancer: // tomcat /
# yum -y install httpd-devel # rpm -ql httpd-devel |grep apxs # tar xf tomcat-connectors-1.2.40-src.tar.gz # cd tomcat-connectors-1.2.40-src/native # ./configure --with-apxs=/usr/sbin/apxs # make && make install
# cat /etc/httpd/conf.d/mod_jk.conf #新添加文件,把之前添加的mod_http.conf暂时改个后续名
LoadModule jk_module modules / mod_jk.so JkWorkersFile / etc / httpd / conf.d / workers.properties JkLogFile logs / mod_jk.log JkLogLevel debug # 调试时使用debug JkMount /* lbA JkMount / status / statA
# cat /etc/httpd/conf.d/workers.properties
worker.list = lbA,statA worker.TomcatA.type = ajp13 worker.TomcatA.host = 172.16 . 19.1 worker.TomcatA.port = 8009 worker.TomcatA.lbfactor = 1 worker.TomcatB.type = ajp13 worker.TomcatB.host = 172.16 . 19.21 worker.TomcatB.port = 8009 worker.TomcatB.lbfactor = 1 worker.lbA.type = lb worker.lbA.sticky_session = 0 worker.lbA.balance_workers = TomcatA,TomcatB worker.statA.type = status 需要在两边的主配置文件中加上jvmRoute参数 # cat /usr/local/tomcat/conf/server.xml <Engine name = ”catalina” defaultHost = ”localhost” jvmRoute = ”TomcatA ” > <Engine name = ”catalina” defaultHost = ”localhost” jvmRoute = ”TomcatB ” > # catalina.sh start # service httpd restart
三、配置node1与node2的memcached
# yum install �Cy install memcached
# service memcached start
# ss �Ctnl |grep 11211
四、测试结果
在浏览器中访问http://172.16.19.1/test,结果如下所示,其session ID在负载均衡环境中保持不变。
负载均衡与session绑定已经可以实现
2、模拟node2故障,停止node2的memcached,可以看到已经转到n1(memcached1)上面了且session保持不变
[root@node2 ~]# service memcached stop