forked from hansonwang99/Spring-Boot-In-Action
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
System Administrator
authored and
System Administrator
committed
Sep 4, 2019
1 parent
db966d1
commit 79cf3c5
Showing
42 changed files
with
2,602 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>cn.codesheep</groupId> | ||
<artifactId>id-spring-boot-starter</artifactId> | ||
<version>1.0.0</version> | ||
|
||
<!-- Properties --> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<jdk.version>1.8</jdk.version> | ||
<spring.version>5.0.9.RELEASE</spring.version> | ||
<slf4j-version>1.7.7</slf4j-version> | ||
</properties> | ||
|
||
<!-- Dependencies --> | ||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-autoconfigure</artifactId> | ||
</dependency> | ||
|
||
<!-- Spring --> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-core</artifactId> | ||
<version>${spring.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-beans</artifactId> | ||
<version>${spring.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context</artifactId> | ||
<version>${spring.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-jdbc</artifactId> | ||
<version>${spring.version}</version> | ||
</dependency> | ||
|
||
<!-- MyBatis --> | ||
<dependency> | ||
<groupId>org.mybatis</groupId> | ||
<artifactId>mybatis</artifactId> | ||
<version>3.2.3</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mybatis</groupId> | ||
<artifactId>mybatis-spring</artifactId> | ||
<version>1.2.4</version> | ||
</dependency> | ||
|
||
<!-- Apache Commons --> | ||
<dependency> | ||
<groupId>commons-collections</groupId> | ||
<artifactId>commons-collections</artifactId> | ||
<version>3.2.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>commons-lang</groupId> | ||
<artifactId>commons-lang</artifactId> | ||
<version>2.6</version> | ||
</dependency> | ||
|
||
<!-- Logger --> | ||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
<version>1.1.3</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>slf4j-api</artifactId> | ||
<version>${slf4j-version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.slf4j</groupId> | ||
<artifactId>log4j-over-slf4j</artifactId> | ||
<version>${slf4j-version}</version> | ||
</dependency> | ||
|
||
<!-- Test. Scope test only --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.10</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-test</artifactId> | ||
<version>${spring.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>mysql</groupId> | ||
<artifactId>mysql-connector-java</artifactId> | ||
<version>5.1.18</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>druid</artifactId> | ||
<version>1.0.19</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-dependencies</artifactId> | ||
<version>2.1.1.RELEASE</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>${jdk.version}</source> | ||
<target>${jdk.version}</target> | ||
<encoding>${project.build.sourceEncoding}</encoding> | ||
</configuration> | ||
<version>3.5.1</version> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-source-plugin</artifactId> | ||
<version>2.3</version> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>jar</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
Binary file not shown.
Binary file not shown.
136 changes: 136 additions & 0 deletions
136
id-spring-boot-starter/src/main/java/com/baidu/fsg/uid/BitsAllocator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* 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 com.baidu.fsg.uid; | ||
|
||
import org.apache.commons.lang.builder.ToStringBuilder; | ||
import org.apache.commons.lang.builder.ToStringStyle; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Allocate 64 bits for the UID(long)<br> | ||
* sign (fixed 1bit) -> deltaSecond -> workerId -> sequence(within the same second) | ||
* | ||
* @author yutianbao | ||
*/ | ||
public class BitsAllocator { | ||
/** | ||
* Total 64 bits | ||
*/ | ||
public static final int TOTAL_BITS = 1 << 6; | ||
|
||
/** | ||
* Bits for [sign-> second-> workId-> sequence] | ||
*/ | ||
private int signBits = 1; | ||
private final int timestampBits; | ||
private final int workerIdBits; | ||
private final int sequenceBits; | ||
|
||
/** | ||
* Max value for workId & sequence | ||
*/ | ||
private final long maxDeltaSeconds; | ||
private final long maxWorkerId; | ||
private final long maxSequence; | ||
|
||
/** | ||
* Shift for timestamp & workerId | ||
*/ | ||
private final int timestampShift; | ||
private final int workerIdShift; | ||
|
||
/** | ||
* Constructor with timestampBits, workerIdBits, sequenceBits<br> | ||
* The highest bit used for sign, so <code>63</code> bits for timestampBits, workerIdBits, sequenceBits | ||
*/ | ||
public BitsAllocator(int timestampBits, int workerIdBits, int sequenceBits) { | ||
// make sure allocated 64 bits | ||
int allocateTotalBits = signBits + timestampBits + workerIdBits + sequenceBits; | ||
Assert.isTrue(allocateTotalBits == TOTAL_BITS, "allocate not enough 64 bits"); | ||
|
||
// initialize bits | ||
this.timestampBits = timestampBits; | ||
this.workerIdBits = workerIdBits; | ||
this.sequenceBits = sequenceBits; | ||
|
||
// initialize max value | ||
this.maxDeltaSeconds = ~(-1L << timestampBits); | ||
this.maxWorkerId = ~(-1L << workerIdBits); | ||
this.maxSequence = ~(-1L << sequenceBits); | ||
|
||
// initialize shift | ||
this.timestampShift = workerIdBits + sequenceBits; | ||
this.workerIdShift = sequenceBits; | ||
} | ||
|
||
/** | ||
* Allocate bits for UID according to delta seconds & workerId & sequence<br> | ||
* <b>Note that: </b>The highest bit will always be 0 for sign | ||
* | ||
* @param deltaSeconds | ||
* @param workerId | ||
* @param sequence | ||
* @return | ||
*/ | ||
public long allocate(long deltaSeconds, long workerId, long sequence) { | ||
return (deltaSeconds << timestampShift) | (workerId << workerIdShift) | sequence; | ||
} | ||
|
||
/** | ||
* Getters | ||
*/ | ||
public int getSignBits() { | ||
return signBits; | ||
} | ||
|
||
public int getTimestampBits() { | ||
return timestampBits; | ||
} | ||
|
||
public int getWorkerIdBits() { | ||
return workerIdBits; | ||
} | ||
|
||
public int getSequenceBits() { | ||
return sequenceBits; | ||
} | ||
|
||
public long getMaxDeltaSeconds() { | ||
return maxDeltaSeconds; | ||
} | ||
|
||
public long getMaxWorkerId() { | ||
return maxWorkerId; | ||
} | ||
|
||
public long getMaxSequence() { | ||
return maxSequence; | ||
} | ||
|
||
public int getTimestampShift() { | ||
return timestampShift; | ||
} | ||
|
||
public int getWorkerIdShift() { | ||
return workerIdShift; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
id-spring-boot-starter/src/main/java/com/baidu/fsg/uid/UidGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* 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 com.baidu.fsg.uid; | ||
|
||
import com.baidu.fsg.uid.exception.UidGenerateException; | ||
|
||
/** | ||
* Represents a unique id generator. | ||
* | ||
* @author yutianbao | ||
*/ | ||
public interface UidGenerator { | ||
|
||
/** | ||
* Get a unique ID | ||
* | ||
* @return UID | ||
* @throws UidGenerateException | ||
*/ | ||
long getUID() 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(long uid); | ||
|
||
} |
Oops, something went wrong.