账单-雪花主键
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package com.accompany.core.util;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
@Slf4j
|
||||
public class WorkerIdUtil {
|
||||
|
||||
public static long generateWorkerId(int port) {
|
||||
try {
|
||||
InetAddress address = InetAddress.getLocalHost();
|
||||
String ip = address.getHostAddress();
|
||||
|
||||
// 将 IP 转换为整数
|
||||
long ipHash = ip.chars().mapToLong(c -> c).sum();
|
||||
long workerId = (ipHash + port) % 32; // 保证在 0~1023 范围内
|
||||
|
||||
log.info("Generated workerId: {} from IP: {} Port: {}", workerId, ip ,port);
|
||||
return workerId;
|
||||
} catch (UnknownHostException e) {
|
||||
throw new RuntimeException("Failed to get local host info", e);
|
||||
}
|
||||
}
|
||||
}
|
@@ -16,7 +16,7 @@ import java.util.Date;
|
||||
@TableName("bill_record")
|
||||
public class BillRecord{
|
||||
|
||||
@TableId(value = "bill_id", type = IdType.AUTO)
|
||||
@TableId(value = "bill_id", type = IdType.ASSIGN_ID)
|
||||
private Long billId;
|
||||
|
||||
private Long uid;
|
||||
|
@@ -1,21 +1,27 @@
|
||||
package com.accompany.sharding.config;
|
||||
|
||||
import com.accompany.common.utils.ResourceUtil;
|
||||
import com.accompany.core.util.WorkerIdUtil;
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Sequence;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
|
||||
import com.github.pagehelper.PageInterceptor;
|
||||
import org.apache.ibatis.plugin.Interceptor;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.context.annotation.*;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.util.Objects;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
@@ -37,6 +43,9 @@ public class MybatisPlusConfig {
|
||||
private String mapperLocations = "classpath*:*/mappers/*.xml,classpath*:mapper/*.xml,accompany/sqlmappers/*.xml,accompany/oauth/*.xml,mapper/world/*Mapper.xml," +
|
||||
"mapper/community/*Mapper.xml,sqlmappers/*.xml,oauth/*.xml,classpath*:/sharding/sqlmappers/*.xml";
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
/**
|
||||
* 添加分页插件
|
||||
*/
|
||||
@@ -82,4 +91,26 @@ public class MybatisPlusConfig {
|
||||
return new DataSourceTransactionManager(shardingDataSource);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public IdentifierGenerator idGenerator() {
|
||||
int port = Integer.parseInt(Objects.requireNonNull(environment.getProperty("server.port")));
|
||||
long workerId = WorkerIdUtil.generateWorkerId(port);
|
||||
return new CustomIdGenerator(workerId, 1); // 设置你的 workerId 和 dataCenterId
|
||||
}
|
||||
|
||||
public static class CustomIdGenerator implements IdentifierGenerator {
|
||||
|
||||
private final Sequence sequence;
|
||||
|
||||
public CustomIdGenerator(long workerId, long dataCenterId) {
|
||||
this.sequence = new Sequence(workerId, dataCenterId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long nextId(Object entity) {
|
||||
// 返回一个雪花算法生成的 Long 类型 ID
|
||||
return sequence.nextId();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user