JBoss EAP 6.3 cluster-session replication)

Source: Internet
Author: User
Tags in domain jboss server

This article is a follow-up of the previous article. In Java Web application, sessions are inevitably used. In the cluster environment, Apache intelligently forwards HTTP requests to a JBoss server. Assume there are two JBoss servers: Server A, server B, and session values on server. When you access a page that depends on a session, if you access server a for the first time, you can get the session value normally and refresh it. If the session value is forwarded to server B, the session value cannot be obtained, the problem arises.

The solution is as simple as making people unbelieve it. Add a line in the app's web. xml<Distributable/>(Premise: JBoss cluster is implemented using mod_cluster). When a session is written to a server with this node, the session is automatically copied to other server nodes.

 

The following is a specific verification:

Network Environment:

For example, there are two machines: 172.21.129.181 (master server & apacha server), 172.21.129.128 (slave server)

 

The IP address of the user's computer is 172.21.129.57 (not shown in the figure)

 

Sample application:

To verify, we create the simplest spring MVC web application.

The Controller code is as follows:

1 package COM. cnblogs. yjmyzz; 2 3 Import javax. servlet. HTTP. httpservletrequest; 4 Import javax. servlet. HTTP. httpsession; 5 6 Import Org. apache. log4j. logger; 7 Import Org. springframework. stereotype. controller; 8 Import Org. springframework. UI. model; 9 Import Org. springframework. web. BIND. annotation. requestmapping; 10 Import Org. springframework. web. BIND. annotation. requestmethod; 11 import Org. springframe Work. web. servlet. modelandview; 12 13 @ controller14 public class homecontroller {15 16 logger log = logger. getlogger (this. getclass (); 17 18 Private Static final string sessionkey = "test "; 19 20/** 21 * write session22 * @ Param request23 * @ return24 */25 @ requestmapping (value = "/session-write", method = requestmethod. get) 26 Public String writesession (httpservletrequest request) {27 httpsession session = Request. getsession (); 28 session. setattribute (sessionkey, "sample value"); 29 return "session/write "; 30} 31 32/** 33 * read session34 * @ Param request35 * @ return36 */37 @ requestmapping (value = "/session-read", method = requestmethod. get) 38 public modelandview readsession (httpservletrequest request) {39 httpsession session = request. getsession (); 40 object sessionvalue = session. getattribute (sessionkey ); 41 modelandview model = new modelandview (); 42 if (sessionvalue! = NULL) {43 model. addobject (sessionkey, sessionvalue); 44} 45 46 try {47 // display several IP addresses to the page to help determine which server 48 string hostinfo = "inetaddress is forwarded to this HTTP request. getlocalhost () = "49 + java.net. inetaddress. getlocalhost () 50 + "<br/> request. getremoteaddr () = "51 + request. getremoteaddr () + ":" + request. getremoteport () 52 + "<br/> X-forwarded-for =" + getuserreadip (request) 53 + "<br/> request. getlocaladdr () = "+ request. getlocaladdr () 54 + ":" + request. getlocalport (); 55 model. addobject ("host", hostinfo); 56 57} catch (exception e) {58 59} 60 model. setviewname ("session/read"); 61 Return Model; 62} 63 64 // get the user's real IP65 private string getuserreadip (httpservletrequest request) {66 If (request. getheader ("X-forwarded-for") = NULL) {67 return request. getremoteaddr (); 68} 69 return request. getheader ("X-forwarded-for"); 70} 71 72}

Write. jsp: (write session)

1 <% @ page Language = "Java" contenttype = "text/html; charset = UTF-8 "2 pageencoding =" UTF-8 "%> 3 <HTML> 4 

Read. jsp: (display session)

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2     pageEncoding="UTF-8"%> 3 

Ready, start JBoss on master server and slave server in domain mode, and start Apache server.

Then visit:

Http: // 172.21.129.181/modclustersample/Write session to the session-write.do

Continue access:

Http: // 172.21.129.181/modclustersample/session-read.do read session

From the output of several IP addresses, this request Apache forwarded to 172.21.129.128 (I .e.: slave server), the user Client IP address is 172.21.129.57, And the Apache server IP address is 172.21.129.181

In addition, the first line indicates that the session value is correctly read: Sample value.

Enter the JBoss console of the master server and stop the JBoss server on the slave master.

Refresh the http: // 172.21.129.181/modclustersample/session-read.do on the user machine again

As you can see, the JBoss server on the slave server has been stopped, so this HTTP request is forwarded to 172.21.129.181 (master server), but the session value can still be output normally, it indicates that the session value has been copied to two JBoss servers while being written. session replication is verified successfully!

JBoss EAP 6.3 cluster-session replication)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.