一、配置背景
最近使用WildFly 9.0.2作為中間件開發系統,給客戶部署的時候需要使用集群來進行負載均衡,一開始想到是使用Nginx。但是只通過Nginx使用 ip_hash 模式沒有做到session的不丟失(也可能是我對Nginx的理解不夠深入)。所以后來google了一下,發現很多Jboss服務器都使用 httpd + mod_cluster 來做集群的搭建,所以就准備使用這種配置來搭建一把。
二、環境准備
WildFly 9.0.2+mod_cluster-1.3.1+Java7+VMware 10:這些都可以去官方下載。
VMware 10 系統配置:本地Win7(192.168.244.1)+Server 2012(192.168.244.132)+Server 2012(192.168.244.133) ,這里的Win7 是本地不需要在虛擬機安裝,主要用作 mod_cluster 的集群管理服務器。兩台Server 2012需要在在虛擬機中搭建,132作為master,133座位slave。master可以統一管理和部署系統環境。
三、配置過程
1、域管理配置
我們需要配置master和slave主機系統的java環境變量,然后通過 wildfly-9.0.2s\bin\domain.bat 來啟動Jboss,如果可以正常啟動,並且出現以下如圖提示,說明基本的環境配置已經成功。
master進行域控制的配置:找到文件 domain/configuration/host.xml 進行修改。
默認的配置為
<interfaces> <interface name="management"> <inet-address value="${jboss.bind.address.management:127.0.0.1}"/> </interface> <interface name="public"> <inet-address value="${jboss.bind.address:127.0.0.1}"/> </interface> <interface name="unsecured"> <inet-address value="127.0.0.1" /> </interface> </interfaces
我們需要去修改管理端口,以至於slave可以連接到master,修改為如下:
<interfaces> <interface name="management" <inet-address value="${jboss.bind.address.management:192.168.244.132}"/> </interface> <interface name="public"> <inet-address value="${jboss.bind.address:192.168.244.132}"/> </interface> <interface name="unsecured"> <inet-address value="192.168.244.132" /> </interface> </interfaces>
slave的環境配置:為了讓slave可以連接到master,同樣找到文件 domain/configuration/host.xml
<host name="master" xmlns="urn:jboss:domain:3.0">
修改為
<host name="slave" xmlns="urn:jboss:domain:3.0">
同時修改 domain-controller 以至於slave可以連接到master的管理端口
<domain-controller> <remote protocol="remote" host="192.168.244.132" port="9999" username="slave" security-realm="ManagementRealm"/> </domain-controller>
上面的username="slave" 主要是為安全配置,需要特定的用戶來連接master。
接下來修改salve的 interfaces 配置為:
<interfaces> <interface name="management"> <inet-address value="${jboss.bind.address.management:192.168.244.133}"/> </interface> <interface name="public"> <inet-address value="${jboss.bind.address:192.168.244.133}"/> </interface> <interface name="unsecure"> <!-- Used for IIOP sockets in the standard configuration. To secure JacORB you need to setup SSL --> <inet-address value="${jboss.bind.address.unsecure:192.168.244.133}"/> </interface> </interfaces>
如果現在你啟動master以后,再去啟動slave,會發現如下報錯,因為我們還沒有配置slave和master之間的認證。
[Host Controller] 20:31:24,575 ERROR [org.jboss.remoting.remote] (Remoting "endpoint" read-1) JBREM000200: Remote connection failed: javax.security.sasl.SaslException: Authentication failed: all available authentication mechanisms failed [Host Controller] 20:31:24,579 WARN [org.jboss.as.host.controller] (Controller Boot Thread) JBAS010900: Could not connect to remote domain controller 192.168.244.132:9999 [Host Controller] 20:31:24,582 ERROR [org.jboss.as.host.controller] (Controller Boot Thread) JBAS010901: Could not connect to master. Aborting. Error was: java.lang.IllegalStateException: JBAS010942: Unable to connect due to authentication failure.
此時,我們需要在master中添加2個user,可以通過 wildfly-9.0.2s\bin\add-user.bat 來添加,具體過程在這里不再描述,過程中需要注意一點就是需要記住password的64位加密后的碼,人員類型選擇 “ManagementRealm”。我在這里添加2個用戶 master 和 slave,密碼都為123456。
在slave找到剛才編輯過的 host.xml,添加<secret value="MTIzNDU2" /> ,“MTIzNDU2”為123456的64位加密碼,配置如下:
<security-realms> <security-realm name="ManagementRealm"> <server-identities> <secret value="MTIzNDU2" /> </server-identities> <authentication> <local default-user="$local" skip-group-loading="true"/> <properties path="mgmt-users.properties" relative-to="jboss.domain.config.dir"/> </authentication> <authorization map-groups-to-roles="false"> <properties path="mgmt-groups.properties" relative-to="jboss.domain.config.dir"/> </authorization> </security-realm> <security-realm name="ApplicationRealm"> <server-identities> <secret value="MTIzNDU2" /> </server-identities> <authentication> <local default-user="$local" allowed-users="*" skip-group-loading="true"/> <properties path="application-users.properties" relative-to="jboss.domain.config.dir"/> </authentication> <authorization> <properties path="application-roles.properties" relative-to="jboss.domain.config.dir"/> </authorization> </security-realm> </security-realms>
進過如上配置我們就可以是的slave和master彼此連接,此時,再去啟動slave的服務,在命令行會看到:Registered remote slave host slave。說明我們在域控制模式下已經將2台主機配置成功。
2、部署測試環境
測試項目:http://pan.baidu.com/s/1eSkaeC6 我在這里提供一個可以下載的項目主要為了測試session是否在服務某個節點失敗以后,是否會正確轉移到另外一個節點。
在master上登陸管理控制台 http://192.168.244.132:9990/console/ ,將master和slave的server-three啟動起來。(如果在master中無法啟動slave的server,可以將salve上的wildfly重新啟動一下)
這里的server-three在master和slave上盡量使用不同的名字,官方文檔說相同的話在做集群的時候可能會出現沖突。接下來部署我們的war包,進入部署頁面,選擇Server Groups 頁面選擇 other-server-group,然后選擇add按鈕部署項目。
部署完畢以后我們的項目會被同事部署到master和master中,這就是wildfly域控制器的功能所在。此時我們訪問以下我們的項目如下,通過如下地址
http://192.168.244.132:8330/cluster-demo/
http://192.168.244.133:8330/cluster-demo/
應該應該可以看到如下界面:
這里我們的訪問端口為8330,為什么不是8080?這里因為我們在host.xml里面進行了如下配置:
<server name="server-three" group="other-server-group" auto-start="false"> <!-- server-three avoids port conflicts by incrementing the ports in the default socket-group declared in the server-group --> <socket-bindings port-offset="250"/> </server>
port-offset 是 250, 所以 8080 + 250 = 8330
接下來我們需要停止wildfly服務,我們需要在master和slave編輯host.xml將我們的serevr-three設置為自動啟動。同時將slave的server-three修改為server-three-slave,這是因為mod_cluster在注冊的時候如果名字相同,可能會注冊失敗。
master
<server name="server-three" group="other-server-group" auto-start="true"> <!-- server-three avoids port conflicts by incrementing the ports in the default socket-group declared in the server-group --> <socket-bindings port-offset="250"/> </server>
slave
<server name="server-three-slave" group="other-server-group" auto-start="true"> <!-- server-three avoids port conflicts by incrementing the ports in the default socket-group declared in the server-group --> <socket-bindings port-offset="250"/> </server>
完成如上配置以后,接下來我們就需要配置httpd 和 mod_cluster 來完成集群配置。
3、mod_cluster 集群的配置
解壓我們下載的mod_cluster安裝文件到某個目錄,然后執行 D:\httpd-2.4\bininstallconf.bat 命令(這里要注意,需要使用管理員權限啟動),然后會發現在目錄D:\httpd-2.4\conf 下面會看到httpd.conf 文件,接下來的主要配置就是修改這個文件。然后安裝httpd服務:httpd.exe -k install -n httpd2.4(安裝的service需要使用管理員啟動,默認是本地系統)
httpd.conf修改的主要內容如下:
Listen 192.168.244.1:80
......
#ServerName 的修改 ServerName 192.168.244.1:80
......
# MOD_CLUSTER_ADDS # Adjust to you hostname and subnet. <IfModule manager_module> Listen 192.168.244.1:8888 #ManagerBalancerName mycluster <VirtualHost 192.168.244.1:8888> # / 標示攔截所有類型的請求 Require ip 192.168.244 標示攔截特定的IP <Location /> Require ip 192.168.244 </Location> KeepAliveTimeout 60 MaxKeepAliveRequests 0 EnableMCPMReceive # don't use multicast ServerAdvertise Off #AdvertiseFrequency 5 #AdvertiseSecurityKey secret #AdvertiseGroup 224.0.1.105:23364 <Location /status> SetHandler mod_cluster-manager #Require ip 192.168.244 Order deny,allow Deny from all Allow from all AllowDisplay on </Location> #設置代理轉發,這里為將所有請求類型都轉發至mycluster負載均衡器 #ProxyPass / balancer://mycluster/ </VirtualHost> </IfModule>
然后啟動service
然后訪問http://192.168.244.1:8888/status,如果可以正確找到master和slave節點的server說明我的集群配置成功。
接下來進行集群的測試,此時會用到我們此前部署的項目cluster-demo,首先我們訪問:http://192.168.244.1/cluster-demo/put.jsp
此時我們會在master的后台看到如下信息
然后我們關閉master的服務,然后訪問:http://192.168.244.1/cluster-demo/get.jsp
我們發現我們put和get的時間相同,說明我們的session內容沒有丟失。到此,我們的集群環境部署完畢。
四、過程總結
整個過程斷斷續續花了2天,發生問題最多的地方是在httpd.conf配置的地方,因為對這里面的配置的介紹文檔比較少。另外如果實在虛擬機做測試最好將虛擬機里面的IP固定一下,有時候虛擬機重啟IP會發生變化,啟動服務的時候會報錯。
五、參考網址
https://docs.jboss.org/author/display/WFLY9/WildFly+9+Cluster+Howto
http://docs.jboss.org/mod_cluster/1.1.0/html/Quick_Start_Guide.html