Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
Y
yifu-mvp
Project
Project
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
fangxinjiang
yifu-mvp
Commits
a57b9a73
Commit
a57b9a73
authored
Mar 08, 2023
by
fangxinjiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
分布式锁
parent
72fda5cf
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
63 additions
and
6 deletions
+63
-6
lock.lua
yifu-common/yifu-common-core/src/main/resources/lock.lua
+16
-0
lua.note
yifu-common/yifu-common-core/src/main/resources/lua.note
+29
-0
unlock.lua
yifu-common/yifu-common-core/src/main/resources/unlock.lua
+12
-0
TDispatchInfoController.java
...us/v1/yifu/social/controller/TDispatchInfoController.java
+2
-2
TPaymentInfoController.java
...lus/v1/yifu/social/controller/TPaymentInfoController.java
+3
-3
TPreDispatchInfoController.java
...v1/yifu/social/controller/TPreDispatchInfoController.java
+1
-1
No files found.
yifu-common/yifu-common-core/src/main/resources/lock.lua
0 → 100644
View file @
a57b9a73
local
key
=
KEYS
[
1
];
local
threadId
=
ARGV
[
1
];
local
releaseTime
=
ARGV
[
2
];
if
(
redis
.
call
(
'exists'
,
key
)
==
0
)
then
redis
.
call
(
'hset'
,
key
,
threadId
,
'1'
);
redis
.
call
(
'expire'
,
key
,
releaseTime
);
return
1
;
end
;
if
(
redis
.
call
(
'hexists'
,
key
,
threadId
)
==
1
)
then
redis
.
call
(
'hincrby'
,
key
,
threadId
,
'1'
);
redis
.
call
(
'expire'
,
key
,
releaseTime
);
return
1
;
end
;
return
0
;
yifu-common/yifu-common-core/src/main/resources/lua.note
0 → 100644
View file @
a57b9a73
local key = KEYS[1]; -- 第1个参数,锁的key
local threadId = ARGV[1]; -- 第2个参数,线程唯一标识
local releaseTime = ARGV[2]; -- 第3个参数,锁的自动释放时间
if(redis.call('exists', key) == 0) then -- 判断锁是否已存在
redis.call('hset', key, threadId, '1'); -- 不存在, 则获取锁
redis.call('expire', key, releaseTime); -- 设置有效期
return 1; -- 返回结果
end;
if(redis.call('hexists', key, threadId) == 1) then -- 锁已经存在,判断threadId是否是自己
redis.call('hincrby', key, threadId, '1'); -- 如果是自己,则重入次数+1
redis.call('expire', key, releaseTime); -- 设置有效期
return 1; -- 返回结果
end;
return 0; -- 代码走到这里,说明获取锁的不是自己,获取锁失败
local key = KEYS[1]; -- 第1个参数,锁的key
local threadId = ARGV[1]; -- 第2个参数,线程唯一标识
if (redis.call('HEXISTS', key, threadId) == 0) then -- 判断当前锁是否还是被自己持有
return nil; -- 如果已经不是自己,则直接返回
end;
local count = redis.call('HINCRBY', key, threadId, -1); -- 是自己的锁,则重入次数-1
if (count == 0) then -- 判断是否重入次数是否已经为0
redis.call('DEL', key); -- 等于0说明可以释放锁,直接删除
return nil;
end;
yifu-common/yifu-common-core/src/main/resources/unlock.lua
0 → 100644
View file @
a57b9a73
local
key
=
KEYS
[
1
];
local
threadId
=
ARGV
[
1
];
if
(
redis
.
call
(
'HEXISTS'
,
key
,
threadId
)
==
0
)
then
return
nil
;
end
;
local
count
=
redis
.
call
(
'HINCRBY'
,
key
,
threadId
,
-
1
);
if
(
count
==
0
)
then
redis
.
call
(
'DEL'
,
key
);
return
nil
;
end
;
yifu-social/yifu-social-biz/src/main/java/com/yifu/cloud/plus/v1/yifu/social/controller/TDispatchInfoController.java
View file @
a57b9a73
...
@@ -325,7 +325,7 @@ public class TDispatchInfoController {
...
@@ -325,7 +325,7 @@ public class TDispatchInfoController {
try
{
try
{
requestId
=
RedisDistributedLock
.
getLock
(
key
,
"10"
);
requestId
=
RedisDistributedLock
.
getLock
(
key
,
"10"
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
ResultConstants
.
NO_GETLOCK_DATA
);
throw
new
RuntimeException
(
ResultConstants
.
NO_GETLOCK_DATA
+
CommonConstants
.
DOWN_LINE_STRING
+
e
.
getMessage
()
);
}
}
try
{
try
{
return
tDispatchInfoService
.
importDiy
(
file
.
getInputStream
(),
orderId
);
return
tDispatchInfoService
.
importDiy
(
file
.
getInputStream
(),
orderId
);
...
@@ -407,7 +407,7 @@ public class TDispatchInfoController {
...
@@ -407,7 +407,7 @@ public class TDispatchInfoController {
try
{
try
{
requestId
=
RedisDistributedLock
.
getLock
(
key
,
"10"
);
requestId
=
RedisDistributedLock
.
getLock
(
key
,
"10"
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
ResultConstants
.
NO_GETLOCK_DATA
);
throw
new
RuntimeException
(
ResultConstants
.
NO_GETLOCK_DATA
+
CommonConstants
.
DOWN_LINE_STRING
+
e
.
getMessage
()
);
}
}
try
{
try
{
return
tDispatchInfoService
.
importReduceDiy
(
file
.
getInputStream
(),
orderId
);
return
tDispatchInfoService
.
importReduceDiy
(
file
.
getInputStream
(),
orderId
);
...
...
yifu-social/yifu-social-biz/src/main/java/com/yifu/cloud/plus/v1/yifu/social/controller/TPaymentInfoController.java
View file @
a57b9a73
...
@@ -165,7 +165,7 @@ public class TPaymentInfoController {
...
@@ -165,7 +165,7 @@ public class TPaymentInfoController {
try
{
try
{
requestId
=
RedisDistributedLock
.
getLock
(
key
,
"10"
);
requestId
=
RedisDistributedLock
.
getLock
(
key
,
"10"
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
ResultConstants
.
NO_GETLOCK_DATA
);
throw
new
RuntimeException
(
ResultConstants
.
NO_GETLOCK_DATA
+
CommonConstants
.
DOWN_LINE_STRING
+
e
.
getMessage
()
);
}
}
try
{
try
{
return
tPaymentInfoService
.
importSocialDiy
(
file
.
getInputStream
());
return
tPaymentInfoService
.
importSocialDiy
(
file
.
getInputStream
());
...
@@ -197,7 +197,7 @@ public class TPaymentInfoController {
...
@@ -197,7 +197,7 @@ public class TPaymentInfoController {
try
{
try
{
requestId
=
RedisDistributedLock
.
getLock
(
key
,
"10"
);
requestId
=
RedisDistributedLock
.
getLock
(
key
,
"10"
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
ResultConstants
.
NO_GETLOCK_DATA
);
throw
new
RuntimeException
(
ResultConstants
.
NO_GETLOCK_DATA
+
CommonConstants
.
DOWN_LINE_STRING
+
e
.
getMessage
()
);
}
}
try
{
try
{
return
tPaymentInfoService
.
importSocialHeFeiDiy
(
file
.
getInputStream
(),
random
,
type
);
return
tPaymentInfoService
.
importSocialHeFeiDiy
(
file
.
getInputStream
(),
random
,
type
);
...
@@ -229,7 +229,7 @@ public class TPaymentInfoController {
...
@@ -229,7 +229,7 @@ public class TPaymentInfoController {
try
{
try
{
requestId
=
RedisDistributedLock
.
getLock
(
key
,
"10"
);
requestId
=
RedisDistributedLock
.
getLock
(
key
,
"10"
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
ResultConstants
.
NO_GETLOCK_DATA
);
throw
new
RuntimeException
(
ResultConstants
.
NO_GETLOCK_DATA
+
CommonConstants
.
DOWN_LINE_STRING
+
e
.
getMessage
()
);
}
}
try
{
try
{
return
tPaymentInfoService
.
batchImportPaymentFundInfo
(
file
.
getInputStream
());
return
tPaymentInfoService
.
batchImportPaymentFundInfo
(
file
.
getInputStream
());
...
...
yifu-social/yifu-social-biz/src/main/java/com/yifu/cloud/plus/v1/yifu/social/controller/TPreDispatchInfoController.java
View file @
a57b9a73
...
@@ -248,7 +248,7 @@ public class TPreDispatchInfoController {
...
@@ -248,7 +248,7 @@ public class TPreDispatchInfoController {
try
{
try
{
requestId
=
RedisDistributedLock
.
getLock
(
key
,
"10"
);
requestId
=
RedisDistributedLock
.
getLock
(
key
,
"10"
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
ResultConstants
.
NO_GETLOCK_DATA
);
throw
new
RuntimeException
(
ResultConstants
.
NO_GETLOCK_DATA
+
CommonConstants
.
DOWN_LINE_STRING
+
e
.
getMessage
()
);
}
}
ExcelUtil
<
TPreDispatchInfo
>
util1
=
null
;
ExcelUtil
<
TPreDispatchInfo
>
util1
=
null
;
try
{
try
{
...
...
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