news 2026/5/23 14:36:24

一次由 DNS 反解析引发的 SpringBoot 启动卡顿问题

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
一次由 DNS 反解析引发的 SpringBoot 启动卡顿问题

TL;DR

使用 114 DNS 时,反解析内网 IP 无响应,导致 SpringBoot 启动时 Liquibase 初始化阻塞约 30 秒

InetAddress.getLocalHost() 获取到的可能是127.0.0.1,而非实际的内网 IP(如 192.168.x.x)

现象

SpringBoot启动时(HikariPool 初始化后)卡顿 30s

2025-11-09 15:14:33 INFO [main] com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...

2025-11-09 15:14:33 INFO [main] com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Added connection com.p6spy.engine.wrapper.ConnectionWrapper@7f6137fb

2025-11-09 15:14:33 INFO [main] com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.

排查

通过 jstack 分析线程栈,定位到 liquibase.util.NetUtil.getLocalHostName() 阻塞

# jps

jstack 66713

下方线程栈显示阻塞点位于 Inet6AddressImpl.getHostByAddr()

"main" #1 prio=5 os_prio=31 cpu=3549.34ms elapsed=14.86s tid=0x000000010a808200 nid=0xd03 runnable [0x000000016fa00000]

java.lang.Thread.State: RUNNABLE

at java.net.Inet6AddressImpl.getHostByAddr(java.base@17.0.14/Native Method)

at java.net.InetAddress$PlatformNameService.getHostByAddr(java.base@17.0.14/InetAddress.java:940)

at java.net.InetAddress.getHostFromNameService(java.base@17.0.14/InetAddress.java:662)

at java.net.InetAddress.getHostName(java.base@17.0.14/InetAddress.java:605)

at java.net.InetAddress.getHostName(java.base@17.0.14/InetAddress.java:577)

at liquibase.util.NetUtil.getLocalHostName(NetUtil.java:79)

at liquibase.sqlgenerator.core.LockDatabaseChangeLogGenerator.<clinit>(LockDatabaseChangeLogGenerator.java:30)

对应源码:

public class LockDatabaseChangeLogGenerator extends AbstractSqlGenerator<LockDatabaseChangeLogStatement> {

@Override

public ValidationErrors validate(LockDatabaseChangeLogStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {

return new ValidationErrors();

}

protected static final String hostname;

protected static final String hostaddress;

protected static final String hostDescription = (System.getProperty("liquibase.hostDescription") == null) ? "" :

("#" + System.getProperty("liquibase.hostDescription"));

static {

try {

// NetUtil.getLocalHostName() 导致阻塞

hostname = NetUtil.getLocalHostName();

hostaddress = NetUtil.getLocalHostAddress();

} catch (Exception e) {

throw new UnexpectedLiquibaseException(e);

}

}

// ...

}

分析

NetUtil.getLocalHostName() 获取机器 IP:遍历网卡,调用 InetAddress.getHostName() 对内网 IP (192.168.10.2)做反解析,114 DNS(114.114.114.114)无响应,则导致阻塞 30s

package liquibase.util;

// ...

public class NetUtil {

// ...

/**

* @return Machine's host name. This method can be better to call than getting it off {@link #getLocalHost()} because sometimes the external address returned by that function does not have a useful hostname attached to it.

* This function will make sure a good value is returned.

*/

public static String getLocalHostName() {

if (hostName == null ) {

try {

// 遍历所有网络接口,找出 已启用 的 非点对点 网络接口,然后打印这些接口上每个非本地地址对应的主机名(hostname)

InetAddress localHost = getLocalHost();

if(localHost != null) {

// 使用指定的 DNS 反解析获取的 IP

hostName = localHost.getHostName();

if (hostName.equals(localHost.getHostAddress())) {

//sometimes the external IP interface doesn't have a hostname associated with it but localhost always does

InetAddress lHost = InetAddress.getLocalHost();

if (lHost != null) {

hostName = lHost.getHostName();

}

}

}

else {

hostName = UNKNOWN_HOST_NAME;

}

} catch (Exception e) {

Scope.getCurrentScope().getLog(NetUtil.class).fine("Error getting hostname", e);

if (hostName == null) {

hostName = UNKNOWN_HOST_NAME;

}

}

}

return hostName;

}

}

大多数公共 DNS(如阿里、Google)在无法解析内网地址时返回 NXDOMAIN,而 114 DNS 无响应,导致 Java 原生反解析方法阻塞

# 耗时:0.228s

nslookup 192.168.10.2 223.5.5.5

Server: 223.5.5.5

Address: 223.5.5.5#53

** server can't find 2.10.168.192.in-addr.arpa: NXDOMAIN

# 耗时:15.137s

nslookup 192.168.10.2 114.114.114.114

;; connection timed out; no servers could be reached

遍历网卡的原因

获取机器 IP 常用的方法:InetAddress.getLocalHost() 获取到的可能是127.0.0.1(与 JDK 实现有关),是一个本地回环地址(loopback address)。而非对外通信用的实际的网络 IP(例如 192.168.x.x 或 10.x.x.x)

为了获取 实际的网络 IP,一般使用类似上面 NetUtil.getLocalHostName() 的方式,遍历网卡获取实际 IP

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/23 2:22:57

dora-rs低延迟数据流框架:实时AI处理的终极解决方案

dora-rs低延迟数据流框架&#xff1a;实时AI处理的终极解决方案 【免费下载链接】dora dora goal is to be a low latency, composable, and distributed data flow. 项目地址: https://gitcode.com/GitHub_Trending/do/dora 痛点分析&#xff1a;现代AI应用的技术瓶颈 …

作者头像 李华
网站建设 2026/5/22 14:04:23

PocketHub移动开发革命:随时随地掌控GitHub项目的高效方案

你是否曾经在通勤路上突然想起要查看一个重要的Pull Request&#xff0c;却因为电脑不在身边而束手无策&#xff1f;或者在外出时收到紧急issue通知&#xff0c;却无法及时响应&#xff1f;这些困扰开发者的移动协作难题&#xff0c;现在有了完美的解决方案。&#x1f680; 【免…

作者头像 李华
网站建设 2026/5/21 6:09:46

常用的贝叶斯代理模型

主要包含两个部分一个代理模型&#xff08;surrogate model&#xff09;&#xff0c;用于对目标函数进行建模。代理模型通常有确定的公式或者能计算梯度&#xff0c;又或者有已知的凹凸性、线性等特性&#xff0c;总之就是更容易用于优化。更泛化地讲&#xff0c;其实它就是一个…

作者头像 李华
网站建设 2026/5/23 2:10:10

71、技术综合指南:涵盖系统、网络、编程与多媒体

技术综合指南:涵盖系统、网络、编程与多媒体 1. 系统基础 Linux 与 Ubuntu :Linux 具有成本低、跨平台开发等优势,可用于桌面和服务器平台。Ubuntu 以 Debian 为基础,有商业支持和丰富的文档资源,包括网络搜索、网站、IRC 及邮件列表等。Ubuntu 有多种变体,如 Kubuntu…

作者头像 李华
网站建设 2026/5/16 20:51:49

Redisson依赖冲突:如何巧妙解决Spring Boot版本不匹配?

Redisson依赖冲突&#xff1a;如何巧妙解决Spring Boot版本不匹配&#xff1f; 【免费下载链接】redisson Redisson - Easy Redis Java client with features of In-Memory Data Grid. Sync/Async/RxJava/Reactive API. Over 50 Redis based Java objects and services: Set, M…

作者头像 李华
网站建设 2026/5/21 21:59:30

5个高效调试技巧:如何实现API开发效率倍增

5个高效调试技巧&#xff1a;如何实现API开发效率倍增 【免费下载链接】swagger-ui Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API. 项目地址: https://gitcode.com/Git…

作者头像 李华