workerman onWorkerStart初始化redis连接疑问
这使用workerman的gateway框架的时候遇到的疑问
Events.php
<?php
/**
* This file is part of workerman.
*
* Licensed under The MIT License
* For full copyright and license information, please see the MIT-LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @author walkor<walkor@workerman.net>
* @copyright walkor<walkor@workerman.net>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
/**
* 用于检测业务代码死循环或者长时间阻塞等问题
* 如果发现业务卡死,可以将下面declare打开(去掉//注释),并执行php start.php reload
* 然后观察一段时间workerman.log看是否有process_timeout异常
*/
//declare(ticks=1);
use \GatewayWorker\Lib\Gateway;
use Workerman\Lib\Timer;
use \Server\Common;
use \Server\Cmd;
/**
* 主逻辑
* 主要是处理 onConnect onMessage onClose 三个方法
* onConnect 和 onClose 如果不需要可以不用实现并删除
*/
class Events
{
public static $redisCon = null ;
/**
* 当客户端连接时触发
* 如果业务不需此回调可以删除onConnect
*
* @param int $client_id 连接id
*/
public static function onConnect($client_id ) {
}
/**
* 当客户端发来消息时触发
* @param int $client_id 连接id
* @param mixed $message 具体消息
*/
public static function onMessage($client_id, $message) {
}
public static function onWorkerStart($businessWorker){
self::initRedis();
}
/**
* 当用户断开连接时触发
* @param int $client_id 连接id
*/
public static function onClose($client_id) {
echo "client_id is $client_id is close \n";
}
//redis的初始化
public static function initRedis(){
$redisConfig = require_once __DIR__ . "/Server/Config/Redis.config.php";
self::$redisCon = Server\Library\RedisDB::getInstance( $redisConfig , 0 );
}
}
RedisDB.php
<?php
namespace Server\Library ;
class RedisDB
{
static $_instance; //存储对象
public $handler;
private $config = null ;
public function __construct($redisConfig , $dbindex = 0){
if (!extension_loaded('redis')) {
throw new Exception("REDIS NOT SUPPORT", 1);
}
$this->handler = new \Redis();
//从配置读取
$this->handler->pconnect($redisConfig['hostname'], $redisConfig['port']);
$this->handler->auth($redisConfig['auth']);
}
public static function getInstance($redisConfig , $index = 0)
{
if (!isset(self::$_instance[$index]) or FALSE == (self::$_instance[$index] instanceof self)) {
self::$_instance[$index] = new self($redisConfig , $index);
}
return self::$_instance[$index];
}
}
start_businessworker.php
我开启了
$worker->count = 8;
疑问:
这初始化redis的连接的时候,我发现会new redis连接8次
id=1017 addr=192.168.2.188:56012 fd=9 name= age=659 idle=659 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=auth
id=1018 addr=192.168.2.188:56013 fd=10 name= age=659 idle=659 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=auth
id=1019 addr=192.168.2.188:56016 fd=11 name= age=659 idle=659 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=auth
id=1020 addr=192.168.2.188:56025 fd=12 name= age=659 idle=659 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=auth
id=1021 addr=192.168.2.188:56024 fd=13 name= age=659 idle=659 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=auth
id=1022 addr=192.168.2.188:56028 fd=14 name= age=659 idle=659 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=auth
id=1023 addr=192.168.2.188:56038 fd=15 name= age=659 idle=659 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=auth
id=1024 addr=192.168.2.188:56039 fd=16 name= age=659 idle=659 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 obl=0 oll=0 omem=0 events=r cmd=auth
但是我上面 初始化连接之后只是赋给一个类的静态变量。这样的话 那岂不是只有1个连接????
难道根据worker的id进行吗?
请指教
上面我redis连接使用了单例的模式!!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
官方描述
因为是8进程,各子进程内资源不共享。所以单例模式在此无效;解决方法:在父进程执行,传入子进程即可。
这个workerman与thinkPHP的实战项目视频不错,利用websocket协议实现了长连接,来做即时通讯,在线客服。地址 http://study.163.com/course/i...