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
e18ab00e
Commit
e18ab00e
authored
Sep 24, 2020
by
胡青青
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
redis缓存命中率低,添加jedisCluster操作redis命令top5日志,日志刷入磁盘,每半个小时刷一次磁盘
parent
e8f3b7b8
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
168 additions
and
0 deletions
+168
-0
JedisClusterAspect.java
...ava/com/pcloud/common/core/aspect/JedisClusterAspect.java
+168
-0
No files found.
pcloud-common-core/src/main/java/com/pcloud/common/core/aspect/JedisClusterAspect.java
0 → 100644
View file @
e18ab00e
package
com
.
pcloud
.
common
.
core
.
aspect
;
import
com.pcloud.common.utils.DateUtils
;
import
lombok.extern.slf4j.Slf4j
;
import
org.aspectj.lang.JoinPoint
;
import
org.aspectj.lang.annotation.AfterReturning
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.aspectj.lang.annotation.Pointcut
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.ResourceUtils
;
import
java.io.*
;
import
java.util.Date
;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.atomic.AtomicInteger
;
import
java.util.concurrent.atomic.LongAdder
;
/**
* 添加监控,查看
*/
@Component
@Aspect
@Slf4j
public
class
JedisClusterAspect
{
//需要统计的方法调用次数
private
LongAdder
allExistsLongAdder
=
new
LongAdder
();
private
LongAdder
allLpushLongAdder
=
new
LongAdder
();
private
LongAdder
allGetLongAdder
=
new
LongAdder
();
private
LongAdder
allRpopLongAdder
=
new
LongAdder
();
private
LongAdder
allHgetAllLongAdder
=
new
LongAdder
();
private
LongAdder
missExistsLongAdder
=
new
LongAdder
();
private
LongAdder
missGetLongAdder
=
new
LongAdder
();
private
LongAdder
missRpopLongAdder
=
new
LongAdder
();
private
LongAdder
missHgetAllLongAdder
=
new
LongAdder
();
private
ConcurrentHashMap
<
String
,
AtomicInteger
>
missKeyMap
=
new
ConcurrentHashMap
<>();
private
ConcurrentHashMap
<
String
,
AtomicInteger
>
tempMap
=
new
ConcurrentHashMap
<>();
public
JedisClusterAspect
()
{
new
PringLogThread
().
start
();
}
@Pointcut
(
"execution(* redis.clients.jedis.JedisCluster.*(..))"
)
public
void
pointcut
()
{
}
class
PringLogThread
extends
Thread
{
@Override
public
void
run
()
{
String
path
=
getResourceBasePath
()
+
File
.
separator
+
"redislog"
+
File
.
separator
;
File
file
=
new
File
(
path
);
if
(!
file
.
exists
())
{
file
.
mkdirs
();
}
while
(
true
)
{
BufferedWriter
out
=
null
;
try
{
Thread
.
sleep
(
30
*
60
*
1000
);
tempMap
.
clear
();
tempMap
.
putAll
(
missKeyMap
);
missKeyMap
.
clear
();
StringBuffer
logStr
=
new
StringBuffer
(
"lpush调用:"
+
allLpushLongAdder
.
intValue
()
+
"次"
);
logStr
.
append
(
"\n"
);
logStr
.
append
(
"exists调用:"
+
allExistsLongAdder
.
intValue
()
+
"次,未命中:"
+
missExistsLongAdder
.
intValue
()
+
"次"
);
logStr
.
append
(
"\n"
);
logStr
.
append
(
"get调用:"
+
allGetLongAdder
.
intValue
()
+
"次,未命中:"
+
missGetLongAdder
.
intValue
()
+
"次"
);
logStr
.
append
(
"\n"
);
logStr
.
append
(
"rpop调用:"
+
allRpopLongAdder
.
intValue
()
+
"次,未命中:"
+
missRpopLongAdder
.
intValue
()
+
"次"
);
logStr
.
append
(
"\n"
);
logStr
.
append
(
"hgetall调用:"
+
allHgetAllLongAdder
.
intValue
()
+
"次,未命中:"
+
missHgetAllLongAdder
.
intValue
()
+
"次"
);
logStr
.
append
(
"\n"
);
String
fileName
=
"redisOperate"
+
DateUtils
.
getYmdHmsTime
()
+
".log"
;
out
=
new
BufferedWriter
(
new
FileWriter
(
path
+
fileName
));
out
.
write
(
logStr
.
toString
());
out
.
newLine
();
for
(
Map
.
Entry
<
String
,
AtomicInteger
>
entry
:
tempMap
.
entrySet
())
{
out
.
write
(
entry
.
getKey
()
+
"未命中:"
+
entry
.
getValue
().
intValue
());
out
.
newLine
();
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
try
{
out
.
close
();
}
catch
(
IOException
e
)
{
}
}
}
}
}
@AfterReturning
(
value
=
"pointcut()"
,
returning
=
"result"
)
public
void
afterReturning
(
JoinPoint
joinPoint
,
Object
result
)
{
String
methodName
=
joinPoint
.
getSignature
().
getName
();
Object
[]
obj
=
joinPoint
.
getArgs
();
String
key
=
obj
[
0
].
toString
();
switch
(
methodName
)
{
case
"exists"
:
allExistsLongAdder
.
add
(
1
);
if
(
result
.
toString
().
equals
(
"false"
))
{
missExistsLongAdder
.
add
(
1
);
addMissCount
(
missKeyMap
,
key
);
}
break
;
case
"lpush"
:
allLpushLongAdder
.
add
(
1
);
break
;
case
"get"
:
allGetLongAdder
.
add
(
1
);
if
(
result
==
null
)
{
missGetLongAdder
.
add
(
1
);
addMissCount
(
missKeyMap
,
key
);
if
(
missKeyMap
.
get
(
key
).
intValue
()
>
5
)
{
log
.
warn
(
"key: {}未命中 {}次"
,
key
,
missKeyMap
.
get
(
key
).
intValue
());
}
}
break
;
case
"rpop"
:
allRpopLongAdder
.
add
(
1
);
if
(
result
==
null
)
{
missRpopLongAdder
.
add
(
1
);
addMissCount
(
missKeyMap
,
key
);
}
break
;
case
"hgetAll"
:
allHgetAllLongAdder
.
add
(
1
);
if
(
result
==
null
)
{
missHgetAllLongAdder
.
add
(
1
);
addMissCount
(
missKeyMap
,
key
);
}
break
;
default
:
}
}
private
static
String
getResourceBasePath
()
{
// 获取跟目录
File
path
=
null
;
try
{
path
=
new
File
(
ResourceUtils
.
getURL
(
"classpath:"
).
getPath
());
}
catch
(
FileNotFoundException
e
)
{
// nothing to do
}
if
(
path
==
null
||
!
path
.
exists
())
{
path
=
new
File
(
""
);
}
String
pathStr
=
path
.
getAbsolutePath
();
// 如果是在eclipse中运行,则和target同级目录,如果是jar部署到服务器,则默认和jar包同级
pathStr
=
pathStr
.
replace
(
"\\target\\classes"
,
""
);
return
pathStr
;
}
public
void
addMissCount
(
ConcurrentHashMap
<
String
,
AtomicInteger
>
map
,
String
key
)
{
AtomicInteger
missCount
=
map
.
get
(
key
);
if
(
missCount
==
null
)
{
missCount
=
new
AtomicInteger
(
1
);
}
else
{
missCount
.
getAndIncrement
();
}
map
.
put
(
key
,
missCount
);
}
}
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