前言
但凡开发过分布式系统的程序员,基本都会遇到分布式写入数据ID生成的问题,而传统简单的方式是直接使用mysql的自动增长,或者oracle的序列等。常见的生产方式。
1、UUID Java自带API,生成一串唯一随机36位字符串。能够保证唯一性,但无法有序递增。由于是本地生成,性能比较高,没有网络消耗。
2、redis生成唯一主键 可以用Redis的原子操作INCR和INCRBY来实现
3、SnowFlake Twitter开源的由64位整数组成分布式ID,性能较高,并且在单机上递增。由于其强依赖机器时钟,如果人为向前调整时钟,很可能会产生重复ID。在分布式研发过程中在使用雪花算法的时候,需要注意的workId的生成,如果避免分布式系统产生重复ID。如下代码采用当前物理网卡地址和jvm的进程ID自动生成。是一个较好的解决方案。一般在一个集群中,MAC+JVM进程PID一样的几率非常小。
public class IdWorker {
private final static long twepoch = 1420041600000L;
private final static long workerIdBits = 5L;
private final static long datacenterIdBits = 5L;
private final static long maxWorkerId = -1L ^ (-1L << workerIdBits);
private final static long maxDatacenterId = -1L ^ (-1L << datacenterIdBits);
private final static long sequenceBits = 12L;
private final static long workerIdShift = sequenceBits;
private final static long datacenterIdShift = sequenceBits + workerIdBits;
private final static long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits;
private final static long sequenceMask = -1L ^ (-1L << sequenceBits);
private static long lastTimestamp = -1L;
private long sequence = 0L;
private final long workerId;
private final long datacenterId;
private static IdWorker idWorker;
public static synchronized IdWorker getInstance() {
if(idWorker == null) {
idWorker = new IdWorker();
}
return idWorker;
}
private IdWorker() {
this.datacenterId = getDatacenterId(maxDatacenterId);
this.workerId = getMaxWorkerId(datacenterId, maxWorkerId);
}
public IdWorker(long workerId, long datacenterId) {
if (workerId > maxWorkerId || workerId < 0) {
throw new IllegalArgumentException(
String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
}
if (datacenterId > maxDatacenterId || datacenterId < 0) {
throw new IllegalArgumentException(
String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId));
}
this.workerId = workerId;
this.datacenterId = datacenterId;
}
public synchronized long nextId() {
long timestamp = timeGen();
if (timestamp < lastTimestamp) {
throw new RuntimeException(String.format(
"Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
}
if (lastTimestamp == timestamp) {
sequence = (sequence + 1) & sequenceMask;
if (sequence == 0) {
timestamp = tilNextMillis(lastTimestamp);
}
} else {
sequence = 0L;
}
lastTimestamp = timestamp;
long nextId = ((timestamp - twepoch) << timestampLeftShift) | (datacenterId << datacenterIdShift)
| (workerId << workerIdShift) | sequence;
return nextId;
}
private long tilNextMillis(final long lastTimestamp) {
long timestamp = this.timeGen();
while (timestamp <= lastTimestamp) {
timestamp = this.timeGen();
}
return timestamp;
}
private long timeGen() {
return System.currentTimeMillis();
}
protected static long getMaxWorkerId(long datacenterId, long maxWorkerId) {
StringBuffer mpid = new StringBuffer();
mpid.append(datacenterId);
String name = ManagementFactory.getRuntimeMXBean().getName();
if (!name.isEmpty()) {
mpid.append(name.split("@")[0]);
}
return (mpid.toString().hashCode() & 0xffff) % (maxWorkerId + 1);
}
protected static long getDatacenterId(long maxDatacenterId) {
long id = 0L;
try {
InetAddress ip = InetAddress.getLocalHost();
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
if (network == null) {
id = 1L;
} else {
byte[] mac = network.getHardwareAddress();
id = ((0x000000FF & (long) mac[mac.length - 1])
| (0x0000FF00 & (((long) mac[mac.length - 2]) << 8))) >> 6;
id = id % (maxDatacenterId + 1);
}
} catch (Exception e) {
System.out.println(" getDatacenterId: " + e.getMessage());
}
return id;
}
public static void main(String[] args) {
IdWorker w = new IdWorker();
System.out.println(w.datacenterId);
System.out.println(w.workerId);
}
}
4、Leaf 美团开源的分布式ID生成器。这个名字来源于莱布尼兹的一句话:世界上没有两片相同的树叶。
|