记得上下班打卡 | git大法好,push需谨慎
Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
liquidnet-bus-v1
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
董敬伟
liquidnet-bus-v1
Commits
7e77d789
Commit
7e77d789
authored
Jun 20, 2021
by
张国柄
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix:ID GEN;
parent
68e52f10
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
147 additions
and
113 deletions
+147
-113
GenSnowFlowerUtil.java
...va/com/liquidnet/commons/lang/util/GenSnowFlowerUtil.java
+129
-0
IDGenerator.java
...ain/java/com/liquidnet/commons/lang/util/IDGenerator.java
+18
-113
No files found.
liquidnet-bus-common/liquidnet-common-base/src/main/java/com/liquidnet/commons/lang/util/GenSnowFlowerUtil.java
0 → 100644
View file @
7e77d789
package
com
.
liquidnet
.
commons
.
lang
.
util
;
import
org.apache.commons.lang3.RandomStringUtils
;
import
org.apache.commons.lang3.RandomUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.SystemUtils
;
import
java.net.Inet4Address
;
import
java.net.UnknownHostException
;
import
java.util.Arrays
;
public
class
GenSnowFlowerUtil
{
private
static
final
long
twepoch
=
961430400000L
;
private
static
final
long
workerIdBits
=
5L
;
private
static
final
long
dataCenterIdBits
=
5L
;
//// 最大支持机器节点数0~31,一共32个
// 最大支持数据中心节点数0~31,一共32个
@SuppressWarnings
({
"PointlessBitwiseExpression"
,
"FieldCanBeLocal"
})
private
static
final
long
maxWorkerId
=
-
1L
^
(-
1L
<<
workerIdBits
);
@SuppressWarnings
({
"PointlessBitwiseExpression"
,
"FieldCanBeLocal"
})
private
static
final
long
maxDataCenterId
=
-
1L
^
(-
1L
<<
dataCenterIdBits
);
// 序列号12位
private
static
final
long
sequenceBits
=
12L
;
// 机器节点左移12位
private
static
final
long
workerIdShift
=
sequenceBits
;
// 数据中心节点左移17位
private
static
final
long
dataCenterIdShift
=
sequenceBits
+
workerIdBits
;
// 时间毫秒数左移22位
private
static
final
long
timestampLeftShift
=
sequenceBits
+
workerIdBits
+
dataCenterIdBits
;
@SuppressWarnings
({
"PointlessBitwiseExpression"
,
"FieldCanBeLocal"
})
private
static
final
long
sequenceMask
=
-
1L
^
(-
1L
<<
sequenceBits
);
// 4095
private
static
final
long
workerId
;
private
static
final
long
dataCenterId
;
private
static
final
boolean
useSystemClock
;
private
static
long
sequence
=
0L
;
private
static
long
lastTimestamp
=
-
1L
;
static
{
workerId
=
getMachineNum
()
%
maxWorkerId
;
dataCenterId
=
getDataCenterNum
()
%
maxDataCenterId
;
useSystemClock
=
true
;
}
/* ---------------------------------------------------------------------------------------- */
public
static
synchronized
String
next
()
{
long
timestamp
=
genTime
();
if
(
timestamp
<
lastTimestamp
)
{
if
(
lastTimestamp
-
timestamp
<
2000
)
{
// 容忍2秒内的回拨,避免NTP校时造成的异常
timestamp
=
lastTimestamp
;
}
else
{
// 如果服务器时间有问题(时钟后退) 报错。
throw
new
IllegalStateException
(
String
.
format
(
"Clock moved backwards. Refusing to generate id for %sms"
,
lastTimestamp
-
timestamp
));
}
}
if
(
timestamp
==
lastTimestamp
)
{
sequence
=
(
sequence
+
1
)
&
sequenceMask
;
if
(
sequence
==
0L
)
{
timestamp
=
tilNextMillis
(
lastTimestamp
);
}
}
else
{
sequence
=
0L
;
}
lastTimestamp
=
timestamp
;
long
l
=
(
timestamp
-
twepoch
)
<<
timestampLeftShift
|
dataCenterId
<<
dataCenterIdShift
|
workerId
<<
workerIdShift
|
sequence
;
// return l;
return
l
+
String
.
valueOf
(
System
.
nanoTime
()).
substring
(
9
);
}
private
static
long
genTime
()
{
return
useSystemClock
?
SystemClock
.
now
()
:
System
.
currentTimeMillis
();
}
private
static
long
tilNextMillis
(
long
lastTimestamp
)
{
long
timestamp
=
genTime
();
// 循环直到操作系统时间戳变化
while
(
timestamp
==
lastTimestamp
)
{
timestamp
=
genTime
();
}
if
(
timestamp
<
lastTimestamp
)
{
// 如果发现新的时间戳比上次记录的时间戳数值小,说明操作系统时间发生了倒退,报错
throw
new
IllegalStateException
(
String
.
format
(
"Clock moved backwards. Refusing to generate id for %sms"
,
lastTimestamp
-
timestamp
));
}
return
timestamp
;
}
/* ---------------------------------------------------------------------------------------- */
private
static
long
getMachineNum
()
{
try
{
int
[]
ints
=
StringUtils
.
toCodePoints
(
Inet4Address
.
getLocalHost
().
getHostAddress
()
);
return
Arrays
.
stream
(
ints
).
sum
();
}
catch
(
UnknownHostException
e
)
{
return
RandomUtils
.
nextLong
(
0
,
31
);
}
}
private
static
long
getDataCenterNum
()
{
int
[]
ints
=
StringUtils
.
toCodePoints
(
StringUtils
.
defaultString
(
SystemUtils
.
getHostName
(),
RandomStringUtils
.
randomAlphabetic
(
5
))
);
return
Arrays
.
stream
(
ints
).
sum
();
}
/* ---------------------------------------------------------------------------------------- */
private
long
getWorkerId
(
long
id
)
{
return
id
>>
workerIdShift
&
~(-
1L
<<
workerIdBits
);
}
private
long
getDataCenterNum
(
long
id
)
{
return
id
>>
dataCenterIdShift
&
~(-
1L
<<
dataCenterIdBits
);
}
private
long
getGenerateDateTime
(
long
id
)
{
return
(
id
>>
timestampLeftShift
&
~(-
1L
<<
41L
))
+
twepoch
;
}
/* ---------------------------------------------------------------------------------------- */
}
liquidnet-bus-common/liquidnet-common-base/src/main/java/com/liquidnet/commons/lang/util/IDGenerator.java
View file @
7e77d789
package
com
.
liquidnet
.
commons
.
lang
.
util
;
package
com
.
liquidnet
.
commons
.
lang
.
util
;
import
java.net.NetworkInterface
;
import
java.time.LocalDateTime
;
import
java.net.SocketException
;
import
java.time.format.DateTimeFormatter
;
import
java.util.Enumeration
;
import
java.util.UUID
;
import
java.util.concurrent.ThreadLocalRandom
;
public
class
IDGenerator
{
public
class
IDGenerator
{
private
static
final
long
twepoch
=
1624118400000L
;
private
static
final
String
CROSS_BAR
=
"-"
;
// private static final long twepoch = 961421243000L;
private
static
final
String
EMPTY_STR
=
""
;
private
static
final
long
workerIdBits
=
2L
;
private
static
final
DateTimeFormatter
MILLISECOND
=
DateTimeFormatter
.
ofPattern
(
"yyyyMMddHHmmssSSS"
);
private
static
final
long
dataCenterIdBits
=
1L
;
//// 最大支持机器节点数0~31,一共32个
// 最大支持数据中心节点数0~31,一共32个
@SuppressWarnings
({
"PointlessBitwiseExpression"
,
"FieldCanBeLocal"
})
private
static
final
long
maxWorkerId
=
-
1L
^
(-
1L
<<
workerIdBits
);
@SuppressWarnings
({
"PointlessBitwiseExpression"
,
"FieldCanBeLocal"
})
private
static
final
long
maxDataCenterId
=
-
1L
^
(-
1L
<<
dataCenterIdBits
);
// 序列号12位
private
static
final
long
sequenceBits
=
12L
;
// 机器节点左移12位
private
static
final
long
workerIdShift
=
sequenceBits
;
// 数据中心节点左移17位
private
static
final
long
dataCenterIdShift
=
sequenceBits
+
workerIdBits
;
// 时间毫秒数左移22位
private
static
final
long
timestampLeftShift
=
sequenceBits
+
workerIdBits
+
dataCenterIdBits
;
@SuppressWarnings
({
"PointlessBitwiseExpression"
,
"FieldCanBeLocal"
})
private
static
final
long
sequenceMask
=
-
1L
^
(-
1L
<<
sequenceBits
);
// 4095
private
static
final
long
workerId
;
public
static
String
nextSnowId
()
{
private
static
final
long
dataCenterId
;
return
GenSnowFlowerUtil
.
next
();
private
static
final
boolean
useSystemClock
;
private
static
long
sequence
=
0L
;
private
static
long
lastTimestamp
=
-
1L
;
static
{
workerId
=
getMachineNum
()
&
maxWorkerId
;
dataCenterId
=
1
;
useSystemClock
=
true
;
}
}
/* ---------------------------------------------------------------------------------------- */
public
static
String
nextTimeId
()
{
return
nextMilliId
()
+
String
.
valueOf
(
System
.
nanoTime
()).
substring
(
9
);
public
static
synchronized
String
nextSnowId
()
{
}
long
timestamp
=
genTime
();
if
(
timestamp
<
lastTimestamp
)
{
if
(
lastTimestamp
-
timestamp
<
2000
)
{
// 容忍2秒内的回拨,避免NTP校时造成的异常
timestamp
=
lastTimestamp
;
}
else
{
// 如果服务器时间有问题(时钟后退) 报错。
throw
new
IllegalStateException
(
String
.
format
(
"Clock moved backwards. Refusing to generate id for %sms"
,
lastTimestamp
-
timestamp
));
}
}
if
(
timestamp
==
lastTimestamp
)
{
sequence
=
(
sequence
+
1
)
&
sequenceMask
;
if
(
sequence
==
0
)
{
timestamp
=
tilNextMillis
(
lastTimestamp
);
}
}
else
{
sequence
=
0L
;
}
lastTimestamp
=
timestamp
;
long
l
=
((
timestamp
-
twepoch
)
<<
timestampLeftShift
)
|
(
dataCenterId
<<
dataCenterIdShift
)
|
(
workerId
<<
workerIdShift
)
|
sequence
;
public
static
String
nextMilliId
()
{
return
LocalDateTime
.
now
().
format
(
MILLISECOND
);
}
// return l;
public
static
String
get32UUID
()
{
return
l
+
(
""
+
System
.
nanoTime
()).
substring
(
9
);
ThreadLocalRandom
random
=
ThreadLocalRandom
.
current
();
return
(
new
UUID
(
random
.
nextLong
(),
random
.
nextLong
())).
toString
().
replace
(
CROSS_BAR
,
EMPTY_STR
);
}
}
/**
/**
...
@@ -97,57 +55,4 @@ public class IDGenerator {
...
@@ -97,57 +55,4 @@ public class IDGenerator {
String
qrCode
=
MD5Utils
.
md5
(
orderTicketId
).
toLowerCase
();
String
qrCode
=
MD5Utils
.
md5
(
orderTicketId
).
toLowerCase
();
return
"QR"
+
qrCode
.
substring
(
5
)
+
""
+
qrCode
.
substring
(
0
,
4
);
return
"QR"
+
qrCode
.
substring
(
5
)
+
""
+
qrCode
.
substring
(
0
,
4
);
}
}
private
static
long
genTime
()
{
return
useSystemClock
?
SystemClock
.
now
()
:
System
.
currentTimeMillis
();
}
private
static
long
tilNextMillis
(
long
lastTimestamp
)
{
long
timestamp
=
genTime
();
// 循环直到操作系统时间戳变化
while
(
timestamp
==
lastTimestamp
)
{
timestamp
=
genTime
();
}
if
(
timestamp
<
lastTimestamp
)
{
// 如果发现新的时间戳比上次记录的时间戳数值小,说明操作系统时间发生了倒退,报错
throw
new
IllegalStateException
(
String
.
format
(
"Clock moved backwards. Refusing to generate id for %sms"
,
lastTimestamp
-
timestamp
));
}
return
timestamp
;
}
private
static
long
getMachineNum
()
{
long
machinePiece
;
StringBuilder
sb
=
new
StringBuilder
();
Enumeration
<
NetworkInterface
>
e
=
null
;
try
{
e
=
NetworkInterface
.
getNetworkInterfaces
();
while
(
e
.
hasMoreElements
())
{
NetworkInterface
ni
=
e
.
nextElement
();
sb
.
append
(
ni
.
toString
());
}
}
catch
(
SocketException
e1
)
{
e1
.
printStackTrace
();
}
machinePiece
=
sb
.
toString
().
hashCode
();
System
.
out
.
printf
(
"\n###MachineNum=%s, MachineCodeString=%s\n\n"
,
machinePiece
,
sb
);
return
machinePiece
;
}
/* ---------------------------------------------------------------------------------------- */
public
long
getWorkerId
(
long
id
)
{
return
id
>>
workerIdShift
&
~(-
1L
<<
workerIdBits
);
}
public
long
getDataCenterId
(
long
id
)
{
return
id
>>
dataCenterIdShift
&
~(-
1L
<<
dataCenterIdBits
);
}
public
long
getGenerateDateTime
(
long
id
)
{
return
(
id
>>
timestampLeftShift
&
~(-
1L
<<
41L
))
+
twepoch
;
}
/* ---------------------------------------------------------------------------------------- */
}
}
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