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
6d246f2e
Commit
6d246f2e
authored
Nov 16, 2017
by
ag
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
style: 调整了一些代码fengge
parent
a2182aad
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
17 deletions
+32
-17
pom.xml
pom.xml
+1
-1
IDGenerator.java
src/main/java/cn/quantgroup/xyqb/Utils/IDGenerator.java
+28
-15
RedisUtils.java
src/main/java/cn/quantgroup/xyqb/Utils/RedisUtils.java
+3
-1
No files found.
pom.xml
View file @
6d246f2e
...
...
@@ -19,7 +19,7 @@
<parent>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-parent
</artifactId>
<version>
1.
2.5
.RELEASE
</version>
<version>
1.
5.6
.RELEASE
</version>
</parent>
...
...
src/main/java/cn/quantgroup/xyqb/Utils/IDGenerator.java
View file @
6d246f2e
...
...
@@ -25,7 +25,9 @@ import java.util.concurrent.locks.ReentrantLock;
* 1. 使用前在配置文件内配置 DATA_CENTER_ID
* <p>
* <p>
* Created by zhiguo.liu on 2017/5/18.
*
* @author zhiguo.liu
* @date 2017/5/18
*/
@Component
public
class
IDGenerator
{
...
...
@@ -38,27 +40,38 @@ public class IDGenerator {
private
static
Lock
lock
=
new
ReentrantLock
();
// data center,默认为 1
/**
* data center,默认为 1
*/
private
static
int
DATA_CENTER_ID
=
1
;
private
static
int
WORKER_ID
=
0
;
// 最高支持同时1W台机器
/**
* 最高支持同时1W台机器
*/
private
static
final
int
MAX_WORK_ID
=
10000
;
// 最高每秒发号 100w
/**
* 最高每秒发号 100w
*/
private
static
final
int
MAX_COUNT
=
999999
;
private
static
AtomicInteger
COUNTER
=
new
AtomicInteger
(
0
);
private
static
long
MAX_TIME_SECOND
;
// Worker ID 字符串
/**
* Worker ID 字符串
*/
private
static
String
WORKER_ID_STR
;
// data center 字符串
/**
* data center 字符串
*/
private
static
String
DATA_CENTER_STR
;
// 最长回退时间,120 秒
/**
* 最长回退时间,120 秒
*/
private
static
int
MAX_BACK_SECOND
=
120
;
static
{
static
{
Date
now
=
new
Date
();
Long
timeSecond
=
now
.
getTime
()
/
1000
;
MAX_TIME_SECOND
=
timeSecond
;
MAX_TIME_SECOND
=
now
.
getTime
()
/
1000
;
}
...
...
@@ -82,8 +95,8 @@ public class IDGenerator {
@PostConstruct
public
void
init
()
{
WORKER_ID
=
(
int
)
(
redis
.
opsForValue
().
increment
(
REDIS_WORK_ID_KEY
+
DATA_CENTER_ID
,
1
)
%
MAX_WORK_ID
);
WORKER_ID_STR
=
String
.
format
(
"%04d"
,
WORKER_ID
);
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
);
}
...
...
@@ -98,14 +111,14 @@ public class IDGenerator {
Integer
counter
=
0
;
if
(
timeSecond
>
MAX_TIME_SECOND
)
{
lock
.
lock
();
if
(
timeSecond
>
MAX_TIME_SECOND
)
{
if
(
timeSecond
>
MAX_TIME_SECOND
)
{
cache
.
put
(
MAX_TIME_SECOND
,
COUNTER
);
COUNTER
=
new
AtomicInteger
(
0
);
MAX_TIME_SECOND
=
timeSecond
;
}
lock
.
unlock
();
}
if
(
timeSecond
==
MAX_TIME_SECOND
)
{
if
(
timeSecond
==
MAX_TIME_SECOND
)
{
counter
=
COUNTER
.
incrementAndGet
();
}
// 时间回退时到 cache 里拿,或者直接抛出错误
...
...
src/main/java/cn/quantgroup/xyqb/Utils/RedisUtils.java
View file @
6d246f2e
...
...
@@ -7,7 +7,9 @@ import org.springframework.data.redis.core.RedisTemplate;
import
redis.clients.jedis.Jedis
;
/**
* Created by zhiguo.liu on 2017/7/28.
*
* @author zhiguo.liu
* @date 2017/7/28
*/
public
class
RedisUtils
{
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
RedisUtils
.
class
);
...
...
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