protected function getSlaveConnection($server_hosts = [])
    {
        $db_config = $this->containers['config']->get('mysql');
        $slave_config = $db_config['slaves'];
        if (!$server_hosts) {
            foreach ($slave_config as $key => $config) {
                $server_hosts[$key] = $config['host'];
            }
        }
        if ($server_hosts) {
            // 一致性HASH
            $target_host = LoadBalancer::getTargetHost($server_hosts);
            foreach ($server_hosts as $key => $server_host) {
                if ($server_host == $target_host) {
                    $slave_target_num = $key;
                    $target_slave_config = $slave_config[$slave_target_num];
                    $this->_slave_db = $target_slave_config['dbname'] ?? '';
                    $this->_slave_host = $target_slave_config['host'] ?? '';
                    $this->_slave_username = $target_slave_config['username'] ?? '';
                    $this->_slave_password = $target_slave_config['password'] ?? '';
                    $this->_slave_options = $target_slave_config['options'] ?? [];
                    $this->getDsn(self::CONN_TYPE_SLAVE);
                    try {
                        $this->getConnection(self::CONN_TYPE_SLAVE);
                    } catch (\PDOException $e) {
                        unset($server_hosts[$slave_target_num]);
                        $this->getSlaveConnection($server_hosts);
                    }
                    break;
                }
            }
        }
    }
protected function getSlaveConnection($server_hosts = [])
    {
        $db_config = $this->containers['config']->get('mysql');
        $slave_config = $db_config['slaves'];
        if (!$server_hosts) {
            foreach ($slave_config as $key => $config) {
                $server_hosts[$key] = $config['host'];
            }
        }
        if ($server_hosts) {
            // 一致性HASH
            $target_host = LoadBalancer::getTargetHost($server_hosts);
            foreach ($server_hosts as $key => $server_host) {
                if ($server_host == $target_host) {
                    $slave_target_num = $key;
                    $target_slave_config = $slave_config[$slave_target_num];
                    $this->_slave_db = $target_slave_config['dbname'] ?? '';
                    $this->_slave_host = $target_slave_config['host'] ?? '';
                    $this->_slave_username = $target_slave_config['username'] ?? '';
                    $this->_slave_password = $target_slave_config['password'] ?? '';
                    $this->_slave_options = $target_slave_config['options'] ?? [];
                    $this->getDsn(self::CONN_TYPE_SLAVE);
                    try {
                        $this->getConnection(self::CONN_TYPE_SLAVE);
                    } catch (\PDOException $e) {
                        unset($server_hosts[$slave_target_num]);
                        $this->getSlaveConnection($server_hosts);
                    }
                    break;
                }
            }
        }
    }
protected function getSlaveConnection ( $ server_hosts = [ ] ) { $ db_config = $ this -> containers [ 'config' ] -> get ( 'mysql' ) ; $ slave_config = $ db_config [ 'slaves' ] ; if ( ! $ server_hosts ) { foreach ( $ slave_config as $ key => $ config ) { $ server_hosts [ $ key ] = $ config [ 'host' ] ; } } if ( $ server_hosts ) { // 一致性HASH $ target_host = LoadBalancer :: getTargetHost ( $ server_hosts ) ; foreach ( $ server_hosts as $ key => $ server_host ) { if ( $ server_host == $ target_host ) { $ slave_target_num = $ key ; $ target_slave_config = $ slave_config [ $ slave_target_num ] ; $ this -> _slave_db = $ target_slave_config [ 'dbname' ] ?? '' ; $ this -> _slave_host = $ target_slave_config [ 'host' ] ?? '' ; $ this -> _slave_username = $ target_slave_config [ 'username' ] ?? '' ; $ this -> _slave_password = $ target_slave_config [ 'password' ] ?? '' ; $ this -> _slave_options = $ target_slave_config [ 'options' ] ?? [ ] ; $ this -> getDsn ( self :: CONN_TYPE_SLAVE ) ; try { $ this -> getConnection ( self :: CONN_TYPE_SLAVE ) ; } catch ( \ PDOException $ e ) { unset ( $ server_hosts [ $ slave_target_num ] ) ; $ this -> getSlaveConnection ( $ server_hosts ) ; } break ; } } } }
Establish slave node mysql connection

@param array $server_hosts
Establish slave node mysql connection