1 Nginx + 12 Tomcat +2DB 實現2個程序負載均衡

根據真實生產環境 總結。
硬件:共計5臺服務器  1臺負載windows、2臺業務windows、2臺數據庫linux
業務:運行兩個程序,兩個數據庫
具體:63服務器安裝 Nginx 做負載 ;61和62服務器各安裝6個Tomcat  跑業務1和業務2 ;59和60各跑一個數據庫;

備註:Nginx最好安裝在Linux服務器上,因爲軟件開發公司原因,這裏選擇安裝到windows服務器上


 63服務器上nginx開兩個端口 8001、8011 ,每個端口都是一個集羣接口
8001對應6個tomcat(分佈在61、62兩臺機器上)
8011對應6個tomcat(分佈在61、62兩臺機器上)
紅色跑的服務1、藍色跑的服務2;兩數據庫互相獨立,沒有負載設置。
服務1和服務2  各由6個tomcat支撐。
 1 Nginx + 12 Tomcat +2DB  實現2個程序負載均衡 - 朝鮮程序員 - 朝鮮程序員的博客
 
OK,現在我們可以開始配置Nginx來實現負載均衡了,其實非常的簡單,只需要配置好Nginx的配置文件即可


修改nginx.conf

#nginx命令
#nginx -t text          configuration file
#nginx -s stop         quick exit
#nginx -s quit         graceful quit
#nginx -s reload changing configuration, starting a new worker, quitting an old worker gracefully
#nginx -s reopen reopening log files

#運行用戶
#user  nobody;

#工作進程,根據硬件調整,大於等於cpu核數
worker_processes  16;
#映射目錄
include       D:/nginx/conflist/*.conf;      集羣具體配置放在這裏

#等待事件
events {
    #每個進程最大連接數(最大連接=連接數x進程數)
    worker_connections  1024;
}





在 D:/nginx/conflist/文件下 有兩個文件    nginx_group1.conf和nginx_group2.conf
分別是兩個集羣的配置信息,由於兩個文件配置信息大部分相同,下面只講解nginx_group1.conf


#錯誤日誌保存位置
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#進程號保存文件
#pid        logs/nginx.pid;

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  60;

    #打開gzip壓縮
    #gzip  on;
    
    #設定負載均衡的服務器列表
    #1-輪詢(默認)
    #     每個請求按時間順序逐一分配到不同的後端服務器,如果後端服務器down掉,能自動剔除。
    #2-weight
    #     指定輪詢機率,weight和訪問比率成正比,用於後端服務器性能不均的情況。
    #     參數weigth參數表示權值,權值越高被分配到的機率越大
    #3-ip_hash
    #     每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端服務器,可以
    #     解決session的問題。
    #4-fair
    #     這種模式會根據後端服務的響應時間來分配,響應時間短的後端優先分配
    upstream mysvr {
            #ip_hash; 
            server 10.10.12.61:8101;
            server 10.10.12.61:8102;
            server 10.10.12.61:8103;   
            server 10.10.12.62:8101;
            server 10.10.12.62:8102;
            server 10.10.12.62:8103;    
    }

    #第一個虛擬主機
    server {
        #監聽IP端口
        listen       8001;
        #主機名
        server_name  localhost;

        #設置字符集
        #charset koi8-r;

        #本虛擬server的訪問日誌 相當於局部變量
        #access_log  logs/host.access.log  main;

        location / {
            root   html;                      # 指向nginx 安裝目錄下的html文件夾,看具體配置  
            index  index.html index.htm;
            #proxy_pass  http://mysvr ;       #請求轉向mysvr 定義的服務器列表
        }

        #中間層轉向mysvr進行負載均衡
        location /JWebService {
            #root   html;
            #index  index.html index.htm;
            proxy_pass  http://mysvr ;       #請求轉向mysvr 定義的服務器列表
            
            #後端的Web服務器可以通過X-Forwarded-For獲取用戶真實IP
            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;                      #nginx跟後端服務器連接超時時間(代理連接超時)
            #proxy_send_timeout 90;                         #後端服務器數據回傳時間(代理髮送超時)
            #proxy_read_timeout 90;                         #連接成功後,後端服務器響應時間(代理接收超時)
            #proxy_buffer_size 4k;                          #設置代理服務器(nginx)保存用戶頭信息的緩衝區大小
            #proxy_buffers 4 32k;                           #proxy_buffers緩衝區,網頁平均在32k以下的話,這樣設置
            #proxy_busy_buffers_size 64k;                   #高負荷下緩衝大小(proxy_buffers*2)
            #proxy_temp_file_write_size 64k;                #設定緩存文件夾大小,大於這個值,將從upstream服務器傳
        }

        #文件訪問直接指向磁盤文件
        location /FileService {
           ##root alias 
           #location ~ ^/awstats/ {
           #root  /home/awstats/;
           #訪問:http://test.com/awstats/ 實際訪問的是/home/awstats/awstats/

           #location ~ ^/awstats/ {                        #使用alias時目錄名後面一定要加“/”
           #alias  /home/awstats/;
           #訪問:http://test.com/awstats/ 實際訪問的是/home/awstats/

           root D:\efuture;    ##會指向D:\efuture\FileService 
           autoindex on;       ##會自動顯示資源目錄  
           index noindex.htm;   
        }

        #訪問/tomcat1 的將重新定向到http://127.0.0.1:8381/
        location /tomcat1/ { 
           proxy_pass       http://127.0.0.1:8381/;
           index  index.html index.htm;
        } 

        #訪問/tomcat2 的將重新定向到http://127.0.0.1:8382/
        location /tomcat2/ { 
           proxy_pass       http://127.0.0.1:8382/;
           index  index.html index.htm;
        } 

        #設定查看Nginx狀態的地址
        location /NginxStatus {
            stub_status on;
            access_log on;
            auth_basic "NginxStatus";
            auth_basic_user_file conf/htpasswd;
        }

        #目錄重定向,訪問/phpadmin/ 的將重新定向到http://_/phpadmin
        #location /phpadmin/ { 
        #   alias   /opt/www/phpadmin/; 
        #   index   index.php; 
        #} 

        #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_group2.conf    設置,主要配置一下內容
 upstream mysvr {
            #ip_hash; 
            server 10.10.12.61:8201;
            server 10.10.12.61:8202;
            server 10.10.12.61:8203;   
            server 10.10.12.62:8201;
            server 10.10.12.62:8202;
            server 10.10.12.62:8203;    
    }

    #第一個虛擬主機
    server {
        #監聽IP端口
        listen       8011;
        #主機名
        server_name  localhost;














首先在61和62上安裝  Tomcat  
分別解壓出6個Tomcat,分別命名爲apache-tomcat-1和apache-tomcat-2  apache-tomcat-3……

然後修改這6個Tomcat的啓動端口,分別設爲8101、8102、8103、8201、8202、8203  
打開Tomcat的conf目錄下的server.xml,修改以下配置:
<Connector port="8101" protocol="HTTP/1.1"              
               connectionTimeout="20000"
               redirectPort="8443" />
<Connector port="8102" protocol="HTTP/1.1"              
               connectionTimeout="20000"
               redirectPort="8443" />
<Connector port="8103" protocol="HTTP/1.1"              
               connectionTimeout="20000"
               redirectPort="8443" />
……

分別啓動tomcat 看是否正常

我們可以修改每個tomcat的主頁,添加內容,在後面測試時便於區分
發佈了184 篇原創文章 · 獲贊 61 · 訪問量 36萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章