Commit 1e2ef8ca authored by tangwei's avatar tangwei

增加雪花id

parent 1c70dada
...@@ -12,6 +12,13 @@ ...@@ -12,6 +12,13 @@
<artifactId>amos-boot-module-patrol-api</artifactId> <artifactId>amos-boot-module-patrol-api</artifactId>
<dependencies> <dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.10</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
......
package com.yeejoin.amos.patrol.core.common.request;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.IdUtil;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
import javax.annotation.PostConstruct;
import java.io.Serializable;
/**
* @description:
* @author: tw
* @createDate: 2023/4/10
*/
public class MyIdGeneratorConfig implements IdentifierGenerator {
/**
* 终端ID
*/
public static long WORKER_ID = 1;
/**
* 数据中心id
*/
public static long DATACENTER_ID = 1;
private Snowflake snowflake = IdUtil.createSnowflake(WORKER_ID, DATACENTER_ID);
@PostConstruct
public void init() {
WORKER_ID = NetUtil.ipv4ToLong(NetUtil.getLocalhostStr());
}
public synchronized long snowflakeId() {
return snowflake.nextId();
}
public synchronized long snowflakeId(long workerId, long datacenterId) {
Snowflake snowflake = IdUtil.createSnowflake(workerId, datacenterId);
return snowflake.nextId();
}
@Override
public Serializable generate(SharedSessionContractImplementor session, Object object)
throws HibernateException {
return snowflakeId(WORKER_ID, DATACENTER_ID);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment