簡(jiǎn)介
wildfly是一個(gè)非常強(qiáng)大的工具聂抢,我們可以輕松的使用wildfly部署應(yīng)用程序钧嘶,更為強(qiáng)大的是,wildfly可以很方便的部署cluster應(yīng)用涛浙。
今天我們通過一個(gè)例子來講解下wildfly如何構(gòu)建cluster應(yīng)用康辑。
下載軟件和相關(guān)組件
假如我們有兩個(gè)host,一個(gè)稱為master轿亮,一個(gè)稱為slave,我們需要在兩個(gè)機(jī)子上面安裝wildfly胸墙,構(gòu)建成domain模式我注。然后需要在Domain controller主機(jī)上面安裝mod_cluster和httpd以組成集群。
首先我們需要下載wildfly-21.0.0.Final.zip迟隅,解壓之后但骨,運(yùn)行domain.sh以開啟domain模式。
配置domain
我們需要將master配置為domain controller智袭,根據(jù)我們之前的文章奔缠,首先配置interfaces,我們需要修改domain/configuration/host.xml:
<interfaces>
<interface name="management"
<inet-address value="${jboss.bind.address.management:10.211.55.7}"/>
</interface>
<interface name="public">
<inet-address value="${jboss.bind.address:10.211.55.7}"/>
</interface>
<interface name="unsecured">
<inet-address value="10.211.55.7" />
</interface>
</interfaces>
因?yàn)槲覀僲aster的ip地址是10.211.55.7吼野,所以需要修改相應(yīng)的值校哎。這里使用的是master對(duì)外的ip地址,從而可以供slave連接到master瞳步。
同樣的闷哆,我們需要修改slave的interface值:
<interfaces>
<interface name="management">
<inet-address value="${jboss.bind.address.management:10.211.55.2}"/>
</interface>
<interface name="public">
<inet-address value="${jboss.bind.address:10.211.55.2}"/>
</interface>
<interface name="unsecured">
<inet-address value="10.211.55.2" />
</interface>
</interfaces>
也需要修改相應(yīng)的ip地址。
接下來是修改host name :
//master
<host name="master" xmlns="urn:jboss:domain:3.0">
//slave
<host name="slave" xmlns="urn:jboss:domain:3.0">
在slave中单起,我們還需要配置domain-controller抱怔,從而讓slave可以連接到master:
<domain-controller>
<remote security-realm="ManagementRealm" >
<discovery-options>
<static-discovery name="master-native" protocol="remote" host="10.211.55.7" port="9999" />
<static-discovery name="master-https" protocol="https-remoting" host="10.211.55.7" port="9993" security-realm="ManagementRealm"/>
<static-discovery name="master-http" protocol="http-remoting" host="10.211.55.7" port="9990" />
</discovery-options>
</remote>
</domain-controller>
接下來,我們需要?jiǎng)?chuàng)建用于連接的security-realm嘀倒,通過add-user.sh命令屈留,我們可以創(chuàng)建添加用戶。
這里我們創(chuàng)建兩個(gè)用戶测蘑,第一個(gè)用戶叫做admin灌危,使用來進(jìn)行domain管理的用戶。
第二個(gè)用戶叫做slave帮寻,這個(gè)用戶用來slave連接到master乍狐。
還記得add-user.sh命令是怎么用的嗎?下面是創(chuàng)建admin用戶的輸出:
./add-user.sh
Enter the details of the new user to add.
Realm (ManagementRealm) :
Username : admin
Password recommendations are listed below. To modify these restrictions edit the add-user.properties configuration file.
- The password should not be one of the following restricted values {root, admin, administrator}
- The password should contain at least 8 characters, 1 alphabetic character(s), 1 digit(s), 1 non-alphanumeric symbol(s)
- The password should be different from the username
Password : passw0rd!
Re-enter Password : passw0rd!
The username 'admin' is easy to guess
Are you sure you want to add user 'admin' yes/no? yes
About to add user 'admin' for realm 'ManagementRealm'
Is this correct yes/no? yes
如果是slave用戶固逗,則需要在下面的問題提示的時(shí)候浅蚪,回答yes
Is this new user going to be used for one AS process to connect to another AS process?
e.g. for a slave host controller connecting to the master or for a Remoting connection for server to server EJB calls.
yes/no? yes
To represent the user add the following to the server-identities definition <secret value="cGFzc3cwcmQh" />
有了slave用戶藕帜,我們就可以使用這個(gè)用戶來配置slave的ManagementRealm了:
<security-realms>
<security-realm name="ManagementRealm">
<server-identities>
<secret value="cGFzc3cwcmQh" />
<!-- This is required for SSL remoting -->
<ssl>
<keystore path="server.keystore" relative-to="jboss.domain.config.dir" keystore-password="jbossas" alias="jboss" key-password="jbossas"/>
</ssl>
</server-identities>
<authentication>
<properties path="mgmt-users.properties" relative-to="jboss.domain.config.dir"/>
</authentication>
</security-realm>
</security-realms>
這樣配置過后,slave和master就可以進(jìn)行連接了惜傲。
創(chuàng)建應(yīng)用程序
這里我引用的是官網(wǎng)的demo程序洽故。實(shí)際上就是一個(gè)非常簡(jiǎn)單的web應(yīng)用。代碼地址 https://github.com/liweinan/cluster-demo/ 盗誊。
我們簡(jiǎn)單進(jìn)行一下講解时甚,基本的代碼邏輯就是在session中存放一個(gè)時(shí)間數(shù)據(jù),然后嘗試從不同的server中取出哈踱,看是否一致荒适,如果一致的話說明cluster集群是有效的。
//設(shè)置session的值
session.setAttribute("current.time", new java.util.Date());
//獲取session的值
session.getAttribute("current.time")
cluster中最重要的就是session共享开镣,或者說session復(fù)制刀诬。我們可以簡(jiǎn)單的在web.xml中使用distributable標(biāo)簽即可。
<web-app>
<display-name>Archetype Created Web Application</display-name>
<distributable/>
</web-app>
這就是我們應(yīng)用程序的全部了邪财。
部署應(yīng)用程序
這次我們從web console中進(jìn)行應(yīng)用程序的部署陕壹。
打開 http://10.211.55.7:9990 ,輸入我們創(chuàng)建的admin用戶名和密碼树埠,即可進(jìn)入管理界面糠馆。
默認(rèn)情況下,會(huì)創(chuàng)建3個(gè)服務(wù)怎憋,分別是server-one又碌,server-two和server-three。
server-one盛霎,server-two是默認(rèn)啟動(dòng)的赠橙,他們屬于main-server-group。而server-three是不啟動(dòng)的愤炸,它屬于other-server-group期揪。
我們看下other-server-group的配置:
<server-group name="other-server-group" profile="full-ha">
<jvm name="default">
<heap size="64m" max-size="512m"/>
</jvm>
<socket-binding-group ref="full-ha-sockets"/>
</server-group>
other-server-group使用的profile是full-ha,看名字就知道整個(gè)profile是為高可用設(shè)計(jì)的规个。那么這個(gè)profile有什么特別之處呢凤薛?
<profile name="full-ha">
...
<subsystem xmlns="urn:jboss:domain:modcluster:5.0">
<proxy name="default" advertise-socket="modcluster" listener="ajp">
<dynamic-load-provider>
<load-metric type="cpu"/>
</dynamic-load-provider>
</proxy>
</subsystem>
<subsystem xmlns="urn:jboss:domain:infinispan:11.0">
...
</subsystem>
<subsystem xmlns="urn:jboss:domain:jgroups:8.0">
<channels default="ee">
<channel name="ee" stack="udp" cluster="ejb"/>
</channels>
<stacks>
<stack name="udp">
...
</stack>
<stack name="tcp">
...
</stack>
</stacks>
</subsystem>
...
</profile>
這個(gè)profile中和ha有關(guān)的就是infinispan,jgroup和modcluster诞仓。通過這些組件缤苫,wildfly就可以來進(jìn)行cluster的組建。
因?yàn)閟erver-three默認(rèn)是停止?fàn)顟B(tài)的墅拭,我們需要在master和slave中分別啟動(dòng)他們活玲。
在Manage Deployments頁面,點(diǎn)擊Add Content,然后選擇我們之前的demo應(yīng)用程序cluster-demo.war舒憾,上傳即可镀钓。
好了,程序已經(jīng)部署好了镀迂,我們可以分別訪問:
http://10.211.55.7:8330/cluster-demo/ 和 http://10.211.55.2:8330/cluster-demo/ 來查看應(yīng)用程序的頁面丁溅。
現(xiàn)在為止,兩個(gè)應(yīng)用程序還是獨(dú)立的探遵,并沒有組合成cluster窟赏,接下來我們將會(huì)進(jìn)行cluster的配置。
還有一點(diǎn)要注意的是箱季,我們需要將master和slave中的server-three修改成不同的名字涯穷,如果是相同的名字,那么我們?cè)诤竺媸褂玫膍od_cluster將會(huì)報(bào)錯(cuò)藏雏,因?yàn)樵谕粋€(gè)server group中不允許出現(xiàn)兩個(gè)相同的名字求豫。
<server name="server-three" group="other-server-group" auto-start="true">
<socket-bindings port-offset="250"/>
</server>
<server name="server-three-slave" group="other-server-group" auto-start="true">
<socket-bindings port-offset="250"/>
</server>
集群配置
軟件部署好之后,我們需要在master機(jī)子上面使用mod_cluster + apache httpd 來啟用集群功能诉稍。
首先安裝httpd:
sudo yum install httpd
然后下載mod_cluster:
http://www.jboss.org/mod_cluster/downloads
將其解壓縮到 /etc/httpd/modules ,然后修改 /etc/httpd/conf/httpd.conf
添加下面的modules:
LoadModule slotmem_module modules/mod_slotmem.so
LoadModule manager_module modules/mod_manager.so
LoadModule proxy_cluster_module modules/mod_proxy_cluster.so
LoadModule advertise_module modules/mod_advertise.so
并且注釋掉下面的modules:
#LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
因?yàn)閜roxy_balancer_module是和proxy_cluster_module相沖突的最疆。
然后修改httpd監(jiān)聽 10.211.55.7:80杯巨。
最后還要在httpd.conf中配上mod_cluster-manager的監(jiān)聽端口:
<VirtualHost 10.211.55.7:10001>
<Directory />
Order deny,allow
Deny from all
Allow from 10.211.55.
</Directory>
# This directive allows you to view mod_cluster status at URL http://10.211.55.4:10001/mod_cluster-manager
<Location /mod_cluster-manager>
SetHandler mod_cluster-manager
Order deny,allow
Deny from all
Allow from 10.211.55.
</Location>
KeepAliveTimeout 60
MaxKeepAliveRequests 0
ManagerBalancerName other-server-group
AdvertiseFrequency 5
</VirtualHost>
我們可以使用service httpd start啟動(dòng)httpd服務(wù)即可。
我們可以通過訪問 http://10.211.55.7/cluster-demo/ 來訪問集群服務(wù)了努酸。
注意服爷,雖然是集群模式,但是我們所有的請(qǐng)求都要先到master機(jī)子上面做轉(zhuǎn)發(fā)获诈。
總結(jié)
wildfly內(nèi)置了很多強(qiáng)大的組件支持仍源,不愧為工業(yè)標(biāo)準(zhǔn)的典范。值的學(xué)習(xí)舔涎。
本文作者:flydean程序那些事
本文鏈接:http://www.flydean.com/wildfly-cluster-domain/
本文來源:flydean的博客
歡迎關(guān)注我的公眾號(hào):「程序那些事」最通俗的解讀笼踩,最深刻的干貨,最簡(jiǎn)潔的教程亡嫌,眾多你不知道的小技巧等你來發(fā)現(xiàn)嚎于!