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
81c369f7
Commit
81c369f7
authored
Feb 28, 2018
by
zhiguo.liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
# 修改 IDGenerator 发号器最终号码的格式,去掉补位用的「0」
parent
9e594dc0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
28 deletions
+22
-28
IDGenerator.java
...ng/src/main/java/cn/quantgroup/tech/util/IDGenerator.java
+22
-28
No files found.
commons-spring/src/main/java/cn/quantgroup/tech/util/IDGenerator.java
View file @
81c369f7
...
@@ -39,13 +39,6 @@ public class IDGenerator {
...
@@ -39,13 +39,6 @@ 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台机器
*/
*/
...
@@ -54,7 +47,11 @@ public class IDGenerator {
...
@@ -54,7 +47,11 @@ 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
;
...
@@ -70,12 +67,6 @@ public class IDGenerator {
...
@@ -70,12 +67,6 @@ 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
>()
{
...
@@ -85,23 +76,14 @@ public class IDGenerator {
...
@@ -85,23 +76,14 @@ 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
*/
*/
...
@@ -146,7 +128,19 @@ public class IDGenerator {
...
@@ -146,7 +128,19 @@ 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
+
String
.
format
(
"%06d"
,
counter
);
return
prefix
+
currentTimeStr
+
DATA_CENTER_STR
+
WORKER_ID_STR
+
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