实验环境:
server1:nginx tomcat memcached
Server6:tomcat memcached
一、jdk部署(server1和server6都要部署)
1、什么是jdk
jdk是java语言的软件开发工具包,,它是整个java开发的核心,它包含了java的运行环境(jvm+java系统类库)和java工具
2、解压到指定目录并且做软链接,以便之后使用
[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
3、增加环境变量
[root@server1 ~]# vim /etc/profile
[root@server1 ~]# source /etc/profile
4、编写一个简单的java程序测试:输出hello world
[root@server1 ~]# vim test.java #编写程序
public class test {
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
[root@server1 ~]# javac test.java #生成.class
[root@server1 ~]# java test #执行程序
Hello World! #执行结果
二、tomcat的部署(server1和server6)
1、什么是tomcat
Tomcat服务器是一个免费的开放源代码的web应用服务器,属于轻量级应用服务器,是开发和调试的首选,tomcat实际上是apache服务器的扩展,但他运行时是独立运行的,所以当运行tomcat时,它实际上作为一个与apache独立的进程单独运行的。当配置正确时,apache为HTNL页面服务,而tomcat实际上运行JSP页面和servlet
2、解压到指定目录并且做软链接
[root@server1 ~]# tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/
[root@server1 ~]# cd /usr/local/
[root@server1 local]# ln -s apache-tomcat-7.0.37/ tomcat
3、开启tomcat
[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
查看端口[root@server1 tomcat]# netstat -antpl
4、更改nginx的配置文件,添加java程序可以访问
[root@server1 tomcat]# cd /usr/local/lnmp/nginx/conf/ #当初源码编译nginx时,编译到这个地方了,所以修改配置文件也在这个地方修改
[root@server1 conf]# vim nginx.conf
73 location ~ \.jsp$ {
74 proxy_pass http://172.25.66.1:8080; #因为我们访问浏览器时访问172.25.66.1:8080有些麻烦,所以可以用nginx的反向代理
75 }
[root@server1 conf]# nginx #开启nginx
[root@server1 conf]# nginx -t #检测语法
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
[root@server1 conf]# nginx -s reload #重启nginx
5、测试tomcat
[root@server1 conf]# cd /usr/local/tomcat/webapps/ROOT/
[root@server1 ROOT]# vim test.jsp
the time is: <%=new java.util.Date() %> #编写一个时间函数
在浏览器中输入172.25.66.1/test.jsp
测试在1和6中轮询,在访问以.jsp结尾的网站时自动转到1或6的8080(nginx)上去
在server1上修改nginx配置文件
[root@server1 nginx-1.14.0]# cd /usr/local/lnmp/nginx/conf/
[root@server1 conf]# vim nginx.conf
17 http {
18 upstream tomcat {
19 server 172.25.66.1:8080;
20 server 172.25.66.6:8080;
21 }
22 include mime.types;
23 default_type application/octet-stream;
76
77 location ~ \.jsp$ {
78 proxy_pass http://tomcat;
79 }
在server1和server6上写对应的测试界面
[root@server1 ~]# cd /usr/local/tomcat/webapps/ROOT/
[root@server1 ROOT]# cat test.jsp
server1---the time is: <%=new java.util.Date() %>
[root@server6 lnmp]# cd /usr/local/tomcat/webapps/ROOT/
[root@server6 ROOT]# cat test.jsp
server6---the time is: <%=new java.util.Date() %>
在浏览器中访问测试 172.25.66.1/test.jsp
修改nginx配置文件,加入sticky模块,测试页面将不会处先轮询
[root@server1 ROOT]# cd /usr/local/lnmp/nginx/conf/
[root@server1 conf]# vim nginx.conf
17 http {
18 upstream tomcat {
19 sticky;
20 server 172.25.66.1:8080;
21 server 172.25.66.6:8080;
22 }
[root@server1 conf]# nginx -t
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
[root@server1 conf]# nginx -s reload
四、在tomcat中实现session共享
1、修改测试页面
[root@server1 conf]# cd /usr/local/tomcat/webapps/ROOT/
[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>
测试172.25.66.1/test.jsp
2、memcache在tomcat中实现session的交叉存储(server1和server6)
交叉存储就是nginx服务输入数据,memcache1和memcache2接收这两个数据,并保存。现在又加上了tomcat1和tomcat2服务器。memcache1这个节点在tomcat2这个节点上存储,memcache2这个节点在tomcat1这个节点上存储,当tomcat1这个节点坏掉,memcache2这个节点就访问自己本机的tomcat2,保证session会话保持,当tomcat两个服务器都宕机之后,会话会继续保存到memcache这个服务器上,保证了session保持
[root@server1 ROOT]# yum install -y memcached
[root@server1 ROOT]# /etc/init.d/memcached start
[root@server1 ROOT]# cd ..
[root@server1 webapps]# cd ..
[root@server1 tomcat]# cd conf/
[root@server1 conf]# vim context.xml
35 <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionM anager"
36 memcachedNodes="n1:172.25.66.1:11211,n2:172.25.66.6:11211"
37 failoverNodes="n1" #server6这写n2
38 requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
39 transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.Kry oTranscoderFactory"
40 />
在/usr.local/tomcat/lib目录下安装jar包
由于我们memcache的版本是1.7,1.6的存在会影响memcache,所以要删除1.6版本的jar包
[root@server1 lib]# rm -fr memcached-session-manager-tc6-1.6.3.jar
查看tomcat日志,当server1和server6两边同时出现次信息,表示共享成功
server1日志的内容:
server6日志的内容:
测试访问172.25.66.1/test.jsp
现在session共享在server1(n1)上,当server1的memcache关掉之后,客户端依旧可以输入name还有key,但是session共享却在server6(n2)上,保证了客户端可以不间断的输入name还有key。
也可以用telnet远程登录服务查看session
[root@server6 tomcat]# yum install -y telnet
[root@server6 tomcat]# telnet localhost 11211