关于服务器:在wildfly-21中搭建cluster集群

4次阅读

共计 6833 个字符,预计需要花费 18 分钟才能阅读完成。

简介

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/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.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 的博客

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

正文完
 0