在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.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用戶,則需要在下面的問題提示的時候,回答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,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啓動httpd服務即可。

我們可以通過訪問 http://10.211.55.7/cluster-demo/ 來訪問集羣服務了。

注意,雖然是集羣模式,但是我們所有的請求都要先到master機子上面做轉發。

總結

wildfly內置了很多強大的組件支持,不愧爲工業標準的典範。值的學習。

本文作者:flydean程序那些事

本文鏈接:http://www.flydean.com/wildfly-cluster-domain/

本文來源:flydean的博客

歡迎關注我的公衆號:「程序那些事」最通俗的解讀,最深刻的乾貨,最簡潔的教程,衆多你不知道的小技巧等你來發現!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章