在wildfly 21中搭建cluster集群
文章目錄
- 簡介
- 下載軟件和相關組件
- 配置domain
- 創建應用程序
- 部署應用程序
- 集群配置
- 總結
簡介
wildfly是一個非常強大的工具,我們可以輕松的使用wildfly部署應用程序,更為強大的是,wildfly可以很方便的部署cluster應用。
今天我們通過一個例子來講解下wildfly如何構建cluster應用。
下載軟件和相關組件
假如我們有兩個host,一個稱為master,一個稱為slave,我們需要在兩個機子上面安裝wildfly,構建成domain模式。然后需要在Domain controller主機上面安裝mod_cluster和httpd以組成集群。
首先我們需要下載wildfly-21.0.0.Final.zip,解壓之后,運行domain.sh以開啟domain模式。
配置domain
我們需要將master配置為domain controller,根據我們之前的文章,首先配置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>因為我們master的ip地址是10.211.55.7,所以需要修改相應的值。這里使用的是master對外的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>也需要修改相應的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>接下來,我們需要創建用于連接的security-realm,通過add-user.sh命令,我們可以創建添加用戶。
這里我們創建兩個用戶,第一個用戶叫做admin,使用來進行domain管理的用戶。
第二個用戶叫做slave,這個用戶用來slave連接到master。
還記得add-user.sh命令是怎么用的嗎?下面是創建admin用戶的輸出:
./add-user.shEnter 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用戶,則需要在下面的問題提示的時候,回答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用戶,我們就可以使用這個用戶來配置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就可以進行連接了。
創建應用程序
這里我引用的是官網的demo程序。實際上就是一個非常簡單的web應用。代碼地址 https://github.com/liweinan/cluster-demo/ 。
我們簡單進行一下講解,基本的代碼邏輯就是在session中存放一個時間數據,然后嘗試從不同的server中取出,看是否一致,如果一致的話說明cluster集群是有效的。
//設置session的值 session.setAttribute("current.time", new java.util.Date()); //獲取session的值 session.getAttribute("current.time")cluster中最重要的就是session共享,或者說session復制。我們可以簡單的在web.xml中使用distributable標簽即可。
<web-app><display-name>Archetype Created Web Application</display-name><distributable/> </web-app>這就是我們應用程序的全部了。
部署應用程序
這次我們從web console中進行應用程序的部署。
打開 http://10.211.55.7:9990 ,輸入我們創建的admin用戶名和密碼,即可進入管理界面。
默認情況下,會創建3個服務,分別是server-one,server-two和server-three。
server-one,server-two是默認啟動的,他們屬于main-server-group。而server-three是不啟動的,它屬于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,看名字就知道整個profile是為高可用設計的。那么這個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>這個profile中和ha有關的就是infinispan,jgroup和modcluster。通過這些組件,wildfly就可以來進行cluster的組建。
因為server-three默認是停止狀態的,我們需要在master和slave中分別啟動他們。
在Manage Deployments頁面,點擊Add Content,然后選擇我們之前的demo應用程序cluster-demo.war,上傳即可。
好了,程序已經部署好了,我們可以分別訪問:
http://10.211.55.7:8330/cluster-demo/ 和 http://10.211.55.2:8330/cluster-demo/ 來查看應用程序的頁面。
現在為止,兩個應用程序還是獨立的,并沒有組合成cluster,接下來我們將會進行cluster的配置。
還有一點要注意的是,我們需要將master和slave中的server-three修改成不同的名字,如果是相同的名字,那么我們在后面使用的mod_cluster將會報錯,因為在同一個server group中不允許出現兩個相同的名字。
<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機子上面使用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因為proxy_balancer_module是和proxy_cluster_module相沖突的。
然后修改httpd監聽 10.211.55.7:80。
最后還要在httpd.conf中配上mod_cluster-manager的監聽端口:
<VirtualHost 10.211.55.7:10001><Directory />Order deny,allowDeny from allAllow 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-managerOrder deny,allowDeny from allAllow from 10.211.55.</Location>KeepAliveTimeout 60MaxKeepAliveRequests 0ManagerBalancerName other-server-groupAdvertiseFrequency 5</VirtualHost>我們可以使用service httpd start啟動httpd服務即可。
我們可以通過訪問 http://10.211.55.7/cluster-demo/ 來訪問集群服務了。
注意,雖然是集群模式,但是我們所有的請求都要先到master機子上面做轉發。
總結
wildfly內置了很多強大的組件支持,不愧為工業標準的典范。值的學習。
本文作者:flydean程序那些事
本文鏈接:http://www.flydean.com/wildfly-cluster-domain/
本文來源:flydean的博客
歡迎關注我的公眾號:「程序那些事」最通俗的解讀,最深刻的干貨,最簡潔的教程,眾多你不知道的小技巧等你來發現!
總結
以上是生活随笔為你收集整理的在wildfly 21中搭建cluster集群的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python3参考秘籍-附PDF下载
- 下一篇: wildfly 21的配置文件和资源管理