[分享]两行代码实现GatewayWorker支持redis认证

294194253

1、在GatewayWorker/Lib/Store.php大概在84行下添加一行代码:self::$instance->auth(\Config\Store::$redis_auth);
2、在Applications/YourApp/Config/Store.php大概在52行下添加一行代码:public static $redis_auth = "";

具体代码如下:

GatewayWorker/Lib/Store.php

 // redis 驱动
        elseif(\Config\Store::$driver == \Config\Store::DRIVER_REDIS)
        {
            if(!isset(\Config\Store::$$config_name))
            {
                echo "\\Config\\Store::$config_name not set\n";
                throw new \Exception("\\Config\\Store::$config_name not set\n");
            }
            if(!isset(self::$instance))
            {
                ini_set('default_socket_timeout',-1);
                self::$instance = new \GatewayWorker\Lib\StoreDriver\Redis();
                $config = \Config\Store::$$config_name;
                // 只选择第一个ip作为服务端
                $address = current($config);
                list($ip, $port) = explode(':', $address);
                $timeout = 1;
                self::$instance->connect($ip, $port, $timeout);
                //redis认证
                self::$instance->auth(\Config\Store::$redis_auth);
                self::$instance->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
            }else{
                try{
                    self::$instance->ping();
                }catch (\RedisException $e){
                    self::$instance = new \GatewayWorker\Lib\StoreDriver\Redis();
                    $config = \Config\Store::$$config_name;
                    // 只选择第一个ip作为服务端
                    $address = current($config);
                    list($ip, $port) = explode(':', $address);
                    $timeout = 1;
                    self::$instance->connect($ip, $port, $timeout);
                    self::$instance->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
                }
            }
            return self::$instance;
        }

Applications/YourApp/Config/Store.php

class Store
{
    // 使用文件存储,注意使用文件存储无法支持workerman分布式部署
    const DRIVER_FILE = 1;
    // 使用memcache存储,支持workerman分布式部署
    const DRIVER_MC = 2;
    // 使用redis存储(推荐),支持workerman分布式部署
    const DRIVER_REDIS = 3;

     // DRIVER_FILE 或者 DRIVER_MC 或者 DRIVER_REDIS(推荐)
    public static $driver = self::DRIVER_REDIS;
    //reids的auth
    public static $redis_auth = "";

    //$driver为DRIVER_MC/DRIVER_REDIS时需要配置memcached/redis服务端ip和端口
    public static $gateway = array(
        '127.0.0.1:6379',
    );

    // $driver为DRIVER_FILE时要配置此项,实际配置在最下面一行
    public static $storePath = '';
}
// 实际在这里配置的Store::$storePath ,默认为临时目录
Store::$storePath = sys_get_temp_dir().'/workerman-your-app/';
4562 1 0
1个回答

walkor 打赏

感谢你的分享

  • 暂无评论
年代过于久远,无法发表回答
×
🔝