Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
commons
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
3
Merge Requests
3
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DevOps
commons
Commits
2435dae8
Commit
2435dae8
authored
Mar 02, 2018
by
zhiguo.liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: 回退 IDGenerator 版本,去掉补位 0 会导致生成号码冲突
parent
fb633645
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
22 deletions
+28
-22
IDGenerator.java
...ng/src/main/java/cn/quantgroup/tech/util/IDGenerator.java
+28
-22
No files found.
commons-spring/src/main/java/cn/quantgroup/tech/util/IDGenerator.java
View file @
2435dae8
...
@@ -39,6 +39,13 @@ public class IDGenerator {
...
@@ -39,6 +39,13 @@ public class IDGenerator {
private
static
final
String
REDIS_WORK_ID_KEY
=
"GLOBAL:WORK:ID:"
;
private
static
final
String
REDIS_WORK_ID_KEY
=
"GLOBAL:WORK:ID:"
;
private
static
final
String
ID_FORMAT
=
"yyyyMMddHHmmss"
;
private
static
final
String
ID_FORMAT
=
"yyyyMMddHHmmss"
;
private
static
Lock
lock
=
new
ReentrantLock
();
/**
* data center,默认为 1
*/
private
static
int
DATA_CENTER_ID
=
1
;
/**
/**
* 最高支持同时1W台机器
* 最高支持同时1W台机器
*/
*/
...
@@ -47,11 +54,7 @@ public class IDGenerator {
...
@@ -47,11 +54,7 @@ public class IDGenerator {
* 最高每秒发号 100w
* 最高每秒发号 100w
*/
*/
private
static
final
int
MAX_COUNT
=
999999
;
private
static
final
int
MAX_COUNT
=
999999
;
private
static
Lock
lock
=
new
ReentrantLock
();
/**
* data center,默认为 1
*/
private
static
int
DATA_CENTER_ID
=
1
;
private
static
AtomicInteger
COUNTER
=
new
AtomicInteger
(
0
);
private
static
AtomicInteger
COUNTER
=
new
AtomicInteger
(
0
);
private
static
long
MAX_TIME_SECOND
;
private
static
long
MAX_TIME_SECOND
;
...
@@ -67,6 +70,12 @@ public class IDGenerator {
...
@@ -67,6 +70,12 @@ public class IDGenerator {
* 最长回退时间,120 秒
* 最长回退时间,120 秒
*/
*/
private
static
int
MAX_BACK_SECOND
=
120
;
private
static
int
MAX_BACK_SECOND
=
120
;
static
{
Date
now
=
new
Date
();
MAX_TIME_SECOND
=
now
.
getTime
()
/
1000
;
}
private
static
LoadingCache
cache
=
CacheBuilder
.
newBuilder
()
private
static
LoadingCache
cache
=
CacheBuilder
.
newBuilder
()
.
expireAfterWrite
(
MAX_BACK_SECOND
,
TimeUnit
.
SECONDS
)
.
expireAfterWrite
(
MAX_BACK_SECOND
,
TimeUnit
.
SECONDS
)
.
build
(
new
CacheLoader
<
Long
,
AtomicInteger
>()
{
.
build
(
new
CacheLoader
<
Long
,
AtomicInteger
>()
{
...
@@ -76,14 +85,23 @@ public class IDGenerator {
...
@@ -76,14 +85,23 @@ public class IDGenerator {
}
}
});
});
static
{
Date
now
=
new
Date
();
MAX_TIME_SECOND
=
now
.
getTime
()
/
1000
;
}
@Autowired
@Autowired
private
StringRedisTemplate
redis
;
private
StringRedisTemplate
redis
;
@Value
(
"${data.center.id}"
)
public
void
setDataCenterId
(
Integer
dataCenterId
)
{
DATA_CENTER_ID
=
dataCenterId
;
}
@PostConstruct
public
void
init
()
{
int
workerId
=
(
int
)
(
redis
.
opsForValue
().
increment
(
REDIS_WORK_ID_KEY
+
DATA_CENTER_ID
,
1
)
%
MAX_WORK_ID
);
WORKER_ID_STR
=
String
.
format
(
"%04d"
,
workerId
);
DATA_CENTER_STR
=
String
.
format
(
"%03d"
,
DATA_CENTER_ID
);
}
/**
/**
* 1. 需要获取 dataCenterId 和 workeId
* 1. 需要获取 dataCenterId 和 workeId
*/
*/
...
@@ -128,19 +146,7 @@ public class IDGenerator {
...
@@ -128,19 +146,7 @@ public class IDGenerator {
}
}
String
currentTimeStr
=
new
SimpleDateFormat
(
ID_FORMAT
,
Locale
.
SIMPLIFIED_CHINESE
).
format
(
now
);
String
currentTimeStr
=
new
SimpleDateFormat
(
ID_FORMAT
,
Locale
.
SIMPLIFIED_CHINESE
).
format
(
now
);
return
prefix
+
currentTimeStr
+
DATA_CENTER_STR
+
WORKER_ID_STR
+
counter
;
return
prefix
+
currentTimeStr
+
DATA_CENTER_STR
+
WORKER_ID_STR
+
String
.
format
(
"%06d"
,
counter
);
}
@Value
(
"${data.center.id}"
)
public
void
setDataCenterId
(
Integer
dataCenterId
)
{
DATA_CENTER_ID
=
dataCenterId
;
}
@PostConstruct
public
void
init
()
{
int
workerId
=
(
int
)
(
redis
.
opsForValue
().
increment
(
REDIS_WORK_ID_KEY
+
DATA_CENTER_ID
,
1
)
%
MAX_WORK_ID
);
WORKER_ID_STR
=
String
.
valueOf
(
workerId
);
DATA_CENTER_STR
=
String
.
valueOf
(
DATA_CENTER_ID
);
}
}
}
}
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