JBoss-4.2.3GA + Apache load balancing and cluster solution configuration process
FromHttp://blog.sina.com.cn/s/blog_4c925dca0100qh2l.html
The company needs to implement load balancing and cluster requirements for Apache + JBoss, but Atang had no experience in this field and started a two-day pioneering journey, needless to say, because the online articles are basically the same, it is estimated that many of them are reproduced from each other, and some changes have basically made some mistakes, in addition, the JBoss versions introduced on the Internet are basically inconsistent with my versions, resulting in the JBoss directory mentioned on the Internet. I may not have it here, which makes it more troublesome, in the process of searching for help from some online buddies, some of them either did not do it, and some just did load balancing for apaceh + Tomcat, which makes it more difficult for allang to solve the problem! Finally, it was solved! Not to mention whether the final implementation effect is as expected, but this solution can be implemented normally. It can be said that 90% has been successful. As for the optimization of Apache and so on, it is another matter! A don't want to talk about it here! I was troubled by my own configuration. I wrote this summary article and shared it with my friends. I hope it will be helpful or helpful to other netizens, and I am very happy!
Deployment Environment
(1) Two Linux servers 192.168.80.13 192.168.80.14
(2) JBoss version 4.2.3.ga (JBoss servers of the above two Linux servers are installed respectively)
(3) Apache 2.2.17 (httpd-2.2.17.tar.gz can be downloaded from the official website). The Apache Server Load balancer is installed on 192.168.80.13.
Apache-HTTP Server
(4) mod_jk version 1.2.3 (version is mod_jk-1.2.31-httpd-2.2.x.so, can be downloaded from the official website )-
Apache Tomcat-Tomcat connectors
|
Note
(1) The mod_jk version must be compatible with the Apache version. Otherwise, it may be installed and Apache will run normally, but Server Load balancer will not work, as shown in
Mod_jk-1.2.31-httpd-2.2.x.so (for example, the previous red text is the version of mod_jk, followed by the version of Apache server)
Deployment test results
(1) Apache Server Load balancer is in normal use, for example,
Http: // 192.168.80.13/test/index. jsp
The preceding address 192.168.80.13 is actually the address of the Apache server. It intercepts the two nodes 192.168.80.13 and
JBoss server corresponding to 192.168.80.14
As shown below
(2) The JBoss cluster function has taken effect. The two JBoss nodes in the cluster can communicate with the AJP cluster.
Load Balancing Methods for this project:
Currently, user-based Server Load balancer is selected, because the request-Based Server Load balancer is not suitable for high-frequency operations due to the high concurrency we need. (For the two cases, refer to the following description)
1. Request-Based Load Balancing
In this way, the Load balancer distributes each HTTP request according to the status of each node. When using this balancing policy, you must copy the user's session between multiple nodes to synchronize the user status of the entire cluster in real time. This operation is called session replication (Session
Replication ). The implementation principle of JBoss is to use Interceptor to intercept the request according to the user's synchronization policy, and then submit the request
Server generates a response.
The advantage of this method is that the customer will not be bound to all specific nodes. As long as another node is alive, the user status will not be lost, and the cluster can continue to work. The disadvantage is that the nodes communicate frequently and the response speed is affected. When multiple concurrent and high-frequency operations are performed, the performance is greatly reduced.
2. User-Based Load Balancing
In this way, when the user sends the first request, the Server Load balancer dynamically allocates the user to a node and records the JVM route of the node. All requests of the user
Will be bound to this JVM route, and the user will only interact with this server. This policy is called sticky session ).
The advantage of this method is that the response speed is fast and there is no need to communicate between multiple nodes. The disadvantage is that when a node dies, all the users it is responsible for will lose the session.
Configure JBoss
- Modify the JVM heap size in the run .. CONF file of the VM. If sun or hp jdk is used, set maxpermsize.
Set java_opts = % java_opts %-xms256m-xmx1024m-XX: maxpermsize = 128 m
2. Application Deployment: copy the jar package of EJB and the war of Web to $ jboss_home/Server/default/deplay.
(In the following sentence, it seems useless.) if it is a cluster environment, copy it to $ jboss_home/Server/default/farm.
3. Deploy the JBoss cluster service
You do not need to use the $ jboss_home/Server/all configuration to deploy the cluster. Only steps 1 and 2 below need to do more work for the default configuration.
(1). Copy the cluster-service.xml and jboss-web-cluster.sar under $ jboss_home/Server/All/deploy to $ jboss_home/Server/default/deploy
(2). Copy jgroups. jar, jbossha. jar, jboss-cache.jar under $ jboss_home/Server/All/lib to $ jboss_home/Server/default/lib
Jbossha. Jar (load org. JBoss. Ha. Framework. server. clusterpartition)
Jgroups. Jar (the underlying communication protocol of the JBoss cluster)
Jboss-cache.jar (loading org. JBoss. cache. AOP. treecacheaop)
4. Configure the JBoss Node
Session replication Configuration
JBoss session replication is an implementation of JBoss session synchronization. The principle is to establish horizontal connections between each JBoss node. Each node synchronizes the session changes of the current node to all other nodes.
JBoss's session replication and HTTP cluster are two mutually compatible and independent systems. Session replication is a horizontal link between nodes, and an HTTP cluster is a vertical link between the Server Load balancer and nodes.
$ Jboss_home/Server/default/deploy/jboss-web-cluster.sar/META-INF/jboss-service.xml and $ jboss_home/Server/default/deploy/cluster-service.xml
Note: The nodes of the cluster must be in the same CIDR block.
Both files must be modified. The former is a web session copy, and the latter is a cluster such as JBoss EJB.
Locate <config> <UDP, and comment out all <config> to </config>. JBoss session replication can be performed through UDP or TCP. UDP adopts the multicast mode, but there are many problems, so TCP is used.
Find <config> <TCP, and change> <config> to </config>. make the following changes to this part:
Change the values of down_thread and up_thread to true.
In the <TCP bind_addr = "$ {JBoss. Bind. Address}">
After <tcpping initial_hosts = ", enter the IP address of the local machine and all other JBoss nodes in the Cluster [7810], for example, <tcpping initial_hosts =" $ {JBoss. BIND. address} [7810], 192.168.80.14 [7810] "> </tcpping>
$ Jboss_home/Server/default/deploy/other parameters in the jboss-web-cluster.sar/META-INF/jboss-service.xm
Clustername indicates that the cluster name is in the same LAN and multiple JBoss clusters can exist. therefore, the cluster names configured for each node in the cluster must be consistent (the cluster name I configured in the actual project is Wenshi), and machine IP addresses do not have special requirements, as long as they can communicate with each other. in theory, multiple JBoss instances can be installed on one machine, which belongs to different clusters. however, this greatly increases complexity and is a bad configuration method. it is not recommended to bother yourself.
Isolationlevel is the isolation level. Optional values include: serializable, repeatable_read, read_committed, read_uncommitted, and none. The isolation level here has the same meaning as the database isolation level. For most web applications, it is usually set to repeatable_read.
Cachemode is the cache mode. Because session replication is implemented through caching, it is actually a replication mode. Optional values include repl_sync and repl_async to determine whether the changes should be synchronized or asynchronous. The default value is repl_async. synchronous replication is used to ensure that the propagation changes before the request is completed. Session synchronization is not delayed, but the efficiency is low.
For example
<Config>
<TCP bind_addr = "172.16.80.13" start_port = "7810" loopback = "true"/>
<Tcpping initial_hosts = "172.16.80.13 [7810], 172.16.80.14 [7810]" port_range = "3" timeout = "3500"
Num_initial_members = "3" up_thread = "true" down_thread = "true"/>
<Merge2 min_interval = "5000" max_interval = "10000"/>
<FD shun = "true" timeout = "2500" max_tries = "5" up_thread = "true" down_thread = "true"/>
<Verify_suspect timeout = "1500" down_thread = "false" up_thread = "false"/>
<Pbcast. nakack down_thread = "true" up_thread = "true" gc_lag = "100"
Retransmit_timeout = "3000"/>
<Pbcast. Stable desired_avg_gossip = "20000" down_thread = "false" up_thread = "false"/>
<Pbcast. GMS join_timeout = "5000" join_retry_timeout = "2000" Shun = "false"
Print_local_addr = "true" down_thread = "true" up_thread = "true"/>
<Pbcast. state_transfer up_thread = "true" down_thread = "true"/>
</Config>
5. Configure the application
Add <distributable/> In the <web-app> segment in the/% JBoss %/Server/default/deploy/jboss-web.deployer/root. War/Web. xml file of the application.
In the jboss-web.xml (this file in which directory, I have not been very clear, online are not indicated which directory in the file, and finally is a new jboss-web.xml, placed with the above Web. add the following content to the XML directory:
<? XML version = "1.0"?>
<JBoss-web>
<Context-root> TRMS </context-root>
<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>
</JBoss-web>
Apache Server Load balancer Configuration
(1) download and install. (Installation Process omitted)
Apache server
Http://httpd.apache.org/download.cgi
Mod_jk
Http://www.apache.org/dist/tomcat/tomcat-connectors/jk/binaries/linux/jk-1.2.31/i386/
(2) Modify % Apache %/CONF/httpd. conf
Add include CONF/mod_jk.conf at the end of the file.
(3) create the file mod_jk.conf under % Apache %/CONF.
The file content is as follows:
# 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/* 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 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/* router indicates that all requests are sent to the router for processing. You can modify the URL to control the sending of certain requests.
(4) Create the workers. properties file under % Apache %/CONF.
The content is:
# Define list of workers that will be used
# For mapping requests
Worker. List = loadbalancer, node1, node2
# Define node1
# Modify the host as your host IP or DNS name.
Worker. node1.port = 8009
Worker. node1.host = 192.168.80.13
Worker. node1.type = ajp13
Worker. node1.lbfactor = 1
Worker. node1.redirect = node2
# Define node2
# Modify the host as your host IP or DNS name.
Worker. node2.port = 8009
Worker. node2.host = 192.168.80.14
Worker. node2.type = ajp13
Worker. node2.lbfactor = 1
Worker. node2.redirect = node1
# Load-balancing behaviour
Worker. loadbalancer. type = LB
Worker. loadbalancer. balance_workers = node1, node2
Worker. loadbalancer. sticky_session = 0
# Worker. List = loadbalancer
# Status worker for managing Load balancer
Worker. Status. type = Status
The node naming rule is worker. node name. xxxx. Therefore, the preceding file defines two nodes: node1 and node2. Port 8009 is the default AJP port of JBoss. In addition, the parameter worker. node2.lbfactor is used as the node load weight. The larger the value, the larger the chance to obtain the load. It can be adjusted based on the hardware performance of the node. The worker. Router. sticky_session parameter specifies whether to use stickysession. If http session replication is configured, No sticky session is required. The above configuration does not use sticky sessions. Worker. server1.redirect = is
Worker to which the Failover is transferred
(5) Configure JBoss to support sticky mod_jk
Modify $ jboss_home/Server/default/deploy/jboss-web.deployer/META-INF/jboss-service.xml
Change <attribute name = "usejk"> false </attribute> to true.
(6) Configure JBoss to support sticky sessions
Modify $ jboss_home/Server/default/deploy/jboss-web.deployer/server. xml
<Engine name = "JBoss. Web" defaulthost = "localhost"> changed
<Engine name = "JBoss. Web" defaulthost = "localhost" jvmroute = "server1"
Note: The value of jvmroute must correspond to the node name in mod_jk. Otherwise, the route cannot be correctly routed.
Test File
Add a new folder/test in the % jboss_home %/Server/default/deploy/jboss-web.deployer/root. War/directory and add the following JSP files to it:
Index. jsp
<% @ Page contenttype = "text/html; charset = ISO-8859-1" %>
<HTML>
<Head>
<Title> test </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<Body onload = "document. Form. Name. Focus ()">
<Br>
<Center>
The host is: <% = java.net. inetaddress. getlocalhost (). tostring () %> <br>
Your session ID is: <% = session. GETID () %> <br>
Your session detail is: <% = session. tostring () %> <br>
Your session context is: <% = session. getsessioncontext () %> <br>
Please input your name: <br>
<Form action = "test_action.jsp" method = "Post" name = "form">
<Input type = "input" name = "name"/>
<Input type = "Submit" value = "Submit">
</Form>
</Center>
</Body>
</Html>
Test_action.jsp
<% @ Page contenttype = "text/html; charset = ISO-8859-1" %>
<HTML>
<Head>
<Title> test action </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<%
String name = request. getparameter ("name ");
Session. setattribute ("name", name );
String host = java.net. inetaddress. getlocalhost (). tostring ();
%>
<Body>
<Br>
<Br>
<Center>
The host is: <% = Host %> <br>
Your session ID is: <% = session. GETID () %> <br>
Your session detail is: <% = session. tostring () %> <br>
Your session context is: <% = session. getsessioncontext () %> <br>
Your name is: <% = Name %> <br>
This name is set into the session. <br>
Please click <a href = "session. jsp"> here </a> to check the session valid or not.
</Center>
</Body>
</Html>
Session. jsp
<% @ Page contenttype = "text/html; charset = ISO-8859-1" %>
<HTML>
<Head>
<Title> test action </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<%
String name = NULL;
If (session. getattribute ("name ")! = NULL)
Name = (string) Session. getattribute ("name ");
String host = java.net. inetaddress. getlocalhost (). tostring ();
%>
<Body>
<Br>
<Br>
<Center>
The host is: <% = Host %> <br>
Your session ID is: <% = session. GETID () %> <br>
Your session detail is: <% = session. tostring () %> <br>
Your session context is: <% = session. getsessioncontext () %> <br>
<%
If (name! = NULL ){
Out. Print ("Your name is" + name + "<br> ");
Out. Print ("the session is valid .");
}
Else {
Out. Print ("the session is invalid !!! ");
}
%>
<A href = "index. jsp"> return! </A>
</Center>
</Body>
<%
If (session. getattribute ("name ")! = NULL)
Session. invalidate ();
%>
</Html>
Access Method: http: // 192.168.80.14/test/index. jsp