fix: NetworkInterfaceChecker can't worker

This commit is contained in:
tjq 2024-08-11 18:22:48 +08:00
parent fea1974014
commit a35573544c
5 changed files with 47 additions and 31 deletions

View File

@ -21,6 +21,7 @@ public class HttpUtils {
client = new OkHttpClient.Builder() client = new OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.SECONDS) .connectTimeout(1, TimeUnit.SECONDS)
.readTimeout(5, TimeUnit.SECONDS) .readTimeout(5, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.build(); .build();
} }

View File

@ -217,9 +217,10 @@ public class NetUtils {
if (networkInterfaceChecker == null) { if (networkInterfaceChecker == null) {
return false; return false;
} }
log.info("[Net] try to choose NetworkInterface by NetworkInterfaceChecker, current NetworkInterface: {}", networkInterface);
try { try {
return networkInterfaceChecker.ok(networkInterface, getFirstReachableInetAddress(networkInterface)); boolean ok = networkInterfaceChecker.ok(networkInterface, getFirstReachableInetAddress(networkInterface));
log.info("[Net] try to choose NetworkInterface by NetworkInterfaceChecker, current NetworkInterface[{}], ok: {}", networkInterface, ok);
return ok;
} catch (Exception e) { } catch (Exception e) {
log.warn("[Net] isPassedCheckerNetworkInterface failed, current networkInterface: {}", networkInterface, e); log.warn("[Net] isPassedCheckerNetworkInterface failed, current networkInterface: {}", networkInterface, e);
} }

View File

@ -35,7 +35,13 @@ public class PingPongSocketServer implements PingPongServer {
} }
// 接收连接如果没有连接accept() 方法会阻塞 // 接收连接如果没有连接accept() 方法会阻塞
try (Socket socket = serverSocket.accept();OutputStream outputStream = socket.getOutputStream();) { try (Socket socket = serverSocket.accept();OutputStream outputStream = socket.getOutputStream();) {
socket.setSoTimeout(2000);
socket.setKeepAlive(false);
outputStream.write(PingPongUtils.PONG.getBytes(StandardCharsets.UTF_8)); outputStream.write(PingPongUtils.PONG.getBytes(StandardCharsets.UTF_8));
// BufferedReader.readLine() 会等待直到遇到换行符\n或回车符\r\n才会返回一行内容如果服务器发送的数据没有这些换行符readLine() 会一直阻塞直到超时
outputStream.write(System.lineSeparator().getBytes(StandardCharsets.UTF_8));
outputStream.flush(); outputStream.flush();
} catch (Exception e) { } catch (Exception e) {
if (!terminated) { if (!terminated) {

View File

@ -30,6 +30,9 @@ public class PingPongUtils {
try (Socket s = new Socket(targetIp, targetPort);InputStream is = s.getInputStream();OutputStream os = s.getOutputStream();BufferedReader br = new BufferedReader(new InputStreamReader(is))) { try (Socket s = new Socket(targetIp, targetPort);InputStream is = s.getInputStream();OutputStream os = s.getOutputStream();BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
s.setSoTimeout(2000);
s.setKeepAlive(false);
// 发送 PING 请求 // 发送 PING 请求
os.write(PING.getBytes(StandardCharsets.UTF_8)); os.write(PING.getBytes(StandardCharsets.UTF_8));
os.flush(); os.flush();

View File

@ -35,13 +35,11 @@ public class WorkerNetUtils {
pingPongServer = new PingPongSocketServer(); pingPongServer = new PingPongSocketServer();
pingPongServer.initialize(port); pingPongServer.initialize(port);
log.info("[WorkerNetUtils] initialize PingPongSocketServer successfully~"); log.info("[WorkerNetUtils] initialize PingPongSocketServer successfully~");
} catch (Exception e) {
log.warn("[WorkerNetUtils] PingPongSocketServer failed to start, which may result in an incorrectly bound IP, please pay attention to the initialize log.", e);
}
String localHostWithNetworkInterfaceChecker = NetUtils.getLocalHostWithNetworkInterfaceChecker(((networkInterface, inetAddress) -> { return NetUtils.getLocalHostWithNetworkInterfaceChecker(((networkInterface, inetAddress) -> {
if (inetAddress == null) { if (inetAddress == null) {
log.info("[WorkerNetUtils] [networkInterface:{}] skip due to inetAddress is null!", networkInterface);
return false; return false;
} }
@ -50,10 +48,13 @@ public class WorkerNetUtils {
String url = String.format(SERVER_CONNECTIVITY_CHECK_URL_PATTERN, address, workerIp, port); String url = String.format(SERVER_CONNECTIVITY_CHECK_URL_PATTERN, address, workerIp, port);
try { try {
String resp = HttpUtils.get(url); String resp = HttpUtils.get(url);
log.info("[WorkerNetUtils] check connectivity by url[{}], response: {}", url, resp); log.info("[WorkerNetUtils] [networkInterface:{},inetAddress:{}] check connectivity by url[{}], response: {}", networkInterface, inetAddress, url, resp);
if (StringUtils.isNotEmpty(resp)) { if (StringUtils.isNotEmpty(resp)) {
ResultDTO<?> resultDTO = JsonUtils.parseObject(resp, ResultDTO.class); ResultDTO<?> resultDTO = JsonUtils.parseObject(resp, ResultDTO.class);
return Boolean.TRUE.toString().equalsIgnoreCase(String.valueOf(resultDTO.getData())); boolean ret = Boolean.TRUE.toString().equalsIgnoreCase(String.valueOf(resultDTO.getData()));
if (ret) {
return true;
}
} }
} catch (Exception ignore) { } catch (Exception ignore) {
} }
@ -61,6 +62,9 @@ public class WorkerNetUtils {
return false; return false;
})); }));
} catch (Exception e) {
log.warn("[WorkerNetUtils] PingPongSocketServer failed to start, which may result in an incorrectly bound IP, please pay attention to the initialize log.", e);
} finally {
if (pingPongServer != null) { if (pingPongServer != null) {
try { try {
pingPongServer.close(); pingPongServer.close();
@ -69,8 +73,9 @@ public class WorkerNetUtils {
log.warn("[WorkerNetUtils] close PingPongSocketServer failed!", e); log.warn("[WorkerNetUtils] close PingPongSocketServer failed!", e);
} }
} }
}
return localHostWithNetworkInterfaceChecker; return NetUtils.getLocalHostWithNetworkInterfaceChecker(null);
} }
} }