Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
midjourney-proxy
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
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
徐少华
midjourney-proxy
Commits
6c618daa
Commit
6c618daa
authored
Jul 15, 2019
by
songxiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加内部接口调用链路跟踪
parent
2d9ecb8d
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
336 additions
and
18 deletions
+336
-18
Application.java
...n/src/main/java/com/pcloud/common/config/Application.java
+22
-0
ResponseDto.java
...mmon/src/main/java/com/pcloud/common/dto/ResponseDto.java
+56
-3
QueueNameConstant.java
...java/com/pcloud/common/mq/constant/QueueNameConstant.java
+14
-0
ServerLinkDTO.java
...src/main/java/com/pcloud/common/mq/dto/ServerLinkDTO.java
+65
-0
ServerLinkQueue.java
...main/java/com/pcloud/common/mq/queue/ServerLinkQueue.java
+54
-0
ResponseHandleUtil.java
...main/java/com/pcloud/common/utils/ResponseHandleUtil.java
+117
-14
ThreadUtil.java
...mon/src/main/java/com/pcloud/common/utils/ThreadUtil.java
+8
-1
No files found.
pcloud-common/src/main/java/com/pcloud/common/config/Application.java
0 → 100644
View file @
6c618daa
package
com
.
pcloud
.
common
.
config
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
/**
* @Author: songx
* @Date: 2019年07月11日, 14:11
*/
@Component
public
class
Application
{
/**
* 应用名称
*/
public
static
String
APP_NAME
;
@Value
(
"${spring.application.name}"
)
public
void
setAppName
(
String
appName
)
{
this
.
APP_NAME
=
appName
;
}
}
pcloud-common/src/main/java/com/pcloud/common/dto/ResponseDto.java
View file @
6c618daa
package
com
.
pcloud
.
common
.
dto
;
package
com
.
pcloud
.
common
.
dto
;
import
java.io.Serializable
;
import
com.alibaba.fastjson.JSON
;
import
com.fasterxml.jackson.annotation.JsonInclude
;
import
com.fasterxml.jackson.annotation.JsonInclude
;
import
com.fasterxml.jackson.annotation.JsonInclude.Include
;
import
com.fasterxml.jackson.annotation.JsonInclude.Include
;
import
java.io.Serializable
;
/**
/**
* @描述:服务响应实体类
* @描述:服务响应实体类
* @作者:shichunshan
* @作者:shichunshan
...
@@ -44,6 +45,26 @@ public class ResponseDto<T> implements Serializable {
...
@@ -44,6 +45,26 @@ public class ResponseDto<T> implements Serializable {
private
T
data
;
private
T
data
;
/**
/**
* 生产者名称
*/
private
String
produceAppName
;
/**
* 生产者IP
*/
private
String
produceIp
;
/**
* 生产者类名
*/
private
String
produceClassName
;
/**
* 生产者方法名
*/
private
String
produceMethodName
;
/**
* 默认成功
* 默认成功
*/
*/
public
ResponseDto
()
{
public
ResponseDto
()
{
...
@@ -110,9 +131,41 @@ public class ResponseDto<T> implements Serializable {
...
@@ -110,9 +131,41 @@ public class ResponseDto<T> implements Serializable {
this
.
data
=
data
;
this
.
data
=
data
;
}
}
public
String
getProduceAppName
()
{
return
produceAppName
;
}
public
void
setProduceAppName
(
String
produceAppName
)
{
this
.
produceAppName
=
produceAppName
;
}
public
String
getProduceClassName
()
{
return
produceClassName
;
}
public
void
setProduceClassName
(
String
produceClassName
)
{
this
.
produceClassName
=
produceClassName
;
}
public
String
getProduceMethodName
()
{
return
produceMethodName
;
}
public
void
setProduceMethodName
(
String
produceMethodName
)
{
this
.
produceMethodName
=
produceMethodName
;
}
public
String
getProduceIp
()
{
return
produceIp
;
}
public
void
setProduceIp
(
String
produceIp
)
{
this
.
produceIp
=
produceIp
;
}
@Override
@Override
public
String
toString
()
{
public
String
toString
()
{
return
"ResponseDto [errCode="
+
errCode
+
", message="
+
message
+
", data="
+
data
+
"]"
;
return
"ResponseDto -> "
+
JSON
.
toJSONString
(
this
)
;
}
}
}
}
pcloud-common/src/main/java/com/pcloud/common/mq/constant/QueueNameConstant.java
0 → 100644
View file @
6c618daa
package
com
.
pcloud
.
common
.
mq
.
constant
;
/**
* @Author: songx
* @Date: 2019年07月11日, 14:48
*/
public
class
QueueNameConstant
{
/**
* 服务调用链路
*/
public
static
final
String
SERVER_LINK
=
"serverLinkQueue"
;
}
pcloud-common/src/main/java/com/pcloud/common/mq/dto/ServerLinkDTO.java
0 → 100644
View file @
6c618daa
package
com
.
pcloud
.
common
.
mq
.
dto
;
import
com.pcloud.common.dto.BaseDto
;
import
lombok.Getter
;
import
lombok.Setter
;
import
lombok.ToString
;
/**
* @Author: songx
* @Date: 2019年07月11日, 14:52
*/
@Getter
@Setter
@ToString
(
callSuper
=
true
)
public
class
ServerLinkDTO
extends
BaseDto
{
private
static
final
long
serialVersionUID
=
2029227731936018946L
;
/**
* 消费者服务名称
*/
private
String
consumerAppName
;
/**
* 消费者IP
*/
private
String
consumerIp
;
/**
* 消费者类名
*/
private
String
consumerClassName
;
/**
* 消费者方法名
*/
private
String
consumerMethodName
;
/**
* 生产者服务名称
*/
private
String
produceAppName
;
/**
* 生产者IP
*/
private
String
produceIp
;
/**
* 生产者类名
*/
private
String
produceClassName
;
/**
* 生产者方法名
*/
private
String
produceMethodName
;
/**
* 完整的调用链路
*/
private
String
links
;
}
pcloud-common/src/main/java/com/pcloud/common/mq/queue/ServerLinkQueue.java
0 → 100644
View file @
6c618daa
/**
*
*/
package
com
.
pcloud
.
common
.
mq
.
queue
;
import
org.springframework.amqp.core.AmqpTemplate
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
com.pcloud.common.exceptions.BizException
;
import
com.pcloud.common.mq.constant.QueueNameConstant
;
import
com.pcloud.common.mq.dto.ServerLinkDTO
;
import
lombok.extern.slf4j.Slf4j
;
/**
* @author:songx
* @date:2019/6/20,11:37
*/
@Slf4j
@Component
public
class
ServerLinkQueue
{
/**
* 服务调用链路QUEUE
*/
public
static
void
send
(
ServerLinkDTO
serverLinkDTO
)
throws
BizException
{
log
.
info
(
"【QUEUE】服务调用链路.[serverLinkDTO]->"
+
serverLinkDTO
);
if
(
serverLinkDTO
==
null
)
{
throw
BizException
.
PARAM_IS_NULL
;
}
getAmqpTemplate
().
convertAndSend
(
QueueNameConstant
.
SERVER_LINK
,
serverLinkDTO
);
}
/**
* 获取实例
*
* @return
*/
public
static
AmqpTemplate
getAmqpTemplate
()
{
return
SingletonInstance
.
amqpTemplate
;
}
@Component
public
static
class
SingletonInstance
{
private
static
AmqpTemplate
amqpTemplate
;
public
SingletonInstance
(
@Autowired
AmqpTemplate
amqpTemplate
)
{
this
.
amqpTemplate
=
amqpTemplate
;
}
}
}
pcloud-common/src/main/java/com/pcloud/common/utils/ResponseHandleUtil.java
View file @
6c618daa
...
@@ -4,16 +4,22 @@ import java.math.BigDecimal;
...
@@ -4,16 +4,22 @@ import java.math.BigDecimal;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.ResponseEntity
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONArray
;
import
com.alibaba.fastjson.JSONArray
;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.JSONObject
;
import
com.google.common.collect.Maps
;
import
com.google.common.collect.Maps
;
import
com.pcloud.common.config.Application
;
import
com.pcloud.common.dto.ResponseDto
;
import
com.pcloud.common.dto.ResponseDto
;
import
com.pcloud.common.exceptions.BizException
;
import
com.pcloud.common.exceptions.BizException
;
import
com.pcloud.common.mq.dto.ServerLinkDTO
;
import
com.pcloud.common.mq.queue.ServerLinkQueue
;
import
com.pcloud.common.utils.nginx.NginxUtils
;
/**
/**
* Created by zengqiang on 17-12-14.
* Created by zengqiang on 17-12-14.
...
@@ -25,10 +31,8 @@ public class ResponseHandleUtil {
...
@@ -25,10 +31,8 @@ public class ResponseHandleUtil {
/**
/**
* 解析一层list包装泛型数据
* 解析一层list包装泛型数据
*
*
* @param obj
* @param obj 需要解析的数据对象
* 需要解析的数据对象
* @param clazz list内元素类型
* @param clazz
* list内元素类型
* @param <T>
* @param <T>
* @return
* @return
*/
*/
...
@@ -39,6 +43,14 @@ public class ResponseHandleUtil {
...
@@ -39,6 +43,14 @@ public class ResponseHandleUtil {
return
JSON
.
parseArray
(
JSON
.
toJSONString
(
obj
),
clazz
);
return
JSON
.
parseArray
(
JSON
.
toJSONString
(
obj
),
clazz
);
}
}
/**
* @param obj
* @param clazzK
* @param clazzV
* @param <K>
* @param <V>
* @return
*/
public
static
<
K
,
V
>
Map
<
K
,
V
>
getMapData
(
Object
obj
,
Class
<
K
>
clazzK
,
Class
<
V
>
clazzV
)
{
public
static
<
K
,
V
>
Map
<
K
,
V
>
getMapData
(
Object
obj
,
Class
<
K
>
clazzK
,
Class
<
V
>
clazzV
)
{
if
(
null
==
obj
)
{
if
(
null
==
obj
)
{
return
null
;
return
null
;
...
@@ -54,6 +66,7 @@ public class ResponseHandleUtil {
...
@@ -54,6 +66,7 @@ public class ResponseHandleUtil {
/**
/**
* 解析map的value是list的泛型数据
* 解析map的value是list的泛型数据
*
* @param obj 需要解析的数据对象
* @param obj 需要解析的数据对象
* @param clazzK key元素类型
* @param clazzK key元素类型
* @param clazzT value中list元素类型
* @param clazzT value中list元素类型
...
@@ -72,6 +85,12 @@ public class ResponseHandleUtil {
...
@@ -72,6 +85,12 @@ public class ResponseHandleUtil {
return
resultMap
;
return
resultMap
;
}
}
/**
* @param entity
* @param clazz
* @param <T>
* @return
*/
public
static
<
T
>
T
parseResponse
(
ResponseEntity
<?>
entity
,
Class
<
T
>
clazz
)
{
public
static
<
T
>
T
parseResponse
(
ResponseEntity
<?>
entity
,
Class
<
T
>
clazz
)
{
Object
obj
=
parseResponse
(
entity
);
Object
obj
=
parseResponse
(
entity
);
if
(
null
==
obj
)
{
if
(
null
==
obj
)
{
...
@@ -120,8 +139,7 @@ public class ResponseHandleUtil {
...
@@ -120,8 +139,7 @@ public class ResponseHandleUtil {
*
*
* @param entity
* @param entity
* @param clazz
* @param clazz
* @param <T>
* @param <T> List元素类型
* List元素类型
* @return
* @return
*/
*/
public
static
<
T
>
List
<
T
>
parseListResponse
(
ResponseEntity
<?>
entity
,
Class
<
T
>
clazz
)
{
public
static
<
T
>
List
<
T
>
parseListResponse
(
ResponseEntity
<?>
entity
,
Class
<
T
>
clazz
)
{
...
@@ -129,17 +147,13 @@ public class ResponseHandleUtil {
...
@@ -129,17 +147,13 @@ public class ResponseHandleUtil {
if
(
null
==
list
)
{
if
(
null
==
list
)
{
return
list
;
return
list
;
}
}
return
JSON
.
parseArray
(
JSON
.
toJSONString
(
list
),
clazz
);
return
JSON
.
parseArray
(
JSON
.
toJSONString
(
list
),
clazz
);
}
}
/**
/**
*
* @param entity
* @param entity
* @param clazzK
* @param clazzK key class类型
* key class类型
* @param clazzV value class类型
* @param clazzV
* value class类型
* @param <K>
* @param <K>
* @param <V>
* @param <V>
* @return
* @return
...
@@ -149,7 +163,6 @@ public class ResponseHandleUtil {
...
@@ -149,7 +163,6 @@ public class ResponseHandleUtil {
if
(
null
==
map
)
{
if
(
null
==
map
)
{
return
map
;
return
map
;
}
}
Map
<
K
,
V
>
result
=
new
HashMap
<>();
Map
<
K
,
V
>
result
=
new
HashMap
<>();
for
(
Object
o
:
map
.
entrySet
())
{
for
(
Object
o
:
map
.
entrySet
())
{
Map
.
Entry
entry
=
(
Map
.
Entry
)
o
;
Map
.
Entry
entry
=
(
Map
.
Entry
)
o
;
...
@@ -193,6 +206,7 @@ public class ResponseHandleUtil {
...
@@ -193,6 +206,7 @@ public class ResponseHandleUtil {
/**
/**
* Map List转换
* Map List转换
*
* @param entity 需要解析的数据对象
* @param entity 需要解析的数据对象
* @param clazzK key元素类型
* @param clazzK key元素类型
* @param clazzT value中list元素类型
* @param clazzT value中list元素类型
...
@@ -206,6 +220,12 @@ public class ResponseHandleUtil {
...
@@ -206,6 +220,12 @@ public class ResponseHandleUtil {
return
getMapListData
(
object
,
clazzK
,
clazzT
);
return
getMapListData
(
object
,
clazzK
,
clazzT
);
}
}
/**
* 内部接口返回值解析
*
* @param entity
* @return
*/
public
static
Object
parseResponse
(
ResponseEntity
<?>
entity
)
{
public
static
Object
parseResponse
(
ResponseEntity
<?>
entity
)
{
if
(
null
==
entity
)
{
if
(
null
==
entity
)
{
LOGGER
.
warn
(
"null response object"
);
LOGGER
.
warn
(
"null response object"
);
...
@@ -232,16 +252,98 @@ public class ResponseHandleUtil {
...
@@ -232,16 +252,98 @@ public class ResponseHandleUtil {
LOGGER
.
warn
(
"【业务异常】:"
+
body
.
getErrCode
()
+
", 异常信息: "
+
body
.
getMessage
());
LOGGER
.
warn
(
"【业务异常】:"
+
body
.
getErrCode
()
+
", 异常信息: "
+
body
.
getMessage
());
throw
new
BizException
(
body
.
getErrCode
(),
body
.
getMessage
());
throw
new
BizException
(
body
.
getErrCode
(),
body
.
getMessage
());
}
}
// 获取当前的整个链路的调用类和方法,发送服务调用链queue
try
{
StackTraceElement
[]
stackTraceElements
=
new
Exception
().
getStackTrace
();
ThreadUtil
.
FIXED_POOL
.
execute
(()
->
sendServerLink
(
body
,
stackTraceElements
));
}
catch
(
Exception
e
)
{
LOGGER
.
warn
(
"【内部调用】获取调用类和方法失败:"
+
e
.
getMessage
(),
e
);
}
return
body
.
getData
();
return
body
.
getData
();
}
}
/**
* 发送服务调用链queue
*
* @param body
*/
private
static
void
sendServerLink
(
ResponseDto
<?>
body
,
StackTraceElement
[]
stackTraceElements
)
{
ServerLinkDTO
serverLinkDTO
=
new
ServerLinkDTO
();
int
index
=
2
;
// 如果第二级是parseMap或parseList表明是当前类的方法,则索引更新为3
String
methodName
=
stackTraceElements
[
index
].
getMethodName
();
if
(
methodName
.
startsWith
(
"parseMap"
)
||
methodName
.
startsWith
(
"parseList"
))
{
index
=
3
;
}
serverLinkDTO
.
setConsumerClassName
(
stackTraceElements
[
index
].
getClassName
());
serverLinkDTO
.
setConsumerMethodName
(
stackTraceElements
[
index
].
getMethodName
());
serverLinkDTO
.
setConsumerAppName
(
Application
.
APP_NAME
);
serverLinkDTO
.
setConsumerIp
(
NginxUtils
.
getLocalAddress
());
serverLinkDTO
.
setProduceAppName
(
body
.
getProduceAppName
());
serverLinkDTO
.
setProduceIp
(
body
.
getProduceIp
());
serverLinkDTO
.
setProduceClassName
(
body
.
getProduceClassName
());
serverLinkDTO
.
setProduceMethodName
(
body
.
getProduceMethodName
());
serverLinkDTO
.
setLinks
(
appendLinks
(
stackTraceElements
,
index
));
try
{
ServerLinkQueue
.
send
(
serverLinkDTO
);
}
catch
(
Exception
e
)
{
LOGGER
.
warn
(
"【内部调用】发送服务调用链QUEUE失败:"
+
e
.
getMessage
(),
e
);
}
}
/**
* 拼接整个调用链路
*
* @param stackTraceElements
* @return
*/
private
static
String
appendLinks
(
StackTraceElement
[]
stackTraceElements
,
int
index
)
{
if
(
stackTraceElements
==
null
||
stackTraceElements
.
length
==
0
)
{
return
null
;
}
int
i
=
0
;
StringBuilder
links
=
new
StringBuilder
();
for
(
StackTraceElement
element
:
stackTraceElements
)
{
// 过滤index以前的数据,index以前的数据都是本类的方法,无需记录
if
(
i
<
index
)
{
i
++;
continue
;
}
String
className
=
element
.
getClassName
();
// 只拼接调用的业务类和方法,JDK的类不拼接
if
(!
className
.
startsWith
(
"com.pcloud"
)
||
className
.
indexOf
(
"$$FastClassBySpringCGLIB$$"
)
>
0
||
className
.
indexOf
(
"$$EnhancerBySpringCGLIB$$"
)
>
0
)
{
continue
;
}
links
.
append
(
className
).
append
(
"."
).
append
(
element
.
getMethodName
()).
append
(
"->"
);
}
return
links
.
toString
();
}
/**
* 内部接口封装返回类型
*
* @param data
* @param <T>
* @return
*/
public
static
<
T
>
ResponseEntity
<
ResponseDto
<
T
>>
toResponse
(
T
data
)
{
public
static
<
T
>
ResponseEntity
<
ResponseDto
<
T
>>
toResponse
(
T
data
)
{
if
(
null
==
data
)
{
if
(
null
==
data
)
{
LOGGER
.
warn
(
"null data object"
);
LOGGER
.
warn
(
"null data object"
);
return
null
;
return
null
;
}
}
ResponseDto
<
T
>
body
=
new
ResponseDto
<>(
data
);
ResponseDto
<
T
>
body
=
new
ResponseDto
<>(
data
);
return
new
ResponseEntity
<
ResponseDto
<
T
>>(
body
,
HttpStatus
.
OK
);
try
{
body
.
setProduceAppName
(
Application
.
APP_NAME
);
body
.
setProduceIp
(
NginxUtils
.
getLocalAddress
());
StackTraceElement
stackTraceElement
=
new
Exception
().
getStackTrace
()[
1
];
body
.
setProduceClassName
(
stackTraceElement
.
getClassName
());
body
.
setProduceMethodName
(
stackTraceElement
.
getMethodName
());
}
catch
(
Exception
e
)
{
LOGGER
.
warn
(
"【内部接口】封装返回类型失败:"
+
e
.
getMessage
(),
e
);
}
return
new
ResponseEntity
<>(
body
,
HttpStatus
.
OK
);
}
}
}
}
\ No newline at end of file
pcloud-common/src/main/java/com/pcloud/common/utils/ThreadUtil.java
View file @
6c618daa
...
@@ -2,6 +2,8 @@ package com.pcloud.common.utils;
...
@@ -2,6 +2,8 @@ package com.pcloud.common.utils;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
/**
/**
* @描述:
* @描述:
...
@@ -11,10 +13,15 @@ import java.util.Map;
...
@@ -11,10 +13,15 @@ import java.util.Map;
*/
*/
public
class
ThreadUtil
{
public
class
ThreadUtil
{
/**
* 固定长度程池
*/
public
static
final
ExecutorService
FIXED_POOL
=
Executors
.
newFixedThreadPool
(
3
);
@SuppressWarnings
(
"rawtypes"
)
@SuppressWarnings
(
"rawtypes"
)
private
static
final
ThreadLocal
ctx
=
new
ThreadLocal
();
private
static
final
ThreadLocal
ctx
=
new
ThreadLocal
();
@SuppressWarnings
({
"rawtypes"
,
"unchecked"
})
@SuppressWarnings
({
"rawtypes"
,
"unchecked"
})
public
static
void
put
(
Object
key
,
Object
value
)
{
public
static
void
put
(
Object
key
,
Object
value
)
{
Map
m
=
(
Map
)
ctx
.
get
();
Map
m
=
(
Map
)
ctx
.
get
();
if
(
m
==
null
)
{
if
(
m
==
null
)
{
...
...
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