jamescauwelier / psredis
流行的 PHP redis 客户端 sentinel 客户端
Requires
- php: >=5.4
- predis/predis: ~1.0
Requires (Dev)
- phake/phake: @stable
- phpunit/phpunit: ~3.7
This package is not auto-updated.
Last update: 2024-09-14 17:53:37 UTC
README
注意:我已经不再积极维护此项目,并正在寻找另一位维护者来接替我。
PHP 客户端用于 redis sentinel 连接,作为其他 redis 客户端的包装器。这个名字代表 PHP Sentinel 客户端为 Redis。我相信其他人会有更具创造性的名字。
安装
最简单的方式是通过 composer 安装。该软件包可在 packagist 上找到,因此安装应该像在您的 composer 文件中添加以下内容一样简单
"require": {
"jamescauwelier/psredis": "~1.1"
},
使用方法
基本示例
最基本的示例默认使用 Predis 适配器。这是启动 PSRedis 所需的最少代码,尽管我们计划在将来使配置更加简洁。
// configure where to find the sentinel nodes $sentinel1 = new Client('192.168.50.40', '26379'); $sentinel2 = new Client('192.168.50.41', '26379'); $sentinel3 = new Client('192.168.50.30', '26379'); // now we can configure the master name and the sentinel nodes $masterDiscovery = new MasterDiscovery('integrationtests'); $masterDiscovery->addSentinel($sentinel1); $masterDiscovery->addSentinel($sentinel2); $masterDiscovery->addSentinel($sentinel3); // discover where the master is $master = $masterDiscovery->getMaster();
自动故障转移
您可以选择让库处理故障转移。这意味着在出现连接错误的情况下,库将根据哨兵的决定选择新的主节点或重新连接到现有的节点。在此重新连接之后,将重试命令,但仅重试一次。此处没有回退选项。
// configuration of $masterDiscovery $masterDiscovery = ... // using the $masterDiscovery as a dependency in an Highly Available Client (HAClient) $HAClient = new HAClient($masterDiscovery); $HAClient->set('test', 'ok'); $test = $HAClient->get('test');
自定义适配器
您可以选择使用哪种类型的客户端适配器,或者甚至编写自己的适配器。如果您编写自己的适配器,需要确保实现 \PSRedis\Client\ClientAdapter 接口。
// we need a factory to create the clients $clientFactory = new PredisClientCreator(); // we need an adapter for each sentinel client too! $clientAdapter = new PredisClientAdapter($clientFactory, Client::TYPE_SENTINEL); $sentinel1 = new Client('192.168.50.40', '26379', $clientAdapter); $clientAdapter = new PredisClientAdapter($clientFactory, Client::TYPE_SENTINEL); $sentinel2 = new Client('192.168.50.41', '26379', $clientAdapter); $clientAdapter = new PredisClientAdapter($clientFactory, Client::TYPE_SENTINEL); $sentinel3 = new Client('192.168.50.30', '26379', $clientAdapter); // now we can configure the master name and the sentinel nodes $masterDiscovery = new MasterDiscovery('integrationtests'); $masterDiscovery->addSentinel($sentinel1); $masterDiscovery->addSentinel($sentinel2); $masterDiscovery->addSentinel($sentinel3); // discover where the master is $master = $masterDiscovery->getMaster();
配置回退
当我们无法发现主节点的位置时,我们需要回退并再次尝试。回退机制是可配置的,并且您可以实现自己的回退策略,方法是实现 \PSRedis\Client\BackoffStrategy
以下是一个使用增量回退策略的示例
$sentinel = new Client('192.168.50.40', '26379'); $masterDiscovery = new MasterDiscovery('integrationtests'); $masterDiscovery->addSentinel($sentinel); // create a backoff strategy (half a second initially and increment with half of the backoff on each succesive try) $incrementalBackoff = new Incremental(500, 1.5); $incrementalBackoff->setMaxAttempts(10); // configure the master discovery with this backoff strategy $masterDiscovery->setBackoffStrategy($incrementalBackoff); // try to discover the master $master = $masterDiscovery->getMaster();
测试
注意:由于 PhpStorm 中的错误不允许我们从 IDE 运行单元测试,因此我们仍然使用 PHPUnit 3.7 进行测试。请参阅 http://youtrack.jetbrains.com/issue/WI-21666
单元测试
我们使用 PHPUnit 进行单元测试,并使用 Phake 进行模拟。两者都通过 composer 安装。可以使用以下命令运行单元测试
./vendor/bin/phpunit -c ./phpunit.xml --bootstrap ./tests/bootstrap.php
集成测试
先决条件
在运行测试之前,请确保已在本机上安装以下软件
运行测试
在运行测试之前,您需要设置虚拟机以运行测试。使用以下命令启动虚拟机
vagrant up
之后,使用以下命令运行集成测试
./vendor/bin/phpunit -c ./phpunit.int.xml --bootstrap ./tests/bootstrap.int.php
您将看到一些需要修复的警告,但测试本身应该全部通过。警告是每次集成测试之前重置环境的结果。