错误复现
主要是想要通过源码的方式分析nacos的工作原理,问题出现的原因:
- 在源码的client模块下启动Nacos服务(2.0.3版本),可以在界面上通过http://localhost:8848/nacos访问成功界面
- 新建springCloudAlibaba的项目,2.2.7.Release版本,引入config和discovery包到服务提供者中
- 添加@EnableClientDiscovery注解到启动类
- 启动服务,出现如下错误:
2022-08-30 17:48:25.206 ERROR 26174 --- [ main] c.a.cloud.nacos.discovery.NacosWatch : namingService subscribe failed, properties:NacosDiscoveryProperties{serverAddr='localhost:8848', endpoint='', namespace='public', watchDelay=30000, logName='', service='alibaba-provider', weight=1.0, clusterName='DEFAULT', group='DEFAULT_GROUP', namingLoadCacheAtStart='false', metadata={preserved.register.source=SPRING_CLOUD}, registerEnabled=true, ip='192.168.2.31', networkInterface='', port=-1, secure=false, accessKey='', secretKey='', heartBeatInterval=null, heartBeatTimeout=null, ipDeleteTimeout=null, failFast=true}
com.alibaba.nacos.api.exception.NacosException: Request nacos server failed:
at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.requestToServer(NamingGrpcClientProxy.java:279) ~[nacos-client-2.0.3.jar:na]
at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.requestToServer(NamingGrpcClientProxy.java:269) ~[nacos-client-2.0.3.jar:na]
... 20 common frames omitted
2022-08-30 17:48:25.238 INFO 26174 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-08-30 17:48:25.567 ERROR 26174 --- [ main] c.a.c.n.registry.NacosServiceRegistry : nacos registry, alibaba-provider register failed...NacosRegistration{nacosDiscoveryProperties=NacosDiscoveryProperties{serverAddr='localhost:8848', endpoint='', namespace='public', watchDelay=30000, logName='', service='alibaba-provider', weight=1.0, clusterName='DEFAULT', group='DEFAULT_GROUP', namingLoadCacheAtStart='false', metadata={preserved.register.source=SPRING_CLOUD}, registerEnabled=true, ip='192.168.2.31', networkInterface='', port=8080, secure=false, accessKey='', secretKey='', heartBeatInterval=null, heartBeatTimeout=null, ipDeleteTimeout=null, failFast=true}},
com.alibaba.nacos.api.exception.NacosException: Request nacos server failed:
解决方案
经过跟踪和查询资料,目前没有找到具体的原因,再一次网上寻找解决方案的过程中,发现有个博主提到不使用源码的方式启动的nacos就不会有这种问题,所以赶紧去尝试了一下,从gitHub中下载了nacos-2.0.3.tar.gz的包解压后,分别以单机和集群的方式启动后,在不改动服务提供者任何代码的情况下,都能够注册成功,至此问题得到决绝。