简介

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 : adminPassword 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 usernamePassword : passw0rd!Re-enter Password : passw0rd!The username 'admin' is easy to guessAre you sure you want to add user 'admin' yes/no? yesAbout 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? yesTo 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/c... 。

咱们简略进行一下解说,根本的代码逻辑就是在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.soLoadModule manager_module modules/mod_manager.soLoadModule proxy_cluster_module modules/mod_proxy_cluster.soLoadModule 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的博客

欢送关注我的公众号:「程序那些事」最艰深的解读,最粗浅的干货,最简洁的教程,泛滥你不晓得的小技巧等你来发现!