ÔõÑùʹÓÃSupervisorÖÎÀíThinkPHP6ÐÐÁУ¿
Ëæ×ÅwebÓ¦ÓõÄÒ»Ö±Éú³¤£¬ÎÒÃÇÐèÒª´¦Àí´ó×ÚµÄʹÃüÀ´¼á³ÖÓ¦ÓõÄÎȹÌÐԺͿÉÓÃÐÔ¡£Ê¹ÓÃÐÐÁÐϵͳ¾ÍÊÇÒ»ÖÖ½â¾ö¼Æ»®¡£thinkphp6ÌṩÁËÄÚÖõÄÐÐÁÐϵͳÀ´ÖÎÀíʹÃü¡£È»¶ø£¬´¦Àí´ó×ÚµÄʹÃüÐèÒª¸üºÃµÄÐÐÁÐÖÎÀí£¬Õâʱ¼ä¿ÉÒÔʹÓÃsupervisorÀ´ÊµÏÖ¡£
±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃSupervisorÖÎÀíThinkPHP6ÐÐÁС£ÔÚ´Ë֮ǰ£¬ÎÒÃÇÐèÒªÏàʶһЩ»ù´¡µÄ¿´·¨£º
ÐÐÁÐϵͳ
ÐÐÁÐϵͳÊÇÒ»ÖÖÒì²½´¦ÀíʹÃüµÄ·½·¨£¬½«Ê¹ÃüÌí¼Óµ½ÐÐÁÐÖУ¬¶ø²»ÊÇÖ±½Ó´¦Àí¡£Ê¹ÃüÒ»µ©±»Ìí¼Óµ½ÐÐÁÐÖУ¬¾Í¿ÉÒÔ±»·ÖÅɵ½²î±ðµÄÊÂÇéÕßÀ´´¦Àí£¬ÒÔ×èÖ¹WebÓ¦Óó¤Ê±¼äÛÕ±Õ¡£ÐÐÁÐϵͳҲ¿ÉÒÔÍê³ÉʹÃüµÄһЩÖØ´ó²Ù×÷¡£
ʹÃü
ÔÚÐÐÁÐϵͳÖУ¬Ê¹Ãü¾ÍÊÇÐèÒªÖ´ÐеÄÊÂÇé¡£¿ÉÒÔ½«ÐèÒªÒì²½Ö´ÐеĴúÂë´ò°ü³ÉÒ»¸öʹÃü£¬È»ºó½«Ê¹ÃüÌí¼Óµ½ÐÐÁÐÖУ¬ÆÚ´ýºóÐø´¦Àí¡£ÎÒÃÇ¿ÉÒÔʹÓÃPHP¿ò¼ÜÖеÄÐÐÁÐ×é¼þ»òµÚÈý·½¿âÀ´ÖÎÀíʹÃü£¬ÈçLaravelµÄÐÐÁÐ×é¼þ»òBeanstalkdµÈ¡£
Supervisor
SupervisorÊÇÒ»¸öÀú³Ì¿ØÖÆϵͳ£¬¿ÉÒÔ¼à¿Ø²¢¿ØÖÆÒ»¸ö»ò¶à¸öÀú³Ì£¬°üÀ¨ÐÐÁÐÊÂÇéÕß¡£Supervisor¿ÉÒÔʹÐÐÁÐϵͳԽ·¢Îȹ̺ͳ¤ÆÚ¡£Ê¹ÓÃSupervisor¿ÉÒÔÔÚʹÃüʧ°Üʱ×Ô¶¯ÖØÆôÊÂÇéÕߣ¬²¢ÇÒ»¹¿ÉÒÔ°ü¹ÜÔÚWebÓ¦ÓÃÖØÆôºóÈÔÈ»¿ÉÒÔ¼ÌÐø´¦ÀíÐÐÁÐʹÃü¡£
ÏàʶÁËÕâЩ»ù´¡¿´·¨Ö®ºó£¬ÎÒÃǽ«ÏÈÈÝÔõÑùʹÓÃSupervisorÖÎÀíThinkPHP6ÐÐÁС£
µÚÒ»²½£º×°ÖÃSupervisor
ҪʹÓÃSupervisor¾ÙÐÐÐÐÁÐÖÎÀí£¬ÎÒÃÇÊ×ÏÈÐèҪװÖÃSupervisor¡£ÔÚUbuntuϵͳÉÏ£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´×°Öãº
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
sudo apt-get install supervisor
µÇ¼ºó¸´ÖÆ
ÔÚCentOSϵͳÉÏ£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´×°Öãº
sudo yum install supervisor
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³Éºó£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÆô¶¯Supervisor£º
sudo systemctl start supervisor
µÇ¼ºó¸´ÖÆ
ͬʱ£¬ÎÒÃÇÒ²ÐèÒªÔÚÉèÖÃÎļþ/etc/supervisor/conf.d/Öн¨ÉèÒ»¸öеÄÉèÖÃÎļþÀ´ÖÎÀíÐÐÁÐÊÂÇéÕß¡£ÎÒÃÇ¿ÉÒÔÔÚ¸ÃĿ¼Öн¨ÉèÒ»¸öí§ÒâÃû³ÆµÄÎļþ£¬Èçlaravel-worker.conf¡£½ÓÏÂÀ´£¬ÎÒÃǽ«ÏÈÈÝÔõÑù±à¼¸ÃÎļþ¡£
µÚ¶þ²½£º±à¼SupervisorÉèÖÃÎļþ
±à¼SupervisorÉèÖÃÎļþ£¬½«ÊÂÇéÕßÌí¼Óµ½Supervisor¼àÊÓÁбíÖС£ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´±à¼¸Õ¸Õ½¨ÉèµÄÉèÖÃÎļþ£º
sudo nano /etc/supervisor/conf.d/laravel-worker.conf
µÇ¼ºó¸´ÖÆ
½«ÒÔÏÂÉèÖÃÌí¼Óµ½ÎļþÖУº
[program:laravel-worker] process_name=%(program_name)s_%(process_num)02d directory=/var/www/laravel #ÐÞ¸ÄΪÄãµÄÏîĿĿ¼ command=php /var/www/laravel/artisan queue:work autostart=true autorestart=true user=www-data #ÐÞ¸ÄΪÄãµÄWebЧÀÍÆ÷ÔËÐÐÓû§ numprocs=8 #ÊÂÇéÕßÊýÄ¿£¬´Ë´¦½¨ÒéÉèÖÃΪCPU½¹µãÊý2-4±¶ redirect_stderr=true stdout_logfile=/var/www/laravel/storage/logs/worker.log #ÐÞ¸ÄΪÄãµÄÈÕÖ¾ÎļþλÖÃ
µÇ¼ºó¸´ÖÆ
½«ÒÔÉÏÉèÖÃÌí¼Óµ½ÎļþÖкó£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´ÖØмÓÔØSupervisorÉèÖÃÎļþ£º
sudo supervisorctl reread sudo supervisorctl update
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬Supervisor¾Í¿ÉÒÔÆô¶¯ÎÒÃǵÄÐÐÁÐÊÂÇéÕß²¢¾ÙÐÐÖÎÀí¡£ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´Éó²éSupervisorÆô¶¯µÄËùÓÐÀú³Ì£º
sudo supervisorctl status
µÇ¼ºó¸´ÖÆ
µÚÈý²½£º²âÊÔÐÐÁÐʹÃü
ÏÖÔÚ£¬ÎÒÃÇÒѾʹÓÃSupervisorÀÖ³ÉÆô¶¯ÁËÐÐÁÐÊÂÇéÕß¡£½ÓÏÂÀ´£¬ÎÒÃÇÐèÒª²âÊÔÒ»ÏÂÐÐÁÐʹÃü¡£Ê×ÏÈ£¬ÇëÈ·±£ÄúµÄÓ¦ÓóÌÐòÖÐÒÑÉèÖúÃÐÐÁС£
ÔÚij¸öµØ·½Ìí¼ÓÒ»¸ö²âÊÔʹÃü£¬ÈÃÐÐÁÐ×îÏÈÊÂÇé¡£ºÃ±È£¬½¨ÉèÒ»¸öE-mails·¢ËÍʹÃü£º
<?php namespace appqueue; use thinkqueueJob; class SendEmail { public $user; public function __construct($user) { $this->user = $user; } public function fire(Job $job, $data) { //·¢ËÍEmailµÄ´úÂë if (Math.random() < 0.5) { // ´¦Àíʧ°Ü $job->release(5);// 5ÃëºóÖØÊÔ } else { // Àֳɴ¦Àí $job->delete(); } } }
µÇ¼ºó¸´ÖÆ
ÔÚ¿ØÖÆÆ÷ÖÐÌí¼ÓÒ»¸öÒªÁ죬Ìí¼ÓʹÃü£º
<?php namespace appcontroller; use thinkController; use thinkqueueQueue; class Email extends Controller { public function index() { $user = ['email' => 'test@test.com', 'name' => 'test']; $job = new ppqueueSendEmail($user); app('queue')->push($job); } }
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬ÎÒÃǾͿÉÒÔÀֳɵØʹÓÃSupervisorÖÎÀíThinkPHP6ÐÐÁС£ÈôÊÇÄúÏëÏàʶ¸ü¶à¹ØÓÚÐÐÁÐϵͳµÄÐÅÏ¢£¬¿ÉÒÔÔĶÁ¹Ù·½Îĵµ¡£Ï£ÍûÕâƪÎÄÕÂÄܹ»×ÊÖúÄúÀÖ³ÉÔËÓªÄúµÄWebÓ¦ÓóÌÐò£¡
ÒÔÉϾÍÊÇÔõÑùʹÓÃSupervisorÖÎÀíThinkPHP6ÐÐÁУ¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡