剛入行沒多久就聽過‘負載均衡'的大名,到現在因為工作接觸的少,所以沒什么太多的認識。但自己又對其非常的好奇,所以前兩天通過查資料,在自己的筆記本上就搭建了一個超簡單的案例(工作中沒有時間,晚上到家了條件又不夠,只能用自己的筆記本將就一下了,重在理解思想。。)
通俗點將,負載均衡就是因為訪問流量太大,導致項目訪問不流暢、甚至宕掉,所以通過一種分流的方式來緩解這種情況。
首先,安裝兩個tomcat,可以是同一個復制成兩個,也可以下載兩個不同版本的tomcat,我就是下載了兩個不同版本的。下載地址:https://tomcat.apache.org/download-80.cgi
(這是8.0版本的,隨便找兩個不是特別老的版本的就行)。
然后啟動兩個tomcat,在啟動前,先更改其中一個的端口號,使得兩個tomcat啟動時不會端口沖突,一個是本身的8080端口,一個是改成了9080端口。配好以后,打開cmd命令窗口,我的tomcat一個放在D:/software/apache-tomcat-8.5.24目錄下,按照如下命令即可啟動,啟動成功會彈出另一個窗口,顯示如下:
打開瀏覽器,輸入http://localhost:9080/,出現如下界面即tomcat啟動成功。另一個采用同樣的步驟即可。
圖1:tomcat8 圖2:tomcat7
再之后,安裝一個nginx,我裝的是穩定版的nginx,下載地址:http://nginx.org/download/nginx-1.12.2.zip,解壓即可使用
在啟動前,必須要對nginx進行一下配置才可實現負載均衡的功能,打開conf文件夾,下面有一個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;
#以下四行是新添加的,兩個IP是兩個tomcat的訪問地址,weight表示分給該服務器的請求比重,兩個都是1,則按照1:1來分配,
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;
#一下兩行是進行修改的,http://netitcast.com和上面添加的要保持一致
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;# }#}}
還是打開cmd窗口,進入到以上目錄,執行命令:start nginx 即啟動成功,然后輸入網址:http://localhost/index.jsp,不斷的進行訪問,就會發現,上方顯示的圖1和圖2在交互的顯示。因為以上的配置weight是以1:2的比重來分配的,所以9080端口的比重就大一些,訪問到圖一(9080端口)的幾率就比較大,訪問到圖二(8080端口)的幾率比較小,這個概率一個是三分之二,一個是三分之一。
當然,這只是一個簡單的示例,在生產環境中開發,肯定還要解決許多問題,比如‘會話保持'等,這個以后明白了再來補充。寫這個的目的就是讓自己對負載均衡更加熟悉,希望大家以后一起努力,加油!!!
|
新聞熱點
疑難解答