###一.安装环境介绍:
【server1】 IP:172.25.39.1 tomcatA,nginx,memcached
注意:nginx可以分开重新安装到另一台虚拟机中
【server2】 IP:172.25.39.2 tomcatB,memcached
【用户主机】 IP:172.25.39.250 client
交叉存储:
TomcatA将 session 存储在 memcachedB上。只有当 MB不可用时,TA 才将 session 存储在 memcachedA 上(MA 是 TAfailoverNode)。使用这种配置的 好处是,当 TA 和 MA 同时崩溃时也不会丢失 session 会话,避免单点故障。
二.软件安装与配置:
1.安装jdk服务:
[root@server1 ~]# tar zxf jdk-7u79-linux-x64.tar.gz -C /usr/local/
[root@server1 ~]# cd /usr/local/
[root@server1 local]# ln -s jdk1.7.0_79/ java
[root@server1 local]# ll
total 48
drwxr-xr-x. 2 root root 4096 Jun 28 2011 bin
drwxr-xr-x. 2 root root 4096 Jun 28 2011 etc
drwxr-xr-x. 2 root root 4096 Jun 28 2011 games
drwxr-xr-x. 2 root root 4096 Jun 28 2011 include
lrwxrwxrwx 1 root root 12 Aug 7 09:58 java -> jdk1.7.0_79/
drwxr-xr-x 8 uucp 143 4096 Apr 11 2015 jdk1.7.0_79
drwxr-xr-x. 2 root root 4096 Jun 28 2011 lib
drwxr-xr-x. 2 root root 4096 Jun 28 2011 lib64
drwxr-xr-x. 2 root root 4096 Jun 28 2011 libexec
drwxr-xr-x 11 root root 4096 Aug 7 09:54 nginx
drwxr-xr-x. 2 root root 4096 Jun 28 2011 sbin
drwxr-xr-x. 5 root root 4096 Jul 27 10:08 share
drwxr-xr-x. 2 root root 4096 Jun 28 2011 src
[root@server1 local]# vim /etc/profile
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
[root@server1 local]# source /etc/profile
[root@server1 ~]# vim test.jsp
- 1
- 2
[root@server1 ~]# mv test.jsp test.java
[root@server1 ~]# javac test.java
[root@server1 ~]# java test
Hello World!
- 1
- 2
- 3
- 4
- 5
2.安装tomcat服务:
[root@server1 ~]# tar zxf apache-tomcat-7.0.90.tar.gz -C /usr/local/
[root@server1 ~]# cd /usr/local/
[root@server1 local]# ls
apache-tomcat-7.0.90 etc include jdk1.7.0_79 lib64 nginx share
bin games java lib libexec sbin src
[root@server1 local]# ln -s apache-tomcat-7.0.90/ tomcat
[root@server1 local]# ls
apache-tomcat-7.0.90 games jdk1.7.0_79 libexec share
bin include lib nginx src
etc java lib64 sbin tomcat
[root@server1 local]# cd tomcat
[root@server1 tomcat]# ls
bin CONTRIBUTING.md logs RELEASE-NOTES webapps
BUILDING.txt lib NOTICE RUNNING.txt work
conf LICENSE README.md temp
[root@server1 tomcat]# bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@server1 tomcat]# ls
bin CONTRIBUTING.md logs RELEASE-NOTES webapps
BUILDING.txt lib NOTICE RUNNING.txt work
conf LICENSE README.md temp
[root@server1 tomcat]# cd webapps/
[root@server1 webapps]# cd ROOT/
[root@server1 ROOT]# pwd
/usr/local/tomcat/webapps/ROOT
[root@server1 ROOT]# ll index.jsp
-rw-r--r-- 1 root root 12197 Jul 3 01:09 index.jsp
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
【测试】:查看tomcat服务是否建立正常
[root@server1 ROOT]# vim test.jsp ##编辑显示内容
- 1
[root@server1 ~]# cd /usr/local/
[root@server1 local]# ls
apache-tomcat-7.0.90 games jdk1.7.0_79 libexec share
bin include lib nginx src
etc java lib64 sbin tomcat
[root@server1 local]# cd nginx/
[root@server1 nginx]# ls
client_body_temp fastcgi_temp logs sbin uwsgi_temp
conf html proxy_temp scgi_temp
[root@server1 nginx]# cd conf/
[root@server1 conf]# vim nginx.conf
[root@server1 conf]# nginx -s reload
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
【测试】 再次访问刷新时,会进行动态变化
【server2】进行相同操作
3.在server1的nginx配置文件中添加应用服务,实现负载均衡:
[root@server1 ~]# cd /usr/local/
[root@server1 local]# ls
apache-tomcat-7.0.90 games jdk1.7.0_79 libexec share
bin include lib nginx src
etc java lib64 sbin tomcat
[root@server1 local]# cd nginx/
[root@server1 nginx]# ls
client_body_temp fastcgi_temp logs sbin uwsgi_temp
conf html proxy_temp scgi_temp
[root@server1 nginx]# cd conf/
[root@server1 conf]# vim nginx.conf
[root@server1 conf]# nginx -s reload
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
##将【server1】里的test.jsp文件发送【server2】中,测试负载均衡
[root@server1 ~]# cd /usr/local/tomcat/
[root@server1 tomcat]# cd webapps/ROOT/
[root@server1 ROOT]# scp test.jsp root@172.25.39.2:/usr/local/tomcat/webapps/ROOT/
The authenticity of host '172.25.39.2 (172.25.39.2)' can't be established.
RSA key fingerprint is ce:b7:35:21:60:9f:f3:8d:f4:25:af:73:ad:ad:bc:ab.
Are you sure you want to continue connecting (yes/no)? YES
Warning: Permanently added '172.25.39.2' (RSA) to the list of known hosts.
root@172.25.39.2's password:
test.jsp 100% 41 0.0KB/s 00:00
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
到此配置完成,可以测试了:
以下为测试页面,保存到/usr/local/tomcat/webapps/ROOT/test.jsp
[root@server1 ROOT]# vim test.jsp
##编辑内容:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
out.println("<br> ID " + session.getId()+"<br>");
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.print("<b>Session list</b>");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"<br>");
System.out.println( name + " = " + value);
}
%>
<form action="test.jsp" method="POST">
name:<input type=text size=20 name="dataName">
<br>
key:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>
[root@server1 ROOT]# scp test.jsp root@172.25.39.2:/usr/local/tomcat/webapps/ROOT/
root@172.25.39.2's password:
test.jsp 100% 968 1.0KB/s 00:00
测试页面为:
先添加use1,再添加use2进行测试,查看效果
会跳转到另一台服务器,且数据没有进行存储
下载安装nginx,添加strickey模块:
[root@server1 ROOT]# nginx -s stop
[root@server1 ~]# tar zxf nginx-sticky-module-ng.tar.gz
[root@server1 ~]# cd /var/local/
[root@server1 nginx-1.14.0]# ./configure –prefix=/usr/local/nginx –with-http_ssl_module –with-http_stub_status_module –with-threads –with-file-aio –add-module=/root/nginx-sticky-module-ng
[root@server1 nginx-1.14.0]# make && make install
[root@server1 conf]# vim nginx.conf
[root@server1 nginx-1.14.0]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 conf]# nginx
测试:当添加数据后,单节点会进行存储
此时的数据在一个结点上可以储存下来,并不能进行session共享
关闭正在工作的节点:
[root@server1 tomcat]# bin/shutdown.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
- 1
- 2
- 3
- 4
- 5
- 6
- 7
三.进行session共享交叉存储搭建:
【server1】【server2】
1.下载memcached储存服务:
[root@server1 ~]# yum install -y memcached
[root@server1 ~]# /etc/init.d/memcached start
Starting memcached: [ OK ]
- 1
- 2
- 3
- 4
2.增加插件包:
[root@foundation39 Desktop]# scp /home/kiosk/Desktop/jar/*.jar root@172.25.39.1:/usr/local/tomcat/lib
root@172.25.39.2's password:
asm-3.2.jar 100% 42KB 42.4KB/s 00:00
kryo-1.04.jar 100% 93KB 92.6KB/s 00:00
kryo-serializers-0.10.jar 100% 59KB 59.2KB/s 00:00
memcached-session-manager-1.6.3.jar 100% 129KB 129.1KB/s 00:00
memcached-session-manager-tc7-1.6.3.jar 100% 9258 9.0KB/s 00:00
minlog-1.2.jar 100% 4879 4.8KB/s 00:00
msm-kryo-serializer-1.6.3.jar 100% 23KB 23.2KB/s 00:00
reflectasm-1.01.jar 100% 11KB 11.3KB/s 00:00
spymemcached-2.7.3.jar 100% 398KB 398.4KB/s 00:00
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
3.配置共享服务文件ip:
[root@server1 lib]# vim /usr/local/tomcat/conf/context.xml
编辑内容:
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:172.25.39.1:11211,n2:172.25.39.1:11211"
failoverNodes="n1"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTransco derFactory"
/>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
4.进行测试:
(1)在真机上测试网页添加数据:【server1】工作
(2)关闭【server1】模拟节点故障
[root@server1 ~]# cd /usr/local/tomcat/bin/
[root@server1 bin]# ./shutdown.sh
(3)刷新真机网页
刚才在【server1】中输入的数据还在