Monday, January 28, 2013

SpringMVC replicates sessions in cluster servers

env:
refer to http://wangxiangblog.blogspot.sg/2012/08/tomcat-cluster.html   to setup cluster servers.

1) this configuration file is for load balancer


worker.list=loadbalancer,cluster1,cluster2
#========cluster1========
worker.cluster1.port=8889                    
worker.cluster1.host=localhost                
worker.cluster1.type=ajp13
worker.cluster1.lbfactor = 1                  
#========cluster2========
worker.cluster2.port=8899
worker.cluster2.host=localhost
worker.cluster2.type=ajp13
# The higher the value of the lbfactor for Tomcat instance, the more work the server will do, and vice versa
worker.cluster2.lbfactor =1                
#========controller======
worker.loadbalancer.type=lb                
worker.loadbalancer.balanced_workers=cluster1,cluster2
# keep requests belonging to the same session (which means the same user) forwarded to the same worker
worker.loadbalancer.sticky_session=true

2) add <distributable/> element in web.xml

3) code in servlet


@RequestMapping(value="/Session1.htm")
public ModelAndView doit(@RequestParam("name") String name,HttpSession session){
sessionObj.setName(name);
session.setAttribute("sessionObj", sessionObj);

ModelAndView mav = new ModelAndView("hello2");
mav.addObject(sessionObj);
return mav;
}


      @RequestMapping(value="/Session2.htm")
public ModelAndView doit(HttpSession session){
SessionObj sob=(SessionObj)session.getAttribute("sessionObj");

ModelAndView mav = new ModelAndView("hello3");
mav.addObject("sessionObj", sob);
return mav;
}




Take Note:
if set sticky as false, session will be lost in cluster env.  so how to resolve it?
Workaround solution to add <distributable/> element to \META-INF\web-fragment.xml in spring-web-3.2.0.RELEASE.jar

-----------------------------
Tried to run the similar test session web app with struts2 in cluster servers to replicate session, but cannot work if sticky=false .


No comments:

Post a Comment