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
6ac83088
Commit
6ac83088
authored
Apr 13, 2018
by
zhiguo.liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
# 精简新发号器结构,修改旧新发号器名称为 OIDGenerator。
parent
74a67b85
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
39 additions
and
145 deletions
+39
-145
OIDGenerator.java
...g/src/main/java/cn/quantgroup/tech/util/OIDGenerator.java
+1
-1
DisposableWorkerIdAssigner.java
...n/quantgroup/tech/util/id/DisposableWorkerIdAssigner.java
+0
-47
IDGenerateException.java
.../java/cn/quantgroup/tech/util/id/IDGenerateException.java
+4
-35
IDGenerator.java
...src/main/java/cn/quantgroup/tech/util/id/IDGenerator.java
+34
-35
UidGenerator.java
...rc/main/java/cn/quantgroup/tech/util/id/UidGenerator.java
+0
-27
No files found.
commons-spring/src/main/java/cn/quantgroup/tech/util/IDGenerator.java
→
commons-spring/src/main/java/cn/quantgroup/tech/util/
O
IDGenerator.java
View file @
6ac83088
...
...
@@ -35,7 +35,7 @@ import java.util.concurrent.locks.ReentrantLock;
@Component
@ConditionalOnClass
(
RedisTemplate
.
class
)
@ConditionalOnProperty
(
name
=
"data.center.id"
)
public
class
IDGenerator
{
public
class
O
IDGenerator
{
private
static
final
String
REDIS_WORK_ID_KEY
=
"GLOBAL:WORK:ID:"
;
private
static
final
String
ID_FORMAT
=
"yyyyMMddHHmmss"
;
...
...
commons-spring/src/main/java/cn/quantgroup/tech/util/id/DisposableWorkerIdAssigner.java
deleted
100644 → 0
View file @
74a67b85
/*
* Copyright (c) 2017 Baidu, Inc. All Rights Reserve.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
cn
.
quantgroup
.
tech
.
util
.
id
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.core.StringRedisTemplate
;
import
org.springframework.stereotype.Component
;
@Component
@ConditionalOnClass
(
RedisTemplate
.
class
)
public
class
DisposableWorkerIdAssigner
{
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
DisposableWorkerIdAssigner
.
class
);
private
static
final
String
REDIS_WORK_ID_KEY
=
"GLOBAL:WORK:ID:"
;
@Autowired
private
StringRedisTemplate
redisTemplate
;
/**
* Assign worker id base on Redis.<p>
* 使用 Redis 的 incr 命令,最后结果为 incr % maxWorkerId
* @param dataCenterId
* @param bitsAllocator
* @return assigned worker id
*/
public
long
assignWorkerId
(
long
dataCenterId
,
BitsAllocator
bitsAllocator
)
{
return
redisTemplate
.
opsForValue
().
increment
(
REDIS_WORK_ID_KEY
+
dataCenterId
,
1
)
%
bitsAllocator
.
getMaxWorkerId
();
}
}
commons-spring/src/main/java/cn/quantgroup/tech/util/id/
Uid
GenerateException.java
→
commons-spring/src/main/java/cn/quantgroup/tech/util/id/
ID
GenerateException.java
View file @
6ac83088
...
...
@@ -16,11 +16,9 @@
package
cn
.
quantgroup
.
tech
.
util
.
id
;
/**
* UidGenerateException
*
* @author yutianbao
* IDGenerateException
*/
public
class
Uid
GenerateException
extends
RuntimeException
{
public
class
ID
GenerateException
extends
RuntimeException
{
/**
* Serial Version UID
...
...
@@ -30,45 +28,16 @@ public class UidGenerateException extends RuntimeException {
/**
* Default constructor
*/
public
Uid
GenerateException
()
{
public
ID
GenerateException
()
{
super
();
}
/**
* Constructor with message & cause
*
* @param message
* @param cause
*/
public
UidGenerateException
(
String
message
,
Throwable
cause
)
{
super
(
message
,
cause
);
}
/**
* Constructor with message
*
* @param message
*/
public
UidGenerateException
(
String
message
)
{
super
(
message
);
}
/**
* Constructor with message format
*
* @param msgFormat
* @param args
*/
public
UidGenerateException
(
String
msgFormat
,
Object
...
args
)
{
super
(
String
.
format
(
msgFormat
,
args
));
}
/**
* Constructor with cause
*
* @param cause
*/
public
Uid
GenerateException
(
Throwable
cause
)
{
public
ID
GenerateException
(
Throwable
cause
)
{
super
(
cause
);
}
...
...
commons-spring/src/main/java/cn/quantgroup/tech/util/id/
DefaultUid
Generator.java
→
commons-spring/src/main/java/cn/quantgroup/tech/util/id/
ID
Generator.java
View file @
6ac83088
...
...
@@ -8,7 +8,10 @@ import org.slf4j.LoggerFactory;
import
org.springframework.beans.factory.InitializingBean
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnClass
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.core.StringRedisTemplate
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.Assert
;
...
...
@@ -18,7 +21,7 @@ import java.util.Date;
import
java.util.concurrent.TimeUnit
;
/**
* Represents an implementation of {@link
Uid
Generator}
* Represents an implementation of {@link
ID
Generator}
* 基于百度开源项目 uid-generator 的增强版,Snowflake Java 实现版本。项目 Github:https://github.com/baidu/uid-generator
* <p>
* uid-generator 通过对 64 位数字分区来生成唯一 ID,由以下组成:
...
...
@@ -55,47 +58,45 @@ import java.util.concurrent.TimeUnit;
* @author zhiguo.liu
*/
@Component
@ConditionalOnProperty
(
name
=
"data.center.id"
)
public
class
DefaultUidGenerator
implements
UidGenerator
,
InitializingBean
{
public
static
final
String
DATETIME_PATTERN
=
"yyyy-MM-dd HH:mm:ss"
;
public
static
final
String
DAY_PATTERN
=
"yyyy-MM-dd"
;
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
DefaultUidGenerator
.
class
);
@ConditionalOnClass
(
RedisTemplate
.
class
)
@ConditionalOnProperty
(
name
=
"id.epochStr"
)
public
class
IDGenerator
implements
InitializingBean
{
private
static
final
String
DATETIME_PATTERN
=
"yyyy-MM-dd HH:mm:ss"
;
private
static
final
String
DAY_PATTERN
=
"yyyy-MM-dd"
;
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
IDGenerator
.
class
);
private
static
final
String
REDIS_WORK_ID_KEY
=
"GLOBAL:WORK:ID:"
;
/**
* Bits allocate
*/
@Value
(
"${
u
id.dataCenterIdBits:9}"
)
@Value
(
"${id.dataCenterIdBits:9}"
)
protected
int
dataCenterIdBits
;
@Value
(
"${
u
id.workerBits:13}"
)
@Value
(
"${id.workerBits:13}"
)
protected
int
workerBits
;
@Value
(
"${
u
id.seqBits:13}"
)
@Value
(
"${id.seqBits:13}"
)
protected
int
seqBits
;
@Value
(
"${data.center.id}"
)
protected
long
dataCenterId
;
/**
* Customer epoch, unit as second. For example 2018-03-01 (ms: 1463673600000)
*/
protected
String
epochStr
;
protected
long
epochSeconds
;
/**
* Stable fields after spring bean initializing
*/
protected
BitsAllocator
bitsAllocator
;
protected
long
workerId
;
/**
* Volatile fields caused by nextId()
*/
protected
long
sequence
=
0L
;
protected
long
lastSecond
=
-
1L
;
/**
* Spring property
*/
@Autowired
pr
otected
DisposableWorkerIdAssigner
workerIdAssigner
;
pr
ivate
StringRedisTemplate
redisTemplate
;
@Override
public
void
afterPropertiesSet
()
throws
Exception
{
...
...
@@ -103,26 +104,24 @@ public class DefaultUidGenerator implements UidGenerator, InitializingBean {
int
timeBits
=
64
-
1
-
dataCenterIdBits
-
workerBits
-
seqBits
;
bitsAllocator
=
new
BitsAllocator
(
timeBits
,
dataCenterIdBits
,
workerBits
,
seqBits
);
// initialize worker id
workerId
=
workerIdAssigner
.
assignWorkerId
(
dataCenterId
,
bitsAllocator
);
workerId
=
redisTemplate
.
opsForValue
().
increment
(
REDIS_WORK_ID_KEY
+
dataCenterId
,
1
)
%
bitsAllocator
.
getMaxWorkerId
(
);
Assert
.
isTrue
(
workerId
<
bitsAllocator
.
getMaxWorkerId
(),
"workerId is too big"
);
Assert
.
isTrue
(
bitsAllocator
.
getMaxDataCenterId
()
!=
0
&&
dataCenterId
<
bitsAllocator
.
getMaxDataCenterId
(),
"dataCenterId is too big"
);
LOGGER
.
info
(
"Initialized bits dataCenterBits:{}, workerBits:{}, seqBits:{}"
,
dataCenterIdBits
,
workerBits
,
seqBits
);
LOGGER
.
info
(
"Initialized nodes, workerId:{}, dataCenterId:{}"
,
workerId
,
dataCenterId
);
}
@Override
public
String
getUID
(
String
preFix
)
throws
UidGenerateException
{
public
String
getID
(
String
preFix
)
throws
IDGenerateException
{
try
{
return
nextId
(
preFix
);
}
catch
(
Exception
e
)
{
LOGGER
.
error
(
"Generate unique id exception. "
,
e
);
throw
new
Uid
GenerateException
(
e
);
throw
new
ID
GenerateException
(
e
);
}
}
@Override
public
String
parseUID
(
String
uidStr
)
{
BigInteger
bigInteger
=
new
BigInteger
(
uidStr
);
public
String
parseID
(
String
idStr
)
{
BigInteger
bigInteger
=
new
BigInteger
(
idStr
);
int
totalBits
=
bigInteger
.
bitLength
();
long
dataCenterIdBits
=
bitsAllocator
.
getDataCenterIdBits
();
...
...
@@ -130,18 +129,18 @@ public class DefaultUidGenerator implements UidGenerator, InitializingBean {
long
sequenceBits
=
bitsAllocator
.
getSequenceBits
();
if
(
totalBits
<
64
)
{
totalBits
=
64
;
long
u
id
=
bigInteger
.
longValue
();
long
sequence
=
(
u
id
<<
(
totalBits
-
sequenceBits
))
>>>
(
totalBits
-
sequenceBits
);
long
workerId
=
(
u
id
<<
(
totalBits
-
workerIdBits
-
sequenceBits
))
>>>
(
totalBits
-
workerIdBits
);
long
dataCenterId
=
(
u
id
<<
(
totalBits
-
dataCenterIdBits
-
workerIdBits
-
sequenceBits
))
>>>
(
totalBits
-
dataCenterIdBits
);
long
id
=
bigInteger
.
longValue
();
long
sequence
=
(
id
<<
(
totalBits
-
sequenceBits
))
>>>
(
totalBits
-
sequenceBits
);
long
workerId
=
(
id
<<
(
totalBits
-
workerIdBits
-
sequenceBits
))
>>>
(
totalBits
-
workerIdBits
);
long
dataCenterId
=
(
id
<<
(
totalBits
-
dataCenterIdBits
-
workerIdBits
-
sequenceBits
))
>>>
(
totalBits
-
dataCenterIdBits
);
if
(
dataCenterIdBits
==
0
)
{
dataCenterId
=
0
;
}
long
deltaSeconds
=
u
id
>>>
(
dataCenterIdBits
+
workerIdBits
+
sequenceBits
);
long
deltaSeconds
=
id
>>>
(
dataCenterIdBits
+
workerIdBits
+
sequenceBits
);
Date
thatTime
=
new
Date
(
TimeUnit
.
SECONDS
.
toMillis
(
epochSeconds
+
deltaSeconds
));
String
thatTimeStr
=
DateFormatUtils
.
format
(
thatTime
,
DATETIME_PATTERN
);
return
String
.
format
(
"{\"
U
ID\":\"%d\",\"timestamp\":\"%s\",\"dataCenterId\":\"%d\",\"workerId\":\"%d\",\"sequence\":\"%d\"}"
,
u
id
,
thatTimeStr
,
dataCenterId
,
workerId
,
sequence
);
return
String
.
format
(
"{\"ID\":\"%d\",\"timestamp\":\"%s\",\"dataCenterId\":\"%d\",\"workerId\":\"%d\",\"sequence\":\"%d\"}"
,
id
,
thatTimeStr
,
dataCenterId
,
workerId
,
sequence
);
}
else
{
long
sequence
=
getBigIntegerFromLength
(
sequenceBits
).
and
(
bigInteger
).
longValue
();
long
workerId
=
getBigIntegerFromLength
(
workerIdBits
).
and
(
bigInteger
.
shiftRight
((
int
)
sequenceBits
)).
longValue
();
...
...
@@ -152,7 +151,7 @@ public class DefaultUidGenerator implements UidGenerator, InitializingBean {
long
deltaSeconds
=
bigInteger
.
shiftRight
((
int
)
dataCenterIdBits
+
(
int
)
workerIdBits
+
(
int
)
sequenceBits
).
longValue
();
Date
thatTime
=
new
Date
(
TimeUnit
.
SECONDS
.
toMillis
(
epochSeconds
+
deltaSeconds
));
String
thatTimeStr
=
DateFormatUtils
.
format
(
thatTime
,
DATETIME_PATTERN
);
return
String
.
format
(
"{\"
U
ID\":\"%d\",\"timestamp\":\"%s\",\"dataCenterId\":\"%d\",\"workerId\":\"%d\",\"sequence\":\"%d\"}"
,
return
String
.
format
(
"{\"ID\":\"%d\",\"timestamp\":\"%s\",\"dataCenterId\":\"%d\",\"workerId\":\"%d\",\"sequence\":\"%d\"}"
,
bigInteger
,
thatTimeStr
,
dataCenterId
,
workerId
,
sequence
);
}
}
...
...
@@ -162,10 +161,10 @@ public class DefaultUidGenerator implements UidGenerator, InitializingBean {
}
/**
* Get
U
ID
* Get ID
*
* @return
U
ID
* @throws
Uid
GenerateException in the case: Clock moved backwards; Exceeds the max timestamp
* @return ID
* @throws
ID
GenerateException in the case: Clock moved backwards; Exceeds the max timestamp
*/
protected
synchronized
String
nextId
(
String
preFix
)
{
long
currentSecond
=
getCurrentSecond
();
...
...
@@ -178,7 +177,7 @@ public class DefaultUidGenerator implements UidGenerator, InitializingBean {
// At the same second, increase sequence
if
(
currentSecond
==
lastSecond
)
{
sequence
=
(
sequence
+
1
)
&
bitsAllocator
.
getMaxSequence
();
// Exceed the max sequence, we wait the next second to generate
uid
// Exceed the max sequence, we wait the next second to generate
ID
if
(
sequence
==
0
)
{
currentSecond
=
getNextSecond
(
lastSecond
);
}
...
...
@@ -227,7 +226,7 @@ public class DefaultUidGenerator implements UidGenerator, InitializingBean {
}
}
@Value
(
"${
u
id.epochStr:2018-03-01}"
)
@Value
(
"${id.epochStr:2018-03-01}"
)
public
void
setEpochStr
(
String
epochStr
)
{
if
(
StringUtils
.
isNotBlank
(
epochStr
))
{
this
.
epochStr
=
epochStr
;
...
...
commons-spring/src/main/java/cn/quantgroup/tech/util/id/UidGenerator.java
deleted
100644 → 0
View file @
74a67b85
package
cn
.
quantgroup
.
tech
.
util
.
id
;
/**
* Represents a unique id generator.
*
* @author yutianbao
*/
public
interface
UidGenerator
{
/**
* Get a unique ID
*
* @return UID
* @throws UidGenerateException
*/
String
getUID
(
String
preFix
)
throws
UidGenerateException
;
/**
* Parse the UID into elements which are used to generate the UID. <br>
* Such as timestamp & workerId & sequence...
*
* @param uid
* @return Parsed info
*/
String
parseUID
(
String
uid
);
}
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