尊龙凯时人生就是搏

ÔõÑùʹÓÃThinkPHP6¾ÙÐÐÒì²½ÈÕÖ¾¼Í¼²Ù×÷£¿

Ëæ×Å»¥ÁªÍøµÄ¸ßËÙÉú³¤£¬ÈÕÖ¾¼Í¼ЧÀͳÉΪÁËÿ¸ö´óÐÍ web Ó¦Óñز»¿ÉÉÙµÄÄ£¿é¡£ÎªÁËÀû±ã¹ýʧÅŲ顢ÐÔÄܼà¿ØµÈÖÖÖÖÐèÇ󣬱¾ÎĽ«ÏÈÈÝÔõÑùʹÓà thinkphp6 ¿ò¼Ü¾ÙÐÐÒì²½ÈÕÖ¾¼Í¼²Ù×÷¡£

1. ʲôÊÇÈÕÖ¾¼Í¼

ÔÚÅÌËã»ú¿ÆѧÁìÓò£¬ÈÕÖ¾¼Í¼ÊÇÖ¸½«ÅÌËã»úϵͳÖб¬·¢µÄÊÂÎñºÍÐÅÏ¢¼Í¼ÏÂÀ´¡£Í¨³££¬ÕâЩ¼Í¼¶¼ÒÔÎļþ»òÊý¾Ý¿âµÄÐÎʽ´æ´¢¡£ÈÕÖ¾¼Í¼ÓÐÖúÓÚÏàʶϵͳÔËÐÐ״̬£¬ÊµÊ±·¢Ã÷Ï¢Õù¾öÎÊÌ⣬½ø¶øÌá¸ßϵͳµÄ¿É¿¿ÐÔºÍÎȹÌÐÔ¡£

ÔÚ web Ó¦ÓÃÖУ¬ÈÕÖ¾¼Í¼¿ÉÒÔ×ÊÖú¿ª·¢Õ߸üºÃµØÏàʶϵͳµÄÓöµ½µÄÎÊÌâºÍ¹ýʧ¡£ÒÀ¾ÝÈÕÖ¾¼Í¼£¬¿ª·¢Õß¿ÉÒÔÇåÎúµØÏàʶӦÓõÄÐÐΪÒÔ¼°¹ýʧ±¬·¢µÄλÖúÍʱ»ú¡£

2. ThinkPHP6 Òì²½ÈÕÖ¾¼Í¼

ÔÚÓ¦Óÿª·¢Àú³ÌÖУ¬ÈÕÖ¾¼Í¼ÊÇÒ»¸ö±Ø²»¿ÉÉÙµÄÄ£¿é¡£²¢ÇÒ£¬ÈÕÖ¾¼Í¼¾­³£ÊÇÒ»¸öºÄʱµÄ²Ù×÷£¬ÈôÊÇͬ²½Ö´ÐеĻ°»áÓ°ÏìϵͳµÄÐÔÄÜ¡£Îª´Ë£¬ThinkPHP6 ÒýÈëÁËÒì²½ÈÕÖ¾¼Í¼µÄ¹¦Ð§£¬ÈÃÈÕÖ¾¼Í¼²»ÔÙÓ°ÏìÓ¦ÓõÄÏìÓ¦ËÙÂÊ¡£

ͨ³£ÔÚ¿ØÖÆÆ÷»òÄ£×ÓÖмͼÈÕÖ¾£¬ÎÒÃÇʹÓÃ×¢Èë PsrLogLoggerInterface ½Ó¿ÚÀ´ÊµÏÖ¡£

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

// Controller»òModelÖÐ
use PsrLogLoggerInterface;

public function index(LoggerInterface $logger){
    $logger->info('hello world');
}

µÇ¼ºó¸´ÖÆ

¼òÆÓµÄʹÓ÷½·¨¡£Ê¹ÓÃÒì²½ÈÕÖ¾¼Í¼£¬½ç˵һ¸öÒì²½ÈÕÖ¾¼Í¼Æ÷£º

use MonologLogger;
use MonologHandlerStreamHandler;

$logger=new Logger("AsyncLogger");
$logger->pushHandler(new StreamHandler('runtime/log/async.log'), Logger::INFO);

µÇ¼ºó¸´ÖÆ

ÈÕÖ¾¼Í¼Æ÷½ç˵ºÃºó£¬Ê¹ÓÃÐÐÁз¢ËÍÈÕÖ¾¼Í¼ÐÅÏ¢£¬ÕâÀïÎÒÃÇÑ¡ÔñʹÓà RabbitMQ µ±×öÐÐÁÐЧÀÍ¡£

// MessageÀà
namespace appcommon;

class Message
{
    /**
     * ¼Í¼ÈÕÖ¾
     * @param $level
     * @param $message
     * @param array $context
     * @return bool
     */
    public static function log($level,$message,array $context=[]){
        $data=[
            'level'=>$level,
            'message'=>$message,
            'context'=>$context,
            'channel'=>'AsyncLogger',
            'datetime'=>date('Y-m-d H:i:s'),
            'host'=>$_SERVER['SERVER_ADDR'] ?? '',
            'uri'=>$_SERVER['REQUEST_URI'] ?? '',
        ];

        $producer=Queue::getConnection('AsyncLogger',true);
        $producer->setExchangeOptions(['name'=>'async_logs','type'=>'topic','durable'=>true])->declareExchange();

        try{
            $producer->publish(json_encode($data),[
                'routing_key' =>'log',
                'exchange' =>'async_logs',
            ]);
            return true;
        }catch (Exception $e){
            return false;
        }
    }
}

µÇ¼ºó¸´ÖÆ

ÆäÖУ¬ÎÒÃÇʹÓà appcommonQueue ÀàÀ´Ìṩ rabbitmq µÄÅþÁ¬ÊµÀý£»dataÖгýÁ˼ͼÈÕÖ¾µÄÐÅÏ¢Í⣬»¹°üÀ¨Ò»Ð©ÇéÐÎÐÅÏ¢£¬ºÃ±Èʱ¼ä¡¢IPµØµã¡¢ÇëÇóµÄuriµØµãµÈ¡£

ÐÐÁд¦Àí³ÌÐò£º

// ConsumerÀà
use BunnyMessage;
use PsrLogLoggerInterface;

class Consumer
{
    /**
     * @param Message $message
     * @param LoggerInterface $logger
     */
    public function process(Message $message,LoggerInterface $logger){
        $body=$message->content;
        $data= json_decode($body,true);
        $channel=$data['channel'] ?? 'default_logger';

        $logger->notice($data['message'], $data);
    }
}

µÇ¼ºó¸´ÖÆ

ËäÈ»£¬ÎÒÃÇ»¹ÐèÒªÒ»¸ö¸¨Öú´¦ÀíÈÕÖ¾µÄÀà¡£

// QueueÀà
namespace appcommon;

use BunnyAsyncClient;
use BunnyChannel;
use BunnyMessage;
use BunnyProtocolMethodBasicConsumeOkFrame;
use BunnyProtocolMethodChannelCloseFrame;
use BunnyProtocolMethodChannelCloseOkFrame;
use BunnyProtocolMethodConnectionCloseFrame;
use BunnyProtocolMethodConnectionCloseOkFrame;
use BunnyProtocolMethodConnectionStartFrame;
use BunnyClientStateEnum;
use BunnyMessage as BunnyMessage;

class Queue
{
    /**
     * @param string $queueName
     * @return Client|null
     */
    public static function getConnection(string $routingKey, bool $persistent=false):?Client
    {
        $config=config('rabbitmq.async_log');
        $client=new Client([
            'host' => $config['host'],
            'port' => $config['port'],
            'user' => $config['user'],
            'password' => $config['password'],
            'vhost' => $config['vhost'],//×¢ÖØ´Ë´¦¸ÄΪÐèÒªµÄ VHOST
            'concurrency' => 2,
        ]);

        try{
            $client->connect();
            $client->channel()
                ->then(function (Channel $channel) use($client,$routingKey,$persistent){
                    $channel->exchangeDeclare('async_logs','topic',true,true);
                    $channel->queueDeclare($routingKey, $passive=false,$durable=true,$exclusive=false,$autoDelete=false,$nowait=false);
                    $channel->queueBind($routingKey, 'async_logs', $routingKey);

                    $channel->consume(
                        function ($msg, Channel $channel, BunnyMessage $message) use($client,$routingKey){
                            $className=config('rabbitmq.async_log.consumer');
                            $consumer=new $className($client,$routingKey);
                            $consumer->process($message,app('log.async_logger'));
                            $channel->ack($msg);//´¦ÀíÐÂÎÅ
                        },
                        $routingKey,//ÐÐÁÐName
                        '',//ÏûºÄTag
                        false,//no_local
                        false,//no_ack
                        false,//exclusive
                        $persistent ? ['delivery_mode'=>2] : []
                    );
                });
        }catch (Exception $e){
            return null;
        }finally{
            return $client;
        }
    }
}

µÇ¼ºó¸´ÖÆ

ÉÏÃæÕâ¶Î´úÂëÖнç˵ÁËÐÐÁÐÅþÁ¬µÄ host¡¢port µÈ£¬Í¨¹ý $client->channel() ½¨ÉèÁËÒ»¸ö channel ¹¤¾ß£¬Í¨¹ý $channel->exchangeDeclare() ºÍ $channel->queueDeclare() ½¨ÉèÁË exchange ºÍ queue£¬²¢½«ËüÃǾÙÐÐÁ˰󶨡£×îºó£¬Ê¹Óà $channel->consume() Òì²½ÏûºÄÐÐÁеÄÐÂÎÅ£¬²¢½«ÐÂÎÅ·¢Ë͵½ÐÂÎÅ´¦ÀíÀàÖС£

3. ×ܽá

±¾ÎÄÏÈÈÝÁËÔõÑùʹÓà ThinkPHP6 ¿ò¼Ü¾ÙÐÐÒì²½ÈÕÖ¾¼Í¼²Ù×÷£¬Ê¹ÈÕÖ¾¼Í¼²»ÔÙÓ°ÏìÓ¦ÓõÄÏìÓ¦ËÙÂÊ¡£×ÜÌåÀ´Ëµ£¬ÒÔÏÂÊDzÙ×÷°ì·¨£º

¿ª·¢×Ô¼ºµÄÒì²½ÈÕÖ¾¼Í¼Æ÷

ʹÓà RabbitMQ ¾ÙÐÐÐÂÎÅÐÐÁд¦Àí

±àдÐÂÎÅ´¦Àí³ÌÐò

ÔÚÏÖʵÏîÄ¿ÖУ¬ÎÒÃÇÐèҪƾ֤ÏêϸµÄÐèÇóÀ´ÓÅ»¯´úÂëºÍµ÷½âÐÐÁеÄÉèÖá£Í¨¹ýÒì²½¼Í¼ÈÕÖ¾£¬¿ÉÒÔÓÐÓÃÌá¸ß web Ó¦ÓõÄÔËÐÐЧÂÊ£¬²¢Ìá¸ßϵͳµÄÎȹÌÐÔÓë¿É¿¿ÐÔ¡£

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

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

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是搏

18523999891

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

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

QR code
sitemap¡¢ÍøÕ¾µØͼ