Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
X
xyqb-user2
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
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
head_group
xyqb-user2
Commits
88f90166
Commit
88f90166
authored
Feb 10, 2020
by
杨锐
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
redis分布式锁,处理/user/center/save/userExtInfo并发问题,导致MySQLIntegrityConstraintViolationException
parent
e4e73cce
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
15 deletions
+16
-15
RedisLock.java
src/main/java/cn/quantgroup/xyqb/util/RedisLock.java
+16
-15
No files found.
src/main/java/cn/quantgroup/xyqb/util/RedisLock.java
View file @
88f90166
package
cn
.
quantgroup
.
xyqb
.
util
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.text.RandomStringGenerator
;
import
org.springframework.dao.InvalidDataAccessResourceUsageException
;
import
org.springframework.data.redis.core.RedisCallback
;
...
...
@@ -17,14 +18,14 @@ import java.util.Random;
* // CODE
* }
* } catch (InterruptedException e) {
*
LOGGER
.warn("获取锁失败:lockKey:{},exception:{}",redisLock.getLockKey(),e.getMessage());
*
log
.warn("获取锁失败:lockKey:{},exception:{}",redisLock.getLockKey(),e.getMessage());
* }finally {
* redisLock.unlock();
* }
* </pre>
*/
@Slf4j
public
class
RedisLock
{
private
final
static
org
.
slf4j
.
Logger
logger
=
org
.
slf4j
.
LoggerFactory
.
getLogger
(
RedisLock
.
class
);
private
RedisTemplate
redisTemplate
;
/**
* Lock key path.
...
...
@@ -56,7 +57,7 @@ public class RedisLock {
public
RedisLock
(
RedisTemplate
redisTemplate
,
String
lockKey
)
{
this
.
redisTemplate
=
redisTemplate
;
this
.
lockKey
=
"lock:"
.
concat
(
lockKey
).
concat
(
"_lock"
);
log
ger
.
info
(
"准备锁 lock:{},{}"
,
getLockKey
());
log
.
info
(
"准备锁 lock:{},{}"
,
getLockKey
());
}
/**
...
...
@@ -107,7 +108,7 @@ public class RedisLock {
return
serializer
.
deserialize
(
data
);
});
}
catch
(
Exception
e
)
{
log
ger
.
error
(
"get redis error, key "
+
key
+
": {}"
,
e
);
log
.
error
(
"get redis error, key "
+
key
+
": {}"
,
e
);
throw
new
InvalidDataAccessResourceUsageException
(
e
.
getMessage
());
}
return
obj
!=
null
?
obj
.
toString
()
:
null
;
...
...
@@ -134,7 +135,7 @@ public class RedisLock {
return
success
;
});
}
catch
(
Exception
e
)
{
log
ger
.
error
(
"setNX redis error, key “"
+
key
+
"”: {}"
,
e
);
log
.
error
(
"setNX redis error, key “"
+
key
+
"”: {}"
,
e
);
}
return
obj
!=
null
?
(
Boolean
)
obj
:
false
;
}
...
...
@@ -156,7 +157,7 @@ public class RedisLock {
return
serializer
.
deserialize
(
ret
);
});
}
catch
(
Exception
e
)
{
log
ger
.
error
(
"getSet redis error, key : {}"
,
key
);
log
.
error
(
"getSet redis error, key : {}"
,
key
);
}
return
obj
!=
null
?
(
String
)
obj
:
null
;
}
...
...
@@ -179,7 +180,7 @@ public class RedisLock {
if
(
this
.
setNX
(
lockKey
,
expiresStr
))
{
// lock acquired
this
.
expiresStr
=
expiresStr
;
log
ger
.
info
(
"获取锁成功 lock:{}"
,
getLockKey
());
log
.
info
(
"获取锁成功 lock:{}"
,
getLockKey
());
return
true
;
}
...
...
@@ -199,7 +200,7 @@ public class RedisLock {
//[分布式的情况下]:如过这个时候,多个线程恰好都到了这里,但是只有一个线程的设置值和当前值相同,他才有权利获取锁
// lock acquired
this
.
expiresStr
=
expiresStr
;
log
ger
.
info
(
"获取锁成功(前一个锁超时) lock:{}"
,
getLockKey
());
log
.
info
(
"获取锁成功(前一个锁超时) lock:{}"
,
getLockKey
());
return
true
;
}
}
...
...
@@ -213,8 +214,8 @@ public class RedisLock {
Thread
.
sleep
(
sleepTime
);
}
catch
(
InvalidDataAccessResourceUsageException
e
)
{
if
(
retryCount
>
3
)
{
log
ger
.
error
(
"redis有可能出现问题不能获取lockKey:{}"
,
lockKey
);
log
ger
.
warn
(
"redis lock 3次重试仍异常,lockKey: {} 进行降级服务...."
,
lockKey
);
log
.
error
(
"redis有可能出现问题不能获取lockKey:{}"
,
lockKey
);
log
.
warn
(
"redis lock 3次重试仍异常,lockKey: {} 进行降级服务...."
,
lockKey
);
return
true
;
}
retryCount
++;
...
...
@@ -225,7 +226,7 @@ public class RedisLock {
Thread
.
sleep
(
100
);
}
}
}
log
ger
.
info
(
"锁失败 lock:{}"
,
getLockKey
());
log
.
info
(
"锁失败 lock:{}"
,
getLockKey
());
return
false
;
}
...
...
@@ -240,18 +241,18 @@ public class RedisLock {
try
{
expStr
=
this
.
get
(
lockKey
);
}
catch
(
InvalidDataAccessResourceUsageException
e
)
{
log
ger
.
warn
(
"redis unlock 异常, lockKey:{} 进行降级服务...."
,
lockKey
);
log
.
warn
(
"redis unlock 异常, lockKey:{} 进行降级服务...."
,
lockKey
);
}
if
(
expStr
==
null
||
this
.
expiresStr
.
equals
(
expStr
))
{
redisTemplate
.
delete
(
lockKey
);
log
ger
.
info
(
"解锁成功 lock:{}"
,
getLockKey
());
log
.
info
(
"解锁成功 lock:{}"
,
getLockKey
());
return
;
}
}
catch
(
Exception
e
)
{
log
ger
.
error
(
"redis unlock 异常 lockKey "
+
lockKey
+
":{} "
,
e
);
log
.
error
(
"redis unlock 异常 lockKey "
+
lockKey
+
":{} "
,
e
);
}
}
log
ger
.
warn
(
"没有锁或者因非本对象产生的锁,不能进行解锁,因为是不安全的"
);
log
.
warn
(
"没有锁或者因非本对象产生的锁,不能进行解锁,因为是不安全的"
);
}
...
...
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