获取服务器IP地址BUG修改(使用容器网络时可以获取IP,改成HOST网络模式获取不到)

修改了这个BUG,现在都可以获取到。
pull/19/head
shanhy 5 years ago
parent c596229d88
commit 3d9252115b

@ -1,28 +1,29 @@
package com.xxl.job.admin.core.id;
import com.xxl.job.admin.core.id.service.MachineService;
import com.xxl.job.admin.core.model.XxlJobMachine;
import com.xxl.job.admin.core.util.MachineUtils;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
import com.xxl.job.admin.core.id.service.MachineService;
import com.xxl.job.admin.core.model.XxlJobMachine;
import com.xxl.job.admin.core.util.MachineUtils;
@Component
@EnableScheduling
public class HeartBeat {
@Autowired
private MachineService machineService;
@Autowired
private MachineService machineService;
@Scheduled(fixedDelay = 10000)
public void checkMachineSurvive(){
String machineIp = MachineUtils.get();
XxlJobMachine xxlJobMachine = machineService.selectByMachineIp(machineIp);
if(xxlJobMachine != null){
machineService.update(machineIp,new Date());
}
}
@Scheduled(fixedDelay = 10000)
public void checkMachineSurvive() {
String machineIp = MachineUtils.getIP();
XxlJobMachine xxlJobMachine = machineService.selectByMachineIp(machineIp);
if (xxlJobMachine != null) {
machineService.update(machineIp, new Date());
}
}
}

@ -43,15 +43,15 @@ public class MachineServiceImpl implements MachineService {
@Override
public Integer getInitMachineId() {
XxlJobMachine xxlJobMachine = selectByMachineIp(MachineUtils.get());
XxlJobMachine xxlJobMachine = selectByMachineIp(MachineUtils.getIP());
Date nowDate = new Date();
int machineId = -1;
if(xxlJobMachine != null){
update(MachineUtils.get(),nowDate);
update(MachineUtils.getIP(),nowDate);
machineId = xxlJobMachine.getMachineId();
}else{
xxlJobMachine = new XxlJobMachine();
xxlJobMachine.setMachineIp(MachineUtils.get());
xxlJobMachine.setMachineIp(MachineUtils.getIP());
xxlJobMachine.setAddTime(nowDate);
xxlJobMachine.setHeartLastTime(nowDate);
Random random = new Random();

@ -1,27 +1,56 @@
package com.xxl.job.admin.core.util;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
*
* @author
*
*/
public class MachineUtils {
private static final Logger logger = LoggerFactory.getLogger(MachineUtils.class);
private static String machineIp = null;
public static String get() {
if(machineIp == null){
try {
//获取本机IP地址
machineIp = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
logger.error("get host error",e);
throw new RuntimeException("get host error");
}
}
return machineIp;
}
private static final Logger logger = LoggerFactory.getLogger(MachineUtils.class);
private static String machineIp = null;
public static String getIP() {
if (machineIp == null) {
String ipv4 = getInet4Address();
logger.info("ipv4={}", ipv4);
machineIp = ipv4;
}
return machineIp;
}
/**
* Ipv4
*/
public static String getInet4Address() {
Enumeration<NetworkInterface> nis;
String ip = null;
try {
nis = NetworkInterface.getNetworkInterfaces();
for (; nis.hasMoreElements();) {
NetworkInterface ni = nis.nextElement();
Enumeration<InetAddress> ias = ni.getInetAddresses();
for (; ias.hasMoreElements();) {
InetAddress ia = ias.nextElement();
// ia instanceof Inet6Address && !ia.equals("")
if (ia instanceof Inet4Address && !ia.getHostAddress().equals("127.0.0.1")) {
ip = ia.getHostAddress();
}
}
}
} catch (Exception e) {
logger.error("getServerIpAddress执行出错" + e.getMessage() + "," + e.getCause());
}
return ip;
}
}

Loading…
Cancel
Save