1.安裝tomcat
tar zxf apache-tomcat-7.0.37.tar.gz -C /usr/local/lnmp/
cd /usr/local/lnmp/ ln -s apache-tomcat-7.0.37/ tomcat cd tomcat/ bin/startup.sh
vim /usr/local/lnmp/nginx/conf/nginx.conf
在location ~ \.php$之後,添上以下內容(82行之後)
location ~ \.jsp$ {
proxy_pass http://172.25.45.1:8080;
}
nginx -s reload
cd /usr/local/lnmp/tomcat/webapps/ROOT vim test.jsp
The time is:<%=new java.util.Date()%>
2.tomcat 集羣
再啓動一個虛擬機server2,把配置好的java-jdk和tomcat複製到啓動的server2中
【server1】
scp -r /usr/local/java/ [email protected]:/usr/local/ scp -r /usr/local/lnmp/tomcat/ [email protected]:/usr/local/ scp /etc/profile [email protected]:/etc/
【server2】
source /etc/profile echo $PATH cd /usr/local/tomcat bin/startup.sh
訪問server2的tomcat
3.rr論尋
【server1】
vim /usr/local/lnmp/nginx/conf/nginx.conf
16 http {
17 upstream westos{
18 server 172.25.45.1:8080;
19 server 172.25.45.2:8080;
20 }
修改server後的端口值爲8080
81 location ~ \.jsp$ {
82 proxy_pass http://westos;
83 }
將proxy_pass改爲http://westos
nginx -t nginx -s reload
cd /usr/local/lnmp/tomcat/webapps/ROOT vim test.jsp
在內容最前面加上server1-
【server2】
cd /usr/local/tomcat/webapps/ROOT
對server2的test.jsp也進行修改,在內容最前面加上server2-
訪問172.25.45.1/test.jsp
頁面顯示會在server1-The time與The time之間切換
關閉server1的tomcat,只顯示server2的/usr/local/lnmp/tomcat/webapps/ROOT/test.jsp 內容
4.ip_hash
vim /usr/local/lnmp/nginx/conf/nginx.conf
在17行upstream westos段下添上ip_hash;
同一個ip訪問的是同一臺服務器
./shutdown.sh
關閉一臺後訪問另外一臺
5.sticky模式
從新編譯1.8版本的nginx
tar zxf nginx-1.8.0.tar.gz tar zxf nginx-sticky-module-1.0.tar.gz
./configure \ --prefix=/usr/local/lnmp/nginx \ --with-http_ssl_module --with-http_stub_status_module \ --add-module=/mnt/nginx-sticky-module-1.0
or ./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --add-module=/root/nginx-sticky-module-1.0
vim /usr/local/lnmp/nginx/conf/nginx.conf
在17行upstream westos段下注釋掉ip_hash添上sticky
vim /usr/local/lnmp/tomcat/webapps/ROOT/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>
sticky 模式當用戶訪問,只要訪問主機tomcat不掛,就一直訪問同一個
vim /usr/local/lnmp/nginx/conf/nginx.conf
註釋掉sticky
nginx -t nginx -s reload
【server1】
vim /usr/local/lnmp/nginx/conf/nginx.conf
取消sticky的註釋
nginx -t nginx -s reload
【server2】
cd /usr/local/tomcat/logs tail -f catalina.out
cd /usr/local/tomcat bin/shutdown.sh
server2關閉,訪問server1