Commit 93f5d3a8 authored by Liu lu's avatar Liu lu

使用协程上下文存储request_id作为唯一的标识

parent 4b663e16
...@@ -9,6 +9,7 @@ use Hyperf\Utils\Context; ...@@ -9,6 +9,7 @@ use Hyperf\Utils\Context;
use Meibuyu\Micro\Annotation\LogTrace; use Meibuyu\Micro\Annotation\LogTrace;
use Meibuyu\Micro\Handler\LogTrace\LogTraceHandler; use Meibuyu\Micro\Handler\LogTrace\LogTraceHandler;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Hyperf\Utils\Coroutine;
/** /**
* @Aspect( * @Aspect(
...@@ -29,7 +30,8 @@ class LogTraceAspect extends AbstractAspect ...@@ -29,7 +30,8 @@ class LogTraceAspect extends AbstractAspect
public function process(ProceedingJoinPoint $proceedingJoinPoint) public function process(ProceedingJoinPoint $proceedingJoinPoint)
{ {
try { try {
$fd = Context::get(ServerRequestInterface::class)->getSwooleRequest()->fd; $object = Context::get(ServerRequestInterface::class);
$fd = $object?$object->getSwooleRequest()->fd:$this->produceUniqueId();
Context::set('log_trace_request_id',$fd); Context::set('log_trace_request_id',$fd);
$originParams = [ $originParams = [
...@@ -61,4 +63,9 @@ class LogTraceAspect extends AbstractAspect ...@@ -61,4 +63,9 @@ class LogTraceAspect extends AbstractAspect
} }
} }
private function produceUniqueId()
{
return time().posix_getpid().Coroutine::id();
}
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment