hyperf自定义连接池创建redis哨兵 1

23 篇文章 0 订阅

这里是用predis写的 , 所以这里需要安装predis composer包

这里哨兵会自动分配读写的

 首先在app \pool 文件夹中创建了这两个文件

其中myConnectionPool是连接池 , RedisIn是创建redis连接

MyConnectionPool.php

<?php
namespace App\Pool;

use Hyperf\Contract\ConnectionInterface;
use Hyperf\Pool\Pool;

class MyConnectionPool extends Pool
{
    public function createConnection(): ConnectionInterface
    {
        return new RedisIn();
    }
}

 RedisIn.php

<?
namespace App\Pool;

use Hyperf\Contract\ConnectionInterface;
use Hyperf\Pool\Connection as BaseConnection;


class RedisIn extends \Predis\Client implements ConnectionInterface
{
    use BaseConnection;
    public function getClient()
    {
       $sentinels = [
            'tcp://114.128.31.197:26379' // 哨兵地址 , 可以配置多个
        ];
        $options = [
            'replication' => 'sentinel',
            'service' => 'sentry_redis', // master-name
            'parameters' => [
                'password' => 'ygyc',    // 主机从机的连接密码
                'database' => 10,        // redis选择库
            ],
        ];
        $client = new \Predis\Client($sentinels, $options);
        return $client;
    }

    public function reconnect(): bool
    {
        return true;
        // TODO: Implement reconnect() method.
    }

    public function check(): bool
    {
        return true;
        // TODO: Implement check() method.
    }

    public function close(): bool
    {
        return true;
        // TODO: Implement close() method.
    }

    public function release(): void
    {
        // TODO: Implement release() method.
    }
}

使用方法

<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;
use Hyperf\Utils\ApplicationContext;

class IndexController extends AbstractController
{
    public function index()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();
        
        $container = ApplicationContext::getContainer();
        $redisPool = $container->get(\App\Pool\MyConnectionPool::class);
      
        $conn = $redisPool->get();
        $redis = $conn->getClient();
        
        
        echo $redis->set('as',1);
        $as =  $redis->get('as');
        
        $conn->release();
        return [
            'method' => $method,
            'message' => "Hello {$user}.",
            'redis' => $as,
        ];
    }
}

这里我不清楚进程池中有多少进程 , 会不会在协程环境有冲突 , 于是我正在改写2.0版本 , 基于hyperf自有的redis连接池进行测试和改动 , 谁让他们这个不好使呢😒

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苗先生的PHP记录

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值