diff --git a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/Sequence.java b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/Sequence.java index 4683d3c835..fca4a65e47 100644 --- a/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/Sequence.java +++ b/mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/Sequence.java @@ -22,6 +22,7 @@ import java.net.InetAddress; import java.net.NetworkInterface; import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.TimeUnit; /** * 分布式高效有序 ID 生产黑科技(sequence) @@ -33,6 +34,13 @@ */ public class Sequence { + /** + * 自动寻找网卡时,默认启动最大时间间隔,超过这个初始化时间打印warn日志 + * + * @since 3.5.6 + */ + public static long MAX_START_INTERVAL_TIME = TimeUnit.SECONDS.toNanos(5); + private static final Log logger = LogFactory.getLog(Sequence.class); /** * 时间起始标记点,作为基准,一般取系统的最近时间(一旦确定不能变动) @@ -78,9 +86,16 @@ public class Sequence { public Sequence(InetAddress inetAddress) { this.inetAddress = inetAddress; + long start = System.nanoTime(); this.datacenterId = getDatacenterId(maxDatacenterId); this.workerId = getMaxWorkerId(datacenterId, maxWorkerId); - initLog(); + long end = System.nanoTime(); + if (end - start > Sequence.MAX_START_INTERVAL_TIME) { + // 一般这里启动慢,是未指定inetAddress时出现,请查看本机hostname,将本机hostname写入至本地系统hosts文件之中进行解析 + logger.warn("Initialization Sequence Very Slow! Get datacenterId:" + this.datacenterId + " workerId:" + this.workerId); + } else { + initLog(); + } } private void initLog() { @@ -131,10 +146,20 @@ protected long getDatacenterId(long maxDatacenterId) { long id = 0L; try { if (null == this.inetAddress) { + if (logger.isDebugEnabled()) { + logger.debug("Use localhost address "); + } this.inetAddress = InetAddress.getLocalHost(); } + if (logger.isDebugEnabled()) { + logger.debug("Get " + inetAddress + " network interface "); + } NetworkInterface network = NetworkInterface.getByInetAddress(this.inetAddress); + if (logger.isDebugEnabled()) { + logger.debug("Get network interface info: " + network); + } if (null == network) { + logger.warn("Unable to get network interface for " + inetAddress); id = 1L; } else { byte[] mac = network.getHardwareAddress();