三、Apache Proxy負載均衡發佈器的安裝配置:
1、Apache發佈器使用系統自帶的httpd服務,在安裝操作系統的時候先選擇好服務器下面的web服務器組件。
2、修改配置web發佈服務的參數文件:
# cd /etc/httpd/conf/
# vim httpd.conf (在最底下添加如下內容)
----> ServerName localhost:80 (發佈的端口說明)
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName localhost
ServerAlias localhost
<proxy balancer://testcluster> (“testcluster”可自己命名,沒什麼特別的意義)BalancerMember ajp://192.168.0.101:8011 loadfactor=1 route=tomcat1 (要與tomcat1中的ajp對應)BalancerMember ajp://192.168.0.101:8012 loadfactor=1 route=tomcat2(要與tomcat2中的ajp對應)BalancerMember ajp://192.168.0.102:8013 loadfactor=1 route=tomcat3(要與tomcat3中的ajp對應)BalancerMember ajp://192.168.0.104:8014 loadfactor=1 route=tomcat4(要與tomcat4中的ajp對應)
</proxy>
ProxyRequests Off
ProxyPass /server-status !
ProxyPass /balancer-manager !
ProxyPass / balancer://testcluster/ stickysession=JSESSIONID (注意balancer前面有空格)
ProxyPa***everse / balancer://testcluster/ (注意balancer前面有空格)
</VirtualHost>
<Location /server-status> (負載均衡服務器的狀態查詢,http://192.168.0.100/server-status)
SetHandler server-status
</Location>
<Location /balancer-manager> (負載均衡服務器的管理查看,http://192.168.0.100/balancer-manager)
SetHandler balancer-manager
</Location>
3、負載均衡集羣測試實例:
(1)在tomcat目錄下面的webapps文件夾中新建test測試文件:
# mkdir webapps/test
# vi index.jsp (文件內容如下)
?<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
request.setCharacterEncoding("UTF-8");
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
out.println("<br> ID " + session.getId()+"<br>"); // 如果有新的 Session 屬性設置
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
} out.print("<b>Session 列表</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="index.jsp" method="POST">
名稱:<input type=text size=20 name="dataName">
<br>
值:<input type=text size=20 name="dataValue">
<br>
<input type=submit>
</form>
</body>
</html>
(2)對Apache負載均衡進行測試:
登錄測試首頁: http://192.168.0.100/test/index.jsp
登錄管理界面:http://192.168.0.100/balancer-manager 可查看“Elected”的選中狀態
登錄狀態界面:http://192.168.0.100/server-status
同時查看各個tomcat的日誌文件,看看上面提交的測試內容是在哪個tomcat中顯示出來
# tail -f /usr/local/tomcat1/logs/catalina.out
# tail -f /usr/local/tomcat2/logs/catalina.out
# tail -f /usr/local/tomcat3/logs/catalina.out
# tail -f /usr/local/tomcat4/logs/catalina.out
正常情況下,提交的測試內容會在四個tomcat之間輪循顯示,實現負載均衡。
4、設置Apache發佈服務開機自啓動
# chkconfig httpd on
四、Nginx負載均衡發佈器的安裝配置:
1、安裝前必須要先下載相關聯的配套軟件包:
(1) pcre套件包下載: ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ (一定要先安裝)
(2) nginx最新版下載: http://www.nginx.org/en/download.html (一定要安裝)
(3) openssl套件包下載:http://www.openssl.org/source/ (可選安裝)
2、相關聯軟件的安裝
(1)pcre套件的安裝,進入下載目錄,進行解壓縮:
# cd /tools
# tar zxvf pcre-8.12.tar.gz
# cd pcre-8.12
# ./configure (默認是安裝在/usr/local/目錄下面,也可指定目錄加參數 --prefix=/usr/local/)
# make && make install (進行make編譯並安裝)
(2)openssl套件的安裝,進入下載目錄,進行解壓縮:(可選)
# cd /tools
# tar zxvf openssl-1.0.0 d.tar.gz
# cd openssl-1.0.0 d
# ./configure (默認是安裝在/usr/local/目錄下面,也可指定目錄加參數 --prefix=/usr/local/)
# make && make install (進行make編譯並安裝)
3、Nginx軟件的安裝和配置
(1)nginx套件的安裝,進入下載目錄,進行解壓縮:
# cd /tools
# tar zxvf nginx-1.0.0 .tar.gz
# cd nginx-1.0.0
# ./configure (默認是安裝在/usr/local/目錄下面,也可指定目錄加參數 --prefix=/usr/local/)
# make && make install (進行make編譯並安裝)
(2)測試安裝是否成功:
# cd /usr/local/nginx/sbin/
# ./nginx -t (如果提示下面的ok和successful成功的話,說明安裝成功了)
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
(3)進行負載均衡站點的配置:
# cd /usr/local/nginx/conf/
# vi nginx.conf (修改增加下面的內容)
#pid logs/nginx.pid; ------- 在第9行左右,去掉前面的“#”註釋號
(新增)upstream tomcat { ------- 負載均衡站點的名稱爲tomcat,可以自己取
ip_hash; (可選,根據來源IP方式選擇web服務器,省略的話按默認的輪循方式選擇web服務器)
server 192.168.0.101:81; ------- web服務器的IP地址及tomcat1發佈端口
server 192.168.0.101:82; ------- web服務器的IP地址及tomcat2發佈端口
server 192.168.0.102:83; ------- web服務器的IP地址及tomcat3發佈端口
server 192.168.0.102:84; ------- web服務器的IP地址及tomcat4發佈端口
}
server {
listen 80; ------- 本機負載均衡的發佈端口80
server_name localhost; ------- 本機服務器名稱
location / {
root html;
index index.html index.htm;
(新增) proxy_pass http://tomcat; ------- 負載均衡指向的發佈服務tomcat
}
注意:“upstream”部分要放在“server { listen 80” 這部分的前面,不能放到下面去,不然會出錯
“proxy_pass”部分要放在localtion這部分的括號裏面,且http://後面跟的名稱要與上面upstream中一樣
(4)啓動負載均衡服務:
# cd /usr/local/nginx/sbin/
# ./nginx -t (測試配置有無錯誤)
# ./nginx (啓動服務)
# netstat -tlnp | grep :80 (查看nginx端口的進程號)
# kill -9 11274 (殺死nginx的主進程,11274是上面查詢出來的主進程號,實際環境中不一定一樣)
(5)在線修改配置參數(在nginx啓動的情況),並讓它重新加載,其間nginx不用停止服務。
# vi nginx.conf
# kill -HUP `cat /usr/local/nginx/logs/nginx.pid`(注意cat命令兩邊必須要有撇號,不是單引號)
(6)設置Nginx開機自動啓動:
# cd /etc/rc.d/init.d/
# vi nginx
----> 增加內容 #chkconfig:35 80 10
#description:Nginx server
/usr/local/nginx/sbin/nginx
# chmod a+x nginx (授予可執行的權限)
# chkconfig --add nginx
# chkconfig nginx on