{"id":12,"date":"2014-07-21T11:42:40","date_gmt":"2014-07-21T03:42:40","guid":{"rendered":"http:\/\/www.codeeel.com\/?p=12"},"modified":"2014-07-23T13:25:04","modified_gmt":"2014-07-23T05:25:04","slug":"yii%e6%a1%86%e6%9e%b6%e6%95%b0%e6%8d%ae%e5%ba%93%e5%a4%9a%e6%95%b0%e6%8d%ae%e5%ba%93%e3%80%81%e4%b8%bb%e4%bb%8e%e3%80%81%e8%af%bb%e5%86%99%e5%88%86%e7%a6%bb","status":"publish","type":"post","link":"https:\/\/www.codeeel.com\/?p=12","title":{"rendered":"Yii\u6846\u67b6\u6570\u636e\u5e93\u591a\u6570\u636e\u5e93\u3001\u4e3b\u4ece\u3001\u8bfb\u5199\u5206\u79bb"},"content":{"rendered":"<p>Yii\u6846\u67b6\u6570\u636e\u5e93\u591a\u6570\u636e\u5e93\u3001\u4e3b\u4ece\u3001\u8bfb\u5199\u5206\u79bb \u5b9e\u73b0<\/p>\n<div><\/div>\n<div>\u529f\u80fd\u63cf\u8ff0\uff1a<\/div>\n<div>1.\u5b9e\u73b0\u4e3b\u4ece\u6570\u636e\u5e93\u8bfb\u5199\u5206\u79bb \u4e3b\u5e93\uff1a\u5199 \u4ece\u5e93(\u53ef\u591a\u4e2a)\uff1a\u8bfb<\/div>\n<div>2.\u4e3b\u6570\u636e\u5e93\u65e0\u6cd5\u8fde\u63a5\u65f6 \u53ef\u8bbe\u7f6e\u4ece\u6570\u636e\u5e93\u662f\u5426 \u53ef\u5199<\/div>\n<div>3.\u6240\u6709\u4ece\u6570\u636e\u5e93\u65e0\u6cd5\u8fde\u63a5\u65f6 \u53ef\u8bbe\u7f6e\u4e3b\u6570\u636e\u5e93\u662f\u5426 \u53ef\u8bfb<\/div>\n<div>4.\u5982\u679c\u4ece\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25 \u53ef\u8bbe\u7f6eN\u79d2\u5185\u4e0d\u518d\u8fde\u63a5<\/div>\n<div><\/div>\n<div>\n<pre class=\"lang:default decode:true \" title=\"\u6570\u636e\u5e93\u4e3b\u4ece\u5206\u914d\u7c7b\">\/**\r\n * \u4e3b\u6570\u636e\u5e93 \u5199 \u4ece\u6570\u636e\u5e93\uff08\u53ef\u591a\u4e2a\uff09\u8bfb\r\n * \u5b9e\u73b0\u4e3b\u4ece\u6570\u636e\u5e93 \u8bfb\u5199\u5206\u79bb \u4e3b\u670d\u52a1\u5668\u65e0\u6cd5\u8fde\u63a5 \u4ece\u670d\u52a1\u5668\u53ef\u5207\u6362\u5199\u529f\u80fd\r\n * \u4ece\u52a1\u5668\u65e0\u6cd5\u8fde\u63a5 \u4e3b\u670d\u52a1\u5668\u53ef\u5207\u6362\u8bfb\u529f\r\n * *\/\r\nclass DbConnectionMan extends CDbConnection {\r\n\r\n    public $timeout = 10; \/\/\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\r\n    public $markDeadSeconds = 600; \/\/\u5982\u679c\u4ece\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25 600\u79d2\u5185\u4e0d\u518d\u8fde\u63a5 \r\n    \/\/\u7528 cache \u4f5c\u4e3a\u7f13\u5b58\u5168\u5c40\u6807\u8bb0\r\n    public $cacheID = 'cache';\r\n\r\n    \/**\r\n     * @var array $slaves.Slave database connection(Read) config array.\r\n     * \u914d\u7f6e\u7b26\u5408 CDbConnection.\r\n     * @example\r\n     * 'components'=&gt;array(\r\n     * \t\t'db'=&gt;array(\r\n     * \t\t\t'connectionString'=&gt;'mysql:\/\/&lt;master&gt;',\r\n     * \t\t\t'slaves'=&gt;array(\r\n     * \t\t\t\tarray('connectionString'=&gt;'mysql:\/\/&lt;slave01&gt;'),\r\n     * \t\t\t\tarray('connectionString'=&gt;'mysql:\/\/&lt;slave02&gt;'),\r\n     * \t\t\t)\r\n     * \t\t)\r\n     * )\r\n     * *\/\r\n    public $slaves = array();\r\n\r\n    \/**\r\n     * \r\n     * \u4ece\u6570\u636e\u5e93\u72b6\u6001 false \u5219\u53ea\u7528\u4e3b\u6570\u636e\u5e93\r\n     * @var bool $enableSlave\r\n     * *\/\r\n    public $enableSlave = true;\r\n\r\n    \/**\r\n     * @var slavesWrite \u7d27\u6025\u60c5\u51b5\u4e3b\u6570\u636e\u5e93\u65e0\u6cd5\u8fde\u63a5 \u5207\u6362\u4ece\u670d\u52a1\u5668\uff08\u8bfb\u5199\uff09.\r\n     *\/\r\n    public $slavesWrite = false;\r\n\r\n    \/**\r\n     * @var masterRead \u7d27\u6025\u60c5\u51b5\u4ece\u4e3b\u6570\u636e\u5e93\u65e0\u6cd5\u8fde\u63a5 \u5207\u6362\u4ece\u4f4f\u670d\u52a1\u5668\uff08\u8bfb\u5199\uff09.\r\n     *\/\r\n    public $masterRead = false;\r\n\r\n    \/**\r\n     * @var _slave\r\n     *\/\r\n    private $_slave;\r\n\r\n    \/**\r\n     * @var _disableWrite \u4ece\u670d\u52a1\u5668\uff08\u53ea\u8bfb\uff09.\r\n     *\/\r\n    private $_disableWrite = true;\r\n\r\n    \/**\r\n     *\r\n     * \u91cd\u5199 createCommand \u65b9\u6cd5,1.\u5f00\u542f\u4ece\u5e93 2.\u5b58\u5728\u4ece\u5e93 3.\u5f53\u524d\u4e0d\u5904\u4e8e\u4e00\u4e2a\u4e8b\u52a1\u4e2d 4.\u4ece\u5e93\u8bfb\u6570\u636e\r\n     * @param string $sql\r\n     * @return CDbCommand\r\n     * *\/\r\n    public function createCommand($sql = null) {\r\n        if ($this-&gt;enableSlave &amp;&amp; !empty($this-&gt;slaves) &amp;&amp; is_string($sql) &amp;&amp; !$this-&gt;getCurrentTransaction() &amp;&amp; self::isReadOperation($sql) &amp;&amp; ($slave = $this-&gt;getSlave())\r\n        ) {\r\n            return $slave-&gt;createCommand($sql);\r\n        } else {\r\n            if (!$this-&gt;masterRead) {\r\n                if ($this-&gt;_disableWrite &amp;&amp; !self::isReadOperation($sql)) {\r\n\r\n                    throw new CDbException(\"Master db server is not available now!Disallow write operation on slave server!\");\r\n                }\r\n            }\r\n            return parent::createCommand($sql);\r\n        }\r\n    }\r\n\r\n    \/**\r\n     * \u83b7\u5f97\u4ece\u670d\u52a1\u5668\u8fde\u63a5\u8d44\u6e90\r\n     * @return CDbConnection\r\n     * *\/\r\n    public function getSlave() {\r\n\r\n        if (!isset($this-&gt;_slave)) {\r\n\r\n            shuffle($this-&gt;slaves);\r\n            foreach ($this-&gt;slaves as $slaveConfig) {\r\n\r\n                if ($this-&gt;_isDeadServer($slaveConfig['connectionString'])) {\r\n                    continue;\r\n                }\r\n                if (!isset($slaveConfig['class']))\r\n                    $slaveConfig['class'] = 'CDbConnection';\r\n\r\n                $slaveConfig['autoConnect'] = false;\r\n                try {\r\n                    if ($slave = Yii::createComponent($slaveConfig)) {\r\n                        Yii::app()-&gt;setComponent('dbslave', $slave);\r\n                        $slave-&gt;setAttribute(PDO::ATTR_TIMEOUT, $this-&gt;timeout);\r\n                        $slave-&gt;setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);\r\n                        $slave-&gt;setActive(true);\r\n                        $this-&gt;_slave = $slave;\r\n                        break;\r\n                    }\r\n                } catch (Exception $e) {\r\n                    $this-&gt;_markDeadServer($slaveConfig['connectionString']);\r\n                    Yii::log(\"Slave database connection failed!\\n\\tConnection string:{$slaveConfig['connectionString']}\", 'warning');\r\n\r\n                    continue;\r\n                }\r\n            }\r\n\r\n            if (!isset($this-&gt;_slave)) {\r\n                $this-&gt;_slave = null;\r\n                $this-&gt;enableSlave = false;\r\n            }\r\n        }\r\n        return $this-&gt;_slave;\r\n    }\r\n\r\n    public function setActive($value) {\r\n        if ($value != $this-&gt;getActive()) {\r\n            if ($value) {\r\n                try {\r\n                    if ($this-&gt;_isDeadServer($this-&gt;connectionString)) {\r\n                        throw new CDbException('Master db server is already dead!');\r\n                    }\r\n                    \/\/PDO::ATTR_TIMEOUT must set before pdo instance create\r\n                    $this-&gt;setAttribute(PDO::ATTR_TIMEOUT, $this-&gt;timeout);\r\n                    $this-&gt;open();\r\n                } catch (Exception $e) {\r\n                    $this-&gt;_markDeadServer($this-&gt;connectionString);\r\n                    $slave = $this-&gt;getSlave();\r\n                    Yii::log($e-&gt;getMessage(), CLogger::LEVEL_ERROR, 'exception.CDbException');\r\n                    if ($slave) {\r\n                        $this-&gt;connectionString = $slave-&gt;connectionString;\r\n                        $this-&gt;username = $slave-&gt;username;\r\n                        $this-&gt;password = $slave-&gt;password;\r\n                        if ($this-&gt;slavesWrite) {\r\n                            $this-&gt;_disableWrite = false;\r\n                        }\r\n                        $this-&gt;open();\r\n                    } else { \/\/Slave also unavailable\r\n                        if ($this-&gt;masterRead) {\r\n                            $this-&gt;connectionString = $this-&gt;connectionString;\r\n                            $this-&gt;username = $this-&gt;username;\r\n                            $this-&gt;password = $this-&gt;password;\r\n                            $this-&gt;open();\r\n                        } else {\r\n                            throw new CDbException(Yii::t('yii', 'CDbConnection failed to open the DB connection.'), (int) $e-&gt;getCode(), $e-&gt;errorInfo);\r\n                        }\r\n                    }\r\n                }\r\n            } else {\r\n                $this-&gt;close();\r\n            }\r\n        }\r\n    }\r\n\r\n    \/**\r\n     * \u68c0\u6d4b\u8bfb\u64cd\u4f5c sql \u8bed\u53e5\r\n     * \r\n     * \u5173\u952e\u5b57\uff1a SELECT,DECRIBE,SHOW ...\r\n     * \u5199\u64cd\u4f5c:UPDATE,INSERT,DELETE ...\r\n     * *\/\r\n    public static function isReadOperation($sql) {\r\n        $sql = substr(ltrim($sql), 0, 10);\r\n        $sql = str_ireplace(array('SELECT', 'SHOW', 'DESCRIBE', 'PRAGMA'), '^O^', $sql); \/\/^O^,magic smile\r\n        return strpos($sql, '^O^') === 0;\r\n    }\r\n\r\n    \/**\r\n     * \u68c0\u6d4b\u4ece\u670d\u52a1\u5668\u662f\u5426\u88ab\u6807\u8bb0 \u5931\u8d25.\r\n     *\/\r\n    private function _isDeadServer($c) {\r\n        $cache = Yii::app()-&gt;{$this-&gt;cacheID};\r\n        if ($cache &amp;&amp; $cache-&gt;get('DeadServer::' . $c) == 1) {\r\n            return true;\r\n        }\r\n        return false;\r\n    }\r\n\r\n    \/**\r\n     * \u6807\u8bb0\u5931\u8d25\u7684slaves.\r\n     *\/\r\n    private function _markDeadServer($c) {\r\n        $cache = Yii::app()-&gt;{$this-&gt;cacheID};\r\n        if ($cache) {\r\n            $cache-&gt;set('DeadServer::' . $c, 1, $this-&gt;markDeadSeconds);\r\n        }\r\n    }\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<p>main.php\u914d\u7f6e\uff1acomponents \u6570\u7ec4\u4e2d<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \" title=\"\u914d\u7f6e\u6587\u4ef6\u8bbe\u7f6e\">'db'=&gt;array(\r\n       'class'=&gt;'application.extensions.DbConnectionMan',\/\/\u6269\u5c55\u8def\u5f84\r\n       'connectionString' =&gt; 'mysql:host=192.168.1.128;dbname=db_xcpt',\/\/\u4e3b\u6570\u636e\u5e93 \u5199\r\n       'emulatePrepare' =&gt; true,\r\n       'username' =&gt; 'root',\r\n       'password' =&gt; 'root',\r\n       'charset' =&gt; 'utf8',\r\n       'tablePrefix' =&gt; 'xcpt_', \/\/\u8868\u524d\u7f00\r\n       'enableSlave'=&gt;true,\/\/\u4ece\u6570\u636e\u5e93\u542f\u7528\r\n\t 'slavesWrite'=&gt;true,\/\/\u7d27\u6025\u60c5\u51b5 \u4e3b\u6570\u636e\u5e93\u65e0\u6cd5\u8fde\u63a5 \u542f\u7528\u4ece\u6570\u636e\u5e93 \u5199\u529f\u80fd\r\n\t  'masterRead'=&gt;true,\/\/\u7d27\u6025\u60c5\u51b5 \u4ece\u6570\u636e\u5e93\u65e0\u6cd5\u8fde\u63a5 \u542f\u7528\u4e3b\u6570\u636e\u5e93 \u8bfb\u529f\u80fd\r\n       'slaves'=&gt;array(\/\/\u4ece\u6570\u636e\u5e93\r\n           array(   \/\/slave1\r\n               'connectionString'=&gt;'mysql:host=localhost;dbname=db_kuitao8.com',\r\n               'emulatePrepare' =&gt; true,\r\n               'username'=&gt;'root',\r\n               'password'=&gt;'root',\r\n               'charset' =&gt; 'utf8',\r\n               'tablePrefix' =&gt; 'xcpt_', \/\/\u8868\u524d\u7f00\r\n           ),\r\n\t array(   \/\/slave2\r\n               'connectionString'=&gt;'mysql:host=localhost;dbname=db_kuitao8.com',\r\n               'emulatePrepare' =&gt; true,\r\n               'username'=&gt;'root',\r\n               'password'=&gt;'root',\r\n               'charset' =&gt; 'utf8',\r\n               'tablePrefix' =&gt; 'xcpt_', \/\/\u8868\u524d\u7f00\r\n           ),\r\n\r\n       ),\r\n   ),<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yii\u6846\u67b6\u6570\u636e\u5e93\u591a\u6570\u636e\u5e93\u3001\u4e3b\u4ece\u3001\u8bfb\u5199\u5206\u79bb \u5b9e\u73b0 \u529f\u80fd\u63cf\u8ff0\uff1a 1.\u5b9e\u73b0\u4e3b\u4ece\u6570\u636e\u5e93\u8bfb\u5199\u5206\u79bb \u4e3b\u5e93\uff1a\u5199 \u4ece\u5e93(\u53ef\u591a\u4e2a)\uff1a\u8bfb 2.\u4e3b\u6570\u636e\u5e93\u65e0\u6cd5\u8fde\u63a5\u65f6 \u53ef\u8bbe\u7f6e\u4ece\u6570\u636e\u5e93\u662f\u5426 \u53ef\u5199 3.\u6240\u6709\u4ece\u6570\u636e\u5e93\u65e0\u6cd5\u8fde\u63a5\u65f6 \u53ef\u8bbe\u7f6e\u4e3b\u6570\u636e\u5e93\u662f\u5426 \u53ef\u8bfb 4.\u5982\u679c\u4ece\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25 \u53ef\u8bbe\u7f6eN\u79d2\u5185\u4e0d\u518d\u8fde\u63a5 \/** * \u4e3b\u6570\u636e\u5e93 \u5199 \u4ece\u6570\u636e\u5e93\uff08\u53ef\u591a\u4e2a\uff09\u8bfb *<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[30,29,31,32],"_links":{"self":[{"href":"https:\/\/www.codeeel.com\/index.php?rest_route=\/wp\/v2\/posts\/12"}],"collection":[{"href":"https:\/\/www.codeeel.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codeeel.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codeeel.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codeeel.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12"}],"version-history":[{"count":21,"href":"https:\/\/www.codeeel.com\/index.php?rest_route=\/wp\/v2\/posts\/12\/revisions"}],"predecessor-version":[{"id":33,"href":"https:\/\/www.codeeel.com\/index.php?rest_route=\/wp\/v2\/posts\/12\/revisions\/33"}],"wp:attachment":[{"href":"https:\/\/www.codeeel.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codeeel.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codeeel.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}