├── src ├── main │ ├── resources │ │ └── application.properties │ └── java │ │ └── net │ │ └── xdevelop │ │ └── snowflake │ │ ├── utils │ │ ├── IpUtils.java │ │ └── DateUtils.java │ │ ├── exception │ │ └── UidGenerateException.java │ │ └── SnowflakeUidGenerator.java └── test │ └── java │ └── net │ └── xdevelop │ └── snowflake │ └── SnowflakeUidGeneratorTests.java ├── .mvn └── wrapper │ ├── maven-wrapper.jar │ └── maven-wrapper.properties ├── .gitignore ├── LICENSE ├── pom.xml ├── README.md ├── mvnw.cmd └── mvnw /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/johnhuang-cn/snowflake-uid/HEAD/.mvn/wrapper/maven-wrapper.jar -------------------------------------------------------------------------------- /.mvn/wrapper/maven-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.3/apache-maven-3.5.3-bin.zip 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | 3 | /target/ 4 | !.mvn/wrapper/maven-wrapper.jar 5 | 6 | ### STS ### 7 | .apt_generated 8 | .classpath 9 | .factorypath 10 | .project 11 | .settings 12 | .springBeans 13 | .sts4-cache 14 | 15 | ### IntelliJ IDEA ### 16 | .idea 17 | *.iws 18 | *.iml 19 | *.ipr 20 | 21 | ### NetBeans ### 22 | /nbproject/private/ 23 | /build/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ -------------------------------------------------------------------------------- /src/main/java/net/xdevelop/snowflake/utils/IpUtils.java: -------------------------------------------------------------------------------- 1 | package net.xdevelop.snowflake.utils; 2 | 3 | public class IpUtils { 4 | public static String longToIpV4(long longIp) { 5 | int octet3 = (int) ((longIp >> 24) % 256); 6 | int octet2 = (int) ((longIp >> 16) % 256); 7 | int octet1 = (int) ((longIp >> 8) % 256); 8 | int octet0 = (int) ((longIp) % 256); 9 | return octet3 + "." + octet2 + "." + octet1 + "." + octet0; 10 | } 11 | 12 | public static long ipV4ToLong(String ip) { 13 | String[] octets = ip.split("\\."); 14 | return (Long.parseLong(octets[0]) << 24) + (Integer.parseInt(octets[1]) << 16) 15 | + (Integer.parseInt(octets[2]) << 8) + Integer.parseInt(octets[3]); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 John Huang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/main/java/net/xdevelop/snowflake/utils/DateUtils.java: -------------------------------------------------------------------------------- 1 | package net.xdevelop.snowflake.utils; 2 | 3 | import java.text.ParseException; 4 | import java.text.SimpleDateFormat; 5 | import java.util.Date; 6 | 7 | public class DateUtils { 8 | /** 9 | * Patterns 10 | */ 11 | public static final String DAY_PATTERN = "yyyy-MM-dd"; 12 | public static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; 13 | 14 | /** 15 | * Parse date by 'yyyy-MM-dd' pattern 16 | * 17 | * @param str 18 | * @return 19 | */ 20 | public static Date parseByDayPattern(String str) { 21 | try { 22 | SimpleDateFormat sdf = new SimpleDateFormat(DAY_PATTERN); 23 | return sdf.parse(str); 24 | } catch (ParseException e) { 25 | throw new RuntimeException(e); 26 | } 27 | } 28 | 29 | /** 30 | * Format date by 'yyyy-MM-dd HH:mm:ss' pattern 31 | * 32 | * @param date 33 | * @return 34 | */ 35 | public static String formatByDateTimePattern(Date date) { 36 | SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_PATTERN); 37 | return sdf.format(date); 38 | } 39 | 40 | /** 41 | * Format date by 'yyyy-MM-dd' pattern 42 | * 43 | * @param date 44 | * @return 45 | */ 46 | public static String formatByDatePattern(Date date) { 47 | SimpleDateFormat sdf = new SimpleDateFormat(DAY_PATTERN); 48 | return sdf.format(date); 49 | } 50 | } -------------------------------------------------------------------------------- /src/main/java/net/xdevelop/snowflake/exception/UidGenerateException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2017 Baidu, Inc. All Rights Reserve. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package net.xdevelop.snowflake.exception; 18 | 19 | /** 20 | * UidGenerateException 21 | * 22 | * @author yutianbao 23 | */ 24 | public class UidGenerateException extends RuntimeException { 25 | 26 | /** 27 | * Serial Version UID 28 | */ 29 | private static final long serialVersionUID = -27048199131316992L; 30 | 31 | /** 32 | * Default constructor 33 | */ 34 | public UidGenerateException() { 35 | super(); 36 | } 37 | 38 | /** 39 | * Constructor with message & cause 40 | * 41 | * @param message 42 | * @param cause 43 | */ 44 | public UidGenerateException(String message, Throwable cause) { 45 | super(message, cause); 46 | } 47 | 48 | /** 49 | * Constructor with message 50 | * 51 | * @param message 52 | */ 53 | public UidGenerateException(String message) { 54 | super(message); 55 | } 56 | 57 | /** 58 | * Constructor with message format 59 | * 60 | * @param msgFormat 61 | * @param args 62 | */ 63 | public UidGenerateException(String msgFormat, Object... args) { 64 | super(String.format(msgFormat, args)); 65 | } 66 | 67 | /** 68 | * Constructor with cause 69 | * 70 | * @param cause 71 | */ 72 | public UidGenerateException(Throwable cause) { 73 | super(cause); 74 | } 75 | 76 | } -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | net.xdevelop 7 | snowflake-uid 8 | 1.0.0 9 | jar 10 | 11 | snowflake-uid 12 | Snowflake uid generator 13 | 14 | 15 | UTF-8 16 | UTF-8 17 | 5.0.7.RELEASE 18 | 1.8 19 | 1.7.25 20 | 21 | 22 | 23 | 24 | org.springframework 25 | spring-core 26 | ${spring.version} 27 | 28 | 29 | 30 | org.slf4j 31 | slf4j-api 32 | 1.7.25 33 | 34 | 35 | 36 | ch.qos.logback 37 | logback-classic 38 | 1.1.3 39 | 40 | 41 | 42 | junit 43 | junit 44 | 4.12 45 | test 46 | 47 | 48 | 49 | 50 | 51 | 52 | org.apache.maven.plugins 53 | maven-compiler-plugin 54 | 55 | ${java.version} 56 | ${java.version} 57 | ${project.build.sourceEncoding} 58 | 59 | 3.7.0 60 | 61 | 62 | org.apache.maven.plugins 63 | maven-source-plugin 64 | 3.0.1 65 | 66 | 67 | package 68 | 69 | jar 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Snowflake-UID Generator 2 | 3 | UidGenerator is a Java implemented, Snowflake based unique ID generator. It referenced the baidu's implementaton of [https://github.com/baidu/uid-generator](https://github.com/baidu/uid-generator). But this generator is a simpler and non-db implementation, the default generator uses the last 24bit value of the ip address as the worker id, 28 bits for timestamp, and 11 bits for sequence. So it can generate 2048 sequence/second for each instance for about 8.7 years. 4 | 5 | The kubernetes pod CIDR is less than /24, so this generator is suitable for k8s environment. If you can set the runtime network CIDR as /16, you can extend the time bits to 32 and sequence bits to 15, then the generator can generate 32768 sequences/seconds for each instance for about 128 years. 6 | 7 | --- 8 | 9 | ## 中文说明: 10 | 11 | UidGenerator是基于Twitter Snowflake算法的分布式ID生成器,参考了百度的实现[https://github.com/baidu/uid-generator](https://github.com/baidu/uid-generator). 不过相对来说要更简单并且不依赖于DB。其默认实现使用28位的时间,24位的worker id和最后的11位用于生成序列。提供单实例每秒2048个序列号,可使用8.7年。 12 | 13 | 这么做的目的是为了让其适合在K8S环境中使用,因为K8S默认的CIDR是/24,所以使用24位worker id可保证每个实例生成的ID的唯一性。当然相应的,单例每秒的id数要少些,不过对大部分应用也是够的,而且通常高可用环境下每个应用不止一个实例。如果运行的k8s环境的CIDR可以设置为/16,那么可以将时间和序列的位数各加4个,这样就可以支持每秒32768个序列,并能使用128年。 14 | 15 | ## Snowflake 16 | 17 | \*\* Snowflake algorithm:\*\* An unique id consists of worker node, timestamp and sequence within that timestamp. Usually, it is a 64 bits number\(long\), and the default bits of that three fields are as follows: 18 | 19 | | sign | delta seconds | worker node id | sequence | 20 | | :---: | :---: | :---: | :---: | 21 | | 1 bit | 28 bits | 24bits | 11bits | 22 | 23 | * sign\(1bit\) 24 | The highest bit is always 0. 25 | 26 | * delta seconds \(28 bits\) 27 | The next 28 bits, represents delta seconds since a customer epoch\(2018-07-01\). The maximum time will be 8.7 years. 28 | 29 | * worker id \(24 bits\) 30 | The next 24 bits, represents the worker node id, maximum value will be 16.7 million. Why set 24 bits instead of the 22 bits of baidu's implementation is because the default CIDR of k8s env is /24. 24 bits can guarantee that each instance has an unique worker id. If you can set the network CIDR to /16, you can decrease the worker id bits, and extend the time bits and sequence bits. 31 | 32 | * sequence \(11 bits\) 33 | the last 11 bits, represents sequence within the one second, maximum is 2048 per second for one instance by default. 34 | 35 | ## Quick Start 36 | 37 | * ### Download the project and import 38 | 39 | This project haven't post to maven repo, so please download/clone this project and run "mvn install" to install it to local maven repo. 40 | 41 | * ### Add maven dependency to project 42 | 43 | ``` 44 | 45 | net.xdevelop 46 | snowflake-uid 47 | 1.0.0 48 | 49 | ``` 50 | * ### Init the bean 51 | 52 | ``` 53 | @Configuration 54 | public class UIDConfig { 55 | @Bean 56 | public SnowflakeUidGenerator customerUidGenerator() { 57 | long workerId = SnowflakeUidGenerator.getWorkerIdByIP(24); 58 | return new SnowflakeUidGenerator(workerId); 59 | } 60 | 61 | // init multiple uid generators for different DB tables 62 | @Bean 63 | public SnowflakeUidGenerator orderUidGenerator() { 64 | long workerId = SnowflakeUidGenerator.getWorkerIdByIP(24); 65 | return new SnowflakeUidGenerator(workerId); 66 | } 67 | } 68 | ``` 69 | * ### Generate the uid 70 | 71 | ``` 72 | @Component 73 | public class CustomerService { 74 | @Autowired 75 | CustomerMapper mapper; 76 | 77 | @Autowired 78 | @Qualifier("customerUidGenerator") 79 | SnowflakeUidGenerator uidGenerator; 80 | 81 | public void addCustomer(Customer customer) { 82 | customer.setCustomerId(uidGenerator.getUID()); 83 | long curTime = System.currentTimeMillis(); 84 | customer.setCreatedDate(curTime); 85 | customer.setLastModifiedDate(curTime); 86 | mapper.insert(customer); 87 | } 88 | } 89 | ``` 90 | * ### Optional: customize the generator 91 | 92 | ``` 93 | @Configuration 94 | public class UIDConfig { 95 | @Bean 96 | public SnowflakeUidGenerator customerUidGenerator() { 97 | long workerId = SnowflakeUidGenerator.getWorkerIdByIP(16); 98 | String baseDate = "2018-08-01"; 99 | int timeBits = 32; 100 | int workerBits = 16; 101 | int seqBits = 15; 102 | return new SnowflakeUidGenerator(workerId, baseDate, timeBits, workerBits, seqBits); 103 | } 104 | } 105 | ``` 106 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /src/test/java/net/xdevelop/snowflake/SnowflakeUidGeneratorTests.java: -------------------------------------------------------------------------------- 1 | package net.xdevelop.snowflake; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Date; 5 | import java.util.HashSet; 6 | import java.util.List; 7 | import java.util.Set; 8 | import java.util.concurrent.ConcurrentSkipListSet; 9 | import java.util.concurrent.atomic.AtomicInteger; 10 | 11 | import org.junit.AfterClass; 12 | import org.junit.Assert; 13 | import org.junit.Before; 14 | import org.junit.Test; 15 | import org.springframework.util.StringUtils; 16 | 17 | import net.xdevelop.snowflake.utils.DateUtils; 18 | 19 | public class SnowflakeUidGeneratorTests { 20 | private int workerId = 132; 21 | private int timeBits = 28; 22 | private int workerBits = 24; 23 | private int seqBits = 11; 24 | private String baseDate = "2018-07-01"; 25 | private static long speed = 0; 26 | 27 | private SnowflakeUidGenerator uidGenerator; 28 | 29 | @Test 30 | public void contextLoads() { 31 | } 32 | 33 | @Before 34 | public void setUp() throws Exception { 35 | uidGenerator = new SnowflakeUidGenerator(workerId, baseDate, timeBits, workerBits, seqBits); 36 | } 37 | 38 | @AfterClass 39 | public static void printSpeed() { 40 | System.out.println(String.format("Speed: %d/s", speed)); 41 | } 42 | 43 | @Test(expected=Exception.class) 44 | public void testConstruct() { 45 | new SnowflakeUidGenerator(2<<24, "2017-07-1", 28, 24, 11); 46 | } 47 | 48 | @Test 49 | public void testIDBits() { 50 | String timestamp = DateUtils.formatByDatePattern(new Date()); 51 | String timeInfo = String.format("\"timestamp\":\"%s", timestamp); 52 | String idsInfo = String.format("\"workerId\":\"%d\"", workerId); 53 | 54 | long uid = uidGenerator.getUID(); 55 | System.out.println(String.format("uid: %d binary str: %s", uid, Long.toBinaryString(uid))); 56 | 57 | String parseInfo = uidGenerator.parseUID(uid); 58 | System.out.println(String.format("uid parsed: %s" , parseInfo)); 59 | Assert.assertTrue(parseInfo.indexOf(timeInfo) > 0); 60 | Assert.assertTrue(parseInfo.indexOf(idsInfo) > 0); 61 | } 62 | 63 | private static final int SIZE = 100000; // 10w 64 | private static final boolean VERBOSE = true; 65 | private static final int THREADS = Runtime.getRuntime().availableProcessors() << 1; 66 | 67 | 68 | /** 69 | * Test for serially generate 70 | */ 71 | @Test 72 | public void testSerialGenerate() { 73 | // Generate UID serially 74 | Set uidSet = new HashSet(SIZE); 75 | for (int i = 0; i < SIZE; i++) { 76 | doGenerate(uidSet, i); 77 | } 78 | 79 | // Check UIDs are all unique 80 | checkUniqueID(uidSet); 81 | } 82 | 83 | /** 84 | * Test for parallel generate 85 | * 86 | * @throws InterruptedException 87 | */ 88 | @Test 89 | public void testParallelGenerate() throws InterruptedException { 90 | AtomicInteger control = new AtomicInteger(-1); 91 | Set uidSet = new ConcurrentSkipListSet<>(); 92 | 93 | // Initialize threads 94 | List threadList = new ArrayList(THREADS); 95 | long start = System.currentTimeMillis(); 96 | for (int i = 0; i < THREADS; i++) { 97 | Thread thread = new Thread(() -> workerRun(uidSet, control)); 98 | thread.setName("UID-generator-" + i); 99 | 100 | threadList.add(thread); 101 | thread.start(); 102 | } 103 | 104 | // Wait for worker done 105 | for (Thread thread : threadList) { 106 | thread.join(); 107 | } 108 | 109 | long end = System.currentTimeMillis(); 110 | speed = SIZE / (end - start) * 1000; 111 | 112 | // Check generate 10w times 113 | Assert.assertEquals(SIZE, control.get()); 114 | 115 | // Check UIDs are all unique 116 | checkUniqueID(uidSet); 117 | } 118 | 119 | /** 120 | * Worker run 121 | */ 122 | private void workerRun(Set uidSet, AtomicInteger control) { 123 | for (;;) { 124 | int myPosition = control.updateAndGet(old -> (old == SIZE ? SIZE : old + 1)); 125 | if (myPosition == SIZE) { 126 | return; 127 | } 128 | 129 | doGenerate(uidSet, myPosition); 130 | } 131 | } 132 | 133 | /** 134 | * Do generating 135 | */ 136 | private void doGenerate(Set uidSet, int index) { 137 | long uid = uidGenerator.getUID(); 138 | String parsedInfo = uidGenerator.parseUID(uid); 139 | uidSet.add(uid); 140 | 141 | // Check UID is positive, and can be parsed 142 | Assert.assertTrue(uid > 0L); 143 | Assert.assertTrue(!StringUtils.isEmpty(parsedInfo)); 144 | 145 | if (VERBOSE) { 146 | System.out.println(Thread.currentThread().getName() + " No." + index + " >>> " + parsedInfo); 147 | } 148 | } 149 | 150 | /** 151 | * Check UIDs are all unique 152 | */ 153 | private void checkUniqueID(Set uidSet) { 154 | System.out.println(uidSet.size()); 155 | Assert.assertEquals(SIZE, uidSet.size()); 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /mvnw.cmd: -------------------------------------------------------------------------------- 1 | @REM ---------------------------------------------------------------------------- 2 | @REM Licensed to the Apache Software Foundation (ASF) under one 3 | @REM or more contributor license agreements. See the NOTICE file 4 | @REM distributed with this work for additional information 5 | @REM regarding copyright ownership. The ASF licenses this file 6 | @REM to you under the Apache License, Version 2.0 (the 7 | @REM "License"); you may not use this file except in compliance 8 | @REM with the License. You may obtain a copy of the License at 9 | @REM 10 | @REM http://www.apache.org/licenses/LICENSE-2.0 11 | @REM 12 | @REM Unless required by applicable law or agreed to in writing, 13 | @REM software distributed under the License is distributed on an 14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | @REM KIND, either express or implied. See the License for the 16 | @REM specific language governing permissions and limitations 17 | @REM under the License. 18 | @REM ---------------------------------------------------------------------------- 19 | 20 | @REM ---------------------------------------------------------------------------- 21 | @REM Maven2 Start Up Batch script 22 | @REM 23 | @REM Required ENV vars: 24 | @REM JAVA_HOME - location of a JDK home dir 25 | @REM 26 | @REM Optional ENV vars 27 | @REM M2_HOME - location of maven2's installed home dir 28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands 29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending 30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven 31 | @REM e.g. to debug Maven itself, use 32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files 34 | @REM ---------------------------------------------------------------------------- 35 | 36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' 37 | @echo off 38 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on' 39 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% 40 | 41 | @REM set %HOME% to equivalent of $HOME 42 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") 43 | 44 | @REM Execute a user defined script before this one 45 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre 46 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending 47 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat" 48 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd" 49 | :skipRcPre 50 | 51 | @setlocal 52 | 53 | set ERROR_CODE=0 54 | 55 | @REM To isolate internal variables from possible post scripts, we use another setlocal 56 | @setlocal 57 | 58 | @REM ==== START VALIDATION ==== 59 | if not "%JAVA_HOME%" == "" goto OkJHome 60 | 61 | echo. 62 | echo Error: JAVA_HOME not found in your environment. >&2 63 | echo Please set the JAVA_HOME variable in your environment to match the >&2 64 | echo location of your Java installation. >&2 65 | echo. 66 | goto error 67 | 68 | :OkJHome 69 | if exist "%JAVA_HOME%\bin\java.exe" goto init 70 | 71 | echo. 72 | echo Error: JAVA_HOME is set to an invalid directory. >&2 73 | echo JAVA_HOME = "%JAVA_HOME%" >&2 74 | echo Please set the JAVA_HOME variable in your environment to match the >&2 75 | echo location of your Java installation. >&2 76 | echo. 77 | goto error 78 | 79 | @REM ==== END VALIDATION ==== 80 | 81 | :init 82 | 83 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn". 84 | @REM Fallback to current working directory if not found. 85 | 86 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% 87 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir 88 | 89 | set EXEC_DIR=%CD% 90 | set WDIR=%EXEC_DIR% 91 | :findBaseDir 92 | IF EXIST "%WDIR%"\.mvn goto baseDirFound 93 | cd .. 94 | IF "%WDIR%"=="%CD%" goto baseDirNotFound 95 | set WDIR=%CD% 96 | goto findBaseDir 97 | 98 | :baseDirFound 99 | set MAVEN_PROJECTBASEDIR=%WDIR% 100 | cd "%EXEC_DIR%" 101 | goto endDetectBaseDir 102 | 103 | :baseDirNotFound 104 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR% 105 | cd "%EXEC_DIR%" 106 | 107 | :endDetectBaseDir 108 | 109 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig 110 | 111 | @setlocal EnableExtensions EnableDelayedExpansion 112 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a 113 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% 114 | 115 | :endReadAdditionalConfig 116 | 117 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" 118 | 119 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" 120 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 121 | 122 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* 123 | if ERRORLEVEL 1 goto error 124 | goto end 125 | 126 | :error 127 | set ERROR_CODE=1 128 | 129 | :end 130 | @endlocal & set ERROR_CODE=%ERROR_CODE% 131 | 132 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost 133 | @REM check for post script, once with legacy .bat ending and once with .cmd ending 134 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat" 135 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd" 136 | :skipRcPost 137 | 138 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' 139 | if "%MAVEN_BATCH_PAUSE%" == "on" pause 140 | 141 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE% 142 | 143 | exit /B %ERROR_CODE% 144 | -------------------------------------------------------------------------------- /mvnw: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # ---------------------------------------------------------------------------- 3 | # Licensed to the Apache Software Foundation (ASF) under one 4 | # or more contributor license agreements. See the NOTICE file 5 | # distributed with this work for additional information 6 | # regarding copyright ownership. The ASF licenses this file 7 | # to you under the Apache License, Version 2.0 (the 8 | # "License"); you may not use this file except in compliance 9 | # with the License. You may obtain a copy of the License at 10 | # 11 | # http://www.apache.org/licenses/LICENSE-2.0 12 | # 13 | # Unless required by applicable law or agreed to in writing, 14 | # software distributed under the License is distributed on an 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 | # KIND, either express or implied. See the License for the 17 | # specific language governing permissions and limitations 18 | # under the License. 19 | # ---------------------------------------------------------------------------- 20 | 21 | # ---------------------------------------------------------------------------- 22 | # Maven2 Start Up Batch script 23 | # 24 | # Required ENV vars: 25 | # ------------------ 26 | # JAVA_HOME - location of a JDK home dir 27 | # 28 | # Optional ENV vars 29 | # ----------------- 30 | # M2_HOME - location of maven2's installed home dir 31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven 32 | # e.g. to debug Maven itself, use 33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files 35 | # ---------------------------------------------------------------------------- 36 | 37 | if [ -z "$MAVEN_SKIP_RC" ] ; then 38 | 39 | if [ -f /etc/mavenrc ] ; then 40 | . /etc/mavenrc 41 | fi 42 | 43 | if [ -f "$HOME/.mavenrc" ] ; then 44 | . "$HOME/.mavenrc" 45 | fi 46 | 47 | fi 48 | 49 | # OS specific support. $var _must_ be set to either true or false. 50 | cygwin=false; 51 | darwin=false; 52 | mingw=false 53 | case "`uname`" in 54 | CYGWIN*) cygwin=true ;; 55 | MINGW*) mingw=true;; 56 | Darwin*) darwin=true 57 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home 58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html 59 | if [ -z "$JAVA_HOME" ]; then 60 | if [ -x "/usr/libexec/java_home" ]; then 61 | export JAVA_HOME="`/usr/libexec/java_home`" 62 | else 63 | export JAVA_HOME="/Library/Java/Home" 64 | fi 65 | fi 66 | ;; 67 | esac 68 | 69 | if [ -z "$JAVA_HOME" ] ; then 70 | if [ -r /etc/gentoo-release ] ; then 71 | JAVA_HOME=`java-config --jre-home` 72 | fi 73 | fi 74 | 75 | if [ -z "$M2_HOME" ] ; then 76 | ## resolve links - $0 may be a link to maven's home 77 | PRG="$0" 78 | 79 | # need this for relative symlinks 80 | while [ -h "$PRG" ] ; do 81 | ls=`ls -ld "$PRG"` 82 | link=`expr "$ls" : '.*-> \(.*\)$'` 83 | if expr "$link" : '/.*' > /dev/null; then 84 | PRG="$link" 85 | else 86 | PRG="`dirname "$PRG"`/$link" 87 | fi 88 | done 89 | 90 | saveddir=`pwd` 91 | 92 | M2_HOME=`dirname "$PRG"`/.. 93 | 94 | # make it fully qualified 95 | M2_HOME=`cd "$M2_HOME" && pwd` 96 | 97 | cd "$saveddir" 98 | # echo Using m2 at $M2_HOME 99 | fi 100 | 101 | # For Cygwin, ensure paths are in UNIX format before anything is touched 102 | if $cygwin ; then 103 | [ -n "$M2_HOME" ] && 104 | M2_HOME=`cygpath --unix "$M2_HOME"` 105 | [ -n "$JAVA_HOME" ] && 106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 107 | [ -n "$CLASSPATH" ] && 108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"` 109 | fi 110 | 111 | # For Migwn, ensure paths are in UNIX format before anything is touched 112 | if $mingw ; then 113 | [ -n "$M2_HOME" ] && 114 | M2_HOME="`(cd "$M2_HOME"; pwd)`" 115 | [ -n "$JAVA_HOME" ] && 116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`" 117 | # TODO classpath? 118 | fi 119 | 120 | if [ -z "$JAVA_HOME" ]; then 121 | javaExecutable="`which javac`" 122 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then 123 | # readlink(1) is not available as standard on Solaris 10. 124 | readLink=`which readlink` 125 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then 126 | if $darwin ; then 127 | javaHome="`dirname \"$javaExecutable\"`" 128 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac" 129 | else 130 | javaExecutable="`readlink -f \"$javaExecutable\"`" 131 | fi 132 | javaHome="`dirname \"$javaExecutable\"`" 133 | javaHome=`expr "$javaHome" : '\(.*\)/bin'` 134 | JAVA_HOME="$javaHome" 135 | export JAVA_HOME 136 | fi 137 | fi 138 | fi 139 | 140 | if [ -z "$JAVACMD" ] ; then 141 | if [ -n "$JAVA_HOME" ] ; then 142 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 143 | # IBM's JDK on AIX uses strange locations for the executables 144 | JAVACMD="$JAVA_HOME/jre/sh/java" 145 | else 146 | JAVACMD="$JAVA_HOME/bin/java" 147 | fi 148 | else 149 | JAVACMD="`which java`" 150 | fi 151 | fi 152 | 153 | if [ ! -x "$JAVACMD" ] ; then 154 | echo "Error: JAVA_HOME is not defined correctly." >&2 155 | echo " We cannot execute $JAVACMD" >&2 156 | exit 1 157 | fi 158 | 159 | if [ -z "$JAVA_HOME" ] ; then 160 | echo "Warning: JAVA_HOME environment variable is not set." 161 | fi 162 | 163 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher 164 | 165 | # traverses directory structure from process work directory to filesystem root 166 | # first directory with .mvn subdirectory is considered project base directory 167 | find_maven_basedir() { 168 | 169 | if [ -z "$1" ] 170 | then 171 | echo "Path not specified to find_maven_basedir" 172 | return 1 173 | fi 174 | 175 | basedir="$1" 176 | wdir="$1" 177 | while [ "$wdir" != '/' ] ; do 178 | if [ -d "$wdir"/.mvn ] ; then 179 | basedir=$wdir 180 | break 181 | fi 182 | # workaround for JBEAP-8937 (on Solaris 10/Sparc) 183 | if [ -d "${wdir}" ]; then 184 | wdir=`cd "$wdir/.."; pwd` 185 | fi 186 | # end of workaround 187 | done 188 | echo "${basedir}" 189 | } 190 | 191 | # concatenates all lines of a file 192 | concat_lines() { 193 | if [ -f "$1" ]; then 194 | echo "$(tr -s '\n' ' ' < "$1")" 195 | fi 196 | } 197 | 198 | BASE_DIR=`find_maven_basedir "$(pwd)"` 199 | if [ -z "$BASE_DIR" ]; then 200 | exit 1; 201 | fi 202 | 203 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"} 204 | echo $MAVEN_PROJECTBASEDIR 205 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" 206 | 207 | # For Cygwin, switch paths to Windows format before running java 208 | if $cygwin; then 209 | [ -n "$M2_HOME" ] && 210 | M2_HOME=`cygpath --path --windows "$M2_HOME"` 211 | [ -n "$JAVA_HOME" ] && 212 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"` 213 | [ -n "$CLASSPATH" ] && 214 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"` 215 | [ -n "$MAVEN_PROJECTBASEDIR" ] && 216 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"` 217 | fi 218 | 219 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain 220 | 221 | exec "$JAVACMD" \ 222 | $MAVEN_OPTS \ 223 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ 224 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ 225 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" 226 | -------------------------------------------------------------------------------- /src/main/java/net/xdevelop/snowflake/SnowflakeUidGenerator.java: -------------------------------------------------------------------------------- 1 | package net.xdevelop.snowflake; 2 | 3 | import java.net.InetAddress; 4 | import java.net.UnknownHostException; 5 | import java.util.Date; 6 | import java.util.concurrent.TimeUnit; 7 | 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | import org.springframework.util.Assert; 11 | 12 | import net.xdevelop.snowflake.exception.UidGenerateException; 13 | import net.xdevelop.snowflake.utils.DateUtils; 14 | import net.xdevelop.snowflake.utils.IpUtils; 15 | 16 | /** 17 | * Generate 64bits uid (long), default allocated as below:
18 | *
{@code
 19 |  * +------+----------------------+----------------+-----------+
 20 |  * | sign |     delta seconds    | worker node id | sequence  |
 21 |  * +------+----------------------+----------------+-----------+
 22 |  *   1bit          28bits              24bits         11bits
 23 |  * }
24 | * 25 | * You can also specified the bits by Spring property setting. 26 | *
  • snowflake.timeBits: default as 32 27 | *
  • snowflake.workerBits: default as 8 28 | *
  • snowflake.dbBits: default as 8 29 | *
  • snowflake.seqBits: default as 15 30 | *
  • snowflake.baseDate: Epoch date string format 'yyyy-MM-dd'. Default as '2018-07-01'

    31 | * 32 | * Note that: The total bits must be 64 -1 33 | * 34 | * @author yutianbao@baidu, john.huang 35 | */ 36 | public class SnowflakeUidGenerator { 37 | private static final Logger LOGGER = LoggerFactory.getLogger(SnowflakeUidGenerator.class); 38 | 39 | public static final int TOTAL_BITS = 1 << 6; 40 | 41 | public static final String DEFAULT_BASE_DATE = "2018-07-01"; 42 | 43 | /** 44 | * Bits for [sign-> second-> workId-> sequence] 45 | */ 46 | protected int signBits = 1; 47 | 48 | // the start time, default is 2018-7-1 49 | protected long baseEpoch = 1530374400000L; 50 | 51 | // delta seconds 52 | protected int timeBits = 32; 53 | 54 | // worker node id bits 55 | protected int workerBits = 8; 56 | 57 | // sequence bits 58 | protected int seqBits = 15; 59 | 60 | /** Volatile fields caused by nextId() */ 61 | protected long sequence = 0L; 62 | protected long lastSecond = -1L; 63 | 64 | protected long maxDeltaSeconds; 65 | protected long maxWorkerId; 66 | protected long maxSequence; 67 | 68 | protected int timestampShift; 69 | protected int workerIdShift; 70 | 71 | protected long workerId; 72 | 73 | /** 74 | * Initialize a uid generator with specified settings. 75 | * 76 | * @param workerId specify an id for the worker, the worker is the app to generate uid 77 | * @param dbId specify an id for the target db 78 | * @param baseDate the base date the generator begin with 79 | * @param timeBits time bits length 80 | * @param workerBits worker id bits length 81 | * @param dbBits target db id bits length 82 | * @param seqBits sequence bits length 83 | */ 84 | public SnowflakeUidGenerator(long workerId, String baseDate, int timeBits, int workerBits, int seqBits) { 85 | this.workerId = workerId; 86 | 87 | Date date = DateUtils.parseByDayPattern(baseDate); 88 | this.baseEpoch = TimeUnit.MILLISECONDS.toSeconds(date.getTime()); 89 | 90 | this.timeBits = timeBits; 91 | this.workerBits = workerBits; 92 | this.seqBits = seqBits; 93 | 94 | int allocateTotalBits = signBits + timeBits + workerBits + seqBits; 95 | Assert.isTrue(allocateTotalBits == TOTAL_BITS, "allocate not enough 64 bits"); 96 | 97 | // initialize max value 98 | this.maxDeltaSeconds = ~(-1L << timeBits); 99 | this.maxWorkerId = ~(-1L << workerBits); 100 | this.maxSequence = ~(-1L << seqBits); 101 | 102 | Assert.isTrue(workerId <= maxWorkerId, String.format("workerId exceed the max value %d", maxWorkerId)); 103 | 104 | // initialize shift 105 | this.timestampShift = workerBits + seqBits; 106 | this.workerIdShift = seqBits; 107 | } 108 | 109 | public SnowflakeUidGenerator(long workerId, int timeBits, int workerBits, int seqBits) { 110 | this(workerId, DEFAULT_BASE_DATE, timeBits, workerBits, seqBits); 111 | } 112 | 113 | public SnowflakeUidGenerator(long workerId) { 114 | this(workerId, DEFAULT_BASE_DATE, 28, 24, 11); 115 | } 116 | 117 | public long getUID() throws UidGenerateException { 118 | try { 119 | return nextId(); 120 | } catch (Exception e) { 121 | LOGGER.error("Generate unique id exception. ", e); 122 | throw new UidGenerateException(e); 123 | } 124 | } 125 | 126 | public String parseUID(long uid) { 127 | // parse UID 128 | long sequence = (uid << (TOTAL_BITS - seqBits)) >>> (TOTAL_BITS - seqBits); 129 | long workerId = (uid << (timeBits + signBits)) >>> (TOTAL_BITS - workerBits); 130 | long deltaSeconds = uid >>> (workerBits + seqBits); 131 | 132 | Date thatTime = new Date(TimeUnit.SECONDS.toMillis(baseEpoch + deltaSeconds)); 133 | String thatTimeStr = DateUtils.formatByDateTimePattern(thatTime); 134 | 135 | // format as string 136 | return String.format("{\"UID\":\"%d\",\"timestamp\":\"%s\",\"workerId\":\"%d\",\"sequence\":\"%d\"}", 137 | uid, thatTimeStr, workerId, sequence); 138 | } 139 | 140 | /** 141 | * Get UID 142 | * 143 | * @return UID 144 | * @throws UidGenerateException in the case: Clock moved backwards; Exceeds the max timestamp 145 | */ 146 | protected synchronized long nextId() { 147 | long currentSecond = getCurrentSecond(); 148 | 149 | // Clock moved backwards, refuse to generate uid 150 | if (currentSecond < lastSecond) { 151 | long refusedSeconds = lastSecond - currentSecond; 152 | throw new UidGenerateException("Clock moved backwards. Refusing for %d seconds", refusedSeconds); 153 | } 154 | 155 | // At the same second, increase sequence 156 | if (currentSecond == lastSecond) { 157 | sequence = (sequence + 1) & maxSequence; 158 | // Exceed the max sequence, we wait the next second to generate uid 159 | if (sequence == 0) { 160 | currentSecond = getNextSecond(lastSecond); 161 | } 162 | 163 | // At the different second, sequence restart from zero 164 | } else { 165 | sequence = 0L; 166 | } 167 | 168 | lastSecond = currentSecond; 169 | 170 | // Allocate bits for UID 171 | long deltaSeconds = currentSecond - baseEpoch; 172 | return (deltaSeconds << timestampShift) | (workerId << workerIdShift) | sequence; 173 | } 174 | 175 | /** 176 | * Get next millisecond 177 | */ 178 | private long getNextSecond(long lastTimestamp) { 179 | long timestamp = getCurrentSecond(); 180 | while (timestamp <= lastTimestamp) { 181 | timestamp = getCurrentSecond(); 182 | } 183 | 184 | return timestamp; 185 | } 186 | 187 | /** 188 | * Get current second 189 | */ 190 | private long getCurrentSecond() { 191 | long currentSecond = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()); 192 | if (currentSecond - baseEpoch > maxDeltaSeconds) { 193 | throw new UidGenerateException("Timestamp bits is exhausted. Refusing UID generate. Now: " + currentSecond); 194 | } 195 | 196 | return currentSecond; 197 | } 198 | 199 | /** 200 | * Allocate bits for UID according to delta seconds & workerId & sequence
    201 | * Note that: The highest bit will always be 0 for sign 202 | * 203 | * @param deltaSeconds 204 | * @param workerId 205 | * @param sequence 206 | * @return 207 | */ 208 | public long allocate(long deltaSeconds, long workerId, long dbId, long sequence) { 209 | return (deltaSeconds << timestampShift) | (workerId << workerIdShift) | sequence; 210 | } 211 | 212 | public void setTimeBits(int timeBits) { 213 | if (timeBits > 0) { 214 | this.timeBits = timeBits; 215 | } 216 | } 217 | 218 | public void setWorkerBits(int workerBits) { 219 | if (workerBits > 0) { 220 | this.workerBits = workerBits; 221 | } 222 | } 223 | 224 | public void setSeqBits(int seqBits) { 225 | if (seqBits > 0) { 226 | this.seqBits = seqBits; 227 | } 228 | } 229 | 230 | /** 231 | * Get the worker id by using the last x bits of the local ip address 232 | * @throws UnknownHostException 233 | */ 234 | public static long getWorkerIdByIP(int bits) throws UidGenerateException { 235 | int shift = 64 - bits; 236 | try { 237 | InetAddress address = InetAddress.getLocalHost(); 238 | long ip = IpUtils.ipV4ToLong(address.getHostAddress()); 239 | long workerId = (ip << shift) >>> shift; 240 | return workerId; 241 | } catch (UnknownHostException e) { 242 | LOGGER.error("Generate unique id exception. ", e); 243 | throw new UidGenerateException(e); 244 | } 245 | } 246 | } --------------------------------------------------------------------------------