开发者

Manage session within distributed application

I'm working on distributed web application and we decided to separate web module from business services to make it more scalable.

Here is the situation: We have one server instance that keeps web application (Controllers, JSPs, etc) and lots of server instances with business services. If web application needs any data it asks any of the开发者_如何学C existing business server via Hessian about it, then got the response and displays data.

Currently we retrieving data from DB based on logged in user and this cannot be changed, so every server should know which user asked to do the job.

My question is: Do you know the solution to keep user session across several independent applications?

For example one of the solutions can be send username with every request but this is not a very good idea for us.

Thanks a lot


There are 2 approaches for this problem:

1) Store all session info in central memcached server

2) Use session-aware load-balancer, which will direct same users to same nodes.


Use distributed hashtable to store and retrieve your sessions from any server. Try Hazelcast for example. It is open source and super simple; see the sample below.

Map<String, Session> mapSessions  = Hazelcast.getMap("sessions");
// session is created or updated so put it into the sessions map
mapSessions.put(sessionId, session);
// any server needing to access a session should just retrieve
// it from the map. 
// Map is distributed/shared so any JVM running Hazelcast can
// read the sessions.
Session session = mapSessions.get(sessionId);

Hazelcast is peer-to-peer. Just include a single jar and start sharing your sessions.


A distributed Memcached server would be a better choice as it can manage the storage gracefully. Another alternative solution is to use Redis as it is quite faster than Memcached and it can store a different variety of data type and it could be used as a database as well

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜