1、配置背景html
最近使用WildFly 9.0.2做爲中間件開發系統,給客戶部署的時候須要使用集羣來進行負載均衡,一開始想到是使用Nginx。可是隻經過Nginx使用 ip_hash 模式沒有作到session的不丟失(也多是我對Nginx的理解不夠深刻)。因此後來google了一下,發現不少Jboss服務器都使用 httpd + mod_cluster 來作集羣的搭建,因此就準備使用這種配置來搭建一把。java
2、環境準備安全
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能夠統一管理和部署系統環境。session
3、配置過程app
一、域管理配置負載均衡
咱們須要配置master和slave主機系統的java環境變量,而後經過 wildfly-9.0.2s\bin\domain.bat 來啓動Jboss,若是能夠正常啓動,而且出現如下如圖提示,說明基本的環境配置已經成功。dom
master進行域控制的配置:找到文件 domain/configuration/host.xml 進行修改。socket
默認的配置爲jsp
<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臺主機配置成功。
二、部署測試環境
測試項目: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 來完成集羣配置。
三、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內容沒有丟失。到此,咱們的集羣環境部署完畢。
4、過程總結
整個過程斷斷續續花了2天,發生問題最多的地方是在httpd.conf配置的地方,由於對這裏面的配置的介紹文檔比較少。另外若是實在虛擬機作測試最好將虛擬機裏面的IP固定一下,有時候虛擬機重啓IP會發生變化,啓動服務的時候會報錯。
5、參考網址
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