背景

环境是CentOS7操作系统,nacos服务宕掉了,启动服务的时候报错。

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.servlet.FilterRegistrationBean]: Factory method ‘distroFilterRegistration’ threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘distroFilter’: Unsatisfied dependency expressed through field ‘distroMapper’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘distroMapper’ defined in URL [jar:file:/usr/local/nacos/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-naming-1.3.1.jar!/com/alibaba/nacos/naming/core/DistroMapper.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘serverMemberManager’ defined in URL [jar:file:/usr/local/nacos/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-core-1.3.1.jar!/com/alibaba/nacos/core/cluster/ServerMemberManager.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.nacos.core.cluster.ServerMemberManager]: Constructor threw exception; nested exception is ErrCode:500, ErrMsg:jmenv.tbsite.net

Caused by: java.net.UnknownHostException: jmenv.tbsite.net
at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:797)
at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1505)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1364)
at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1298)

排查

1、查看nacos服务是否启动

ps -ef|grep nacos


这里发现服务并未启动

2、定位nacos服务位置

whereis nacos

3、尝试启动nacos

进入nacos的bin目录(这里LZ的目录是/usr/local/nacos/nacos/bin),运行启动脚本startup.sh启动nacos服务

sh startup.sh

4、查看启动日志

进入nacos的logs目录(这里LZ的目录是/usr/local/nacos/nacos/logs),使用tail命令查看实时日志。

tail -fn 500 start.out

就查看到了上述java.net.UnknownHostException: jmenv.tbsite.net错误了

解决

启动时指定参数-m standalone单机模式启动就可以了

sh startup.sh -m standalone

这是为什么呢,原因

原来是nacos启动模式未设置导致的,在没有使用-n参数去指定 Nacos Server 的名称的情况下,Nacos默认会将自己注册到一个命名空间为”jmenv.tbsite.net”的服务中心中。如果你的服务器无法连接到该服务中心,就会导致启动失败。而通过指定-m standalone参数,启动Nacos单机模式,就可以避免这个问题,因为在单机模式下,Nacos不会去注册到任何服务中心中,而是直接启动自己的服务。