账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    workerman onWorkerStart初始化redis连接疑问
    116
    0

    这使用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连接使用了单例的模式!!!!

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 旧⊙人往事 普通会员 1楼

      在Workerman框架中,WorkerStart方法是Worker进程在启动时调用的,用于初始化Redis连接。以下是一个基本的示例,说明在WorkerStart方法中初始化Redis连接的步骤:

      1. 配置Redis连接参数: 在Workerman的配置文件(通常是worker.confworker.php)中,添加以下Redis连接参数:

      php 'redis' => [ 'host' => '127.0.0.1', 'port' => 6379, 'db' => '0', 'password' => 'your_password', 'persistent' => true, ],

      其中:

      • host:Redis服务器的IP地址或主机名。
      • port:Redis服务器的端口号,默认为6379。
      • db:Redis数据库的名称,默认为0。
      • password:Redis服务器的密码,如果使用的是密码认证,需要设置此参数。
      • persistent:如果为true,表示Redis连接是持久化的,即Redis服务器会保存连接信息,当Worker进程停止后,Redis连接将不会自动断开,需要手动关闭。

      注意:请替换your_password为实际的Redis密码。

      1. 初始化Redis连接: 在WorkerStart方法中,使用Redis::connect()函数初始化Redis连接。这个函数需要在Worker进程中调用,例如:

      php public function WorkerStart() { $redis = Redis::connect('127.0.0.1', 6379, 'your_password'); // Redis连接已成功初始化 }

      这个函数通过调用Redis::connect()函数并传入服务器地址、端口号、密码和持久化参数(如果使用了persistent参数)来初始化Redis连接。

      1. 处理Redis连接错误: 在WorkerStart方法中,如果Redis连接初始化失败,可以处理可能出现的错误,例如Redis::connect()函数无法连接到服务器或Redis服务器已经关闭。例如:

      ```php public function WorkerStart() { if ($redis === null) { throw new WorkerException('Failed to initialize Redis connection.'); }

         // 处理Redis连接错误
      

      } ```

      这个方法首先检查Redis连接是否初始化成功,如果连接失败,抛出一个WorkerException异常,并说明失败的原因。

      1. 其他可能的WorkerStart方法调用: 在实际使用中,WorkerStart方法通常会与其他Worker进程的启动方法(如WorkerStartWorker)一起使用,确保在启动Worker进程之前已经成功初始化了Redis连接。例如:

      ```php public function WorkerStartWorker() { // 初始化Redis连接 $redis = Redis::connect('127.0.0.1', 6379, 'your_password');

         // 验证Redis连接
         if ($redis === null) {
             throw new WorkerException('Failed to initialize Redis connection.');
         }
      
         // ...其他WorkerStartWorker方法调用
      

      } ```

      这个方法在启动Worker进程之前,首先初始化Redis连接,然后验证连接是否成功。如果连接失败,抛出一个WorkerException异常,并说明失败的原因。

      通过上述步骤,你在WorkerStart方法中可以初始化Redis连接,并确保在Worker进程中正确地连接到Redis服务器。注意,Redis连接的初始化可能会受到Redis服务器性能、配置文件、Worker进程配置等多种因素的影响,因此在实际使用中可能需要根据具体情况进行调整和优化。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部