database - 如何在集群环境中管理 cfc 的状态

标签 database session coldfusion session-state cfml

我一直在 cfml 应用程序中使用 session 范围来执行诸如存储当前登录的 user 对象之类的操作。太棒了!

user.isLoggedIn()user.hasPremiumAccess()user.hasRole('admin')

在尝试将我的应用程序迁移到集群(云)环境时,我意识到依赖 session 范围并不理想,因为正在运行的应用程序的每个实例都有自己的服务器内存。我知道我可以使用“粘性 session ”,但我不愿意,因为这会限制 Amazon Elastic Beanstalk 之类的东西自由地上下旋转应用程序实例(基于负载)。

我也知道我可以使用 client 范围以集群友好的方式存储简单值,但是对于复杂数据(例如我描述的用户对象)呢?您将如何存储用户对象,或者我可以使用什么其他方法?

我可以根据需要更改应用程序。

** 编辑 ** 需要明确的是,并不是我不能使用粘性 session ,而是我不想使用粘性 session (或 session 复制)。原因是这样我就可以充分利用不依赖服务器/实例内存来管理 session 状态的完全可扩展性优势。这种方法允许 Elastic Beanstalk 等服务自由创建和拆除应用程序服务器实例,而完全不会影响用户。使用粘性 session 不允许这样做。

我考虑过的一些可能的解决方案包括:

  1. 序列化/反序列化用户对象 user 的“状态”以存储在客户端范围中,并在每次页面加载时“重新初始化”用户
  2. 序列化/反序列化用户对象 user 的“状态”以存储在 NoSQL 数据库中,并在每次页面加载时“重新初始化”用户

最佳答案

如果您不/不能使用“粘性 session ”,那么另一个选择是实现 session 复制。这实际上是将内存中存储的 session 复制到集群中的每个节点。是的,这样做会产生开销。

来自文档:

To implement session failover for the server instances in a cluster, enable session replication for each server instance. Session replication coordinates session information in real time among the server instances in a cluster. Enabling session replication lets Tomcat automatically route a request to a running server if the current server is unavailable.

Note: When a cluster uses session replication, session data is copied to other servers in the cluster each time it is modified. This can degrade performance if you store a significant amount of information in session scope. If you plan to store a significant amount of information in session scope, consider storing this information in client variables saved in a database.

From - Enabling clustering for load balancing and failover

该页面下方提到的另一个警告:

If you are using session replication, go to the Memory Variables page and enable J2EE sessions. Enable J2EE sessions for all server instances in the cluster. If J2EE sessions are not enabled in the ColdFusion Administrator, session replication does not function properly.CFC serialization lets you use J2EE session replication in a cluster and have access to the CFCs in session data across all instances in the cluster. Session replication also ensures that that Session scope variables are replicated across the cluster. However, session replication does not support replication of arrays in Session scope CFCs or variables.You can also preserve and access data in a CFC in the case of session failover. ColdFusion structures stored inside the session scope are available in the session scope, even after failover. For example, if you are running multiple ColdFusion instances to balance server load, you can store useful data, including CFCs, inside the session so that you can access the data across all the pages that are served in that session.

并且还要检查不久前同一问题的答案(请注意,ColdFusion 10 的早期版本中存在一个错误,不允许 session 复制工作)- https://serverfault.com/a/602373/135433

关于database - 如何在集群环境中管理 cfc 的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35727867/

相关文章:

javascript - 使用 Node JS 将数据从 Excel 导入 MySql

session - 使用 wsadmin 列出事件 session

ajax - 使用 Coldfusion 的 CFFILE 标签监控来自 FFMpeg 的进度日志

c# - 数据未写入MYSQL数据库

Android 数据库 SQLite 自动增量不递增并且违反了主要约束

MySQL:根据多行数据合并多个相关表

laravel - 升级到 Laravel 5.2 会使所有 session 失效

ios - 解析无效 session token 错误1.8.2

file-upload - 如何防止 Coldfusion 中的多文件上传超时?

coldfusion - 使用 FindNoCase 查找 url 字符串