×ðÁú¿­Ê±¹ÙÍøµÇ¼

Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel QueuesºÍSupervisorÖÎÀíÒ첽ʹÃü £¿

laravel¿ª·¢£ºÔõÑùʹÓÃlaravel queuesºÍsupervisorÖÎÀíÒ첽ʹÃü £¿

ÔÚÏÖ´úWebÓ¦ÓóÌÐòÖУ¬Ò첽ʹÃüÒѳÉΪһÑùƽ³£ÓªÒµÖв»¿É»òȱµÄÒ»²¿·Ö ¡£Ò첽ʹÃü¿ÉÒÔÌá¸ßÓ¦ÓóÌÐòµÄÏìӦʱ¼ä£¬ÓÅ»¯Óû§ÌåÑé²¢ÔöÇ¿Ó¦ÓóÌÐòµÄ¿ÉÀ©Õ¹ÐÔ ¡£Laravel QueuesÊÇLaravel¿ò¼ÜÌṩµÄÒ»¸öÇ¿Ê¢¹¤¾ß£¬ÓÃÓÚ´¦ÀíÒ첽ʹÃüºÍÐÂÎÅÐÐÁÐ ¡£±¾ÎĽ«ÏÈÈÝLaravel QueuesµÄ¿´·¨ºÍʹÓÃÒªÁ죬²¢ÍŽáSupervisorÀ´ÖÎÀíÒ첽ʹÃü ¡£

ʲôÊÇLaravel Queues £¿

Laravel QueuesÊÇÒ»ÖÖÓÃÓÚ´¦ÀíÒ첽ʹÃüºÍÐÂÎÅÐÐÁеÄÒªÁì ¡£Í¨¹ýLaravel Queues£¬Äú¿ÉÒÔ½«ºÄʱµÄʹÃü·ÅÈëÐÐÁÐÖУ¬¶ø²»»áÓ°ÏìWebÇëÇóµÄÏìӦʱ¼ä ¡£ÀýÈ磬·¢Ë͵ç×ÓÓʼþ£¬´¦ÀíÊÓƵ»òÌìÉúPDF£¬ÕâЩ¶¼ÊǺÄʱµÄ²Ù×÷£¬Ê¹ÓÃÐÐÁн«ËüÃÇ°²Åŵ½ºǫ́´¦Àí£¬¿ÉÒÔʹӦÓóÌÐòÔ½·¢¸ßЧºÍÏìÓ¦ ¡£

Laravel Queuesͨ¹ýһЩÄÚÖõÄÐÐÁÐÇý¶¯³ÌÐòÀ´Ö§³Ö¶à¸öºó¶ËÊÖÒÕ£¬ÀýÈçDatabase£¬Redis£¬BeanstalkdºÍAmazon SQS ¡£Õâʹ¿ª·¢Ö°Ô±¿ÉÒÔʹÓÃËûÃÇϲ»¶µÄÐÐÁÐÊÖÒÕÀ´´¦ÀíÒ첽ʹÃü ¡£

ʹÓÃLaravel Queues

ÏÂÃæÎÒÃǽ«Ò»²½²½ÏÈÈÝÔõÑùʹÓÃLaravel Queues´¦ÀíÒ첽ʹÃü ¡£

µÚÒ»²½£ºÉèÖÃÐÐÁÐÇý¶¯³ÌÐò

LaravelÉèÖÃÎļþÖÐÓÐÒ»¸öÃûΪqueue.phpµÄÎļþ£¬Äú¿ÉÒÔʹÓÃËüÀ´ÉèÖÃQueuesºÍÐÐÁÐÇý¶¯³ÌÐò ¡£Äú¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÌìÉúqueue.phpÎļþ£º

php artisan queue:table
php artisan queue:failed-table
php artisan migrate

µÇ¼ºó¸´ÖÆ

Õ⽫ÌìÉúǨáãÎļþºÍÐÐÁбí ¡£ÔËÐÐmigrateÏÂÁîÒÔÖ´ÐÐǨáã ¡£

ÔÚqueue.phpÎļþÖУ¬Äú¿ÉÒÔÑ¡ÔñʹÓöàÖÖÐÐÁÐÇý¶¯³ÌÐò£º

DatabaseÇý¶¯³ÌÐò – ½«Ê¹Ãü´æ´¢ÔÚÊý¾Ý¿âÖУ¬¿É×÷ΪÈëÃż¶ÐÐÁÐϵͳ ¡£

RedisÇý¶¯³ÌÐò – ʹÓÃRedisµÄÄÚÖÃÐÐÁÐÖ§³Ö ¡£

BeanstalkdÇý¶¯³ÌÐò – ʹÓÃBeanstalkdÐÂÎÅÐÐÁÐЧÀÍ ¡£

Amazon SQSÇý¶¯³ÌÐò – ʹÓÃAmazon Simple Queue Service£¨SQS£© ¡£

ÀýÈ磬ÈôÊÇÄúҪʹÓÃRedisÐÐÁÐÇý¶¯³ÌÐò£¬ÇëÉèÖÃqueue.phpÎļþÈçÏ£º

'default' => env('QUEUE_CONNECTION', 'redis'),
'connections' => [
    'redis' => [
        'driver' => 'redis',
        'connection' => 'default',
        'queue' => env('REDIS_QUEUE', 'default'),
        'retry_after' => 90,
        'block_for' => null,
    ],
]

µÇ¼ºó¸´ÖÆ

µÚ¶þ²½£º½¨ÉèÒ»¸öÐÐÁÐʹÃüÀà

½ÓÏÂÀ´£¬ÄúÐèÒª½¨ÉèÒ»¸öÐÐÁÐʹÃüÀ࣬ÓÃÓÚ´¦ÀíÒ첽ʹÃü ¡£Õâ¸öÀàÓ¦¸ÃÊÇÒ»¸ö¼òÆÓµÄPHPÀ࣬¸ÃÀà½ç˵ÁËʹÃüµÄÂß¼­ ¡£ÀýÈ磬ÒÔÏ´úÂëÊÇÒ»¸öÓÃÓÚ·¢Ë͵ç×ÓÓʼþµÄÒ첽ʹÃüÀࣺ

class SendEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $email;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($email)
    {
        $this->email = $email;
    }
    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        Mail::to($this->email)->send(new WelcomeEmail());
    }
}

µÇ¼ºó¸´ÖÆ

¸ÃÀàʵÏÖÁËShouldQueue½Ó¿Ú£¬ÕâÊDZØÐèµÄ£¬ÓÃÓÚ¸æËßLaravel½«¸ÃÀàת»»ÎªÒ첽ʹÃüÀà ¡£handle()ÒªÁì½ç˵ÁËʹÃüµÄÏêϸÂß¼­£¬Òò´Ë¿ÉÒÔÔÚÕâÀïÖ´ÐÐÄúÐèÒªÒì²½´¦ÀíµÄʹÃü ¡£

µÚÈý²½£º½«Ê¹ÃüÍÆÈëÐÐÁÐ

ÏÖÔÚÄúÒѾ­×¼±¸ºÃÁËÐÐÁÐʹÃüºÍÐÐÁÐÇý¶¯³ÌÐò£¬ÏÂÒ»²½Êǽ«Ê¹Ãü·ÅÈëÐÐÁÐÖÐ ¡£Ê¹ÓÃÒÔÏ´úÂ룬ÔÚÏîÄ¿µÄÈκÎλÖÃŲÓÃÒ»¸öEloquentÐÐÁУº

use AppJobsSendEmail; 
use IlluminateSupportFacadesQueue;
...
Queue::push(new SendEmail('example@test.com'));

µÇ¼ºó¸´ÖÆ

»òÕßÄú¿ÉÒÔʹÓÃdispatch()ÒªÁ콫ʹÃü·ÅÈëÐÐÁÐÖУ¬ÈçÏÂËùʾ£º

SendEmail::dispatch('example@test.com');

µÇ¼ºó¸´ÖÆ

µÚËIJ½£ºÖ´ÐÐÐÐÁÐʹÃü

Ò»µ©Äú½«Ê¹Ãü·ÅÈëÐÐÁÐÖУ¬Ê¹Ãü½«»á±» dispatch µ½ÐÐÁÐÖУ¬ÆÚ´ýÖ´ÐÐ ¡£Äú¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´ÔËÐÐÐÐÁУº

php artisan queue:work

µÇ¼ºó¸´ÖÆ

ÔËÐдËÏÂÁ»áÆô¶¯Ò»¸ö¼àÌýÆ÷²¢´¦ÀíÐÐÁÐÖеÄʹÃü ¡£

ʹÓÃSupervisorÖÎÀíÒ첽ʹÃü

ÓÉÓÚÐÐÁÐʹÃüÐèÒªÔÚºǫ́ÔËÐУ¬Òò´ËÐèÒªÔÚЧÀÍÆ÷ÉÏÉèÖÃÀú³ÌÊØ»¤³ÌÐò£¬ÒÔÈ·±£Ê¹Ãü¿ÉÒÔÒ»Á¬Ö´ÐÐ ¡£SupervisorÊÇÒ»ÖÖ³£ÓõÄÀú³ÌÊØ»¤³ÌÐò£¬¿ÉÒÔÈ·±£ºǫ́Àú³Ì²»»áÒì³£ÖÕÖ¹£¬²¢ÔÚÐèҪʱÖØÐÂÆô¶¯ËüÃÇ ¡£

µÚÒ»²½£º×°ÖÃSupervisor

ÔÚUbuntuϵͳÖУ¬Äú¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´×°ÖÃSupervisor£º

sudo apt-get update
sudo apt-get install supervisor

µÇ¼ºó¸´ÖÆ

µÚ¶þ²½£º½¨ÉèÒ»¸öSupervisorÉèÖÃÎļþ

ÔÚ/etc/supervisor/conf.dĿ¼Öн¨ÉèÒ»¸öÉèÖÃÎļþ£¬ÀýÈçmyqueue.conf£º

nano /etc/supervisor/conf.d/myqueue.conf

µÇ¼ºó¸´ÖÆ

½«ÒÔÏÂÄÚÈÝÌí¼Óµ½ÉèÖÃÎļþÖУ¬È·±£¸ü¸Ä·¾¶£¬ÏÂÁîºÍÓû§ÃûÒÔÆ¥ÅäÄúµÄ³ÌÐò£º

[program:myqueue]
process_name=%(program_name)s_%(process_num)02d
command=/usr/bin/php /path/to/artisan queue:work --sleep=3 --tries=3 --daemon
autostart=true
autorestart=true
user=username
numprocs=1
redirect_stderr=true
stdout_logfile=/path/to/storage/logs/myqueue.log

µÇ¼ºó¸´ÖÆ

µÚÈý²½£ºÖØмÓÔØSupervisor

ÔÚÄú¸ü¸ÄÁËSupervisorµÄÉèÖÃÎļþÖ®ºó£¬ÄúÐèҪ֪ͨSupervisorÖØмÓÔØÉèÖÃÎļþ ¡£Ê¹ÓÃÒÔÏÂÏÂÁî¿ÉÒÔÖØмÓÔØSupervisor£º

sudo supervisorctl reread

sudo supervisorctl update

sudo supervisorctl start all

µÇ¼ºó¸´ÖÆ

µÚËIJ½£ºÉó²éSupervisorÈÕÖ¾

Äú¿ÉÒÔÔÚSupervisorµÄÈÕÖ¾ÎļþÖÐÉó²éÒ첽ʹÃüµÄÊä³öºÍ¹ýʧÐÅÏ¢ ¡£ÀýÈ磬Äú¿ÉÒÔÉó²éÊʲÅÉèÖÃÎļþÖÐÖ¸¶¨µÄ·¾¶ºÍÈÕÖ¾ÎļþÃû³ÆÀ´Éó²éSupervisorÈÕÖ¾£º

tail -f /path/to/storage/logs/myqueue.log

µÇ¼ºó¸´ÖÆ

½áÂÛ

±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃLaravel QueuesºÍSupervisorÀ´ÖÎÀíÒ첽ʹÃü£¬Ê¹ÓÃLaravel Queues¿ÉÒÔÇáËɽ«ºÄʱµÄʹÃü·ÅÈëÐÐÁÐÖУ¬²¢Ê¹Ó¦ÓóÌÐò¸ü¸ßЧºÍÏìÓ¦ ¡£Ê¹ÓÃSupervisor¿ÉÒÔÈ·±£ºǫ́ʹÃü¿ÉÒÔÒ»Á¬ÔËÐУ¬²¢ÔÚÐèҪʱ×Ô¶¯ÖØÐÂÆô¶¯ ¡£Ï£Íû¸ÃÎÄÕ¶ÔÄúµÄ¿ª·¢ÓÐËù×ÊÖú ¡£

ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel QueuesºÍSupervisorÖÎÀíÒ첽ʹÃü £¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼ ¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý ¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ