项目场景:

提示:Windows环境下启动Nacos报错:

Caused by: com.alibaba.nacos.api.exception.NacosException: java.net.UnknownHostException: jmenv.tbsite.net
    at com.alibaba.nacos.core.cluster.lookup.AddressServerMemberLookup.run(AddressServerMemberLookup.java:122)
    at com.alibaba.nacos.core.cluster.lookup.AddressServerMemberLookup.start(AddressServerMemberLookup.java:74)
    at com.alibaba.nacos.core.cluster.ServerMemberManager.initAndStartLookup(ServerMemberManager.java:155)
    at com.alibaba.nacos.core.cluster.ServerMemberManager.init(ServerMemberManager.java:144)
    at com.alibaba.nacos.core.cluster.ServerMemberManager.<init>(ServerMemberManager.java:129)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:175)
    ... 108 common frames omitted
Caused by: java.net.UnknownHostException: jmenv.tbsite.net
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:220)
    at java.base/java.net.Socket.connect(Socket.java:609)
    at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:177)
    at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:474)
    at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:569)
    at java.base/sun.net.www.http.HttpClient.<init>(HttpClient.java:242)
    at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:341)
    at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:362)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1253)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1187)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1081)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1015)
    at com.alibaba.nacos.common.http.client.request.JdkHttpClientRequest.execute(JdkHttpClientRequest.java:109)
    at com.alibaba.nacos.common.http.client.NacosRestTemplate.execute(NacosRestTemplate.java:482)
    at com.alibaba.nacos.common.http.client.NacosRestTemplate.get(NacosRestTemplate.java:72)
    at com.alibaba.nacos.core.cluster.lookup.AddressServerMemberLookup.syncFromAddressUrl(AddressServerMemberLookup.java:145)
    at com.alibaba.nacos.core.cluster.lookup.AddressServerMemberLookup.run(AddressServerMemberLookup.java:113)
    ... 117 common frames omitted

问题描述

提示:Nacos默认是以集群的方式启动的,如果本地没有配置集群,启动会出现以上异常提示:

提示:如果还是希望以集群模式启动, 在 nacos/conf 目录下,将文件 cluster.conf.example 复制一份,重命名为 cluster.conf ,并将所有内容注释,重新启动

  • 当nacos以集群模式启动时,方法内的chooseLookup 方法,默认会检查nacos/conf目录下是否存在 cluster.conf文件,如果不存在,返回LookupType.ADDRESS_SERVER,如果存在, 返回LookupType.FILE_CONFIG
  • 根据LookupType ,LookupFactory.createLookUp方法返回FileConfigMemberLookup或 AddressServerMemberLookup
    这会在 ServerMemberManager 中,调用 实例的start() 方法
  • 因此,当cnacos未找到cluster.conf,且是以集群模式(默认)启动时,会报错误
  • 当然,根据源码,也可以在application.properties中配置 nacos.core.member.lookup.type=file

解决方案:

一使用 standalone 模式启动:

  1. windows下 cmd startup.cmd -m standalone
  2. linux下 sh startup.sh -m standalone

修改配置文件

  1. 也可以在application.properties中配置nacos.core.member.lookup.type=file