剛?cè)胄袥](méi)多久就聽(tīng)過(guò)‘負(fù)載均衡'的大名,到現(xiàn)在因?yàn)楣ぷ鹘佑|的少,所以沒(méi)什么太多的認(rèn)識(shí)。但自己又對(duì)其非常的好奇,所以前兩天通過(guò)查資料,在自己的筆記本上就搭建了一個(gè)超簡(jiǎn)單的案例(工作中沒(méi)有時(shí)間,晚上到家了條件又不夠,只能用自己的筆記本將就一下了,重在理解思想。。)
通俗點(diǎn)將,負(fù)載均衡就是因?yàn)樵L問(wèn)流量太大,導(dǎo)致項(xiàng)目訪問(wèn)不流暢、甚至宕掉,所以通過(guò)一種分流的方式來(lái)緩解這種情況。
首先,安裝兩個(gè)tomcat,可以是同一個(gè)復(fù)制成兩個(gè),也可以下載兩個(gè)不同版本的tomcat,我就是下載了兩個(gè)不同版本的。下載地址:https://tomcat.apache.org/download-80.cgi
(這是8.0版本的,隨便找兩個(gè)不是特別老的版本的就行)。
然后啟動(dòng)兩個(gè)tomcat,在啟動(dòng)前,先更改其中一個(gè)的端口號(hào),使得兩個(gè)tomcat啟動(dòng)時(shí)不會(huì)端口沖突,一個(gè)是本身的8080端口,一個(gè)是改成了9080端口。配好以后,打開(kāi)cmd命令窗口,我的tomcat一個(gè)放在D:\software\apache-tomcat-8.5.24目錄下,按照如下命令即可啟動(dòng),啟動(dòng)成功會(huì)彈出另一個(gè)窗口,顯示如下:
打開(kāi)瀏覽器,輸入http://localhost:9080/,出現(xiàn)如下界面即tomcat啟動(dòng)成功。另一個(gè)采用同樣的步驟即可。
圖1:tomcat8 圖2:tomcat7
再之后,安裝一個(gè)nginx,我裝的是穩(wěn)定版的nginx,下載地址:http://nginx.org/download/nginx-1.12.2.zip,解壓即可使用
在啟動(dòng)前,必須要對(duì)nginx進(jìn)行一下配置才可實(shí)現(xiàn)負(fù)載均衡的功能,打開(kāi)conf文件夾,下面有一個(gè)nginx.conf文件,配置如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#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; |
#以下四行是新添加的,兩個(gè)IP是兩個(gè)tomcat的訪問(wèn)地址,weight表示分給該服務(wù)器的請(qǐng)求比重,兩個(gè)都是1,則按照1:1來(lái)分配,
1
2
3
4
5
6
7
8
9
|
upstream netitcast.com{ server 127.0.0.1:8080 weight=1; server 127.0.0.1:9080 weight=2; } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; |
#一下兩行是進(jìn)行修改的,http://netitcast.com和上面添加的要保持一致
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
location / { proxy_pass http://netitcast.com; proxy_redirect default; } #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; # } #} } |
還是打開(kāi)cmd窗口,進(jìn)入到以上目錄,執(zhí)行命令:start nginx 即啟動(dòng)成功,然后輸入網(wǎng)址:http://localhost/index.jsp,不斷的進(jìn)行訪問(wèn),就會(huì)發(fā)現(xiàn),上方顯示的圖1和圖2在交互的顯示。因?yàn)橐陨系呐渲脀eight是以1:2的比重來(lái)分配的,所以9080端口的比重就大一些,訪問(wèn)到圖一(9080端口)的幾率就比較大,訪問(wèn)到圖二(8080端口)的幾率比較小,這個(gè)概率一個(gè)是三分之二,一個(gè)是三分之一。
當(dāng)然,這只是一個(gè)簡(jiǎn)單的示例,在生產(chǎn)環(huán)境中開(kāi)發(fā),肯定還要解決許多問(wèn)題,比如‘會(huì)話保持'等,這個(gè)以后明白了再來(lái)補(bǔ)充。寫(xiě)這個(gè)的目的就是讓自己對(duì)負(fù)載均衡更加熟悉,希望大家以后一起努力,加油!!!
原文鏈接:http://www.cnblogs.com/lishiwei/archive/2017/12/21/8076437.html