账单-雪花主键-workerId-通过docker环境变量注入

This commit is contained in:
2025-09-08 17:39:13 +08:00
parent 15b0134c0a
commit 93a4a6bb97
2 changed files with 69 additions and 19 deletions

View File

@@ -12,26 +12,36 @@ import java.util.Enumeration;
@Slf4j
public class WorkerIdUtil {
public static long generateWorkerId() {
public static long generateDataCenterId() {
InetAddress host = null;
try {
host = InetAddress.getByName("host.docker.internal");
} catch (UnknownHostException 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 = null;
// 优先使用Docker环境变量HOST_IP
String hostIpEnv = System.getenv("HOST_IP");
if (hostIpEnv != null && !hostIpEnv.isEmpty()) {
ip = hostIpEnv;
log.info("[workerId] 使用环境变量HOST_IP: {}", ip);
} else {
// 如果没有设置HOST_IP环境变量则使用原来的逻辑获取本地IP
if (null == host){
try {
host = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
log.error("[workerId] 获取不到localhost的ip");
throw new RuntimeException(e);
}
}
ip = host.getHostAddress();
}
if (StringUtils.isBlank(ip)){
log.error("[workerId] 获取不到ip");
throw new RuntimeException("[workerId] 获取不到ip");
}
String ip = host.getHostAddress();
byte[] mac = null;
// 只有在没有使用环境变量且host不为null的情况下才尝试获取MAC地址
try {
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(host);
if (networkInterface != null) {
@@ -40,7 +50,7 @@ public class WorkerIdUtil {
} catch (SocketException e) {
log.error("[workerId] 获取不到ip {} 的 mac地址", ip);
}
// 如果通过InetAddress找不到MAC地址则尝试遍历网络接口
if (mac == null) {
try {
@@ -62,14 +72,53 @@ public class WorkerIdUtil {
if (mac == null) {
log.warn("[workerId] 无法获取MAC地址使用IP地址哈希生成workerId");
long workerId = Math.abs(ip.hashCode()) % 32;
log.info("Generated workerId: {} from IP: {}", workerId, ip);
log.info("[workerId] workerId: {} from IP: {}", workerId, ip);
return workerId;
}
// 根据 MAC 或 IP 哈希生成 workerId
long workerId = Math.abs(Arrays.hashCode(mac)) % 32;
log.info("Generated workerId: {} from IP: {} MAC: {}", workerId, ip, Arrays.toString(mac));
log.info("[workerId] dataCenterId: {} from IP: {} MAC: {}", workerId, ip, Arrays.toString(mac));
return workerId;
}
/**
* 从Docker宿主机端口生成workerId
* 优先使用SERVICE_PORT环境变量也可使用DOCKER_PORT或SERVER_PORT环境变量
* 最后使用Spring Boot启动端口作为后备方案
*
* @return 生成的workerId (0-31之间)
*/
public static long generateWorkerIdFromPort() {
// 优先使用SERVICE_PORT环境变量根据用户的docker run命令
String servicePortEnv = System.getenv("SERVICE_PORT");
if (servicePortEnv != null && !servicePortEnv.isEmpty()) {
try {
int port = Integer.parseInt(servicePortEnv);
long workerId = Math.abs(port) % 32;
log.info("[workerId] 使用环境变量SERVICE_PORT: {}, 生成workerId: {}", port, workerId);
return workerId;
} catch (NumberFormatException e) {
log.warn("[workerId] SERVICE_PORT环境变量不是有效数字: {}", servicePortEnv);
}
}
// 使用Spring Boot启动端口作为后备方案
String springBootPort = System.getProperty("server.port");
if (springBootPort != null && !springBootPort.isEmpty()) {
try {
int port = Integer.parseInt(springBootPort);
long workerId = Math.abs(port) % 32;
log.info("[workerId] 使用Spring Boot启动端口: {}, 生成workerId: {}", port, workerId);
return workerId;
} catch (NumberFormatException e) {
log.warn("[workerId] Spring Boot启动端口不是有效数字: {}", springBootPort);
}
}
// 如果都没有设置默认返回1
log.warn("[workerId] 未设置SERVICE_PORT或者Spring Boot启动端口默认使用workerId: 1");
return 1;
}
}

View File

@@ -87,8 +87,9 @@ public class MybatisPlusConfig {
@Bean
public IdentifierGenerator idGenerator() {
long workerId = WorkerIdUtil.generateWorkerId();
return new CustomIdGenerator(workerId, 1); // 设置你的 workerId 和 dataCenterId
long workerId = WorkerIdUtil.generateWorkerIdFromPort();
long dataCenterId = WorkerIdUtil.generateDataCenterId();
return new CustomIdGenerator(workerId, dataCenterId); // 设置你的 workerId 和 dataCenterId
}
public static class CustomIdGenerator implements IdentifierGenerator {