mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-06 16:48:24 +08:00
fix: UnknownHostException in SnowFlakeIDKeyGenerator.java
This commit is contained in:
parent
74b820b9c3
commit
526a7a80b7
@ -18,6 +18,7 @@ package com.mybatisflex.core.keygen.impl;
|
|||||||
import com.mybatisflex.core.exception.FlexExceptions;
|
import com.mybatisflex.core.exception.FlexExceptions;
|
||||||
import com.mybatisflex.core.keygen.IKeyGenerator;
|
import com.mybatisflex.core.keygen.IKeyGenerator;
|
||||||
import com.mybatisflex.core.util.StringUtil;
|
import com.mybatisflex.core.util.StringUtil;
|
||||||
|
import org.apache.ibatis.logging.LogFactory;
|
||||||
|
|
||||||
import java.lang.management.ManagementFactory;
|
import java.lang.management.ManagementFactory;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
@ -163,15 +164,13 @@ public class SnowFlakeIDKeyGenerator implements IKeyGenerator {
|
|||||||
* 根据网卡 MAC 地址计算余数作为数据中心 ID。
|
* 根据网卡 MAC 地址计算余数作为数据中心 ID。
|
||||||
*/
|
*/
|
||||||
protected long getDataCenterId(long maxDataCenterId) {
|
protected long getDataCenterId(long maxDataCenterId) {
|
||||||
long id = 0L;
|
long id = 1L;
|
||||||
try {
|
try {
|
||||||
if (address == null) {
|
if (address == null) {
|
||||||
address = InetAddress.getLocalHost();
|
address = InetAddress.getLocalHost();
|
||||||
}
|
}
|
||||||
NetworkInterface network = NetworkInterface.getByInetAddress(address);
|
NetworkInterface network = NetworkInterface.getByInetAddress(address);
|
||||||
if (null == network) {
|
if (null != network) {
|
||||||
id = 1L;
|
|
||||||
} else {
|
|
||||||
byte[] mac = network.getHardwareAddress();
|
byte[] mac = network.getHardwareAddress();
|
||||||
if (null != mac) {
|
if (null != mac) {
|
||||||
id = ((0x000000FF & (long) mac[mac.length - 2]) | (0x0000FF00 & (((long) mac[mac.length - 1]) << 8))) >> 6;
|
id = ((0x000000FF & (long) mac[mac.length - 2]) | (0x0000FF00 & (((long) mac[mac.length - 1]) << 8))) >> 6;
|
||||||
@ -179,7 +178,7 @@ public class SnowFlakeIDKeyGenerator implements IKeyGenerator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw FlexExceptions.wrap(e, "dataCenterId: %s", e.getMessage());
|
LogFactory.getLog(SnowFlakeIDKeyGenerator.class).error(e.toString(), e);
|
||||||
}
|
}
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user