我正在寻找一种避免内存会话复制/群集的方法,并将会话存储在数据库中。此时使用Tomcat的JDBCStore是无用的,因为它只在数据库中存储非活动会话以保存服务器内存。 有什么建议吗?
I am searching for a way to avoid in-memory session replication/clustering and store the session in a database. Using Tomcat's JDBCStore is useless at this point, because it only stores inactive sessions in the database to save the servers memory. Any suggestions?
先谢谢 Fabian
Thanks upfront Fabian
推荐答案如果您不想按照应该使用的方式使用会话,那么根本不要使用它 - 开发自己的会话对象。它仍然可以实现 HttpSession ,甚至可以从 HttpSession 的实现扩展。
If you don't want to use the session in the way it is supposed to be used, then don't use it at all - develop your own session object. It can still implement HttpSession, and even extend from an implementation of HttpSession.
您可以使用过滤器来包装您的请求,以便它返回您的会话对象而不是标准对象。在您的会话中,您可以将内容存储在数据库而不是内存中。
You can use a Filter to wrap your request so that it returns your session object rather than the standard one. In your session you can store things in DB instead of in-memory.
您可以使用 Hazelcast - 它提供分布式集合。但我想这将花费与配置会话复制相同的工作量。并且会话复制并不困难 - 所有容器都支持它。
Instead of writing to the DB, you can use Hazelcast - it provides distributed collections. But I guess it will take the same amount of effort as to configure session replication. And session replication is not something that hard - it is supported by all containers.
这些是粗略的指导方针,任务不会是微不足道的。我建议坚持使用标准会话使用模式,并且只在真正需要时才将内容存储在数据库中。
These are rough guidelines, the task will not be trivial. And I'd recommend sticking to the standard session usage patterns, and storing things in DB only if really needed.
为了避免复制的需要,您可以尝试使用粘性会话 - 即当负载均衡器将用户定向到服务器时,该用户的每个后续请求发送到同一台服务器。
In order to avoid the need of replication you might try using sticky sessions - i.e. when a user is directed to a server by the load balancer, each subsequent request by that user is sent to the same server.
更多推荐
Tomcat:在数据库中存储会话
发布评论