1. 程式人生 > >Nginx+Tomcat搭建高性能負載均衡集群-Windows本地測試版

Nginx+Tomcat搭建高性能負載均衡集群-Windows本地測試版

other doc mime filename 連接超時 src 日誌 col process

一、安裝Tomcat和Nginx

首先安裝兩個apache-tomcat-8.0.41,下載地址:http://tomcat.apache.org

並安裝一個nginx-1.13.0,下載地址http://nginx.org/en/download.html

都是綠色版,直接解壓就能用,不需要進行環境變量之類的配置的。

這裏碰到個小問題:公司電腦環境變量配的是jdk6,所以我的Tomcat8啟動的時候黑窗口一閃而過,JDK版本不匹配的原因,把jdk6換成jdk8之後,Tomcat8正常啟動。

二、修改Tomcat的端口號

技術分享

我的第一個Tomcat是默認的,只修改第二個Tomcat,確保兩個Tomcat能同時啟動,需要修改下面三處的端口號(O(∩_∩)O我只是在默認多口號加了1~):

技術分享

技術分享

技術分享

三、修改Tomcat的默認頁面,用於識別訪問的是哪個Tomcat

技術分享

技術分享

另一個Tomcat這裏添加的是=====Tomcat1

四、修改Nginx的配置

技術分享

配置內容

  1 #user  nobody;
  2 worker_processes  1;#工作進程的個數,一般與計算機的cpu核數一致
  3 
  4 #error_log  logs/error.log;
  5 #error_log  logs/error.log  notice;
  6 #error_log  logs/error.log  info;
  7 
  8 #pid        logs/nginx.pid;
9 10 11 events { 12 worker_connections 1024;#單個進程最大連接數(最大連接數=連接數*進程數) 13 } 14 15 16 http { 17 include mime.types;#文件擴展名與文件類型映射表 18 default_type application/octet-stream;#默認文件類型 19 20 #log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘ 21 # ‘$status $body_bytes_sent "$http_referer" ‘
22 # ‘"$http_user_agent" "$http_x_forwarded_for"‘; 23 24 #access_log logs/access.log main; 25 26 sendfile on;#開啟高效文件傳輸模式,sendfile指令指定nginx是否調用sendfile函數來輸出文件, 27 #對於普通應用設為 on,如果用來進行下載等應用磁盤IO重負載應用,可設置為off, 28 #以平衡磁盤與網絡I/O處理速度,降低系統的負載。註意:如果圖片顯示不正常把這個改成off。 29 #tcp_nopush on; 30 31 #keepalive_timeout 0; 32 keepalive_timeout 65;#長連接超時時間,單位是秒 33 34 #gzip on;#啟用Gizp壓縮 35 36 #服務器的集群 37 upstream netitcast.com { #服務器集群名字 38 server 127.0.0.1:8080 weight=1;#服務器配置 weight是權重的意思,權重越大,分配的概率越大。 39 server 127.0.0.1:8081 weight=2; 40 } 41 42 #當前的Nginx的配置 43 server { 44 listen 80;#監聽80端口,可以改成其他端口 45 server_name localhost;#當前服務的域名 46 47 #charset koi8-r; 48 49 #access_log logs/host.access.log main; 50 51 location / { 52 root html; 53 index index.html index.htm; 54 proxy_pass http://netitcast.com; 55 proxy_redirect default; 56 } 57 58 #error_page 404 /404.html; 59 60 # redirect server error pages to the static page /50x.html 61 # 62 error_page 500 502 503 504 /50x.html; 63 location = /50x.html { 64 root html; 65 } 66 67 # proxy the PHP scripts to Apache listening on 127.0.0.1:80 68 # 69 #location ~ \.php$ { 70 # proxy_pass http://127.0.0.1; 71 #} 72 73 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 74 # 75 #location ~ \.php$ { 76 # root html; 77 # fastcgi_pass 127.0.0.1:9000; 78 # fastcgi_index index.php; 79 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 80 # include fastcgi_params; 81 #} 82 83 # deny access to .htaccess files, if Apache‘s document root 84 # concurs with nginx‘s one 85 # 86 #location ~ /\.ht { 87 # deny all; 88 #} 89 } 90 91 92 # another virtual host using mix of IP-, name-, and port-based configuration 93 # 94 #server { 95 # listen 8000; 96 # listen somename:8080; 97 # server_name somename alias another.alias; 98 99 # location / { 100 # root html; 101 # index index.html index.htm; 102 # } 103 #} 104 105 106 # HTTPS server 107 # 108 #server { 109 # listen 443 ssl; 110 # server_name localhost; 111 112 # ssl_certificate cert.pem; 113 # ssl_certificate_key cert.key; 114 115 # ssl_session_cache shared:SSL:1m; 116 # ssl_session_timeout 5m; 117 118 # ssl_ciphers HIGH:!aNULL:!MD5; 119 # ssl_prefer_server_ciphers on; 120 121 # location / { 122 # root html; 123 # index index.html index.htm; 124 # } 125 #} 126 127 }

其實也就修改了以下兩個地方:

技術分享

五、開始測試

通過startup.bat直接啟動兩個Tomcat。會出現兩個黑窗口,裏面有Tomcat的啟動日誌。

技術分享

接著通過Nginx目錄下的nginx.exe啟動Nginx。

技術分享

六、測試結果

如下圖所示,瀏覽器中訪問localhost,不斷刷新,訪問的Tomcat會在1和2中來回切換,切換的概率是由所配置的權重決定的。

技術分享

技術分享

Nginx+Tomcat搭建高性能負載均衡集群-Windows本地測試版