Skip to content

Commit

Permalink
增加雪花ID默认初始化日志打印.
Browse files Browse the repository at this point in the history
  • Loading branch information
nieqiurong committed Apr 6, 2024
1 parent 1902e10 commit 2ce0240
Showing 1 changed file with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
* 分布式高效有序 ID 生产黑科技(sequence)
Expand All @@ -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);
/**
* 时间起始标记点,作为基准,一般取系统的最近时间(一旦确定不能变动)
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 2ce0240

Please sign in to comment.