實驗環境:
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