(企业 / 公司项目)SpringBoot实现雪花算法id注册功能
2023-12-13 04:06:11
以下是使用 Spring Boot 实现雪花算法的完整代码。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SnowflakeGeneratorApplication {
public static void main(String[] args) {
SpringApplication.run(SnowflakeGeneratorApplication.class, args);
}
}
import org.springframework.stereotype.Component;
@Component
public class SnowflakeGenerator {
private final long START_STMP = 1480166465631L;
// 每部分占用的位数,注意不能超过63位,即 long 类型的最大位数
private final long SEQUENCE_BIT = 12; // 序列号占用的位数
private final long MACHINE_BIT = 5; // 机器标识占用的位数
private final long DATACENTER_BIT = 5;// 数据中心占用的位数
// 每部分最大值
private final long MAX_DATACENTER_NUM = -1L ^ (-1L << DATACENTER_BIT);
private final long MAX_MACHINE_NUM = -1L ^ (-1L << MACHINE_BIT);
private final long MAX_SEQUENCE = -1L ^ (-1L << SEQUENCE_BIT);
// 每部分向左的位移
private final long MACHINE_LEFT = SEQUENCE_BIT;
private final long DATACENTER_LEFT = SEQUENCE_BIT + MACHINE_BIT;
private final long TIMESTMP_LEFT = DATACENTER_LEFT + DATACENTER_BIT;
private long datacenterId; // 数据中心
private long machineId; // 机器标识
private long sequence = 0L; // 序列号
private long lastStmp = -1L; // 上一次时间戳
public SnowflakeGenerator() {
this.datacenterId = getDatacenterId(MAX_DATACENTER_NUM);
this.machineId = getMachineId(MAX_MACHINE_NUM);
}
public synchronized long nextId() {
long currStmp = getNewstmp();
if (currStmp < lastStmp) {
throw new RuntimeException("Clock moved backwards. Refusing to generate id");
}
if (currStmp == lastStmp) {
sequence = (sequence + 1) & MAX_SEQUENCE;
if (sequence == 0L) {
currStmp = getNextMill();
}
} else {
sequence = 0L;
}
lastStmp = currStmp;
return (currStmp - START_STMP) << TIMESTMP_LEFT //
| datacenterId << DATACENTER_LEFT // 数据中心部分
| machineId << MACHINE_LEFT // 机器标识部分
| sequence; // 序列号部分
}
private long getNextMill() {
long mill = getNewstmp();
while (mill <= lastStmp) {
mill = getNewstmp();
}
return mill;
}
private long getNewstmp() {
return System.currentTimeMillis();
}
/**
* 获取 maxDatacenterId
*/
protected 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();
if (mac != null) {
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("get data center id error:" + e.getMessage());
}
return id;
}
/**
* 获取 maxWorkerId
*/
protected long getMachineId(long maxWorkerId) {
long id = 0L;
try {
InetAddress ip = InetAddress.getLocalHost();
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
if (network == null) {
id = 1L;
} else {
byte[] mac = network.getHardwareAddress();
if (mac != null) {
id = ((0x0000FF & (long) mac[mac.length - 2]) | (0x00FF00 & (((long) mac[mac.length - 1]) << 8))) >> 4;
id = id % (maxWorkerId + 1);
}
}
} catch (Exception e) {
System.out.println("get machine id error:" + e.getMessage());
}
return id;
}
}
使用:
@Autowired
private SnowflakeGenerator snowflakeGenerator;
long id = snowflakeGenerator.nextId();
文章来源:https://blog.csdn.net/qq_60870118/article/details/134882770
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!