import lombok.extern.slf4j.Slf4j;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketExcept服务器托管网ion;
import java.net.UnknownHostException;
import java.util.concurrent.ThreadLocalRandom;
/**
* 分布式高效有序ID生成
*
* 优化开源项目:https://gitee.com/yu120/sequence
*/
@Slf4j
public final class IdGenerator {
/**
* 时间起始标记点,作为基准,一般取系统的最近时间(一旦确定不能变动)
*/
private static final long twepoch = 1288834974657L;
/**
* 机器标识位数
*/
private final long workerIdBits = 5L;
private final long datacenterIdBits = 5L;
private final long maxWorkerId = ~(-1L maxWorkerId || workerId maxDatacenterId || datacenterId 0) {
// 获取jvm进程id
mpid.append(name.split("@")[0]);
}
/*
* MAC + PID 的 hashcode 获取16个低位
*/
return (mpid.toString().服务器托管网hashCode() & 0xffff) % (maxWorkerId + 1);
}
/**
* 数据标识id部分
*/
private long getDatacenterId(long maxDatacenterId) {
long id = 0L;
try {
if (null == this.inetAddress) {
this.inetAddress = InetAddress.getLocalHost();
}
NetworkInterface network = NetworkInterface.getByInetAddress(this.inetAddress);
if (null == network) {
id = 1L;
} else {
byte[] mac = network.getHardwareAddress();
if (null != mac) {
id = ((0x000000FF & (long) mac[mac.length - 2]) | (0x0000FF00 & (((long) mac[mac.length - 1]) > 6;
id = id % (maxDatacenterId + 1);
}
}
} catch (SocketException | UnknownHostException e) {
log.warn(" getDatacenterId: ", e);
}
return id;
}
/**
* 获取下一个 ID
*
* @return 下一个 ID
*/
public synchronized long nextId() {
long timestamp = timeGen();
//闰秒
if (timestamp > 22) + twepoch;
}
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
相关推荐: .NET周刊【7月第2期 2023-07-09】
由于这周比较忙,只给出了标题和链接,没有具体的简介。 另外根据粉丝朋友的反馈,”.NET周报” 更名为 “.NET周刊”,希望大家喜欢 : ) 国内文章 🎉Avalonia 11.0.0 正式版发布 https://www.cnblogs.com/hejial…