简体   繁体   English

针对memcached使用PHP-memcache进行会话故障转移

[英]Sessions Failover with PHP-memcache against memcached

Colleagues! 同事!

I'm running php 5.3 (5.3.8) with memcache (2.2.6) client library (http://pecl.php.net/package/memcache) to deal with memcached server. 我正在运行带有memcache(2.2.6)客户端库(http://pecl.php.net/package/memcache)的php 5.3(5.3.8)来处理memcached服务器。 My goal is to have failover solution for sessions engine, namely: 我的目标是为会话引擎提供故障转移解决方案,即:

  • Only native php sessions support (no custom handlers) 只有本机php会话支持(没有自定义处理程序)
  • Few memcached servers in the pool 池中很少有memcached服务器

What I expect is that in case if one of memcached servers is down, php will attempt to utilize the second server in the pool [will successfully connect it and become happy], however when first memcached server in the pool is down I'm receiving the following error: 我期望的是,如果其中一个memcached服务器关闭,php将尝试利用池中的第二台服务器[将成功连接并变得快乐],但是当池中的第一个memcached服务器关闭时,我正在接收以下错误:

Session start failed. Original message: session_start(): Server 10.0.10.111 (tcp 11211) failed with: Connection refused (111)

while relevant php settings are: 而相关的PHP设置是:

session.save_handler memcache
session.save_path tcp://10.0.10.111:11211?persistent=1&weight=1&timeout=1&retry_interval=10, tcp://10.0.10.110:11211?persistent=1&weight=1&timeout=1&retry_interval=10

and memcache settings (while I think that it's near to standard) are: 和memcache设置(虽然我认为它接近标准)是:

Directive   Local Value
memcache.allow_failover 1
memcache.chunk_size 8192
memcache.default_port   11211
memcache.default_timeout_ms 1000
memcache.hash_function  crc32
memcache.hash_strategy  standard
memcache.max_failover_attempts  20

Memcached still running on the second server and perfectly accessible from the WEB server: Memcached仍在第二台服务器上运行,可从WEB服务器完全访问:

telnet 10.0.10.110  11211
Trying 10.0.10.110...
Connected to 10.0.10.110 (10.0.10.110).
Escape character is '^]'.
get aaa
END
quit
Connection closed by foreign host.

So in other words, instead of querying all of the listed servers sequentially it crashes after unsuccessful attempt to connect the first server in the queue. 换句话说,不是按顺序查询所有列出的服务器,而是在尝试连接队列中的第一个服务器失败后崩溃。 Finally I do realize that there are releases of 3.0.x client library available, however it does not look too reliable for me as it still in beta version. 最后我意识到有3.0.x客户端库可用,但它对我来说看起来不太可靠,因为它仍处于测试版。

Please advice how can I get desired behavior with standard PHP, client lib and server. 请建议如何使用标准PHP,客户端lib和服务器获得所需的行为。

Thanks a lot! 非常感谢!

Best, Eugene 最好的,尤金

Use the Memcached extension. 使用Memcached扩展。 Note that there are two memcache plugins for PHP. 请注意,PHP有两个memcache插件。 One is called Memcache, the other is called Memcached. 一个叫Memcache,另一个叫Memcached。 Yes, that's confusing, but true anyway. 是的,这令人困惑,但无论如何都是真的。

The Memcache plugin supports those complex URL's you're using, with the protocol identifier (tcp) and the parameters (persistency and so on), while the Memcached plugin supports connection pools. Memcache插件支持您正在使用的复杂URL,协议标识符(tcp)和参数(持久性等),而Memcached插件支持连接池。

The documentation you're mentioning in the comments above ( http://www.php.net/manual/en/memcached.sessions.php ) is about the Memcached extension, not about Memcache. 您在上面的评论中提到的文档( http://www.php.net/manual/en/memcached.sessions.php )是关于Memcached扩展,而不是关于Memcache。

Update: Some interesting read: https://serverfault.com/questions/164350/can-a-pool-of-memcache-daemons-be-used-to-share-sessions-more-efficiently 更新:一些有趣的读物: https//serverfault.com/questions/164350/can-a-pool-of-memcache-daemons-be-used-to-share-sessions-more-efficiently

I would like to thank everybody who participated this question, the answer is the following: in reality memcache (not memcache d ) as session handler supports comma separated servers as the session.save_path , moreover it supports failover. 我要感谢参与这个问题的每个人,答案如下:实际上,memcache(不是memcache d )作为会话处理程序支持逗号分隔服务器作为session.save_path ,而且它支持故障转移。 The error mentioned above Session start failed. Original message: session_start(): Server 10.0.10.111 (tcp 11211) failed with: Connection refused (111) 上面提到的错误Session start failed. Original message: session_start(): Server 10.0.10.111 (tcp 11211) failed with: Connection refused (111) Session start failed. Original message: session_start(): Server 10.0.10.111 (tcp 11211) failed with: Connection refused (111) had only 8th (Notice) level. Session start failed. Original message: session_start(): Server 10.0.10.111 (tcp 11211) failed with: Connection refused (111)只有第8(通知)级别。 In fact engine just informs you about the fact that one of the servers is unavailable (which is logical, as otherwise how will you know?) and then successfully connects to the second server and using it. 事实上,引擎只是告诉你一个服务器不可用的事实(这是合乎逻辑的,否则你怎么知道?)然后成功连接到第二个服务器并使用它。

So all of the misunderstanding has been caused by weak documentation, memcache/memcached confusions and paranoid (E_ALL) settings of my custom error handler. 所以所有的误解都是由我的自定义错误处理程序的文档,memcache / memcached混乱和偏执(E_ALL)设置导致的。 In the meantime the issue has been resolved by ignoring notices referring to error Connection refused (111) in the session establishing context 与此同时,通过忽略在会话建立上下文中引用错误Connection refused (111)通知来解决该问题

You must change the hash strategy 您必须更改哈希策略

Change your config to 将配置更改为

memcache.hash_strategy  consistent

When you make the hash strategy to consistent memcache copies the data across multiple servers. 当您将哈希策略设置为一致的内存缓存时,会跨多个服务器复制数据。 If one of the servers is down, it retries to copy it on the next request. 如果其中一个服务器已关闭,它将重试在下一个请求中复制它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM