springboot引入雪花算法依赖,以及使用

1.pom.xml引入:

<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.20</version>
</dependency>

2.工具类:

package com.example.anrora_bg.config;

import cn.hutool.core.lang.Singleton;
/**
* 雪花算法工具类
*/
public class SnowFlakeUtil {
private static final long START_STMP = 1420041600000L;
private static final long SEQUENCE_BIT = 9L;
private static final long MACHINE_BIT = 2L;
private static final long DATACENTER_BIT = 2L;
private static final long MAX_SEQUENCE = 511L;
private static final long MAX_MACHINE_NUM = 3L;
private static final long MAX_DATACENTER_NUM = 3L;
private static final long MACHINE_LEFT = 9L;
private static final long DATACENTER_LEFT = 11L;
private static final long TIMESTMP_LEFT = 13L;
private long datacenterId;
private long machineId;
private long sequence = 0L;
private long lastStmp = -1L;

public SnowFlakeUtil(long datacenterId, long machineId) {
if (datacenterId <= 3L && datacenterId >= 0L) {
if (machineId <= 3L && machineId >= 0L) {
this.datacenterId = datacenterId;
this.machineId = machineId;
} else {
throw new IllegalArgumentException("machineId can't be greater than MAX_MACHINE_NUM or less than 0");
}
} else {
throw new IllegalArgumentException("datacenterId can't be greater than MAX_DATACENTER_NUM or less than 0");
}
}

public synchronized long nextId() {
long currStmp = this.getNewstmp();
if (currStmp < this.lastStmp) {
throw new RuntimeException("Clock moved backwards. Refusing to generate id");
} else {
if (currStmp == this.lastStmp) {
this.sequence = this.sequence + 1L & 511L;
if (this.sequence == 0L) {
currStmp = this.getNextMill();
}
} else {
this.sequence = 0L;
}

this.lastStmp = currStmp;
return currStmp - 1420041600000L << 13 | this.datacenterId << 11 | this.machineId << 9 | this.sequence;
}
}

private long getNextMill() {
long mill;
for(mill = this.getNewstmp(); mill <= this.lastStmp; mill = this.getNewstmp()) {
}

return mill;
}

private long getNewstmp() {
return System.currentTimeMillis();
}

public static Long getDefaultSnowFlakeId() {
return ((SnowFlakeUtil)Singleton.get(SnowFlakeUtil.class, new Object[]{1L, 1L})).nextId();
}

public static void main(String[] args) {
for(int i = 0; i < 10; ++i) {
System.out.println(getDefaultSnowFlakeId());
System.out.println(getDefaultSnowFlakeId().toString().length());
}

}
}

3:测试

package com.example.anrora_bg;

import com.example.anrora_bg.config.SnowFlakeUtil;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class AnroraBgApplication {

public static void main(String[] args) {

SpringApplication.run(AnroraBgApplication.class, args);
Long id = SnowFlakeUtil.getDefaultSnowFlakeId();
System.out.println(id);
}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot使用雪花算法生成唯一ID,你可以按照以下步骤进行: 1. 添加依赖:首先,在你的Spring Boot项目的pom.xml文件中添加snowflake算法依赖配置。可以使用snowflake-id-generator库,如前面所示的依赖配置。 2. 创建雪花算法ID生成器:在你的项目中创建一个雪花算法ID生成器的类,可以命名为SnowflakeIdGenerator或者其他你喜欢的名称。你可以使用snowflake-id-generator库提供的SnowflakeIdGenerator类,也可以自己实现一个ID生成器。 3. 配置参数:在生成器类中,你需要配置雪花算法所需的参数,包括工作机器ID。可以通过配置文件或者代码来设置这些参数。 4. 生成唯一ID:在需要生成唯一ID的地方,调用雪花算法ID生成器的方法来生成ID。例如,可以通过调用generateId()方法来获取一个唯一ID。 下面是一个简单的示例代码: ```java @Component public class SnowflakeIdGenerator { private final SnowflakeIdWorker idWorker; public SnowflakeIdGenerator(@Value("${snowflake.workerId}") long workerId) { this.idWorker = new SnowflakeIdWorker(workerId); } public long generateId() { return idWorker.nextId(); } } ``` 在上面的示例中,我们使用SnowflakeIdWorker类来实现雪花算法的ID生成器。通过构造函数传入工作机器ID,然后调用nextId()方法来生成唯一ID。 注意:你还需要在配置文件中配置snowflake.workerId参数,用于设置工作机器ID。 这样,你就可以在Spring Boot项目中使用雪花算法生成唯一ID了。通过注入SnowflakeIdGenerator类,调用generateId()方法即可获取唯一ID。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值