feat(sj_1.1.0-beta2): 优化获取ip工具
This commit is contained in:
parent
ebbcbbd990
commit
ac4fbf60f6
2
pom.xml
2
pom.xml
@ -21,7 +21,7 @@
|
||||
<java.version>17</java.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<revision>1.1.0-beta1</revision>
|
||||
<revision>1.1.0-beta2</revision>
|
||||
<dingding-talk.version>1.0.0</dingding-talk.version>
|
||||
<netty-all.version>4.1.94.Final</netty-all.version>
|
||||
<hutool-all.version>5.8.25</hutool-all.version>
|
||||
|
@ -1,10 +1,15 @@
|
||||
package com.aizuda.snailjob.common.core.util;
|
||||
|
||||
import com.aizuda.snailjob.common.core.exception.SnailJobCommonException;
|
||||
import com.aizuda.snailjob.common.log.SnailJobLog;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.UnknownHostException;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@ -18,9 +23,12 @@ import java.util.regex.Pattern;
|
||||
*/
|
||||
public class NetUtil {
|
||||
|
||||
private static final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$");
|
||||
private static final String ANY_HOST_VALUE = "0.0.0.0";
|
||||
private final static String LOCAL_HOST = "127.0.0.1";
|
||||
private final static String URL_IPV4 = "http://{0}:{1}/{2}";
|
||||
private final static String URL_IPV6 = "http://[{0}]:{1}/{2}";
|
||||
private static volatile InetAddress LOCAL_ADDRESS = null;
|
||||
|
||||
/**
|
||||
* 获取URL
|
||||
@ -46,13 +54,135 @@ public class NetUtil {
|
||||
* @return 本机网卡IP地址
|
||||
*/
|
||||
public static String getLocalIpStr() {
|
||||
|
||||
try {
|
||||
return InetAddress.getLocalHost().getHostAddress();
|
||||
} catch (UnknownHostException e) {
|
||||
throw new SnailJobCommonException("未获取HostAddress");
|
||||
return getLocalAddress().getHostAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取InetAddress借鉴 dubbo
|
||||
* see: org.apache.dubbo.common.utils.NetUtils#getLocalAddress
|
||||
*
|
||||
* @return InetAddress
|
||||
*/
|
||||
public static InetAddress getLocalAddress() {
|
||||
if (LOCAL_ADDRESS != null) {
|
||||
return LOCAL_ADDRESS;
|
||||
}
|
||||
InetAddress localAddress = getLocalAddress0();
|
||||
LOCAL_ADDRESS = localAddress;
|
||||
return localAddress;
|
||||
}
|
||||
|
||||
private static InetAddress getLocalAddress0() {
|
||||
InetAddress localAddress = null;
|
||||
try {
|
||||
localAddress = InetAddress.getLocalHost();
|
||||
Optional<InetAddress> addressOp = toValidAddress(localAddress);
|
||||
if (addressOp.isPresent()) {
|
||||
return addressOp.get();
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
SnailJobLog.LOCAL.warn("get local address error", e);
|
||||
}
|
||||
|
||||
try {
|
||||
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
|
||||
while (interfaces.hasMoreElements()) {
|
||||
try {
|
||||
NetworkInterface network = interfaces.nextElement();
|
||||
if (network.isLoopback() || network.isVirtual() || !network.isUp()) {
|
||||
continue;
|
||||
}
|
||||
Enumeration<InetAddress> addresses = network.getInetAddresses();
|
||||
while (addresses.hasMoreElements()) {
|
||||
try {
|
||||
Optional<InetAddress> addressOp = toValidAddress(addresses.nextElement());
|
||||
if (addressOp.isPresent()) {
|
||||
try {
|
||||
if (addressOp.get().isReachable(100)) {
|
||||
return addressOp.get();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
SnailJobLog.LOCAL.warn("get local address error", e);
|
||||
}
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
SnailJobLog.LOCAL.warn("get local address error", e);
|
||||
}
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
SnailJobLog.LOCAL.warn("get local address error", e);
|
||||
}
|
||||
|
||||
return localAddress;
|
||||
}
|
||||
|
||||
private static Optional<InetAddress> toValidAddress(InetAddress address) {
|
||||
if (address instanceof Inet6Address) {
|
||||
Inet6Address v6Address = (Inet6Address) address;
|
||||
if (isPreferIPV6Address()) {
|
||||
return Optional.ofNullable(normalizeV6Address(v6Address));
|
||||
}
|
||||
}
|
||||
if (isValidV4Address(address)) {
|
||||
return Optional.of(address);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
static boolean isValidV4Address(InetAddress address) {
|
||||
if (address == null || address.isLoopbackAddress()) {
|
||||
return false;
|
||||
}
|
||||
String name = address.getHostAddress();
|
||||
return (name != null
|
||||
&& IP_PATTERN.matcher(name).matches()
|
||||
&& !ANY_HOST_VALUE.equals(name)
|
||||
&& !LOCAL_HOST.equals(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* normalize the ipv6 Address, convert scope name to scope id.
|
||||
* e.g.
|
||||
* convert
|
||||
* fe80:0:0:0:894:aeec:f37d:23e1%en0
|
||||
* to
|
||||
* fe80:0:0:0:894:aeec:f37d:23e1%5
|
||||
* <p>
|
||||
* The %5 after ipv6 address is called scope id.
|
||||
* see java doc of {@link Inet6Address} for more details.
|
||||
*
|
||||
* @param address the input address
|
||||
* @return the normalized address, with scope id converted to int
|
||||
*/
|
||||
static InetAddress normalizeV6Address(Inet6Address address) {
|
||||
String addr = address.getHostAddress();
|
||||
int i = addr.lastIndexOf('%');
|
||||
if (i > 0) {
|
||||
try {
|
||||
return InetAddress.getByName(addr.substring(0, i) + '%' + address.getScopeId());
|
||||
} catch (UnknownHostException e) {
|
||||
// ignore
|
||||
SnailJobLog.LOCAL.debug("Unknown IPV6 address: ", e);
|
||||
}
|
||||
}
|
||||
return address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an ipv6 address
|
||||
*
|
||||
* @return true if it is reachable
|
||||
*/
|
||||
static boolean isPreferIPV6Address() {
|
||||
boolean preferIpv6 = Boolean.getBoolean("java.net.preferIPv6Addresses");
|
||||
if (!preferIpv6) {
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -0,0 +1,27 @@
|
||||
package com.aizuda.snailjob.server.job.task.support.callback;
|
||||
|
||||
import com.aizuda.snailjob.common.core.enums.JobTaskTypeEnum;
|
||||
import com.aizuda.snailjob.template.datasource.persistence.mapper.JobTaskMapper;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author opensnail
|
||||
* @date 2024-06-24 22:51:20
|
||||
* @since sj_1.1.0
|
||||
*/
|
||||
@Component
|
||||
public class MapClientCallbackHandler extends MapReduceClientCallbackHandler {
|
||||
public MapClientCallbackHandler(JobTaskMapper jobTaskMapper) {
|
||||
super(jobTaskMapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JobTaskTypeEnum getTaskInstanceType() {
|
||||
return JobTaskTypeEnum.MAP;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void callback(ClientCallbackContext context) {
|
||||
super.callback(context);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user