公司兩臺服務器都安裝了tomcat,配置apache做爲負載均衡,當一臺服務器出現故障時還能保證業務正常運行。apache
Server1:192.168.1.100vim
Server2:192.168.1.200瀏覽器
一、查詢是否已經安裝了apachetomcat
rpm -qa | grep httpd服務器
二、使用yum安裝apache,負載配置是同樣的。session
yum -y install httpd*負載均衡
三、啓動apache,設置爲開機自啓動。測試
systemctl start httpd3d
systemctl enable httpd代理
四、配置負載均衡
loadfactor參數:能夠看到請求是按5:5分配的,loadfactor 參數取值範圍爲1-100。
route參數:訪問時能夠分清楚是請求的哪一臺服務器
vim /etc/httpd/conf/httpd.conf
<VirtualHost *:80>
ServerName www.ht.com
ProxyRequests Off
#設置使用進入的HTTP請求頭來發送代理請求
ProxyPreserveHost On
ProxyPass / balancer://htcluster/
ProxyPassReverse / balancer://htcluster/
ErrorLog "/var/log/httpd/htlog/error.log"
CustomLog "/var/log/httpd/htlog/access.log" common
<Proxy balancer://htcluster>
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
BalancerMember http://192.168.1.100:9000/ route=assets1 loadfactor=5 connectiontimeout=5 timeout=5
BalancerMember http://192.168.1.200:9000/ route=assets2 loadfactor=5 connectiontimeout=5 timeout=5
ProxySet lbmethod=byrequests
ProxySet stickysession=ROUTEID
</Proxy>
</VirtualHost>
五、重啓apache,訪問測試
systemctl restart httpd
訪問是否達到負載均衡。
谷歌訪問:
獵豹訪問:
使用兩個瀏覽器訪問查看route兩個不同,負載均衡完成。