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

1、在GatewayWorker/Lib/Store.php大概在84行下添加一行代码:self::$instance[$config_name]->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[$config_name]))
{
ini_set('default_socket_timeout',-1);
self::$instance[$config_name] = new \GatewayWorker\Lib\StoreDriver\Redis();
$config = \Config\Store::$$config_name;
// 只选择第一个ip作为服务端
$address = current($config);
list($ip, $port) = explode(':', $address);
$timeout = 1;
self::$instance[$config_name]->connect($ip, $port, $timeout);
//redis认证
self::$instance[$config_name]->auth(\Config\Store::$redis_auth);
self::$instance[$config_name]->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
}else{
try{
self::$instance[$config_name]->ping();
}catch (\RedisException $e){
self::$instance[$config_name] = new \GatewayWorker\Lib\StoreDriver\Redis();
$config = \Config\Store::$$config_name;
// 只选择第一个ip作为服务端
$address = current($config);
list($ip, $port) = explode(':', $address);
$timeout = 1;
self::$instance[$config_name]->connect($ip, $port, $timeout);
self::$instance[$config_name]->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
}
}
return self::$instance[$config_name];
}


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/';
已邀请:

walkor

赞同来自:

感谢你的分享

该问题目前已经被锁定, 无法添加新回复