账单-雪花主键-workerId兼容docker网络环境

This commit is contained in:
khalil
2025-07-04 16:46:50 +08:00
committed by khalil
parent 36a1bed313
commit 95b93c7d08
2 changed files with 32 additions and 19 deletions

View File

@@ -3,24 +3,44 @@ package com.accompany.core.util;
import lombok.extern.slf4j.Slf4j;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Arrays;
@Slf4j
public class WorkerIdUtil {
public static long generateWorkerId(int port) {
public static long generateWorkerId() {
InetAddress host = null;
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;
host = InetAddress.getByName("host.docker.internal");
} catch (UnknownHostException e) {
throw new RuntimeException("Failed to get local host info", e);
log.error("[workerId] 获取不到docker宿主机的ip");
}
if (null == host){
try {
host = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
log.error("[workerId] 获取不到localhost的ip");
throw new RuntimeException(e);
}
}
String ip = host.getHostAddress();
byte[] mac = null;
try {
mac = NetworkInterface.getByInetAddress(host).getHardwareAddress();
} catch (SocketException e) {
log.error("[workerId] 获取不到ip {} 的 mac地址", ip);
throw new RuntimeException(e);
}
// 根据 MAC 或 IP 哈希生成 workerId
long workerId = Math.abs(Arrays.hashCode(mac)) % 32;
log.info("Generated workerId: {} from IP: {} MAC: {}", workerId, ip, mac);
return workerId;
}
}

View File

@@ -11,17 +11,14 @@ 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;
/**
@@ -43,9 +40,6 @@ 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;
/**
* 添加分页插件
*/
@@ -93,8 +87,7 @@ public class MybatisPlusConfig {
@Bean
public IdentifierGenerator idGenerator() {
int port = Integer.parseInt(Objects.requireNonNull(environment.getProperty("server.port")));
long workerId = WorkerIdUtil.generateWorkerId(port);
long workerId = WorkerIdUtil.generateWorkerId();
return new CustomIdGenerator(workerId, 1); // 设置你的 workerId 和 dataCenterId
}