负载平衡集群中的 PHP 会话 - 如何? [英] PHP sessions in a load balancing cluster - how?

查看:27
本文介绍了负载平衡集群中的 PHP 会话 - 如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我有一个完全罕见的负载平衡 PHP 网站的独特场景.令人失望的是 - 它过去没有进行负载平衡.现在我们开始遇到问题了...

OK, so I've got this totally rare an unique scenario of a load balanced PHP website. The bummer is - it didn't used to be load balanced. Now we're starting to get issues...

目前唯一的问题是 PHP 会话.一开始自然没有人想到这个问题,所以 PHP 会话配置保留为默认值.因此,两台服务器都有自己的一小部分会话文件,而将下一个请求扔给另一台服务器的用户是不幸的,因为那没有他在第一个服务器上创建的会话.

Currently the only issue is with PHP sessions. Naturally nobody thought of this issue at first so the PHP session configuration was left at its defaults. Thus both servers have their own little stash of session files, and woe is the user who gets the next request thrown to the other server, because that doesn't have the session he created on the first one.

现在,我一直在阅读有关如何解决这种情况的 PHP 手册.在那里我发现了 session_set_save_handler() 的好功能.(而且,巧合的是,

Now, I've been reading PHP manual on how to solve this situation. There I found the nice function of session_set_save_handler(). (And, coincidentally, this topic on SO) Neat. Except I'll have to call this function in all the pages of the website. And developers of future pages would have to remember to call it all the time as well. Feels kinda clumsy, not to mention probably violating a dozen best coding practices. It would be much nicer if I could just flip some global configuration option and Voilà - the sessions all get magically stored in a DB or a memory cache or something.

关于如何做到这一点的任何想法?<小时>补充:澄清 - 我希望这是标准解决方案的标准情况.仅供参考 - 我有一个可用的 MySQL 数据库.肯定有一些现成的代码可以解决这个问题吗?当然,我可以编写自己的会话保存内容和 Greg 看起来很有前途 - 但这感觉就像重新发明轮子.:P<小时>添加 2: 负载平衡基于 DNS.我不确定这是如何工作的,但我想它应该类似于 这个.<小时>添加 3: 好的,我看到一种解决方案是使用 auto_prepend 选项在每个脚本中插入对 session_set_save_handler() 的调用并编写我的自己的数据库持久器,也许会调用 memcached 以获得更好的性能.很公平.

Any ideas on how to do this?


Added: To clarify - I expect this to be a standard situation with a standard solution. FYI - I have a MySQL DB available. Surely there must be some ready-to-use code out there that solves this? I can, of course, write my own session saving stuff and auto_prepend option pointed out by Greg seems promising - but that would feel like reinventing the wheel. :P
Added 2: The load balancing is DNS based. I'm not sure how this works, but I guess it should be something like this.
Added 3: OK, I see that one solution is to use auto_prepend option to insert a call to session_set_save_handler() in every script and write my own DB persister, perhaps throwing in calls to memcached for better performance. Fair enough.

还有什么方法可以避免我自己编写所有这些代码?像一些著名且经过良好测试的 PHP 插件?

Is there also some way that I could avoid coding all this myself? Like some famous and well-tested PHP plugin?

很晚才添加:这是我最后的方式:PHP+MySQL中如何正确实现自定义会话持久器?

此外,我只是在所有页面中手动包含了会话处理程序.

Also, I simply included the session handler manually in all pages.

推荐答案

您可以设置 PHP 来处理数据库中的会话,因此您的所有服务器共享相同的会话信息,因为所有服务器为此使用相同的数据库.

You could set PHP to handle the sessions in the database, so all your servers share same session information as all servers use the same database for that.

可以在此处找到.

这篇关于负载平衡集群中的 PHP 会话 - 如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆