一、為什么需要對Tomcat服務器做負載均衡:
Tomcat服務器作為一個Web服務器,其並發數在300-500之間,如果有超過500的並發數便會出現Tomcat不能響應新的請求的情況,嚴重影響網站的運行。另外,在訪問量大的情況下,Tomcat的線程數會不斷增加。由於Tomcat自身對內存的占用有控制,當對內存的占用達到最大值時便會出現內存溢出,對網站的訪問嚴重超時等現象,這時便需要重新啟動Tomcat以釋放占用的內存,這樣做便會阻斷網站運行。
所以對Tomcat做負載均衡便很有必要。目前可以和Tomcat做負載均衡的主流服務器是Apache,但是Nginx由於功能多、配置簡單等優點逐漸成為很多負載均衡服務器的首選。Nginx的並發數可達到50000,所以理論上可以和Tomcat以1:100的比例來配置,這樣便可以很好的解決網站並發瓶頸問題。而且Nginx、apache是基於http反向代理方式,位於ISO模型的第七層應用層。直白些就是TCP UDP 和http協議的區別,Nginx不能為基於TCP協議的應用提供負載均衡。
二. 配置方法
1.下載相應的服務器,本人兩個Tomcat的版本都采用6.0的.Nginx采用1.5.6版本
2. Nginx介紹
目錄結構
Nginx-
|_ conf 配置目錄
|_ contrib
|_ docs 文檔目錄
|_ logs 日志目錄
|_ temp 臨時文件目錄
|_ html 靜態頁面目錄
|_ nginx.exe 主程序
window下安裝Nginx極其簡單,解壓縮到一個無空格的英文目錄即可(個人習慣,擔心中文出問題),雙擊nginx啟動,這里我安裝到:D:\test目錄. 若果想停止nginx,dos環境運行命令:nginx -s stop.
nginx.conf配置
Nginx配置文件默認在conf目錄,主要配置文件為nginx.conf,我們安裝在D:\server\nginx-0.8.20、默認主配置文件為D:\server\nginx-0.8.20\nginx.conf。下面是nginx作為前端反向代理服務器的配置。
- Nginx.conf代碼
- #Nginx所用用戶和組,window下不指定
- #user nobody;
- #工作的子進程(通常等於CPU數量或者1倍於CPU)
- worker_processes 1;
- #錯誤日志存放路徑
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
- #指定pid存放文件
- #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;
- #客戶端上傳文件大小控制
- client_max_body_size 8m;
- #gzip on;
- upstream localhost {
- server localhost:8080;
- server localhost:8000;
- #根據ip計算將請求分配各那個后端tomcat,許多人誤認為可以解決session問題,其實並不能。
- #同一機器在多網情況下,路由切換,ip可能不同
- ip_hash;
- }
- server {
- listen 9999;
- server_name localhost;
- #charset koi8-r;
- #access_log logs/host.access.log main;
- location / {
- root html;
- index index.html index.htm;
- #此處的 http://localhost與upstream localhost對應
- proxy_pass http://localhost;
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- client_max_body_size 10m;
- client_body_buffer_size 128k;
- proxy_connect_timeout 100;
- proxy_send_timeout 100;
- proxy_read_timeout 100;
- proxy_buffer_size 4k;
- proxy_buffers 4 32k;
- proxy_busy_buffers_size 64k;
- proxy_temp_file_write_size 64k;
- }
- #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.conf
① 在#gzip on;后面加入下面配置:
- upstream localhost {
- server localhost:8080;
- server localhost:8088;
- ip_hash;
- }
其中serverlocalhost:8080為第一個Tomcat的啟動地址,server localhost:8088為第二個Tomcat的啟動地址,ip_hash用於做session同步其實是不起作用的。
② 修改第一個server{}配置中的listen 80;改為新的端口號,因為我的本機80端口被其他服務器占用,因此將此處改為listen 9999;
- location / {
- root html;
- index index.html index.htm;
- }
- 改為:
- location / {
- root html;
- index index.html index.htm;
- #此處的 http://localhost與upstream localhost對應
- proxy_pass http://localhost;
- proxy_redirect off;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- client_max_body_size 10m;
- client_body_buffer_size 128k;
- proxy_connect_timeout 100;
- proxy_send_timeout 100;
- proxy_read_timeout 100;
- proxy_buffer_size 4k;
- proxy_buffers 4 32k;
- proxy_busy_buffers_size 64k;
- proxy_temp_file_write_size 64k;
- }
3.tomcat的配置
修改其中一個tomcat的server.xml配置文件即可
第一處端口修改:
- <!-- 修改port端口:18006 倆個tomcat不能重復,端口隨意,別太小-->
- lt;Server port="8000" shutdown="SHUTDOWN">
第二處端口修改
- <!-- port="18081" tomcat監聽端口,隨意設置,別太小 -->
- <Connector port="18081" protocol="HTTP/1.1"
- connectionTimeout="20000"
- redirectPort="8443" />
第三處端口修改:
Java代碼
- "8009" protocol="AJP/1.3" redirectPort="8443" />
驗證配置與測試負載均衡
首先測試nginx配置是否正確,測試命令:nginx -t (默認驗證:conf\nginx.conf),也可以指定配置文件路徑
其次驗證兩個tomcat,啟動兩個tomcat不出現端口沖突即為成功
最后驗證配置負載均衡,啟動nginx雙機nginx.exe文件或者用start nginx啟動 打開瀏覽器輸入地址http://localhost:9999看到下面畫面表示成功:
此圖說明已經成功跳轉到tomcat 輸入測試路徑可以看到測試項目的首頁就表示基本成功。
至此window下nginx+tomcat負載均衡配置結束,關於tomcat Session的問題通常是采用memcached,或者采用nginx_upstream_jvm_route,他是一個Nginx的擴展模塊,用來實現基於 Cookie的 Session Sticky的功能。如果tomcat過多不建議session同步,server間相互同步session很耗資源,高並發環境容易引起Session風暴。請根據自己應用情況合理采納session解決方案。
=========================================================================================
在nginx目錄下,運行 nginx -t ,出現如下圖所示則配置正確。
然后啟動nginx
一些命令:
start nginx 啟動nginx
nginx -s stop 停止nginx
配置Tomcat,端口號分別為9001,9002 ,調試至正常運行。啟動tomcat
兩個Tomcat部署完畢后,輸入http://localhost:9000/,刷新多次后,注意上圖中標注的地方,THIS IS TOMCAT-1,THIS IS TOMCAT-2 說明訪問了不同的tomcat .
至此,windows下nginx tomcat 負載均衡配置結束。
=========================================================================================
一、為什么需要對Tomcat服務器做負載均衡?
Tomcat服務器作為一個Web服務器,其並發數在300-500之間,如果超過500的並發數會出現Tomcat不能響應新的請求的情況,嚴重影響網站的運行。同時如果訪問量非常大的情況下,Tomcat的線程數會不斷增加。因此會占據大量內存,嚴重時出現內存溢出的現象,這時需要重啟Tomcat以釋放內存,阻斷了網站的運行。
所以對Tomcat做負載均衡便很有必要。目前可以和Tomcat做負載均衡的主流服務器是Apache,但是Nginx由於功能多、配置簡單等優點成為很多負載均衡服務器的首選。Nginx的並發數可達到50000,所以理論上可以和Tomcat以1:100的比例來配置,這邊可以很好的解決網站並發瓶頸問題。
二、Nginx+Tomca在Windows下負載均衡的配置:
先秀一下我的環境: Nginx1.3.11 + Tomcat 6.0(2個)、Win 7。
下面是配置過程:
1、將nginx-1.3.11.rar直接解壓到D:盤。
2、將apache-tomcat-6.0.26.rar解壓到D:盤,既然是做負載均衡將tomcat拷貝兩份,分別命名tomcat_1、tomcat_2。
3、將要發布的項目部署分別部署到兩個Tomcat根目錄下的webapps下,保證兩個Tomcat下的項目名相同。
4、修改tomcat_2的配置文件,該配置文件位於/conf下,文件名為server.xml, 修改其中的<Server port="8005" shutdown="SHUTDOWN"> 為<Server port="8006" shutdown="SHUTDOWN">,修改其中的
1
2
3
|
<
Connector
port
=
"8080 protocol="
HTTP/1.1"
connectionTimeout
=
"20000"
redirectPort
=
"8443"
/>
|
為
1
2
3
|
<
Connector
port
=
"8088 protocol="
HTTP/1.1"
connectionTimeout
=
"20000"
redirectPort
=
"8443"
/>
|
1
|
<
Connector
port
=
"8009"
protocol
=
"AJP/1.3"
redirectPort
=
"8443"
/>
|
(ajp是一個協議,tomcat可以用來服務器集群,也可以在csdn找到更詳細的ajp資料)為
1
|
<
Connector
port
=
"7009"
protocol
=
"AJP/1.3"
redirectPort
=
"8443"
/>
|
5、修改Nginx配置文件nginx.conf,該文件位於Nginx根目錄下的/conf目錄下
a、在#gzip on;后面加入下面配置:
1
2
3
4
5
|
upstream localhost {
server localhost:8080 weight=5;
server localhost:8080 weight=5;
ip_hash;
}
|
其中server localhost:8080為tomcat_1的啟動地址, server localhsot:8088為tomcat_2的啟動地址,ip_hash用於做session同步。
b、修改
1
2
3
4
|
location / {
root html;
index index.html index.htm;
}
|
改為:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
location / {
root html;
index index.html index.htm;
proxy_pass http://localhost;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
|
其中proxy_pass參數和upstream localhost{}對應。
經過上面這些步驟,負載均衡配置完成,下面分別啟動tomcat_1、tomcat_2,然后雙擊nginx根目錄下nginx.exe文件或者使用start nginx啟動(關閉是: nginx -s stop),打開瀏覽器,輸入地址: http://localhost便可看到tomcat的首頁了。