nacos2.1.0遇到的坑,一直连接本地localhost:8848等

2年前 (2022) 程序员胖胖胖虎阿
502 0 0

nacos2.1.0遇到的坑,注册不上和一直连接本地localhost:8848等

最近想做一个springcloud的项目,在配置nacos的时候遇到了一些小问题,分享出来。

1.本地服务注册不上

首选我们先启动nacos的本地服务

sh startup.sh -m standalone

nacos2.1.0遇到的坑,一直连接本地localhost:8848等
打开浏览器输入
localhost:8848/nacos
nacos2.1.0遇到的坑,一直连接本地localhost:8848等
输入用户名和密码,默认是nacos

打开服务管理

nacos2.1.0遇到的坑,一直连接本地localhost:8848等
现在去启动后端的服务

可以看到后台没有报错
nacos2.1.0遇到的坑,一直连接本地localhost:8848等
但是,回到nacos的后台,服务并没有注册上,这个时候就可以去检查一下你的pom.xml里面有没有导入一个包

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

就是这个web包,这时候我们把服务重新启动一下

看下日志发现服务已经注册了
nacos2.1.0遇到的坑,一直连接本地localhost:8848等
看下nacos的后台,服务已经注册上了
nacos2.1.0遇到的坑,一直连接本地localhost:8848等

2.远程服务注册不上

在本地测试完成后,我想着在我的服务器上也部署一个nacos(具体怎么部署看我另外一篇文章哈,或者百度)。

部署完成后启动nacos

修改本地配置文件的地址,如下
nacos2.1.0遇到的坑,一直连接本地localhost:8848等

spring:
  cloud:
    nacos:
#      server-addr: localhost:8848 本地
      server-addr: 132.121.121.121:8848  #远程
      discovery:
        server-addr: ${spring.cloud.nacos.server-addr}

直接启动服务,可以发现报错了,提示注册不上

com.alibaba.nacos.api.exception.NacosException: Request nacos server failed: 
	at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.requestToServer(NamingGrpcClientProxy.java:288) ~[nacos-client-2.1.0.jar:na]
	at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.doRegisterService(NamingGrpcClientProxy.java:128) ~[nacos-client-2.1.0.jar:na]
	at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.registerService(NamingGrpcClientProxy.java:114) ~[nacos-client-2.1.0.jar:na]
	at com.alibaba.nacos.client.naming.remote.NamingClientProxyDelegate.registerService(NamingClientProxyDelegate.java:94) ~[nacos-client-2.1.0.jar:na]
	at com.alibaba.nacos.client.naming.NacosNamingService.registerInstance(NacosNamingService.java:145) ~[nacos-client-2.1.0.jar:na]
	at com.alibaba.cloud.nacos.registry.NacosServiceRegistry.register(NacosServiceRegistry.java:74) ~[spring-cloud-starter-alibaba-nacos-discovery-2021.1.jar:2021.1]
	at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.register(AbstractAutoServiceRegistration.java:232) [spring-cloud-commons-3.0.2.jar:3.0.2]
	at com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration.register(NacosAutoServiceRegistration.java:78) [spring-cloud-starter-alibaba-nacos-discovery-2021.1.jar:2021.1]
	at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.start(AbstractAutoServiceRegistration.java:133) [spring-cloud-commons-3.0.2.jar:3.0.2]
	at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.bind(AbstractAutoServiceRegistration.java:98) [spring-cloud-commons-3.0.2.jar:3.0.2]
	at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.onApplicationEvent(AbstractAutoServiceRegistration.java:86) [spring-cloud-commons-3.0.2.jar:3.0.2]
	at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.onApplicationEvent(AbstractAutoServiceRegistration.java:47) [spring-cloud-commons-3.0.2.jar:3.0.2]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176) [spring-context-5.3.12.jar:5.3.12]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169) [spring-context-5.3.12.jar:5.3.12]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143) [spring-context-5.3.12.jar:5.3.12]
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:421) [spring-context-5.3.12.jar:5.3.12]
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:378) [spring-context-5.3.12.jar:5.3.12]
	at org.springframework.boot.web.servlet.context.WebServerStartStopLifecycle.start(WebServerStartStopLifecycle.java:46) [spring-boot-2.5.6.jar:2.5.6]
	at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) [spring-context-5.3.12.jar:5.3.12]
	at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) [spring-context-5.3.12.jar:5.3.12]
	at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) [spring-context-5.3.12.jar:5.3.12]
	at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_333]
	at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) [spring-context-5.3.12.jar:5.3.12]
	at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) [spring-context-5.3.12.jar:5.3.12]
	at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) [spring-context-5.3.12.jar:5.3.12]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) [spring-context-5.3.12.jar:5.3.12]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.5.6.jar:2.5.6]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-2.5.6.jar:2.5.6]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:434) ~[spring-boot-2.5.6.jar:2.5.6]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) ~[spring-boot-2.5.6.jar:2.5.6]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.6.jar:2.5.6]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.6.jar:2.5.6]
	at com.hdg.user.HdgServerUserApplication.main(HdgServerUserApplication.java:13) ~[classes/:na]
Caused by: com.alibaba.nacos.api.exception.NacosException: Client not connected, current status:STARTING
	at com.alibaba.nacos.common.remote.client.RpcClient.request(RpcClient.java:651) ~[nacos-client-2.1.0.jar:na]
	at com.alibaba.nacos.common.remote.client.RpcClient.request(RpcClient.java:631) ~[nacos-client-2.1.0.jar:na]
	at com.alibaba.nacos.client.naming.remote.gprc.NamingGrpcClientProxy.requestToServer(NamingGrpcClientProxy.java:278) ~[nacos-client-2.1.0.jar:na]
	... 32 common frames omitted
2022-06-26 10:59:45.431 ERROR 26156 --- [           main] c.a.c.n.registry.NacosServiceRegistry    : nacos registry, hdg-server-user register 
failed...NacosRegistration{nacosDiscoveryProperties=NacosDiscoveryProperties{serverAddr='localhost:8848', endpoint='', namespace='', 
watchDelay=30000, logName='', service='hdg-server-user', weight=1.0, clusterName='DEFAULT', group='DEFAULT_GROUP', 
namingLoadCacheAtStart='false', metadata={preserved.register.source=SPRING_CLOUD}, registerEnabled=true, ip='192.168.1.5', networkInterface='', 
port=9528, secure=false, accessKey='', secretKey='', heartBeatInterval=null, heartBeatTimeout=null, ipDeleteTimeout=null}},

看错误发现修改后的地址还是在找本地的localhost:8848,就很奇怪,明明修改,就是不行。这时候就要去看下nacos的官网,
nacos2.1.0遇到的坑,一直连接本地localhost:8848等
可以看到nacos2.0后的版本添加两个端口。

这时候就去服务器上放开两个端口。
nacos2.1.0遇到的坑,一直连接本地localhost:8848等
然后重启本地的服务,这时候可以看到服务已经注册上去了。

还有的同学可能还是注册不上,那么又要看下你的pom.xml文件,里面有没有加入这么一个包

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>

导入包,重启服务
可以看到日志里面,已经有注册信息了
nacos2.1.0遇到的坑,一直连接本地localhost:8848等
远程的nacos,也注册上了
nacos2.1.0遇到的坑,一直连接本地localhost:8848等
以上就是我遇到的问题,希望能帮助到你,喜欢点一波关注,谢谢!
转发请标明出处,原创不易!

相关文章

暂无评论

暂无评论...