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
2887e583
Commit
2887e583
authored
May 22, 2017
by
zhiguo.liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
# 添加在 ID 生成器中通过 value 注解注入 dataCenterId .
parent
3e966f73
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
7 additions
and
24 deletions
+7
-24
IDGenerator.java
src/main/java/cn/quantgroup/xyqb/Utils/IDGenerator.java
+7
-24
No files found.
src/main/java/cn/quantgroup/xyqb/Utils/IDGenerator.java
View file @
2887e583
...
@@ -2,6 +2,7 @@ package cn.quantgroup.xyqb.Utils;
...
@@ -2,6 +2,7 @@ package cn.quantgroup.xyqb.Utils;
import
com.google.common.base.Stopwatch
;
import
com.google.common.base.Stopwatch
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.data.redis.core.StringRedisTemplate
;
import
org.springframework.data.redis.core.StringRedisTemplate
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
...
@@ -14,7 +15,11 @@ import java.util.concurrent.CountDownLatch;
...
@@ -14,7 +15,11 @@ import java.util.concurrent.CountDownLatch;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
/**
/**
* Created by liuzhiguo1 on 2017/5/18.
* 注意事项:
* 1. 使用前在配置文件内配置 DATA_CENTER_ID
*
*
* Created by zhiguo.liu on 2017/5/18.
*/
*/
@Component
@Component
public
class
IDGenerator
{
public
class
IDGenerator
{
...
@@ -41,10 +46,10 @@ public class IDGenerator {
...
@@ -41,10 +46,10 @@ public class IDGenerator {
// data center 字符串
// data center 字符串
private
static
String
DATA_CENTER_STR
;
private
static
String
DATA_CENTER_STR
;
@Autowired
@Autowired
private
StringRedisTemplate
redis
;
private
StringRedisTemplate
redis
;
@Value
(
"${data.center.id}"
)
public
static
void
setDataCenterId
(
int
dataCenterId
)
{
public
static
void
setDataCenterId
(
int
dataCenterId
)
{
DATA_CENTER_ID
=
dataCenterId
;
DATA_CENTER_ID
=
dataCenterId
;
}
}
...
@@ -83,26 +88,4 @@ public class IDGenerator {
...
@@ -83,26 +88,4 @@ public class IDGenerator {
return
prefix
+
CURRENT_TIME_STR
+
DATA_CENTER_STR
+
WORKER_ID_STR
+
String
.
format
(
"%06d"
,
++
COUNTER
);
return
prefix
+
CURRENT_TIME_STR
+
DATA_CENTER_STR
+
WORKER_ID_STR
+
String
.
format
(
"%06d"
,
++
COUNTER
);
}
}
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
{
WORKER_ID
=
1
;
WORKER_ID_STR
=
String
.
format
(
"%04d"
,
WORKER_ID
);
DATA_CENTER_STR
=
String
.
format
(
"%03d"
,
DATA_CENTER_ID
);
Stopwatch
watch
=
Stopwatch
.
createStarted
();
CountDownLatch
countDownLatch
=
new
CountDownLatch
(
100
);
ConcurrentHashMap
hashMap
=
new
ConcurrentHashMap
();
for
(
int
i
=
0
;
i
<
100
;
i
++)
{
new
Thread
(()
->
{
for
(
int
j
=
0
;
j
<
1000
;
j
++)
{
hashMap
.
put
(
getId
(
"BT"
),
"xx"
);
}
countDownLatch
.
countDown
();
}).
start
();
}
countDownLatch
.
await
();
watch
.
stop
();
System
.
out
.
println
(
hashMap
.
size
()
+
", "
+
watch
.
elapsed
(
TimeUnit
.
MILLISECONDS
)
+
"ms"
);
}
}
}
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