laravelÐÐÁÐÔõôʵÏÖÐÂÎÅÍÆËÍ
Ëæ×Å»¥ÁªÍøµÄѸÃÍÉú³¤£¬ÐÂÎÅÍÆËÍÒѾ³ÉΪÁËÏÖ´úÓ¦ÓóÌÐòÖбز»¿ÉÉÙµÄÒ»²¿·Ö¡£ÔÚÐí¶àÇéÐÎÏ£¬µ±Óû§ÓëÓ¦ÓóÌÐò¾ÙÐн»»¥Ê±£¬Ó¦ÓóÌÐòÐèÒª¼´Ê±µØ·¢ËÍÐÂÎÅÒÔ»ñµÃÓû§µÄ·´Ïì¡£ÔÚÕâÖÖÇéÐÎÏ£¬Ê¹ÓÃÐÂÎÅÐÐÁÐÀ´´¦ÀíÕâЩÐÂÎÅ¿ÉÒÔ¼«´óµØÌá¸ßÓ¦ÓóÌÐòµÄÐÔÄܺͿÉÉìËõÐÔ¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÏÈÈÝÔõÑùʹÓÃlaravelÐÐÁÐÀ´ÊµÏÖÐÂÎÅÍÆËÍ¡£
ʲôÊÇLaravelÐÐÁУ¿
LaravelÐÐÁÐÊÇÒ»ÖÖÓÃÓÚÒì²½´¦ÀíʹÃüµÄ¹¤¾ß¡£ÔÚLaravelÓ¦ÓóÌÐòÖУ¬Í¨¹ý½«ÐèÒª´¦ÀíµÄʹÃüÍÆË͵½ÐÐÁÐÖУ¬¿ÉÒÔʵÏÖÒì²½´¦ÀíÕâЩʹÃü¡£ÕâÖÖ´¦Àí·½·¨¿ÉÒÔ¼«´óµØÌá¸ßÓ¦ÓóÌÐòµÄÐÔÄܺͿÉÀ©Õ¹ÐÔ£¬²¢ïÔ̶ÔÓû§µÄÏìӦʱ¼ä¡£
ʹÓÃLaravelÐÐÁÐʵÏÖÐÂÎÅÍÆËÍ
ÔÚLaravelÓ¦ÓóÌÐòÖУ¬¿ÉÒÔͨ¹ýÒÔÏ°취À´ÊµÏÖÐÂÎÅÍÆËÍ£º
×°ÖÃLaravelÐÐÁÐ
Ê×ÏÈ£¬ÐèҪװÖÃLaravelÐÐÁС£ÔÚLaravelÓ¦ÓóÌÐòÖУ¬LaravelÐÐÁÐÊÇͨ¹ýComposer¾ÙÐÐ×°Öõġ£Í¨¹ýÔÚÖÕ¶ËÖÐʹÓÃÒÔÏÂÏÂÁ¿ÉÒÔ×°ÖÃComposer£º
composer require illuminate/queue
µÇ¼ºó¸´ÖÆ
ÉèÖÃLaravelÐÐÁÐ
ÔÚ×°ÖÃLaravelÐÐÁÐÖ®ºó£¬ÐèÒª¾ÙÐÐһЩÉèÖá£ÔÚLaravelÓ¦ÓóÌÐòÖУ¬¿ÉÒÔͨ¹ýÒÔϼ¸¸ö°ì·¨¾ÙÐÐÉèÖãº
A. ÔÚ .env ÎļþÖÐÌí¼ÓÒÔÏÂÉèÖÃÐÅÏ¢£º
QUEUE_CONNECTION=redis REDIS_HOST=localhost REDIS_PASSWORD=null REDIS_PORT=6379
µÇ¼ºó¸´ÖÆ
B. ÔÚ config/queue.php ÎļþÖоÙÐÐÉèÖãº
'connections' => [ 'redis' => [ 'driver' => 'redis', 'connection' => 'default', 'queue' => env('QUEUE_NAME', 'default'), 'retry_after' => 90, 'block_for' => null, ], ],
µÇ¼ºó¸´ÖÆ
±àдÐÂÎÅÍÆËÍ´úÂë
ÔÚÍê³ÉÁËLaravelÐÐÁеÄÉèÖÃÖ®ºó£¬¿ÉÒÔ±àд´úÂëÀ´ÊµÏÖÐÂÎÅÍÆËÍ¡£ÏÂÃæÊDZàд´úÂëµÄÒ»¸öʾÀý£º
use Illuminate\Support\Facades\Queue; use App\Jobs\PushNotification; $data = [ 'title' => 'New Notification', 'message' => 'You have a new notification', 'user_id' => 1, ]; Queue::push(new PushNotification($data));
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃǽ«ÐèÒª·¢Ë͵Ä֪ͨÊý¾Ý°ü×°³ÉÒ»¸öÊý×飬²¢½«ÆäÍÆË͵½ÐÐÁÐÖС£ÔÚÕâÖ®ºó£¬LaravelÐÐÁн«ÒÔÒì²½·½·¨´¦Àí´ËʹÃü£¬²¢Ö´ÐÐ PushNotification ʹÃü¡£
±àдÐÐÁÐʹÃü´úÂë
ÔÚÉÏÊöʾÀýÖУ¬ÎÒÃǽ«Í¨ÖªÊý¾ÝÍÆË͵½ÐÐÁÐÖС£½ÓÏÂÀ´£¬ÐèÒª±àдһ¸ö´¦ÀíÐÐÁÐÖÐÍÆËÍ֪ͨʹÃüµÄʹÃü´úÂë¡£ÏÂÃæÊÇÒ»¸öʾÀý£º
namespace App\Jobs; use Illuminate\Support\Facades\Log; class PushNotification implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; protected $data; /** * Create a new job instance. * * @param array $data */ public function __construct(array $data) { $this->data = $data; } /** * Execute the job. * * @return void */ public function handle() { // Handle the notification here Log::info('Notification sent to user ID ' . $this->data['user_id']); } }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃǽç˵ÁËÒ»¸ö PushNotification ʹÃü£¬²¢½¨ÉèÁËÒ»¸ö½á¹¹º¯ÊýÀ´³õʼ»¯Ê¹ÃüËùÐèµÄÊý¾Ý¡£ÔÚʹÃüµÄ handle ÒªÁìÖУ¬ÎÒÃǽ«´¦Àí·¢ËÍ֪ͨµÄÂß¼£¬²¢¼Í¼֪ͨÒÑ·¢Ë͸øÄĸöÓû§¡£
Æô¶¯ÐÐÁд¦ÀíÆ÷
ÔÚÍê³ÉÁËÐÐÁÐʹÃüµÄ±àд֮ºó£¬ÐèÒªÆô¶¯ÐÐÁд¦ÀíÆ÷ÒÔÖ´ÐÐÐÐÁÐÖеÄʹÃü¡£¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÆô¶¯ÐÐÁд¦ÀíÆ÷£º
php artisan queue:work
µÇ¼ºó¸´ÖÆ
ËäÈ»£¬Ò²¿ÉÒÔͨ¹ýÔËÐÐÒÔÏÂÏÂÁîÀ´Ö¸¶¨ÐÐÁÐÅþÁ¬£º
php artisan queue:work redis
µÇ¼ºó¸´ÖÆ
ÔÚLaravelÐÐÁÐÖУ¬¿ÉÓõÄÐÐÁд¦ÀíÆ÷°üÀ¨£ºSync¡¢Database¡¢Redis¡¢Beanstalkd¡¢Amazon SQS ºÍ Null¡£ÉÏÊöÏÂÁî½ÓÄÉĬÈϵÄredisÐÐÁÐÅþÁ¬¡£¿ÉÒÔͨ¹ý¸ü¸Ä .env ÎļþÀ´¸ü¸ÄĬÈÏÐÐÁÐÅþÁ¬ºÍÐÐÁÐÃû³Æ¡£
²âÊÔ
Íê³ÉÒÔÉÏËùÓа취֮ºó£¬ÎÒÃÇ¿ÉÒÔ²âÊÔÎÒÃǵÄÐÂÎÅÍÆËÍÊÇ·ñÕý³£ÊÂÇ顣ͨ¹ý½«ÍÆË͵ÄÐÂÎżÍ¼ÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔÈ·¶¨Ê¹ÃüÊÇ·ñÀֳɵØÌí¼Óµ½ÁËÐÐÁС£ÒÔÏÂÊÇͨ¹ýʹÓÃÈÕÖ¾ÎļþÀ´²âÊÔÐÂÎÅÍÆË͵ÄʾÀý£º
use Illuminate\Support\Facades\Queue; use App\Jobs\PushNotification; $data = [ 'title' => 'New Notification', 'message' => 'You have a new notification', 'user_id' => 1, ]; Queue::push(new PushNotification($data)); Log::info('Notification sent to user ID ' . $data['user_id']);
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öʾÀýÖУ¬ÎÒÃǼͼÁË֪ͨÒÑ·¢Ë͸øÄĸöÓû§¡£ÔÚÆô¶¯ÐÐÁд¦ÀíÆ÷Ö®ºó£¬Éó²éÈÕÖ¾ÎļþÒÔÈ·ÈÏ֪ͨÊÇ·ñÒÑ·¢ËÍ¡£
½áÂÛ
µ±Óû§ÓëÓ¦ÓóÌÐò¾ÙÐн»»¥Ê±£¬ÐÂÎÅÍÆËÍÒѾ³ÉΪÁËÏÖ´úÓ¦ÓóÌÐòÖбز»¿ÉÉÙµÄÒ»²¿·Ö¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁËÔõÑùʹÓÃLaravelÐÐÁÐÀ´ÊµÏÖÐÂÎÅÍÆËÍ¡£Í¨¹ýʹÓÃLaravelÐÐÁУ¬¿ÉÒÔʵÏÖÒì²½´¦ÀíÓ¦ÓóÌÐòÖеÄʹÃü£¬´Ó¶øÌá¸ßÓ¦ÓóÌÐòµÄÐÔÄܺͿÉÀ©Õ¹ÐÔ¡£LaravelÐÐÁв»µ«Ò×ÓÚʹÓ㬲¢ÇÒÊÇÒ»ÖÖ¼òÆÓ¶øÇ¿Ê¢µÄ¹¤¾ß£¬¿ÉÓÃÓÚ½«ÐÂÎÅÍÆË͵½Ó¦ÓóÌÐòÖеÄÈκÎ×°±¸»òƽ̨¡£
ÒÔÉϾÍÊÇlaravelÐÐÁÐÔõôʵÏÖÐÂÎÅÍÆË͵ÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡