java 在多服务器环境中管理会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12891569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:39:52  来源:igfitidea点击:

Manage sessions in multiple server environment

javasessiontomcat

提问by cherit

Suppose I have more than one web application servers running and I am logging in a User from Server1 thus his session starts.As http is stateless, suppose if the next request goes to Server3 than the Sever1 which was used to login to the application,if I use cookies, hidden form , its not going to work in Server2.

假设我有多个 Web 应用程序服务器在运行,并且我正在从 Server1 登录一个用户,因此他的会话开始。由于 http 是无状态的,假设下一个请求是否发送到 Server3 而不是用于登录到应用程序的 Sever1,如果我使用 cookies, hidden form ,它在 Server2 中不起作用。

So how do I manage the session ?, maybe by generating an ID (or even reusing the jsessioid generated ) and storing it in a central database,so that all servers can access this session ID and validate it before processing the request.Then in that case, I need to develop a mechanism to store all the session data as object to the database.

那么我如何管理会话?,可能是通过生成一个 ID(甚至重用生成的 jsessioid )并将其存储在中央数据库中,以便所有服务器都可以访问此会话 ID 并在处理请求之前对其进行验证。然后在那个在这种情况下,我需要开发一种机制来将所有会话数据作为对象存储到数据库中。

Is there any other built in mechanisms available ?

是否有其他可用的内置机制?

采纳答案by Manish

If you are deploying application on more than one server, you should use "Clustering". Application servers are able to handle this scenario using "session replication". With session replication, each server will have a copy of the active users session. IF the first request goes to server A and second request goes to server B, it will be transparent to application code and end user.

如果您在多台服务器上部署应用程序,您应该使用“集群”。应用服务器能够使用“会话复制”来处理这种情况。通过会话复制,每个服务器都将拥有活动用户会话的副本。如果第一个请求发送到服务器 A,第二个请求发送到服务器 B,则它对应用程序代码和最终用户是透明的。

For clustering/session replication in Tomcat, you can have a look at thislink.

对于 Tomcat 中的集群/会话复制,您可以查看链接。

回答by Ali786

Spring provides the session management:

Spring 提供了会话管理:

Spring Session makes it trivial to support clustered sessions without being tied to an application container specific solution. It also provides transparent integration with:

HttpSession - allows replacing the HttpSession in an application container (i.e. Tomcat) neutral way, with support for providing session IDs in headers to work with RESTful APIs

WebSocket - provides the ability to keep the HttpSession alive when receiving WebSocket messages

WebSession - allows replacing the Spring WebFlux's WebSession in an application container neutral way Source: Spring docs.

Spring Session 使得支持集群会话变得微不足道,而无需绑定到应用程序容器特定的解决方案。它还提供与以下各项的透明集成:

HttpSession - 允许在应用程序容器(即 Tomcat)中性方式中替换 HttpSession,并支持在标头中提供会话 ID 以使用 RESTful API

WebSocket - 提供在接收 WebSocket 消息时保持 HttpSession 活动的能力

WebSession - 允许以应用程序容器中立的方式替换 Spring WebFlux 的 WebSession 来源:Spring 文档。

Please check this for further information: https://spring.io/projects/spring-session#overview

请检查此以获取更多信息:https: //spring.io/projects/spring-session#overview

回答by Novice User

If your infrastructure ( Server 1, 2...) are connected to a single network appliance like Citrix Netscalar, then you can use IP or Cookie Persistence so that netscalar sends further requests to the same server.

如果您的基础架构(服务器 1、2...)连接到像 Citrix Netscalar 这样的单个网络设备,那么您可以使用 IP 或 Cookie Persistence,以便 netscalar 向同一服务器发送更多请求。