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ÊÂÎñÊÇÒì²½µÄÂðµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡