启动日志

以下是个 Spring Boot 项目启动日志(已刨除无关部分)

1
2
3
4
5
...
10:30:10.110 INFO  [background-preinit] org.hibernate.validator.internal.util.Version --- HV000001: Hibernate Validator 5.4.1.Final
10:30:15.135 WARN  [main] org.springframework.boot.StartupInfoLogger --- InetAddress.getLocalHost().getHostName() took 5001 milliseconds to respond. Please verify your network configuration (macOS machines may need to add entries to /etc/hosts).
10:30:20.142 INFO  [main] com.example.test.Application --- Starting Application on jioby.local with PID 67279 (/XXX/target/classes started by jioby in /XXX)
...

原因分析

上述日志均有个特点,每个涉及 DNS 解析的都会花费 5 秒钟左右。

然后根据日志提示,看看 InetAddress.getLocalHost() 需要执行多久:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import java.net.InetAddress;

public class Demo {

    public static void main(String[] args) {
        System.out.printf("Start: %tT%n", System.currentTimeMillis());
        try {
            System.out.println(InetAddress.getLocalHost());
            System.out.printf("Mid: %tT%n", System.currentTimeMillis());
            System.out.println(InetAddress.getLocalHost());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        System.out.printf("End: %tT%n", System.currentTimeMillis());
    }
}

输出结果:

1
2
3
4
5
Start: 17:27:58
jioby.local/192.168.131.48
Mid: 17:28:03
jioby.local/192.168.131.48
End: 17:28:08

再看看 InetAddress.getLocalHost() 的源码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public static InetAddress getLocalHost() throws UnknownHostException {

    SecurityManager security = System.getSecurityManager();
    try {
        // 本地返回的是 hostname,比如我的就是 jioby.local
        String local = impl.getLocalHostName();

        if (security != null) {
            security.checkConnect(local, -1);
        }

        // 不与 localhost 匹配,故没法走回环地址
        if (local.equals("localhost")) {
            return impl.loopbackAddress();
        }

        InetAddress ret = null;
        synchronized (cacheLock) {
            long now = System.currentTimeMillis();
            if (cachedLocalHost != null) {
                // 缓存 local 的 IP 地址 5 秒钟
                if ((now - cacheTime) < maxCacheTime) // Less than 5s old?
                    ret = cachedLocalHost;
                else
                    cachedLocalHost = null;
            }

            // we are calling getAddressesFromNameService directly
            // to avoid getting localHost from cache
            if (ret == null) {
                InetAddress[] localAddrs;
                try {
                    // 慢在此处 DNS 解析
                    localAddrs =
                        InetAddress.getAddressesFromNameService(local, null);
                } catch (UnknownHostException uhe) {
                    // Rethrow with a more informative error message.
                    UnknownHostException uhe2 =
                        new UnknownHostException(local + ": " +
                                                 uhe.getMessage());
                    uhe2.initCause(uhe);
                    throw uhe2;
                }
                cachedLocalHost = localAddrs[0];
                cacheTime = now;
                ret = localAddrs[0];
            }
        }
        return ret;
    } catch (java.lang.SecurityException e) {
        return impl.loopbackAddress();
    }
}

解决办法

设置本机 hosts,让主机名走回环地址即可。

hosts 添加如下一行:

1
2
127.0.0.1       localhost   jioby.local
::1             localhost   jioby.local

此处的 jioby.local 即主机名,macOS 或 Linux 可通过 hostname 查看。

如果你想改 macOS 主机名,可参考这篇文章:搞懂 macOS 上的主机名/hostname/ComputerName