agboon / yii2-redis-ha
基本的redis-ha客户端
1.5.1
2023-01-16 09:51 UTC
Requires
This package is auto-updated.
Last update: 2024-09-16 13:43:47 UTC
README
这个Yii2组件基于核心yii2-redis组件https://github.com/yiisoft/yii2-redis,但提供了与带哨兵的master-slave redis集群交互的能力。
相当简单——从给定的哨兵中获取主服务器地址并连接到它,然后就像\yii\redis\Connection一样操作。
它只与哨兵一起工作,不会连接没有哨兵的redis主机。
连接过程的工作流程
遍历给定的哨兵,寻找一个能够给出主服务器地址的活跃哨兵。
如果哨兵没有响应或者响应为空,那么我们会尝试下一个。
如果没有收到成功的回复,那么连接将失败。
找到redis主服务器后,我们将连接到它。
如果连接到主服务器失败,我们将不会尝试其他任何操作,我们将失败。
如果我们成功连接到主服务器,然后我们将使用role
命令检查它是否真的是主服务器(https://redis.ac.cn/topics/sentinel-clients)。
如果不是——我们将失败,如果是——我们成功连接。
建议的配置
Redis HA集群必须使用至少2个redis服务器和3个哨兵来完成。
我认为,考虑到这个实现,在本地为每个应用程序保留一个哨兵服务器在性能方面更好,以避免不必要的开销。
如果我们预先缓存了给定的主服务器,这不会很重要,但我们没有。我们每次在连接到redis之前都会询问哨兵。
这就是我看到的简单架构
------- -------
| | | |
| Redis | | Redis |
| | | |
------- \ /-------
| \ / |
| \ / |
| \-----------/ |
| |\ /| |
| | Sentinel | |
| | . . | |
| --.------.- |
| . . |
| . . |
-----|-------.--- -.----|----------
| | . | | . | |
| Sentinel. . .| . . .|. . Sentinel |
| | | | | |
| (localhost) | | (localhost) |
| | | | | |
| | | | | |
| APP | | APP |
| | | |
----------------- -----------------
使用方法
基本示例
'session' => [
'class' => '\yii\redis\Session',
'redis' => [
'class' => '\pyurin\yii\redisHa\Connection',
'masterName' => 'mymaster',
'sentinels' => [
'localhost'
]
]
],
哨兵服务器的查询顺序与数组中存在的顺序相同。
安装
通过composer可用
"require" : {
"pyurin/yii2-redis-ha":"*"
},