Apache与jboss集群配置

时间:2022-10-05 00:25:08

 

Apache与jboss集群配置,实现负载均衡与容错的功能。Apache做为软件负载均衡器,负责请求的分发。Jboss做为应用服务器,利用jgroup协议,处理容错及状态复制与转移。

 

一 apache的配置

 

a. 将下载的apache mod_jk重命名为mod_jk.so,复制到APACHE_HOME/modules/中,修改 APACHE_HOME/conf/httpd.conf  ,在文件末添加:Include conf/mod-jk.conf

 

b. 在APACHE_HOME/conf/目录创建新文件mod-jk.conf,内容如下:

                            # Load mod_jk module  

                            # Specify the filename of the mod_jk lib  

                            LoadModule jk_module modules/mod_jk.so  

                            # Where to find workers.properties  

                            JkWorkersFile conf/workers.properties  

                            # Where to put jk logs  

                            JkLogFile logs/mod_jk.log  

                            # Set the jk log level [debug/error/info]  

                            JkLogLevel info  

                            # Select the log format  

                            JkLogStampFormat "[%a %b %d %H:%M:%S %Y]" 

                            # JkOptions indicates to send SSK KEY SIZE  

                            JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories  

                            # JkRequestLogFormat  

                            JkRequestLogFormat "%w %V %T" 

                            # Mount your applications  

                            JkMount /application/* loadbalancer  

                            # You can use external file for mount points.  

                            # It will be checked for updates each 60 seconds.  

                            # The format of the file is: /url=worker  

                            # /examples/*=loadbalancer  

                            JkMountFile conf/uriworkermap.properties  

                            # Add shared memory.  

                            # This directive is present with 1.2.10 and  

                            # later versions of mod_jk, and is needed for  

                            # for load balancing to work properly  

                            JkShmFile logs/jk.shm  

                            # Add jkstatus for managing runtime data  

                            <Location /jkstatus/>  

                                     JkMount status  

                                     Order deny,allow  

                                     Deny from all  

                                     Allow from 127.0.0.1 

                            </Location>  

 

         JkMount: 连接你的Web应用上下文, 比如:JkMount /cluster-test/* loadbalancer

        

c. 在 在APACHE_HOME/conf/目录创建新文件uriworkermap.properties,置为空,这个文件主要是做url 路由的,来配置哪些请求由JK处理哪些请求让APACHE处理. 基本格式为/url=worker_name;

                            /jmx-console=loadbalancer  

                            /jmx-console/*=loadbalancer  

                            /web-console=loadbalancer  

                            /web-console/*=loadbalancer 

 

d. 在 APACHE_HOME/conf/目录创建新文件workers.properties,内容如下:

                            # Define list of workers that will be used  

                            # for mapping requests  

                            worker.list=loadbalancer,status  

                            # Define Node1  

                            # modify the host as your host IP or DNS name.  

                            worker.node1.port=8009 

                            worker.node1.host=node1.mydomain.com  

                            worker.node1.type=ajp13  

                            worker.node1.lbfactor=1 

                            worker.node1.cachesize=10 

                            # Define Node2  

                            # modify the host as your host IP or DNS name  

                            worker.node2.port=8009 

                            worker.node2.host= node2.mydomain.com  

                            worker.node2.type=ajp13  

                            worker.node2.lbfactor=1 

                            worker.node2.cachesize=10 

                            # Load-balancing behaviour  

                            worker.loadbalancer.type=lb  

                            worker.loadbalancer.balance_workers=node1,node2  

                            worker.loadbalancer.sticky_session=1 

                            #worker.list=loadbalancer  

                            # Status worker for managing load balancer  

                            worker.status.type=status  

        

上面的文件中配置了两个节点,name分别为node1,node2,指定两个节点的IP,并在worker.loadbalancer.balance_workers中指定所有的node列表,worker.loadbalancer.sticky_session设置是否启用“粘着的”Session,sticky session是指来自同一IP的请求将被发送到同一个Jboss节点,sticky session设为0的话同一session的不同请求会被负载均衡分发到不同的jboss节点上。

 

二JBoss节点配置

a. 修改JBOSS_HOME/server/all/deploy/jboss-web.deployer/server.xml文件

         注释掉http connector(8080)

        

b. 修改JBOSS_HOME/server/all/deploy/jboss-web.deployer/server.xml文件,AJP13 connector:

         将Engine 修改如下:

                            <Engine defaultHost="localhost" jvmRoute="node1">  

                            ...  

                            </Engine>

 

         node1和node2分别对应相应ip的jboss;

 

c. 修改 JBOSS_HOME/server/all/deploy/ jboss-web.deployer /META-INF/jboss-service.xml,找到<attribute>该为:<attribute>true</attribute>

 

/********************** 说明 begin *****************************/

snapshotMode: 复制脏会话的时机

“instant”: 在每个请求处理之后(缺省值)

“interval”: 间隔复制。异步会话复制消息每隔X毫秒发送;在该模式下,强制保持会话粘性保持

snapshotInterval: 当snapshotMode为”interval”时,使用该设置的值,单位为毫秒。缺省值为1000ms

cacheName: JBossCache服务的JMX名字,缺省为"jboss.cache:service=TomcatClusteringCache“

useJK: 如果使用mod_jk作为负载均衡器则设为true

/********************** 说明 end ******************************/

三 设置Session同步

a. 编辑JBOSS_HOME/server/all/deploy/jbossweb-cluster.sar/META-INF/jboss-service.xml,找到<attribute>,有udp和tcp两种方式,UDP采用多播方式,但问题比较多,所以采用TCP方式。

找到<config> <tcp,将><config>到</config>生效.并对该部分进行以下修改:

将全部down_thread和up_thread的false都改为true. windows平台lookloop = true

将bind_addr改为本机ip,<tcpping initial_hosts 值设为:node1 IP[7810],node2 IP[7810] ;

 

/********************** 说明 begin *****************************/

用于JBossWeb集群的TreeCache服务在deploy/jboss-web-cluster.sar下的META-INF/jboss-service.xml文件中配置

CacheMode属性

REPL_ASYNC(缺省)

复制消息被异步发送()发完即丢失

提高应用的响应性

REPL_SYNC

复制消息被同步发送

如果不保持会话粘性可使用此模式

JGroups协议栈配置

/********************** 说明 end ******************************/

 

b. 修改应用程序的web.xml文件,如下所示:

 

 

                   <?xml version="1.0" encoding="UTF-8"?> 

                   <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 

                            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

                            xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "> 

                            <distributable /> 

                    ...   

                   </web-app>

 

/********************** 说明 begin *****************************/

会话复制在每个Web应用的WEB-INF/web.xml中使能

加入空的<distributable/>元素

/********************** 说明 end ******************************/

                  

c. 在web-inf中添加jboss-web.xml文件,内容如下:

                   <?xml version="1.0" encoding="UTF-8"?> 

                   <!DOCTYPE jboss-web PUBLIC  

                            "-//JBoss//DTD Web Application 4.2//EN"  

                            "http://www.jboss.org/j2ee/dtd/jboss-web_4_2.dtd"> 

                   <jboss-web> 

                            <replication-config> 

                                     <replication-trigger>SET_AND_NON_PRIMITIVE_GET</replication-trigger> 

                                               <replication-granularity>SESSION</replication-granularity> 

                                               <replication-field-batch-mode>true</replication-field-batch-mode> 

                            </replication-config> 

                            <context-root>/</context-root> 

                   </jboss-web>

                  

/********************** 说明 begin *****************************/

针对每一个Web应用配置会话复制WEB-INF/jboss-web.xml

“replication-config”元素:复制粒度

Session, Attibute,Field

“replication-trigger”元素:决定一个脏会话的条件

“SET_AND_GET”:任何属性被访问时标记为脏会话

“SET_AND_NON_PRIMITIVE_GET“:任何返回非原始值的属性被访问时标记为脏会话。为缺省值

“SET”: 任何属性被修改时标记为脏会话

“replicationFieldBatchMode”

如果为True, 对每个请求的所有Fied修改以批处理方式复制

仅在复制粒度为Fied时有效

 

/********************** 说明 end ******************************/

四 启动负载均衡器apache

 

         在/usr/local/apache2/bin目录下执行./httpd –k start

 

         在/usr/local/apache2/logs/mod_jk.log中,看到apache初始化mod_jk的日志:

         [Wed Aug 01 10:36:11 2007][6636:63168] [info] init_jk::mod_jk.c (2743): mod_jk/1.2.23 initialized

         [Wed Aug 01 10:36:11 2007][6637:63168] [info] init_jk::mod_jk.c (2743): mod_jk/1.2.23 initialized

         后面如有错误信息,是因为集群中的节点尚未启动,暂时忽略。

 

         启动Jboss节点

         在集群各机器的/usr/local/jboss/bin,分别执行/run.sh –c all -b ip