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

ThinkPHP6ÖÐÔõÑù¾ÙÐÐÐÂÎÅÐÐÁвÙ×÷£¿

Ëæ×Å»¥ÁªÍøµÄÉú³¤£¬Ó¦Óó¡¾°Ô½À´Ô½ÖØ´ó£¬ÐÔÄÜÒªÇóÒ²Ô½À´Ô½¸ß ¡£ÐÂÎÅÐÐÁУ¨message queue£©ÊÇÒ»Öֵ䷶µÄÒ첽ͨѶµÄ·½·¨£¬¿ÉÒÔÔڸ߲¢·¢µÄ³¡¾°ÏÂÌá¸ß³ÌÐòµÄÐÔÄܺÍÎȹÌÐÔ ¡£¶øÔÚphpÓïÑÔÖУ¬thinkphp6¿ò¼ÜÒ²ÌṩÁËÐÂÎÅÐÐÁеÄÖ§³Ö£¬±¾ÎĽ«¼òÆÓÏÈÈÝÔõÑùÔÚthinkphp6ÖоÙÐÐÐÂÎÅÐÐÁвÙ×÷ ¡£

ÇéÐδ

Ê×ÏÈ£¬ÔÚʹÓÃÐÂÎÅÐÐÁÐ֮ǰ£¬ÐèҪװÖÃÐÂÎÅÐÐÁÐ×é¼þ»òЧÀÍÆ÷ ¡£ÕâÀïÎÒÃÇÒÔRabbitMQ×÷ΪÐÂÎÅÐÐÁÐЧÀÍÆ÷ ¡£×°ÖÃRabbitMQ¿ÉÒԲο¼¹Ù·½Îĵµ»òÕßÆäËûÍøÂç×ÊÔ´ ¡£

Æä´Î£¬ÔÚThinkPHP6ÖУ¬¿ÉÒÔͨ¹ýcomposer×°Öùٷ½ÌṩµÄÐÂÎÅÐÐÁÐ×é¼þ£ºthink-amqp ¡£¿ÉÒÔÔÚÖÕ¶ËÖÐʹÓÃÒÔÏÂÏÂÁî¾ÙÐÐ×°Öãº

composer require topthink/think-amqp

µÇ¼ºó¸´ÖÆ

ÉèÖÃÎļþ

×°ÖÃÍê×é¼þÖ®ºó£¬ÐèÒªÔÚconfigĿ¼ÏµÄamqp.phpÎļþÖоÙÐÐÏà¹ØÉèÖà ¡£Ê¾Àý£º

<?php
return [
    'default' => [
        'host'          => '127.0.0.1',
        'port'          => 5672,
        'vhost'         => '/',
        'login'         => 'guest',
        'password'      => 'guest',
        // ÊÇ·ñ×Ô¶¯¿ªÆôͨµÀ£¬Ä¬ÒÔΪtrue
        'auto_declare'  => true,
        // ÐÐÁÐÁбí
        'queue_list'    => [
            'default'   => [
                'queue_name'    => 'default',
            ],
        ],
        // ½»Á÷»úÁбí
        'exchange_list' => [
            'default'   => [
                'exchange_name' => 'default',
                // ĬÈÏʹÓÃdirect½»Á÷»úÀàÐÍ£¬Ò²¿ÉÒÔʹÓÃÆäËûÀàÐÍ
                'exchange_type' => 'direct',
            ],
        ],
        // °ó¶¨Áбí
        'bind_list'     => [
            'default'   => [
                'queue_name'    => 'default',
                'exchange_name' => 'default',
            ],
        ],
    ],
];

µÇ¼ºó¸´ÖÆ

ÉÏÊöÉèÖÃÎļþÖУ¬¡®default¡¯ÊÇÅþÁ¬Ãû³Æ£¬Êý×éÖаüÀ¨ÁËÅþÁ¬ÐÅÏ¢¡¢ÐÐÁÐÁÐ±í¡¢½»Á÷»úÁбíºÍ°ó¶¨Áбí ¡£ÔÚÐÐÁÐÁбíºÍ½»Á÷»úÁбíÖУ¬¿ÉÒÔ½ç˵¶à¸öÐÐÁкͽ»Á÷»ú¼°ÆäÏà¹ØÉèÖà ¡£ÔÚ°ó¶¨ÁбíÖУ¬¿ÉÒÔ½ç˵ÐÐÁкͽ»Á÷»úµÄ°ó¶¨¹Øϵ ¡£

Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»

×¢ÖØ£ºÔÚʹÓÃÐÐÁÐÃû¡¢½»Á÷»úÃûºÍ°óÃüÃûʱ£¬ÐèÒª°ü¹ÜÆäΨһÐÔ ¡£

·¢ËÍÐÂÎÅ

·¢ËÍÐÂÎÅ¿ÉÒÔʹÓÃAMQPÀàÖеÄproducerÒªÁ죬ʾÀý£º

<?php
namespace appindexcontroller;

use thinkmqpAMQP;

class Index
{
    public function index()
    {
        $config = config('amqp.default');
        $exchange_name = 'default';
        $routing_key = 'default';
        $message = "hello world";
        $producer = AMQP::instance($config)->producer($exchange_name, $routing_key);
        $producer->publish($message);
        echo "send message success";
    }
}

µÇ¼ºó¸´ÖÆ

ÉÏÊö´úÂëÖУ¬$configÊÇÉÏÃæÉèÖÃÎļþÖеġ®default¡¯ÅþÁ¬ÐÅÏ¢£¬$exchange_nameÊǽ»Á÷»úÃû£¬$routing_keyÊÇ·Óɼü£¬$messageΪÐÂÎÅÄÚÈÝ ¡£

ÎüÊÕÐÂÎÅ

½ÓÊÜÐÂÎÅÐèҪʹÓÃAMQPÀàÖеÄconsumerÒªÁìºÍconsumingÒªÁ죬ʾÀý£º

<?php
namespace appindexcontroller;

use thinkmqpAMQP;

class Index
{
    public function queue()
    {
        $config = config('amqp.default');
        $queue_name = 'default';
        $callback = function ($envelope, $queue) {
            $msg = $envelope->getBody();
            echo $msg."
";
            $queue->ack($envelope->getDeliveryTag());
        };
        $consumer = AMQP::instance($config)->consumer($queue_name);
        $consumer->consume($callback);
    }
}

µÇ¼ºó¸´ÖÆ

ÉÏÊö´úÂëÖУ¬$configÊÇÉÏÃæÉèÖÃÎļþÖеġ®default¡¯ÅþÁ¬ÐÅÏ¢£¬$queue_nameÊÇÐÐÁÐÃû£¬$callbackÊǻص÷º¯Êý ¡£Ôڻص÷º¯ÊýÖУ¬Ê×ÏÈ»ñÈ¡ÐÂÎÅÄÚÈÝ£¬È»ºóÖ´ÐÐackÒªÁ죬ÌåÏÖÐÂÎÅÒѱ»ÏûºÄ ¡£

×ܽá

ÒÔÉϾÍÊÇÔÚThinkPHP6ÖÐʹÓÃÐÂÎÅÐÐÁеļòÆÓʾÀý ¡£Í¨¹ýÐÂÎÅÐÐÁеÄʹÓ㬿ÉÒÔ¶Ô³ÌÐò¾ÙÐнâñîºÏ£¬ÌáÉýϵͳµÄÐÔÄܺÍÎȹÌÐÔ ¡£¹ØÓÚ¸ü¶àµÄÐÐÁÐÀàÐÍ¡¢ÐÂÎÅÈ·ÈÏ»úÖƺͼ¯Èº¼Æ»®µÈ£¬¿ÉÒԲο¼¹Ù·½Îĵµ¾ÙÐÐѧϰºÍÏàʶ ¡£

ÒÔÉϾÍÊÇThinkPHP6ÖÐÔõÑù¾ÙÐÐÐÂÎÅÐÐÁвÙ×÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

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

Ïà¹ØÐÂÎÅ

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

18523999891

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

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

QR code
ÍøÕ¾µØͼ