爲什麼要負載均衡?
Tomcat服務器作爲一個Web服務器,其併發數在300-500之間,如果超過500的併發數會出現Tomcat不能響應新的請求的情況,嚴重影響網站的運行。同時如果訪問量非常大的情況下,Tomcat的線程數會不斷增加。因此會佔據大量內存,嚴重時出現內存溢出的現象,這時需要重啓Tomcat以釋放內存,阻斷了網站的運行。
所以對Tomcat做負載均衡便很有必要。目前可以和Tomcat做負載均衡的主流服務器是Apache,但是Nginx由於功能多、配置簡單等優點成爲很多負載均衡服務器的首選。Nginx的併發數可達到50000,所以理論上可以和Tomcat以1:100的比例來配置,這邊可以很好的解決網站併發瓶頸問題。
實驗環境
Nginx1.10.2 +Tomcat 8.0.35(3個)、Win 10
一、安裝Nginx
如果啓動成功後,可以通過瀏覽器:
輸入http://localhost 或者http://localhost:80 來驗證是否成功
二、安裝Tomcat
分別複製三個Tomcat修改三個啓動端口,使三個Tomcat能在一臺計算機上啓動。
2.1 第一步:修改 端口號
默認值8005,加上2 ,變成 8007
默認值是 8080 ;加上2;變成 8082
默認值是 8009;加上2;變成8011
2.2第二步:修改tomcat-users.xml
添加網頁上面,管理app的能力
添加如下內容(Password部分自己指定,或者就使用默認的即可,方便大家交流記憶。):
2.3第三步:添加jar包
啓動tomcat 8082實例
2.4第四步:修改context.xml
添加如下內容:
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<Manager className="com.radiadesign.catalina.session.RedisSessionManager"
host="192.168.81.137"
port="6379"
database="0"
password="admin"
maxInactiveInterval="60" />
如果redis已經設置了,要求輸入授權密碼的,需要配置password選項,否則會報下面的錯誤2,NOAUTH錯誤
2.5 第五步:驗證是否成功
重複上述步驟:
啓動tomcat 8083,8084
三、tomcat中添加測試項目來驗證tomcat session的共享
在tomcat 8082中添加web項目:
在webapps目錄下,就簡單建一個文件夾sharesesson,並且在下面新建兩個頁面write.jsp
read.jsp ;內容如下:
write.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>session</title>
</head>
<body>
<h1>tomcat8082</h1>
<%String s = session.getId(); //獲取session ID號 %>
<%=s %>
<%
session.setAttribute("cyp", "123456");
%>
</body>
</html>
read.jsp<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>session</title>
</head>
<body>
<h1>tomcat8082</h1>
<%String s = session.getId(); //獲取session ID號 %>
<%=s %>
<br/>
<br/>
<%=(String)session.getAttribute("cyp")%>
</body>
</html>
重複上面步驟:分別在tomcat8083,tomcat8084下面分別添加sharesessionweb項目;內容跟上面的write.jsp 和read.jsp 內容一樣。
只是注意<h1>tomcat8082</h1>這句話,修改爲相應的tomcat對應的內容:
Tomcat8083–> <h1>tomcat8083</h1>
Tomcat8084–> <h1>tomcat8084</h1>
3.1第一步:
訪問http://localhost:8082/sharesession/write.jsp
讓session生效
訪問http://localhost:8082/sharesession/read.jsp
讀取session內容:
3.3 第三步:
訪問tomcat8083的read.jsp
http://localhost:8083/sharesession/read.jsp
session的內容正常讀取出來,已經達到的在tomcat8082上面寫session,通過redis的共享內容,在tomcat8083 上面讀session;
3.4 第四步:
訪問tomcat804的read.jsp
http://localhost:8084/sharesession/read.jsp
session的內容正常讀取出來,已經達到的在tomcat8082上面寫session,通過redis的共享內容,在tomcat8084 上面讀session;
總結實現方式:
Tomcat8082 –> redis (寫)
Tomcat8083 <- redis(讀)
Tomcat8084 <- redis(讀)
以上所有步驟只能解決了tomcat的session同享的問題。
四、讓nginx 反向代理 tomcat的服務修改nginx的位置文件nginx.conf
具體的內容如下:
#user nobody;
worker_processes 1;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream localhost {
server localhost:8082 weight=1;
server localhost:8083 weight=2;
server localhost:8084 weight=3;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://localhost;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
五、驗證負載均衡是否成功:雙擊nginx根目錄下nginx.exe文件
或者使用start nginx啓動(關閉是: nginx -s stop)
打開瀏覽器,
輸入地址:
http://localhost/sharesession/write.jsp
http://localhos/sharesession/read.jsp
不停的刷新,會訪問到不同的tomcat
六、Nignx 常用命令
常用nginx 命令:
停止:nginx -s stop
nginx.exe -s quit:stop是快速停止nginx,可能並不保存相關信息;quit是完整有序的停止nginx,並保存相關信息。
修改配置使配置生效:nginx -s reload
檢查配 nginx -t