LaravelÊÂÎñÊÇÒì²½µÄÂð
laravelÊÇÒ»¿îºÜÊÇÊ¢ÐеÄphp¿ò¼Ü£¬ËüÌṩÁ˸»ºñµÄ¹¦Ð§ºÍ¹¤¾ß£¬¿ÉÒÔ×ÊÖú¿ª·¢Ö°Ô±¸ü¿ì½ÝµØ¹¹½¨webÓ¦ÓóÌÐò¡£ÆäÖÐÒ»¸öºÜÊÇÖ÷ÒªµÄ¹¦Ð§¾ÍÊÇÊÂÎñϵͳ¡£Í¨¹ýËü£¬Ó¦ÓóÌÐòÖеĸ÷¸ö×é¼þ¿ÉÒÔͨ¹ý½ç˵ÊÂÎñºÍ¼àÌýÆ÷µÄ·½·¨À´½âñîºÏ²¢ÊµÏÖ¸ü¸ßЧµÄÐ×÷¡£ÄÇôÎÊÌâÀ´ÁË£ºlaravelÊÂÎñÊÇÒì²½µÄÂð£¿ÕâÊÇÒ»¸öÒý·¢ÕùÒéµÄÎÊÌ⣬ÏÂÃæÎÒÃÇÀ´Ì½ÌÖһϡ£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÃ÷È·Ò»ÏÂʲôÊÇÒì²½ÊÂÎñ¡£Òì²½ÊÂÎñ¿ÉÒÔÃ÷ȷΪÊÂÎñ±¬·¢ºó£¬´¥·¢ÊÂÎñ´¦Öóͷ£µÄÀú³ÌÓëÊÂÎñ×Ô¼ºµÄ±¬·¢ÊÇÒì²½µÄ£¬Ò²¾ÍÊÇ˵´¦Öóͷ£ÊÂÎñµÄ´úÂë»áÔÚÊÂÎñ±¬·¢ºóÁ¬Ã¦·µ»Ø£¬¶ø²»±Øͬ²½ÆÚ´ýÊÂÎñµÄ´¦Öóͷ£Íê³É¡£Ïà±È֮ϣ¬Í¬²½ÊÂÎñ¾ÍÊÇÊÂÎñ±¬·¢ºó´¦Öóͷ£Àú³ÌÊÇͬ²½µÄ£¬Ò²¾ÍÊÇ˵´¦Öóͷ£´úÂëÐèÒªÆÚ´ýÊÂÎñ´¦Öóͷ£Íê³É²Å»ª¼ÌÐøÖ´ÐС£
ÔÚLaravelÖУ¬ÊÂÎñµÄĬÈÏ´¦Öóͷ£·½·¨ÊÇͬ²½µÄ¡£Ò²¾ÍÊÇ˵£¬ÔÚÊÂÎñ±¬·¢Ê±£¬¼àÌýÆ÷»áÛÕ±ÕÄ¿½ñÏß³ÌÖ±µ½ÊÂÎñ´¦Öóͷ£Íê³É¡£ÕâÊÇLaravelµÄĬÈÏÐÐΪ£¬µ«ÈôÊÇÎÒÃÇÐèҪʵÏÖÒì²½ÊÂÎñ´¦Öóͷ£Ôõô°ìÄØ£¿
ºÜ¼òÆÓ£¬LaravelΪÎÒÃÇÌṩÁËÒì²½ÊÂÎñµÄ½â¾ö¼Æ»®¡£ÔÚLaravel 5.5¼°ÒÔÉÏ°æ±¾ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃLaravelÐÐÁУ¨Laravel Queue£©À´ÊµÏÖÒì²½ÊÂÎñ´¦Öóͷ£¡£ÐÐÁÐÊÇÒ»ÖÖÔÚºǫ́ÔËÐÐʹÃüµÄ»úÖÆ£¬Õâ¸ö»úÖÆÔÊÐíÎÒÃǽ«Ê¹ÃüÍƳٵ½ÉÔºóÖ´ÐУ¬ÕâÑùÎÒÃǾͿÉÒÔͬ²½/Òì²½Ö´ÐÐʹÃü£¬¶ø²»±ØÆÚ´ýËüÃÇÍê³É¡£
ÏêϸÀ´Ëµ£¬ÎÒÃÇÖ»ÐèҪʹÓÃÒÔϼ¸²½¾Í¿ÉÒÔʵÏÖÒì²½ÊÂÎñ´¦Öóͷ££º
1.ʹÓÃÐÐÁÐÇý¶¯
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚconfig/queue.phpÉèÖÃÎļþÖÐÉèÖÃÐÐÁеÄÇý¶¯£¬LaravelÖ§³Ö¶àÖÖÐÐÁÐÇý¶¯·½·¨£¬ºÃ±Èdatabase¡¢redis¡¢beanstalkdµÈ£¬Æ¾Ö¤ÏÖÕæÏàÐÎÑ¡ÔñÊʺϵÄÇý¶¯¡£
2.½ç˵Òì²½ÊÂÎñ
ÔÚ½ç˵ÊÂÎñµÄͬʱ£¬ÎÒÃÇ¿ÉÒÔΪÊÂÎñÖ¸¶¨Òì²½¼àÌýÆ÷£¬ÒªÁìÊÇʹÓÃÏÂÁîphp artisan make:listenerÀ´½¨ÉèÒ»¸ö¼àÌýÆ÷À࣬²¢½«–queuedÑ¡ÏîÉèÖÃΪtrue£¬ÈçÏÂËùʾ£º
php artisan make:listener UserRegistered –queued
È»ºóÔÚÊÂÎñÀàÖÐÖ¸¶¨¸ÃÒì²½¼àÌýÆ÷£¬ÈçÏÂËùʾ£º
class UserRegistered
{
use Dispatchable, InteractsWithSockets, SerializesModels; public $user; public function __construct(User $user) { $this->user = $user; } public function broadcastOn() { return new PrivateChannel('channel-name'); } public function broadcastAs() { return 'server.created'; } public function shouldQueue() { return true; } public function handle() { // ÐèÒªÒì²½´¦Öóͷ£µÄÏêϸÂß¼ }
µÇ¼ºó¸´ÖÆ
}
ÔÚÕâ¸öÊÂÎñÀàÖУ¬ÎÒÃÇʹÓÃshouldQueueÒªÁìÖ¸¶¨¸ÃÊÂÎñÐèÒªÒì²½´¦Öóͷ££¬²¢ÔÚhandleÒªÁìÖÐʵÏÖÒì²½´¦Öóͷ£µÄÏêϸÂß¼¡£
3.´¥·¢Òì²½ÊÂÎñ
´¥·¢Òì²½ÊÂÎñºÍ´¥·¢Í¨Ë×ÊÂÎñÒ»Ñù£¬Ö»ÐèҪʹÓÃeventº¯Êý²¢×ª´ïÊÂÎñ¹¤¾ß¼´¿É£¬ÈçÏÂËùʾ£º
event(new UserRegistered($user));
ÕâÑù£¬µ±ÓÐÓû§×¢²áʱ£¬Laravel¾Í»áÔÚºǫ́½¨ÉèÒ»¸öÒ첽ʹÃüÐÐÖ¹ÖøÃÊÂÎñ£¬²¢ÈÃÄ¿½ñÏß³ÌÁ¬Ã¦·µ»Ø¡£ÊÂÎñ´¦Öóͷ£µÄÀú³Ì½«ÔÚºǫ́Öð²½µØÖ´ÐУ¬Ö±µ½Íê³É¡£
×ܽáÀ´Ëµ£¬LaravelµÄÊÂÎñϵͳĬÈÏÊÇͬ²½µÄ£¬µ«ÎÒÃÇ¿ÉÒÔʹÓÃÐÐÁУ¨Laravel Queue£©»úÖÆÀ´ÊµÏÖÒì²½ÊÂÎñ´¦Öóͷ£¡£ÕâÖÖ·½·¨Ïà½ÏÁ¿Í¬²½ÊÂÎñ£¬Äܹ»Ìá¸ßÓ¦ÓóÌÐòµÄÏìÓ¦ÐÔ£¬²¢ÇÒÔÚÓ¦¶Ô¸ß²¢·¢µÄÇéÐÎÏÂÓÐןüºÃµÄÌåÏÖ¡£µ«ÖµµÃ×¢ÖصÄÊÇ£¬Òì²½ÊÂÎñÒ²ÐèҪ˼Á¿ÖÖÖÖÎÊÌ⣬ÈçÐÐÁÐʹÃü³¬Ê±¡¢ÏûºÄÕßå´»úµÈ£¬ÐèÒªÓÐÏìÓ¦µÄ½â¾ö¼Æ»®À´°ü¹Ü´¦Öóͷ£Ð§¹ûµÄ׼ȷÐԺͿɿ¿ÐÔ¡£
ÒÔÉϾÍÊÇLaravelÊÂÎñÊÇÒì²½µÄÂðµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡