Using tomcat in round robin mode

2019-05-08 21:11发布

问题:

I want to run my tomcat-instances in a configuration where requests are served to several tomcat-instance via round robin. I don't want to use any internal cluster manager.

As far as I see if every request is served by different tomcats, an unknown sessionId would arrive at a tomcat, so it would be forced to create an new session and overwrite the old sessionId. So for every request a new session would be created. This seems to be a lot of overhead.

Is my view on that things right? Is there a way to disable tomcats sessions management?

Regards, Michael

回答1:

Basically you have two choices:

1) Replicate your sessions so they become reachable by any Tomcat node. Solutions: Tomcat Cluster, memcached-session-manager, possibly others.

2) Use a load balancer and implement sticky sessions. First requests will be routed randomly on a round robin basis, but subsequent requests will stick to the same server. Solutions: mod_proxy, hardware traffic managers.

The disadvantage of the first option is that session replication is costly, not very reliable and often requires Serializable-only data to be put in session.

The disadvantage of the second approach is that if you shut down your Tomcat for maintenance, the users will be forced to log in again.

You are incorrectly assuming that "for every request a new session would be created". The new session will be created only if not created before on that same server, or if it was created but already expired.



回答2:

We usually used Tomcat behind an Apache web server with mod_jk for load balancing the requests across the Tomcat instances.

With sticky sessions a user will only get a session upon the first request and will afterwards always be directed to the Tomcat from where his session originates. So there is no need to replicate sessions across all Tomcats and the requests will be distributed across the Tomcats, too.

Of course, this does not ensure some kind of round-robin which you asked for.



回答3:

A session will only be created once your code requests the session so if your application doesn't require the session then just simply dont access it. Checkout the section on getSession() in HttpServletRequest

http://download.oracle.com/javaee/5/api/javax/servlet/http/HttpServletRequest.html#getSession(boolean)

I'm not sure if there is a way to replicate the session across different tomcat instances however if you require some user state without session then you can use cookies instead.

EDIT: If you do need to replicate the session you could probably start by reading this tomcat document. http://tomcat.apache.org/tomcat-5.5-doc/cluster-howto.html