赞
踩
多语言新雪花算法(SnowFlake IdGenerator)
Twitter雪花算法SnowFlake改造: 兼容JS截短位数的53bit分布式ID生成器
Mybatis-plus Snowflake算法生成id 基于Sequence: IdWorker.getId()
sequence: 分布式高效有序ID生产黑科技(sequence)
Hutool 基于UUID、ObjectId、Snowflake的生成方案
bid-spring-boot-starter: 全局唯一ID(比如模块标识+日期+自增序号,类似单据编号必须为RD202009080001 递增),同时还支持雪花ID Snowflake算法
NanoId 21位不重复字符串(字母加数字)
Hutool >= 5.7.20
NanoId.randomNanoId()
示例:BXpOVuLkOGdiu6bwxV34i
服务器id+12位毫秒+3位纳秒
OrderId= machineId+ (System.currentTimeMillis()+“”).substring(1)+
(System.nanoTime()+“”).substring(7,10);
22位自增id
生成规则 13位毫秒+2位IP后缀+7位自增
import cn.hutool.core.collection.ConcurrentHashSet; import com.weibo.bop.fms.common.util.IPUtil; import lombok.extern.slf4j.Slf4j; import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; @Slf4j public class CustomIdGenerator { /** * 自增起始值 */ private static final int START_NO = 1000000; /** * 自增最大值 */ private static final int END_NO = 9999999; private static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger(START_NO); /** * 自增id 22位 * 生成规则 13位毫秒+2位IP后缀+7位自增 */ public static String nextId() { // 生成最大的自增值后重置 if (ATOMIC_INTEGER.get() == END_NO) { ATOMIC_INTEGER.set(START_NO); } int increment = ATOMIC_INTEGER.getAndIncrement(); return System.currentTimeMillis() + IPUtil.getLocalIpSuffix() + increment; } public static void main(String[] args) { //String id = CustomIdGenerator.nextId(); //log.info("{}", id); threadTest(); } public static void threadTest() { int count = 1000; final CountDownLatch latch = new CountDownLatch(count); Set<String> set = new ConcurrentHashSet<>(); for (int i = 0; i < count; i++) { Thread thread = new Thread(() -> { String l = CustomIdGenerator.nextId(); log.info("{}", l); boolean add = set.add(l); if (!add) { System.out.println("重复" + l); } latch.countDown(); }); thread.start(); } try { latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } } } /** * IP相关工具类 * * @author zhaoyang10 * @date 2019-03-02 */ @Slf4j public class IPUtil { private IPUtil() { } private static String IP_SUFFIX = null; public static String getLocalIpSuffix() { if (null != IP_SUFFIX) { return IP_SUFFIX; } try { InetAddress addr = InetAddress.getLocalHost(); // 172.17.0.4 172.17.0.199 String hostAddress = addr.getHostAddress(); int len = 4; int suffixLen = 2; if (null != hostAddress && hostAddress.length() > len) { String ipSuffix = hostAddress.trim().split("\\.")[3]; if (ipSuffix.length() == suffixLen) { IP_SUFFIX = ipSuffix; return IP_SUFFIX; } ipSuffix = "0" + ipSuffix; IP_SUFFIX = ipSuffix.substring(ipSuffix.length() - 2); return IP_SUFFIX; } IP_SUFFIX = ThreadLocalRandom.current().nextInt(10, 20) + ""; return IP_SUFFIX; } catch (Exception e) { log.error("获取IP失败:{}", e.getMessage()); IP_SUFFIX = ThreadLocalRandom.current().nextInt(10, 20) + ""; return IP_SUFFIX; } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。