??? ?? ??? ThinkPHP6? ???? ??? ??????
Jun 12, 2023 am 09:57 AM???? ??? ??? ?? ?? ???? ?? ??? ? ??????? ?? ??? ?????. ?? ?? ?? ? ?? ????? ?? ??? ?? ??? ???? ?? ?? ? ????? ThinkPHP6 ?????? ???? ??? ?? ??? ???? ??? ?????.
1. ????
??? ?? ???? ??? ??? ????? ???? ??? ? ??? ???? ?? ?????. ????? ??? ???? ???? ??????? ?????. ??? ???? ?? ??? ???? ??? ??? ?? ? ?????? ???? ???? ???? ????? ? ??? ???.
? ???????? ??? ???? ????? ???? ??? ??? ? ? ???? ? ??? ? ? ????. ??? ???? ???? ??????? ??? ??? ??? ??? ??? ???? ??? ? ????.
2. ThinkPHP6 ??? ??
?????? ?? ???? ??? ?? ?????. ???, ??? ????? ??? ?? ??? ??? ??? ? ? ?? ?? ???? ??? ??? ????. ?? ?? ThinkPHP6??? ??? ?? ??? ???? ??? ? ?? ??????? ?? ??? ??? ??? ??? ???.
????? ????? ??? ???? ? ??? PsrLogLoggerInterface
?????? ???? ?? ?????. PsrLogLoggerInterface
接口來實現(xiàn)。
// 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);
日志記錄器定義好后,使用隊列發(fā)送日志記錄信息,這里我們選擇使用 RabbitMQ 當做隊列服務(wù)。
// 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
中除了記錄日志的信息外,還包含一些環(huán)境信息,比如時間、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()
創(chuàng)建了一個 channel 對象,通過 $channel->exchangeDeclare()
和 $channel->queueDeclare()
創(chuàng)建了 exchange 和 queue,并將它們進行了綁定。最后,使用 $channel->consume()
rrreee
rrreee
??? ??? ? ???? ???? ?? ??? ????. ???? RabbitMQ? ??? ???? ????? ?????.rrreee
? ?appcommonQueue
???? ???? ?? ?? ?? ??? data
?? ?? ? IP? ?? ?? ?? ??? ???? ????. ??, ??? URI ?? ? - ? ???: rrreee
- ?? ?? ??? ???? ???? ?????. rrreee
- ? ??? ? ??? ???, ?? ?? ???? ???,
$client->channel()
? ?? ?? ??? ????,? ?? ?? ??? ?????. >$channel->exchangeDeclare ()
?$channel->queueDeclare()
? exchange? queue? ???? ??????. ?????$channel->consume()
? ???? ???? ???? ?????? ???? ?? ???? ??? ?? ???? ????.
? ??? ??? ?? ??? ThinkPHP6? ???? ??? ??????? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

ThinkPHP ????? ????? ??? ?????: Composer? ????, ???? ????? ???? php bin/console? ????, ?? ???? ??? http://localhost:8000? ?????.

ThinkPHP?? ??? PHP ????? ??? ?? ??? ????. ??? ???? 3.2, 5.0, 5.1, 6.0? ????, ??? ??? ??? ???? ??? ??? ???? ? ?????. ?? ?? ??? ThinkPHP 6.0.16???. ??? ??? ? PHP ??, ?? ?? ?? ? ???? ??? ??????. ??? ??? ??? ???? ?? ?? ??? ???? ?? ????.

ThinkPHP Framework? ???? ???? ??: ThinkPHP Framework? ?? ????? ?????? ??? ???. ThinkPHP ?? ????? ???? ?? ???(?? ??)? ????. ?????? ?? ????? ?????. ? ??? ?????. ThinkPHP ??????? ??????. ThinkPHP ?????? URL? ???? ?????.

Laravel? ThinkPHP ?????? ?? ??: ThinkPHP? ????? ??? ? ??? ??? ?? Laravel?? ??? ????. Laravel? ? ????? ??? ??????? ?? ThinkPHP? ? ??? ? ????.

ThinkPHP ?? ??: PHP, Composer ? MySQL ??? ?????. Composer? ???? ????? ????. ThinkPHP ?????? ???? ?????. ?????? ??? ?????. ?????? ??? ?????. ??????? ???? http://localhost:8000? ?????.

ThinkPHP? ?? ????, ?? ???, ?? ?? ? ?????? ???? ?? ??? ?? ??? PHP ????????. ?? ?? ???? ??? ?? 10,000? ??? ??? ??? ? ??? JD.com, Ctrip? ?? ??? ? ??? ? ?????? ????? ?? ?? ?????? ?? ?????.

Java ????? ?? ??? ?? ?????? ??? ? ?? ??? ???? ???. ??: ?? ?? ?? ???? ???? ??? ?? ???: ??? ?? ?? ?? ???: ??? ???? ?? ?? ?? ?? ???? ??: ?? ?? ? ?? ?? ??

ThinkPHP ????? ????? ??? ???? ???. 1. ?? ????? ????. 2. ??????? ?????. 4. ?????? ??? ???? ??? ?????. 7. ????? ?????. ?? ???? ??? ?? ??, ???? ?? ??? ? ???? ??? ?????.
