Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
PubTools-tools
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
liuxingyu
PubTools-tools
Commits
3810529e
Commit
3810529e
authored
Aug 11, 2023
by
liuxingyu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改文件夹名称
parent
85311783
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
144 additions
and
45 deletions
+144
-45
pom.xml
redisson-lock/pom.xml
+2
-2
AdminApplication.java
.../src/main/java/com/mushiny/redisson/AdminApplication.java
+18
-0
TestController.java
.../java/com/mushiny/redisson/controller/TestController.java
+98
-2
RedissonLockUtil.java
...ain/java/com/mushiny/redisson/utils/RedissonLockUtil.java
+1
-1
SnowFlakeUtil.java
...c/main/java/com/mushiny/redisson/utils/SnowFlakeUtil.java
+4
-19
application.yml
redisson-lock/src/main/resources/application.yml
+21
-0
application.yml_bak
redisson-lock/src/main/resources/application.yml_bak
+0
-21
No files found.
redisson-lock/pom.xml
View file @
3810529e
...
...
@@ -13,8 +13,8 @@
<artifactId>
redisson-lock
</artifactId>
<packaging>
jar
</packaging>
<properties>
<maven.compiler.source>
11
</maven.compiler.source>
<maven.compiler.target>
11
</maven.compiler.target>
<maven.compiler.source>
8
</maven.compiler.source>
<maven.compiler.target>
8
</maven.compiler.target>
</properties>
<dependencies>
...
...
redisson-lock/src/main/java/com/mushiny/redisson/AdminApplication.java
0 → 100644
View file @
3810529e
/*
* Copyright (c) 2020 牧星仓库管理系统 All rights reserved.
*
* http://www.mushiny.com
*
* 版权所有,侵权必究!
*/
package
com
.
mushiny
.
redisson
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
@SpringBootApplication
public
class
AdminApplication
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
AdminApplication
.
class
,
args
);
}
}
redisson-lock/src/main/java/com/mushiny/redisson/controller/TestController.java
View file @
3810529e
...
...
@@ -3,11 +3,15 @@ package com.mushiny.redisson.controller;
import
com.mushiny.redisson.enums.LockType
;
import
com.mushiny.redisson.utils.RedissonLockUtil
;
import
com.mushiny.redisson.utils.ResultForLock
;
import
jodd.net.HttpStatus
;
import
org.redisson.api.RLock
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
javax.servlet.http.HttpServletResponse
;
/**
* 描述信息
*
...
...
@@ -24,8 +28,10 @@ public class TestController {
public
int
count
=
0
;
@PostMapping
(
"api/ping"
)
public
ResultForLock
<
String
>
contextLoads
()
{
public
int
errCount
=
0
;
@PostMapping
(
"api/reentrant"
)
public
ResultForLock
<
String
>
reentrant
(
HttpServletResponse
response
)
{
String
threadId
=
Thread
.
currentThread
().
getName
()
+
Thread
.
currentThread
().
getId
();
redissonLockUtil
.
getLockByKey
(
"265213225444411"
,
LockType
.
REENTRANT_LOCK
.
value
());
Boolean
getLock
=
redissonLockUtil
.
tryLock
(
50000L
,
"265213225444411"
,
LockType
.
REENTRANT_LOCK
.
value
());
...
...
@@ -47,7 +53,97 @@ public class TestController {
redissonLockUtil
.
releaseLock
(
"265213225444411"
,
LockType
.
REENTRANT_LOCK
.
value
());
}
}
else
{
errCount
++;
System
.
out
.
println
(
"拿锁失败"
+
errCount
);
response
.
setStatus
(
500
);
return
new
ResultForLock
().
error
();
}
return
new
ResultForLock
().
ok
(
threadId
);
}
@PostMapping
(
"api/reentrantWatchDog"
)
public
ResultForLock
<
String
>
reentrantWatchDog
(
HttpServletResponse
response
)
{
String
threadId
=
Thread
.
currentThread
().
getName
()
+
Thread
.
currentThread
().
getId
();
redissonLockUtil
.
getLockByKey
(
"265213225444411"
,
LockType
.
REENTRANT_LOCK
.
value
());
Boolean
getLock
=
redissonLockUtil
.
tryLock
(
1000L
,-
1L
,
"265213225444411"
,
LockType
.
REENTRANT_LOCK
.
value
());
if
(
getLock
)
{
System
.
out
.
println
(
threadId
+
"拿锁成功"
);
try
{
count
++;
jdbcTemplate
.
execute
(
"UPDATE descrease a SET a.number = a.number-1"
);
// SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// Date startDate = new Date();
// System.out.println(threadId + "执行调度ping" + dateFormat.format(startDate));
Thread
.
sleep
(
2000
);
// System.out.println("结束时间为" + dateFormat.format(new Date()));
System
.
out
.
println
(
"执行数量为"
+
count
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
new
ResultForLock
().
error
();
}
finally
{
redissonLockUtil
.
releaseLock
(
"265213225444411"
,
LockType
.
REENTRANT_LOCK
.
value
());
}
}
else
{
System
.
out
.
println
(
"拿锁失败"
);
response
.
setStatus
(
500
);
return
new
ResultForLock
().
error
();
}
return
new
ResultForLock
().
ok
(
threadId
);
}
/**
* 手动加锁和解锁,最常用的方式
* @return
*/
@PostMapping
(
"api/reentrantByManual"
)
public
ResultForLock
<
String
>
reentrantByManual
()
{
String
threadId
=
Thread
.
currentThread
().
getName
()
+
Thread
.
currentThread
().
getId
();
RLock
rLock
=
redissonLockUtil
.
getLockByKey
(
"265213225444411"
,
LockType
.
REENTRANT_LOCK
.
value
());
// 支持过期解锁功能,10秒钟以后自动解锁, 无需调用unlock方法手动解锁
//lock.lock(10, TimeUnit.SECONDS);
rLock
.
lock
();
// * 加锁 锁的有效期默认30秒
try
{
count
++;
jdbcTemplate
.
execute
(
"UPDATE descrease a SET a.number = a.number-1"
);
Thread
.
sleep
(
2000
);
System
.
out
.
println
(
"执行数量为"
+
count
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
new
ResultForLock
().
error
();
}
finally
{
rLock
.
unlock
();
}
return
new
ResultForLock
().
ok
(
threadId
);
}
/**
* 使用公平锁,先请求的线程先拿到锁
* @return
*/
@PostMapping
(
"api/fair"
)
public
ResultForLock
<
String
>
fair
()
{
String
threadId
=
Thread
.
currentThread
().
getName
()
+
Thread
.
currentThread
().
getId
();
redissonLockUtil
.
getLockByKey
(
"265213225444411"
,
LockType
.
FAIR_LOCK
.
value
());
Boolean
getLock
=
redissonLockUtil
.
tryLock
(
50000L
,
"265213225444411"
,
LockType
.
FAIR_LOCK
.
value
());
if
(
getLock
)
{
System
.
out
.
println
(
threadId
+
"拿锁成功"
);
try
{
count
++;
jdbcTemplate
.
execute
(
"UPDATE descrease a SET a.number = a.number-1"
);
Thread
.
sleep
(
2000
);
System
.
out
.
println
(
"执行数量为"
+
count
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
return
new
ResultForLock
().
error
();
}
finally
{
redissonLockUtil
.
releaseLock
(
"265213225444411"
,
LockType
.
REENTRANT_LOCK
.
value
());
}
}
else
{
System
.
out
.
println
(
"拿锁失败"
);
return
new
ResultForLock
().
error
();
}
return
new
ResultForLock
().
ok
(
threadId
);
}
...
...
redisson-lock/src/main/java/com/mushiny/redisson/utils/RedissonLockUtil.java
View file @
3810529e
...
...
@@ -104,7 +104,7 @@ public class RedissonLockUtil {
lock
=
rLock
.
tryLock
(
waitTime
,
releaseTime
,
TimeUnit
.
MILLISECONDS
);
if
(!
lock
)
{
log
.
error
(
"分布式锁加锁:{}失败,请重新尝试"
);
throw
new
RedissonException
(
RedissonErrorCode
.
METHOD_IN_EXECUTION
);
//
throw new RedissonException(RedissonErrorCode.METHOD_IN_EXECUTION);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
...
...
redisson-lock/src/main/java/com/mushiny/redisson/utils/SnowFlakeUtil.java
View file @
3810529e
...
...
@@ -2,6 +2,7 @@ package com.mushiny.redisson.utils;
import
cn.hutool.core.util.IdUtil
;
import
cn.hutool.core.util.RandomUtil
;
import
org.springframework.stereotype.Component
;
import
java.util.UUID
;
...
...
@@ -101,25 +102,9 @@ public class SnowFlakeUtil {
* 产生下一个String类型的ID
*/
public
synchronized
String
nextStrId
()
{
String
prefix
=
RandomUtil
.
randomString
(
10
);
long
currStamp
=
getNewStamp
();
if
(
currStamp
<
lastStamp
)
{
throw
new
RuntimeException
(
"时钟后移,拒绝生成ID!"
);
}
if
(
currStamp
==
lastStamp
)
{
// 相同毫秒内,序列号自增
sequence
=
(
sequence
+
1
)
&
MAX_SEQUENCE
;
// 同一毫秒的序列数已经达到最大
if
(
sequence
==
0L
)
{
currStamp
=
getNextMill
();
}
}
else
{
// 不同毫秒内,序列号置为0
sequence
=
0L
;
}
lastStamp
=
currStamp
;
Long
outStamp
=
(
currStamp
-
START_STAMP
)
<<
8
;
return
prefix
+
outStamp
;
String
prefix
=
RandomUtil
.
randomString
(
5
);
String
netId
=
String
.
valueOf
(
nextId
());
return
prefix
+
netId
;
}
private
long
getNextMill
()
{
...
...
redisson-lock/src/main/resources/application.yml
0 → 100644
View file @
3810529e
server
:
port
:
10101
servlet
:
context-path
:
/
shutdown
:
graceful
#开启优雅停机,默认IMMEDIATE是立即关机
tomcat
:
maxThreads
:
1000
connection-timeout
:
15000ms
keepAliveTimeout
:
15000
maxKeepAliveRequests
:
200
spring
:
datasource
:
driver-class-name
:
com.mysql.cj.jdbc.Driver
url
:
jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2b8
username
:
root
password
:
123456
redisson-lock/src/main/resources/application.yml_bak
deleted
100644 → 0
View file @
85311783
#server:
# port: 10101
# servlet:
# context-path: /
# shutdown: graceful #开启优雅停机,默认IMMEDIATE是立即关机
# tomcat:
# maxThreads: 1000
# connection-timeout: 15000ms
# keepAliveTimeout: 15000
# maxKeepAliveRequests: 200
#
#spring:
# datasource:
# driver-class-name: com.mysql.cj.jdbc.Driver
# url: jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2b8
# username: root
# password: 123456
#
#
#
#
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