接上一篇繼續,Domain模式解決了統一管理多臺jboss的問題,今天我們來學習如何利用mod_cluster來實現負載均衡、容錯。
mod_cluster是jboss的一個開源集群模塊(基于apache 2.2.x),官網地址為http://mod-cluster.jboss.org/ ,下面是使用步驟:
一、從官網下載binary文件
http://mod-cluster.jboss.org/downloads/1-2-6-Final-bin 這上面有各種OS的版本,本文以windows為試驗環境,應該下載http://downloads.jboss.org/mod_cluster//1.2.6.Final/windows/mod_cluster-1.2.6.Final-windows-amd64.zip 這個版本
(注:如果是linux環境,應該下載http://downloads.jboss.org/mod_cluster//1.2.6.Final/linux-x86_64/mod_cluster-1.2.6.Final-linux2-x64.tar.gz)
二、解壓安裝
將下載下來的文件解壓到jboss master server機器上的某個目錄(假定master server的IP為172.21.129.181),比如C:\Apache2.2\ ,mod_cluster其實就是一個包含集群模塊的apache server,如果你的機器上之前有安裝過apache server,且自動安裝成了windows服務,強烈建議先卸載,否則啟動mod_cluster對應的apache時,有可能啟動的是之前的版本。
注:如果是linux環境安裝,強烈建議將壓縮包中的httpd目錄,解壓到/opt/jboss/目錄下,因為linux版本的很多腳本和配置里,都是寫死這個目錄,如果修改了目錄,apachectl、http.conf這些文件里的路徑全都要改,十分麻煩
2.1 先執行C:\Apache2.2\bin\installconf.bat 生成默認配置 (注:linux環境下安裝時,無需執行該步驟,直接進入2.2改http.conf文件即可)
2.2 修改C:\Apache2.2\conf\httpd.conf
a)
#Listen 12.34.56.78:80
Listen 80
40行,把原來的端口8000,改成80,這樣方便直接用 http://172.21.129.181/xxx 來訪問應用
b)
164行改成
ServerName 172.21.129.181:80
c)
最后的mod_cluster部分,參考下面修改
# MOD_CLUSTER_ADDS # Adjust to you hostname and subnet. <IfModule manager_module> Listen 172.21.129.181:6666 ManagerBalancerName mycluster <VirtualHost 172.21.129.181:6666> <Location /> Order deny,allow #Deny from all Allow from all </Location> KeepAliveTimeout 300 MaxKeepAliveRequests 0 #ServerAdvertise on http://127.0.0.1:6666 AdvertiseFrequency 5 #AdvertiseSecurityKey secret #AdvertiseGroup 224.0.1.105:23364 EnableMCPMReceive <Location /mod_cluster_manager> SetHandler mod_cluster-manager Order deny,allow #Deny from all Allow from all </Location> </VirtualHost> </IfModule>
主要是把127.0.0.1,改成master server對應的真實IP,另外把Deny from all注釋掉,把緊貼著的一行改成Allow from all,去掉這些訪問上的限制
下面是完整的http.conf參考內容:

1 ServerRoot "C:/Apache2.2" 2 Listen 80 3 LoadModule actions_module modules/mod_actions.so 4 LoadModule alias_module modules/mod_alias.so 5 LoadModule auth_basic_module modules/mod_auth_basic.so 6 LoadModule authz_host_module modules/mod_authz_host.so 7 LoadModule autoindex_module modules/mod_autoindex.so 8 LoadModule dir_module modules/mod_dir.so 9 LoadModule log_config_module modules/mod_log_config.so 10 LoadModule mime_module modules/mod_mime.so 11 12 ServerAdmin jimmy@localhost.localdomain 13 ServerName 172.21.129.181:80 14 DocumentRoot "C:/Apache2.2/htdocs" 15 16 <Directory /> 17 Options FollowSymLinks 18 AllowOverride None 19 Order deny,allow 20 Allow from all 21 </Directory> 22 23 <Directory "C:/Apache2.2/htdocs"> 24 Options Indexes FollowSymLinks 25 AllowOverride None 26 Order allow,deny 27 Allow from all 28 </Directory> 29 30 <IfModule dir_module> 31 DirectoryIndex index.html 32 </IfModule> 33 34 <FilesMatch "^\.ht"> 35 Order allow,deny 36 Deny from all 37 Satisfy All 38 </FilesMatch> 39 40 ErrorLog "logs/error_log" 41 LogLevel warn 42 <IfModule log_config_module> 43 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined 44 LogFormat "%h %l %u %t \"%r\" %>s %b" common 45 46 <IfModule logio_module> 47 LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio 48 </IfModule> 49 CustomLog "logs/access_log" common 50 </IfModule> 51 52 <IfModule alias_module> 53 ScriptAlias /cgi-bin/ "C:/Apache2.2/cgi-bin/" 54 </IfModule> 55 56 <Directory "C:/Apache2.2/cgi-bin"> 57 AllowOverride None 58 Options None 59 Order allow,deny 60 Allow from all 61 </Directory> 62 63 DefaultType text/plain 64 65 <IfModule mime_module> 66 TypesConfig conf/mime.types 67 AddType application/x-compress .Z 68 AddType application/x-gzip .gz .tgz 69 </IfModule> 70 71 LoadModule proxy_module modules/mod_proxy.so 72 LoadModule proxy_ajp_module modules/mod_proxy_ajp.so 73 LoadModule proxy_http_module modules/mod_proxy_http.so 74 LoadModule proxy_cluster_module modules/mod_proxy_cluster.so 75 LoadModule manager_module modules/mod_manager.so 76 LoadModule slotmem_module modules/mod_slotmem.so 77 LoadModule advertise_module modules/mod_advertise.so 78 79 # MOD_CLUSTER_ADDS 80 <IfModule manager_module> 81 Listen 172.21.129.181:6666 82 ManagerBalancerName mycluster 83 <VirtualHost 172.21.129.181:6666> 84 <Location /> 85 Order deny,allow 86 Allow from all 87 </Location> 88 KeepAliveTimeout 300 89 MaxKeepAliveRequests 0 90 AdvertiseFrequency 5 91 EnableMCPMReceive 92 <Location /mod_cluster_manager> 93 SetHandler mod_cluster-manager 94 Order deny,allow 95 Allow from all 96 </Location> 97 </VirtualHost> 98 </IfModule>
2.3 將httpd安裝成windows服務
C:\Apache2.2\bin\httpd -k install
再啟動apache
C:\Apache2.2\bin\httpd -k start
順利的話,瀏覽http://172.21.129.181:6666/mod_cluster_manager 應該能看到類似以下界面:(注:不要用google chrome瀏覽器,chrome打不開這個地址,不知道是啥原因,換成IE或Firefox就可以了)
這是很關鍵的一個界面,用來管理/監控所有節點,如果沒成功,請仔細檢查前面的步驟是否有誤。
三、以domain模式啟動所有jboss master/slave server
參考上一篇的做法,啟動jboss master server / slave server,完成后,刷新 http://172.21.129.181:6666/mod_cluster_manager,會看到多出了好多節點
如果沒有新的節點顯示在該界面,檢查jboss master server中創建的group,對應的profile是否為full-ha或ha
點擊相關的鏈接,還能看到更詳細的信息
下面是點擊 show INFO output后的內容:
Node: [1],Name: d2c49277-af22-32b1-95be-b70e3ff20237,Balancer: mycluster,LBGroup: ,Host: 172.21.129.128,Port: 8010,Type: ajp,Flushpackets: Off,Flushwait: 10,Ping: 10,Smax: 65,Ttl: 60,Elected: 0,Read: 0,Transfered: 0,Connected: 0,Load: 100
Node: [2],Name: 708b3ee3-fd4d-39eb-b24c-3384bd11958c,Balancer: mycluster,LBGroup: ,Host: 172.21.129.128,Port: 8009,Type: ajp,Flushpackets: Off,Flushwait: 10,Ping: 10,Smax: 65,Ttl: 60,Elected: 0,Read: 0,Transfered: 0,Connected: 0,Load: 100
Node: [3],Name: c4051fb6-9cc9-30aa-967b-6ba1294026b8,Balancer: mycluster,LBGroup: ,Host: 172.21.129.181,Port: 8011,Type: ajp,Flushpackets: Off,Flushwait: 10,Ping: 10,Smax: 65,Ttl: 60,Elected: 0,Read: 0,Transfered: 0,Connected: 0,Load: 100
Node: [4],Name: 17181717-8b5c-3289-b6ea-fedcee2a27cb,Balancer: mycluster,LBGroup: ,Host: 172.21.129.181,Port: 8012,Type: ajp,Flushpackets: Off,Flushwait: 10,Ping: 10,Smax: 65,Ttl: 60,Elected: 3,Read: 3355,Transfered: 0,Connected: 0,Load: 100
Node: [5],Name: 2465cf8d-9cb0-3248-b335-09c02f13b6fe,Balancer: mycluster,LBGroup: ,Host: 172.21.129.181,Port: 8009,Type: ajp,Flushpackets: Off,Flushwait: 10,Ping: 10,Smax: 65,Ttl: 60,Elected: 0,Read: 0,Transfered: 0,Connected: 0,Load: -1
Node: [6],Name: 68a1e90f-6645-38e0-8157-219ab2004148,Balancer: mycluster,LBGroup: ,Host: 172.21.129.181,Port: 8010,Type: ajp,Flushpackets: Off,Flushwait: 10,Ping: 10,Smax: 65,Ttl: 60,Elected: 0,Read: 0,Transfered: 0,Connected: 0,Load: -1
Node: [7],Name: b403bcec-5eb5-37a2-a956-51d273b87548,Balancer: mycluster,LBGroup: ,Host: 172.21.129.57,Port: 8011,Type: ajp,Flushpackets: Off,Flushwait: 10,Ping: 10,Smax: 65,Ttl: 60,Elected: 0,Read: 0,Transfered: 0,Connected: 0,Load: 100
Node: [8],Name: 6e5ac603-5f19-3c90-8f97-d0bf01124c94,Balancer: mycluster,LBGroup: ,Host: 172.21.129.57,Port: 8012,Type: ajp,Flushpackets: Off,Flushwait: 10,Ping: 10,Smax: 65,Ttl: 60,Elected: 0,Read: 0,Transfered: 0,Connected: 0,Load: 100
Vhost: [2:1:1], Alias: example.com
Vhost: [2:1:2], Alias: localhost
Vhost: [2:1:3], Alias: default-host
Vhost: [4:1:4], Alias: default-host
Vhost: [4:1:5], Alias: localhost
Vhost: [4:1:6], Alias: example.com
Vhost: [1:1:7], Alias: example.com
Vhost: [1:1:8], Alias: localhost
Vhost: [1:1:9], Alias: default-host
Vhost: [3:1:10], Alias: default-host
Vhost: [3:1:11], Alias: localhost
Vhost: [3:1:12], Alias: example.com
Vhost: [6:1:13], Alias: localhost
Vhost: [6:1:14], Alias: default-host
Vhost: [7:1:15], Alias: example.com
Vhost: [8:1:16], Alias: example.com
Vhost: [8:1:17], Alias: localhost
Vhost: [8:1:18], Alias: default-host
Vhost: [7:1:19], Alias: localhost
Vhost: [7:1:20], Alias: default-host
Context: [2:1:1], Context: /SSO, Status: ENABLED
Context: [4:1:2], Context: /SSO, Status: ENABLED
Context: [1:1:3], Context: /SSO, Status: ENABLED
Context: [3:1:4], Context: /SSO, Status: DISABLED
Context: [7:1:5], Context: /SSO, Status: ENABLED
Context: [8:1:6], Context: /SSO, Status: ENABLED
注意一下:雖然我們有8臺jboss server,但是mod_cluster貌似最多只能支持6個node的負載均衡,有2個node的轉發因子是-1,即未生效(但大多數情況下,已經夠用了)。mod_cluster還有其它一些限制,詳情可參考 http://docs.jboss.org/mod_cluster/1.2.0/html_single/#d0e85
四、負載均衡、容錯驗證
在上一篇里,已經在my-group-1、my-group-2上部署了一個名為SSO的應用,現在可以直接用 http://172.21.129.181/SSO/ 來訪問了,apache會智能轉發到6個節點中的某一個,如果訪問正常,說明負載均衡已經生效。
繼續把其它server停掉幾臺,再繼續訪問http://172.21.129.181/SSO/ ,如果仍然正常,說明某幾個node損壞,并不影響正常使用,即容錯成功。
所有server全shutdown后,http://172.21.129.181/SSO/ 將無法訪問。
最后附一張:架構圖
參考文章:
文章列表