Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
meibuyu-micro
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
without authentication
meibuyu-micro
Commits
ffa98b7c
Commit
ffa98b7c
authored
Mar 30, 2022
by
Liu lu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
使用协程上下文存储request_id作为唯一的标识
parent
b0966f8e
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
13 additions
and
4 deletions
+13
-4
AsyncCoroutineAspect.php
src/Aspect/AsyncCoroutineAspect.php
+4
-1
LogTraceAspect.php
src/Aspect/LogTraceAspect.php
+5
-0
LogTraceHandler.php
src/Handler/LogTrace/LogTraceHandler.php
+4
-3
No files found.
src/Aspect/AsyncCoroutineAspect.php
View file @
ffa98b7c
...
@@ -7,6 +7,7 @@ use Hyperf\Di\Aop\AbstractAspect;
...
@@ -7,6 +7,7 @@ use Hyperf\Di\Aop\AbstractAspect;
use
Hyperf\Di\Aop\ProceedingJoinPoint
;
use
Hyperf\Di\Aop\ProceedingJoinPoint
;
use
Meibuyu\Micro\Annotation\AsyncCoroutine
;
use
Meibuyu\Micro\Annotation\AsyncCoroutine
;
use
Hyperf\Utils\Coroutine
;
use
Hyperf\Utils\Coroutine
;
use
Hyperf\Utils\Context
;
/**
/**
* @Aspect(
* @Aspect(
...
@@ -29,9 +30,11 @@ class AsyncCoroutineAspect extends AbstractAspect
...
@@ -29,9 +30,11 @@ class AsyncCoroutineAspect extends AbstractAspect
// 切面切入后,执行对应的方法会由此来负责
// 切面切入后,执行对应的方法会由此来负责
// $proceedingJoinPoint 为连接点,通过该类的 process() 方法调用原方法并获得结果
// $proceedingJoinPoint 为连接点,通过该类的 process() 方法调用原方法并获得结果
// 在调用前进行某些处理
// 在调用前进行某些处理
return
Coroutine
::
create
(
function
()
use
(
$proceedingJoinPoint
){
$fd
=
Context
::
get
(
'log_trace_request_id'
);
return
Coroutine
::
create
(
function
()
use
(
$proceedingJoinPoint
,
$fd
){
try
{
try
{
Context
::
set
(
'log_trace_request_id'
,
$fd
);
LogTraceHandler
::
recordProcess
(
LogTraceHandler
::
recordProcess
(
'投递到子协程任务,id:'
.
Coroutine
::
id
()
'投递到子协程任务,id:'
.
Coroutine
::
id
()
.
' ,类:'
.
$proceedingJoinPoint
->
className
.
' ,类:'
.
$proceedingJoinPoint
->
className
...
...
src/Aspect/LogTraceAspect.php
View file @
ffa98b7c
...
@@ -5,8 +5,10 @@ use Hyperf\Di\Annotation\Aspect;
...
@@ -5,8 +5,10 @@ use Hyperf\Di\Annotation\Aspect;
use
Hyperf\Di\Aop\AbstractAspect
;
use
Hyperf\Di\Aop\AbstractAspect
;
use
Hyperf\Di\Aop\ProceedingJoinPoint
;
use
Hyperf\Di\Aop\ProceedingJoinPoint
;
use
Hyperf\HttpServer\Contract\RequestInterface
;
use
Hyperf\HttpServer\Contract\RequestInterface
;
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
;
/**
/**
* @Aspect(
* @Aspect(
...
@@ -27,6 +29,9 @@ class LogTraceAspect extends AbstractAspect
...
@@ -27,6 +29,9 @@ class LogTraceAspect extends AbstractAspect
public
function
process
(
ProceedingJoinPoint
$proceedingJoinPoint
)
public
function
process
(
ProceedingJoinPoint
$proceedingJoinPoint
)
{
{
try
{
try
{
$fd
=
Context
::
get
(
ServerRequestInterface
::
class
)
->
getSwooleRequest
()
->
fd
;
Context
::
set
(
'log_trace_request_id'
,
$fd
);
$originParams
=
[
$originParams
=
[
'called_params'
=>
$proceedingJoinPoint
->
getArguments
(),
'called_params'
=>
$proceedingJoinPoint
->
getArguments
(),
'http_params'
=>
make
(
RequestInterface
::
class
)
->
all
()
'http_params'
=>
make
(
RequestInterface
::
class
)
->
all
()
...
...
src/Handler/LogTrace/LogTraceHandler.php
View file @
ffa98b7c
...
@@ -9,6 +9,7 @@ namespace Meibuyu\Micro\Handler\LogTrace;
...
@@ -9,6 +9,7 @@ namespace Meibuyu\Micro\Handler\LogTrace;
use
Meibuyu\Micro\Model\LogTrace
;
use
Meibuyu\Micro\Model\LogTrace
;
use
Hyperf\Utils\Coroutine
;
use
Hyperf\Utils\Coroutine
;
use
Hyperf\Utils\Context
;
use
Swoole\Server
;
use
Swoole\Server
;
use
Throwable
;
use
Throwable
;
...
@@ -66,10 +67,10 @@ class LogTraceHandler
...
@@ -66,10 +67,10 @@ class LogTraceHandler
* @return string
* @return string
* @throws \Exception
* @throws \Exception
*/
*/
public
static
function
getRequestId
(
$isInAsyncCoroutine
=
false
)
public
static
function
getRequestId
()
{
{
$workId
=
posix_getpid
();
$workId
=
posix_getpid
();
$cid
=
$isInAsyncCoroutine
?
Coroutine
::
parentId
(
Coroutine
::
id
())
:
Coroutine
::
id
(
);
$cid
=
Context
::
get
(
'log_trace_request_id'
);
if
(
!
$cid
)
throw
new
\Exception
(
'无法使用协程标记录日志'
);
if
(
!
$cid
)
throw
new
\Exception
(
'无法使用协程标记录日志'
);
return
container
(
Server
::
class
)
->
stats
()[
'start_time'
]
.
$workId
.
$cid
;
return
container
(
Server
::
class
)
->
stats
()[
'start_time'
]
.
$workId
.
$cid
;
}
}
...
@@ -114,7 +115,7 @@ class LogTraceHandler
...
@@ -114,7 +115,7 @@ class LogTraceHandler
$logInfo
.=
"
\n\n
"
;
$logInfo
.=
"
\n\n
"
;
container
(
LogTraceQueue
::
class
)
->
addToQueue
([
container
(
LogTraceQueue
::
class
)
->
addToQueue
([
'request_id'
=>
$requestId
?:
self
::
getRequestId
(
$isInAsyncCoroutine
),
'request_id'
=>
self
::
getRequestId
(
),
'process_info'
=>
$logInfo
'process_info'
=>
$logInfo
]);
]);
// $log = LogTrace::where('request_id', self::getRequestId())->first();
// $log = LogTrace::where('request_id', self::getRequestId())->first();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment