Tomc
tomcat簡介:Tomcat 服務器是一個免費的開放源代碼的Web 應用服務器,屬於輕量級應用服務器,在中小型系統和併發訪問用戶不是很多的場合下被普遍使用,是開發和調試JSP 程序的首選。對於一個初學者來說,可以這樣認爲,當在一臺機器上配置好Apache 服務器,可利用它響應HTML(標準通用標記語言下的一個應用)頁面的訪問請求。實際上Tomcat 部分是Apache 服務器的擴展,但它是獨立運行的,所以當你運行tomcat 時,它實際上作爲一個與Apache 獨立的進程單獨運行的
實驗環境:
server1:172.25.45.1
server2:172.25.45.2
iptables off and selinux disabled
安裝tomcat
所需要的軟件包: apache-tomcat-7.0.37.
tar
.gz
1 | tar zxf apache-tomcat-7.0.37. tar .gz -C /usr/local/lnmp/ |
1 2 3 4 | cd /usr/local/lnmp/ ln -s apache-tomcat-7.0.37/ tomcat cd tomcat/ bin /startup .sh |
1 | vim /usr/local/lnmp/nginx/conf/nginx .conf |
在location ~ \.php$之後,添上以下內容(82行之後)
location ~ \.jsp$ {
proxy_pass http://172.25.45.1:8080;
}
1 | nginx -s reload |
1 2 | 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】
1 2 3 |
【server2】
1 2 3 4 | source /etc/profile echo $PATH cd /usr/local/tomcat bin /startup .sh |
訪問server2的tomcat
3.rr論尋
【server1】
1 | 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
1 2 | nginx -t nginx -s reload |
1 2 | cd /usr/local/lnmp/tomcat/webapps/ROOT vim test .jsp |
在內容最前面加上server1-
【server2】
1 | 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
1 | vim /usr/local/lnmp/nginx/conf/nginx .conf |
在17行upstream westos段下添上ip_hash;
同一個ip訪問的是同一臺服務器
1 | . /shutdown .sh |
關閉一臺後訪問另外一臺
5.sticky模式
從新編譯1.8版本的nginx
1 2 | tar zxf nginx-1.8.0. tar .gz tar zxf nginx-sticky-module-1.0. tar .gz |
1 2 3 4 | . /configure \ --prefix= /usr/local/lnmp/nginx \ --with-http_ssl_module --with-http_stub_status_module \ --add-module= /mnt/nginx-sticky-module-1 .0 |
1 2 | or . /configure --prefix= /usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --add-module= /root/nginx-sticky-module-1 .0 |
1 | vim /usr/local/lnmp/nginx/conf/nginx .conf |
在17行upstream westos段下注釋掉ip_hash添上sticky
1 | 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不掛,就一直訪問同一個
1 | vim /usr/local/lnmp/nginx/conf/nginx .conf |
註釋掉sticky
1 2 | nginx -t nginx -s reload |
【server1】
1 | vim /usr/local/lnmp/nginx/conf/nginx .conf |
取消sticky的註釋
1 2 | nginx -t nginx -s reload |
【server2】
1 2 | cd /usr/local/tomcat/logs tail -f catalina.out |
1 2 | cd /usr/local/tomcat bin /shutdown .sh |