ÔÚ¶à¸öjobs serverʱ£¬PHPµÄgearmanÀ©Õ¹ÔÚjobs server¶Ë¿Ú²»Í¨µÄÇé¿öÏ»á×Ô¶¯¼ì²â³öÀ´£¬´Ó¶ø×Ô¶¯Çл»µ½ÁíÒ»¸ö£»µ«ÊÇÔÚIP²»Í¨µÄÇé¿öÏ£¬¾Í»á³ö´íÁË¡£
¸Ã·â×°½â¾öÁ˼¸¸öÎÊÌ⣺
1¡¢Jobs ServerµÄIPÈç¹ûͻȻ²»¿É´ï£¨ÀýÈç»úÆ÷¹Ø»ú£©£¬worker»á×Ô¶¯ÖØÐÂÌí¼Ójobs server£¨Ä¬Èϻᱨ´íÈ»ºóworkerÖÕÖ¹£©£»
2¡¢Ìí¼Ójobs serverʱºòÖ»Ìí¼ÓÓÐЧµÄserver£¨Èç¹û°ÑIP²»Í¨µÄserverÌí¼Ó½øÈ¥£¬ºóÃæ»á±¨´í£©£»
PS£º×îÏÂÃæÄǸöº¯Êý¾ÍÊÇÎÒÓÃÀ´¼ì²âÓÐЧjobs serverµÄ£¬´Ö±©µÄ·½·¨:
/** * worker Àà */ class GW{ private $enabled = False; private $worker = null; private $config = array(); private $task = array(); //×¢²áµÄÈÎÎñÐÅÏ¢ /** * ´´½¨worker¶ÔÏó£¬Ìí¼Ójob·þÎñÆ÷ * @param array $config job server config * @return void */ public function __construct($config) { $this->config = $config; $this->initWorker(); } /** * ×¢²áÈÎÎñ¼°´¦Àíº¯Êý * @param string $task_name Ҫע²áµÄ"ÈÎÎñ" * @param string $fun_name ¶ÔÓ¦µÄ´¦Àíº¯ÊýµÄº¯ÊýÃû * @return boolean */ public function regTask($task_name, $fun_name){ if (!$this->enabled){ $ret = false; } else { //Register and add callback function $ret = $this->worker->addFunction($task_name, $fun_name); if ($ret){ $this->task[$task_name] = $fun_name; } } return $ret; } /** * ÔËÐÐworker * @return void/boolean */ public function run(){ if (!$this->enabled){ return False; } while(@$this->worker->work() || $this->worker->returnCode()!=GEARMAN_SUCCESS) { // ÔËÐÐÖгö´í //echo "error: " . $this->worker->error() . "\n"; //echo "return_code: " . $this->worker->returnCode() . "\n"; // ×Ô¶¯ÖØÆôworker£¬²¢ÖØÐÂ×¢²á֮ǰµÄtask if($this->worker->returnCode()!=GEARMAN_SUCCESS) { $this->enabled = False; $this->initWorker(); $ret = $this->reloadTask(); if ($ret){ // ÖØÆô³É¹¦ $this->run(); } else { // ÖØÆôʧ°Ü } } } } /** * Ìí¼ÓÓÐЧµÄjobs serverµ½workerÖÐ */ private function initWorker() { $this->worker = new GearmanWorker(); //add job server foreach ($this->config as $value) { $host = trim(strval($value['host'])); $port = array_key_exists('port', $value) ? intval($value['port']) : 4730; if(!check_conn($host, $port)){ continue; } else { $this->worker->addServer($host,$port); $this->enabled = True; } } } /** * ÖØÐÂ×¢²á֮ǰµÄÈÎÎñ */ private function reloadTask(){ $ret = False; foreach ($this->task as $task_name => $fun_name){ $ret = $this->regTask($task_name, $fun_name); } return $ret; } /** * ÊÇ·ñÒÑÌí¼ÓÓÐЧjobs server */ public function isEnabled(){ return $this->enabled; } } /** * client Àà */ class GC{ private $enabled = false; private $client = null; /** * ´´½¨client¶ÔÏó£¬Ìí¼Ójob·þÎñÆ÷ * @param array $config job server config * @param int $timeout ³¬Ê±Ê±¼ä£¨ºÁÃ룩 * @return void */ public function __construct($config, $timeout=5000) { $this->client = new GearmanClient(); //add job server foreach ($config as $value) { $host = trim(strval($value['host'])); $port = array_key_exists('port', $value) ? intval($value['port']) : 4730; if(!check_conn($host, $port)){ continue; } else { $this->client->addServer($host,$port); $this->enabled = True; } } $this->enabled && $this->client->setTimeout($timeout); } /** * ·¢ËÍÏûÏ¢£¬²¢µÈ´ýÏìÓ¦ * @param string ÈÎÎñÃû * @param string ¸ÃÈÎÎñµÄÊý¾Ý * @return mixed job server·µ»ØµÄ½á¹û */ public function send($task_name, $task_data) { if (!$this->enabled){ $ret = false; } else { $ret = $this->client->do($task_name, strval($task_data)); } return $ret; } /** * ÊÇ·ñÒÑÌí¼ÓÓÐЧjobs server */ public function isEnabled(){ return $this->enabled; } } /** * ÍøÂç¼ì²â * @param string »úÆ÷IP * @param string »úÆ÷port * @return bool */ function check_conn($ip, $port = 4730) { // socketÁ´½Ó²âÊÔ,200ms³¬Ê± @$fp = fsockopen($ip, $port, $errno, $errstr, 0.2); if ($fp){ $fp && fclose($fp); return true; } else { return false; } }
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- php³£ÓÃÕýÔò±í´ïʽ
- phpÐÔÄܼà²âÄ£¿éXHProf
- ÈÃCI¿ò¼ÜÖ§³Öservice²ã
- ʹÓÃPHPÉú³É´øLOGOµÄ¸öÐÔ»¯¶þάÂëͼÏñ
- ¹ØÓÚCodeIgniterÄã¿ÉÄܲ»ÖªµÀµÄ5¸ö֪ʶµã
- Memcache ºÁÃ뼶³¬Ê±¼°ÆäËû³£¼ûÎÊÌâ»ã×Ü
- [PHP±Ê¼Ç]PHPQueryÒ»¸ö´¦ÀíDOMµÄÀûÆ÷
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- PHP»ñÈ¡Óû§µÄÕæʵIP£¬²¢ÅжÏÊÇ·ñÄÚÍøIP
- PHP ´íÎóÈÕÖ¾ error_log
- ÀûÓÃbigpipe»úÖÆʵÏÖÒ³ÃæÄ£¿éµÄÒì²½äÖȾ chunked¼¼Êõ
- php¿ØÖÆÎļþÏÂÔØËÙ¶È
- js + php ¶ÁÈ¡¡¢²¥·ÅÊÓƵÁ÷ ¼æÈÝfirefox£¬c....
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- UNICODE Óë UTF-8 µÄ¹Øϵ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- phpʹÓÃmb_detect_encoding¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ