HTTP Session Replication Example
NOTE: this is a user contribution. Some sections might be obsolete or incorrect. If you wanna find a more up to date example which has been created by a JBoss developer, please go to ReplicatedCounter wiki.
(Also see http://www.jboss.org/index.html?module=bb&op=viewtopic&p=3853535#3853535 for another explanation of how to set things up).
Setting Apache and Tomcat loadbalancer with mod_jk1.2
1)Download Apache 1.3.x to a location like c:\Apache\\ 2)Download mod_jk1.2.x.dll to c:\apache\modules and rename it to mod_jk.dll\\ 3)Edit httpd.conf file of Apache under c:\apache\conf\\ Add the following two lines at the end of the file\\ #Include mod_jk Include conf/mod-jk.conf 4) Create a new file called as mod-jk.conf under the conf directory of apache Copy everything in between ==== #=============================================== #Load mod_jk module LoadModule jk_module modules/mod_jk.dll #Locate workers.properties JkWorkersFile conf/workers.properties #Where to put JK Logs JkLogFile logs/mod_jk.log #Set jk log level [debug/error/info] JkLogLevel info #Select the log format JkLogStampFormat "[%a %b %d %H:%M:%S %Y] " #JkOptions indicate to send SSL Key Size JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories #JkRequestLogFormat set the request format JkRequestLogFormat "%w %V %T" JkMount /* loadbalancer #====================================================
Set up Apache Load Balancer with a new file "workers.properties" under the conf directory of Apache
# # workers.properties # # ------------------------ # First tomcat server # ------------------------ worker.TOM1.port=8009 worker.TOM1.host=localhost worker.TOM1.type=ajp13 worker.TOM1.lbfactor=1 worker.TOM1.local_worker=1 worker.TOM1.cachesize=10 # ------------------------ # 2nd tomcat server # ------------------------ worker.TOM2.port=8109 worker.TOM2.host=localhost worker.TOM2.type=ajp13 worker.TOM2.lbfactor=1 worker.TOM2.local_worker=1 worker.TOM2.cachesize=10 #--------------------------------------------- # Define load Balancing behavior #--------------------------------------------- worker.loadbalancer.type=lb worker.loadbalancer.balanced_workers=TOM1,TOM2 worker.loadbalancer.sticky_session=1 worker.loadbalancer.local_worker_only=1 worker.list=loadbalancer
Note: JBOSS_HOME is referred to as "JB" below #Copy the file "sample-bindings.xml" from docs/examples/binding-example to JB. On my machine it is under c:\jboss-3.2.5 #Go to JB/server #Copy directory "all" to instance1 and instance2 Windows: xcopy all instance1 /S Windows: xcopy all instance2 /S Unix: cp -R all instance1 Unix: cp -R all instance2 4) Go to JB/server/instance1/jbossweb-tomcat50.sar Edit the file "server.xml" Add a name to this instance of Tomcat as below with "jvmRoute" attribute. <Engine name="jboss.web" defaultHost="localhost" jvmRoute="TOM1"></Engine> 4.1) Go to JB/server/instance2/jbossweb-tomcat50.sar Edit the file "server.xml" Add a name to this instance of Tomcat as below with "jvmRoute" attribute. <Engine name="jboss.web" defaultHost="localhost" jvmRoute="TOM2"></Engine> 5) Go to JB/server/instance2/conf Edit the file "jboss-service.xml" You will need to uncomment the section for ServiceBindingManager and also update the location of the xml file sample-bindings.xml (Location will be from JB/bin directory) <!-- | Binding service manager for port/host mapping. This is a sample | config that demonstrates a JBoss instances with a server name 'jboss1' | loading its bindings from an XML file using the ServicesStoreFactory | implementation returned by the XMLServicesStoreFactory. | | ServerName: The unique name assigned to a JBoss server instance for | lookup purposes. This allows a single ServicesStore to handle mulitiple | JBoss servers. | | StoreURL: The URL string passed to org.jboss.services.binding.ServicesStore | during initialization that specifies how to connect to the bindings store. | StoreFactory: The org.jboss.services.binding.ServicesStoreFactory interface | implementation to create to obtain the ServicesStore instance. --> <mbean code="org.jboss.services.binding.ServiceBindingManager" name="jboss.system:service=ServiceBindingManager"> <attribute name="ServerName">ports-01</attribute> <attribute name="StoreURL">../sample-bindings.xml</attribute> <attribute name="StoreFactoryClassName"> org.jboss.services.binding.XMLServicesStoreFactory </attribute> </mbean>
For JBoss4, additional changes for ServiceBindingManager are:[ConfigurePorts|DOC-9376].
6) Under JB\server\instance2\deploy, create a folder named test.war and drop into it a file testsessionreplication.jsp containing the following code: <html> <body bgcolor=blue> <center> <%=request.getSession().getId() %> <h1>Tomcat 1</h1> </body> </html> Do the same with JB\server\instance2\deploy\ with the following code: <html> <body bgcolor=green> <center> <%=request.getSession().getId() %> <h1>Tomcat 2</h1> </body> </html> 7) Go to JB/bin in different terminal/command windows. Start instance1 run -c all Start instance2 run -c instance2 8) Start Apache Server 9) Go to browser and test: [http://localhost/test/testsessionreplication.jsp] You should see a blue page with sessionid and Tomcat1 10) Shutdown instance1 (Do ctrl-c) 11) Go to browser and test [http://localhost/test/testsessionreplication.jsp] You should see a green page with sessionid and Tomcat2 Basically you can now start and stop either of the two tomcat instances in any order. The session is replicated and as long as one of them is running, you will always see a response on browser.
From a frustrated developer...
"On JBoss 3.2.5 and Tomcat 5.0 (and possibly other versions) make sure you edit jbossweb-tomcat50.sar\META-INF\jboss-service.xml
and change the flag to false. Otherwise, although your session replication will work from a creation point of view, updates will not be replicated! I could find very little other mention of this anywhere on the Internet :)"