账单-雪花主键-workerId兼容docker网络环境
This commit is contained in:
@@ -3,24 +3,44 @@ package com.accompany.core.util;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
import java.net.NetworkInterface;
|
||||||
|
import java.net.SocketException;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class WorkerIdUtil {
|
public class WorkerIdUtil {
|
||||||
|
|
||||||
public static long generateWorkerId(int port) {
|
public static long generateWorkerId() {
|
||||||
|
InetAddress host = null;
|
||||||
try {
|
try {
|
||||||
InetAddress address = InetAddress.getLocalHost();
|
host = InetAddress.getByName("host.docker.internal");
|
||||||
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) {
|
} 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -11,17 +11,14 @@ import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
|
|||||||
import com.github.pagehelper.PageInterceptor;
|
import com.github.pagehelper.PageInterceptor;
|
||||||
import org.apache.ibatis.plugin.Interceptor;
|
import org.apache.ibatis.plugin.Interceptor;
|
||||||
import org.mybatis.spring.annotation.MapperScan;
|
import org.mybatis.spring.annotation.MapperScan;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||||
import org.springframework.context.annotation.*;
|
import org.springframework.context.annotation.*;
|
||||||
import org.springframework.core.env.Environment;
|
|
||||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||||
import org.springframework.transaction.PlatformTransactionManager;
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.Properties;
|
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," +
|
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";
|
"mapper/community/*Mapper.xml,sqlmappers/*.xml,oauth/*.xml,classpath*:/sharding/sqlmappers/*.xml";
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private Environment environment;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加分页插件
|
* 添加分页插件
|
||||||
*/
|
*/
|
||||||
@@ -93,8 +87,7 @@ public class MybatisPlusConfig {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public IdentifierGenerator idGenerator() {
|
public IdentifierGenerator idGenerator() {
|
||||||
int port = Integer.parseInt(Objects.requireNonNull(environment.getProperty("server.port")));
|
long workerId = WorkerIdUtil.generateWorkerId();
|
||||||
long workerId = WorkerIdUtil.generateWorkerId(port);
|
|
||||||
return new CustomIdGenerator(workerId, 1); // 设置你的 workerId 和 dataCenterId
|
return new CustomIdGenerator(workerId, 1); // 设置你的 workerId 和 dataCenterId
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user