In the previous part (link) I get you running with a simple Glassfish 3.1 cluster setup with 2 instances running on 2 nodes. Now we have a cluster setup and can deploy an application one time and it will run on both nodes, no big deal, it doesn’t get us anywhere. So in part 2 we will do some modifications to our virtualbox server setup and create a web application with sessions replicated to both instances.
Prerequisites:
- The server and cluster setup from part 1 (link)
Preparation of host and Virtualbox guests:
In part 1 we used n1 and n2 as hostname, this creates trouble for this part. A key information for the sessions is the server-hostname (domain) and we cannot share sessions between totally different hosts.
- Update guest server hostname
Change etc/hosts and /etc/hostname
Server n1 becomes n1.test.com127.0.0.1 localhost 127.0.1.1 n1.test.com 127.0.0.1 n1.test.com 192.168.56.102 n2.test.com
Server n2 becomes n2.test.com
127.0.0.1 localhost 127.0.1.1 n2.test.com 127.0.0.1 n2.test.com 192.168.56.101 n1.test.com
Remarks:
- You can use any domain name (other than test.com, which I dont own btw).
- The IP addresses must fit your own Virtualbox setup.
- Update your host
The host running Virtualbox. Change /etc/hosts192.168.56.101 n1.test.com 192.168.56.102 n2.test.com
- Check multicast
The communication between the nodes is using multicast for the session replication. The Glassfish team gives us a tool to verify if your servers can “see” each other.
Go to the bin folder of the Glassfish installation and execute ./asadmin validate-multicast on both nodes
You should get feedback like this
Preparation of Glassfish Cluster
We need to update the node information for former n2 server.
- Modify node configuration (point to new hostname)
Create a simple web application with Netbeans
We will create a simple JSP app that displays the session id and some extra information.
- Create Web Application WebSessionDemo
- Update the index.jsp page
Netbeans creates the default page, which we need to update a bit to display the session information<%@page import="java.text.SimpleDateFormat"%> <%@page import="java.text.DateFormat"%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>WEB Session Demo</title> </head> <body> <h1>Session Details:</h1> <% HttpSession httpSession = request.getSession(); out.println("Session ID: " + httpSession.getId()+ "<br />"); DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS"); String created = formatter.format(httpSession.getCreationTime()); out.println("Session created: " + created + "<br />"); String lastAccess = formatter.format(httpSession.getLastAccessedTime()); out.println("Session last access: " + lastAccess + "<br />"); out.println("Server Request: " + request.getServerName() + "<br />"); out.println("Server Instance executing: " + System.getProperty("com.sun.aas.instanceName") + "(" + java.net.InetAddress.getLocalHost().getHostName() + ")" +"<br />"); %> </body> </html>
First Test-Run
- Lets deploy this app (like the application in the part 1) and see what happens..
- No change, we have one application deployed into 1 cluster running in 2 instances. Each one maintains its own session.
Changes to the application
We need to make a few configuration changes to the application in order to support the session replication we look for.
- Add a web.xml
By default it is no longer around for Java EE6 - Change web.xml
Tick the distributable option (or add <distributable/> to the xml file) - Change sun-web.xml
Add session config section with domain setting<session-config> <cookie-properties> <property name="cookieDomain" value="test.com"/> </cookie-properties> </session-config> root@ubuntu:/disk2#
Remark: This will allow Glassfish to share the same cookie for ALL subdomains under test.com, even the cookie in our test contains only the session-id. There might be certain risk in doing so. Check your policies and security requirements.
- Clean and Build
Second Test-Run
- Deploy the application
Use the availability option ! - Test
We have the same session replicated from instance 1 on node 1 replicated to instance 2 on node 2. That is the most simple HA setup, on of the nodes could go offline (try by switching off one of the virtualbox guests) and the other one is still alive (and so is our session).
The next improvement would be to use load-balancing, so the user will open only 1 domain and the cluster setup will quietly do all the magic in the background.
In the next part I will look into resources in the cluster context. Stay tuned.
References:
Pingback: Glassfish 3.1 – Clustering Tutorial « The JavaDude Weblog
how about in windows?
i use clustering in windows,and use 2 machine.
1 machine for DAS,Load balancing,and server instance.
1 machine for server instance.
the cluster work successful,but the session replication not work..
the session in machine 1 is not replicated to machine2.
i test command “asadmin validate-multicast”,but only one node (loopback),no reply from other host.
i have checklist availability when i deploy application,and change the web.xml to distributable. but its not work.
but,if i test in same host (localhost in machine1),which have 2 instances server,thats work..the session replicated to other instance(machine 2).
what must i do to handle that session replication?
thanks for the reply…:)
btw,i use virtual box to make that 2 machine.
machine 1 : ip address: 192.168.56.101
machine 2 : ip address: 192.168.56.102
command ping and ssh succesfull to that machine.
the system time of machine 1 and machine 2 are same.
I have the same problem, do you resolved this problem ?
Regards,
I just followed the tutorial and it worked pretty well.
To check session replication, make sure you remove all your old cookies (n1.domain.com, n2.domain.com) from your browser, otherwise it won’t work.
About the validate-multicast command, it must be run _simultaneously_ on all the nodes to get the replies.
Thanks again, javadude, for this nice tutorial.
How do I run “run _simultaneously_ on all the nodes to get the replies”?
From the tutorial, it seems just use the default option
Check if you have added tag in you web.xml
Hi,
Thanks for great tutorial!
What about part 3 – resources and load balancing? When will be available?
The HA features are not available in the community edition of GF. But still need to look into it for our commercial deployments. Hope find some time for it.
Hi
Thanks for the TUT (I also did on Win – Now I am also going to try on Ubuntu), I am looking forward to the next part!
Interesting to see the cookie name can be used as well. Though this may not be needed if you are having a load balancer in front and the browser is hitting the loadbalancer with same url always. Thanks for sharing
Thank you, this was a very useful post. Solved my session replication problem!
we had issue with session replication between nodes, we try to run cluster on different machines. suddenly turned that all nodes must be in the same subnet, it is important.
waiting for the next part , to get the magic !!!!!!1
Hi, thanks for your post. I’m having troubles with the second part: I still have different session ids on my two instances… And if I log in the first instance, then when i log with the same account in the second i get out of the first instance, Can you help me? Thanks,
Andrea
just put these glassfish-web.xml and web.xml into your web app
glassfish-web.xml
Keep a copy of the generated servlet class’ java code.
web.xml
30
Hi,
I am working on very simillar setup right now and can’t make the session replication tp work. I still get different JSESSIONID in the two browser tabs.
Is there any chance you could put the project files somewhere?
Thanks
Great well written post. Doing this replicate JPA cache on instance?
If not should I use Eclipselink cache coordination to achieve this, is there any
alternative ways, solutions?
Thanks