|
@@ -15,6 +15,7 @@
|
|
*/
|
|
*/
|
|
package com.baomidou.mybatisplus.core.toolkit;
|
|
package com.baomidou.mybatisplus.core.toolkit;
|
|
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
|
|
import org.apache.ibatis.logging.Log;
|
|
import org.apache.ibatis.logging.Log;
|
|
import org.apache.ibatis.logging.LogFactory;
|
|
import org.apache.ibatis.logging.LogFactory;
|
|
|
|
|
|
@@ -22,6 +23,7 @@ import java.lang.management.ManagementFactory;
|
|
import java.net.InetAddress;
|
|
import java.net.InetAddress;
|
|
import java.net.NetworkInterface;
|
|
import java.net.NetworkInterface;
|
|
import java.net.UnknownHostException;
|
|
import java.net.UnknownHostException;
|
|
|
|
+import java.util.Optional;
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -73,11 +75,40 @@ public class Sequence {
|
|
*/
|
|
*/
|
|
private long lastTimestamp = -1L;
|
|
private long lastTimestamp = -1L;
|
|
|
|
|
|
|
|
+ private InetAddress inetAddress;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @deprecated 3.4.3
|
|
|
|
+ */
|
|
|
|
+ @Deprecated
|
|
public Sequence() {
|
|
public Sequence() {
|
|
|
|
+ this.inetAddress = getLocalHost();
|
|
|
|
+ this.datacenterId = getDatacenterId(maxDatacenterId);
|
|
|
|
+ this.workerId = getMaxWorkerId(datacenterId, maxWorkerId);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public Sequence(InetAddress inetAddress) {
|
|
|
|
+ this.inetAddress = inetAddress;
|
|
this.datacenterId = getDatacenterId(maxDatacenterId);
|
|
this.datacenterId = getDatacenterId(maxDatacenterId);
|
|
this.workerId = getMaxWorkerId(datacenterId, maxWorkerId);
|
|
this.workerId = getMaxWorkerId(datacenterId, maxWorkerId);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private InetAddress getLocalHost() {
|
|
|
|
+ try {
|
|
|
|
+ return InetAddress.getLocalHost();
|
|
|
|
+ } catch (UnknownHostException e) {
|
|
|
|
+ throw new MybatisPlusException(e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @return InetAddress
|
|
|
|
+ * @since 3.4.3
|
|
|
|
+ */
|
|
|
|
+ protected InetAddress getInetAddress() {
|
|
|
|
+ return Optional.ofNullable(this.inetAddress).orElseGet(this::getLocalHost);
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 有参构造器
|
|
* 有参构造器
|
|
*
|
|
*
|
|
@@ -112,25 +143,13 @@ public class Sequence {
|
|
return (mpid.toString().hashCode() & 0xffff) % (maxWorkerId + 1);
|
|
return (mpid.toString().hashCode() & 0xffff) % (maxWorkerId + 1);
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * 获取 InetAddress
|
|
|
|
- *
|
|
|
|
- * @return InetAddress
|
|
|
|
- * @throws UnknownHostException
|
|
|
|
- * @since 3.4.3
|
|
|
|
- */
|
|
|
|
- protected InetAddress getInetAddress() throws UnknownHostException {
|
|
|
|
- return InetAddress.getLocalHost();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 数据标识id部分
|
|
* 数据标识id部分
|
|
*/
|
|
*/
|
|
protected long getDatacenterId(long maxDatacenterId) {
|
|
protected long getDatacenterId(long maxDatacenterId) {
|
|
long id = 0L;
|
|
long id = 0L;
|
|
try {
|
|
try {
|
|
- InetAddress ip = this.getInetAddress();
|
|
|
|
- NetworkInterface network = NetworkInterface.getByInetAddress(ip);
|
|
|
|
|
|
+ NetworkInterface network = NetworkInterface.getByInetAddress(this.getInetAddress());
|
|
if (network == null) {
|
|
if (network == null) {
|
|
id = 1L;
|
|
id = 1L;
|
|
} else {
|
|
} else {
|