[Solved] failed to req API:localhost:8848/nacos/v1/ns/instance. code:500 msg: java.net.ConnectException

1. Error Messages
nacos connect error:

2022-03-23 10:06:48 |ERROR |main |SpringApplication.java:826 |org.springframework.boot.SpringApplication |Application run failed
java.lang.IllegalStateException: failed to req API:/nacos/v1/ns/instance after all servers([localhost:8848]) tried: failed to req API:localhost:8848/nacos/v1/ns/instance. code:500 msg: java.net.ConnectException: Connection refused: connect
    at com.alibaba.nacos.client.naming.net.NamingProxy.reqAPI(NamingProxy.java:467)
    at com.alibaba.nacos.client.naming.net.NamingProxy.reqAPI(NamingProxy.java:389)
    at com.alibaba.nacos.client.naming.net.NamingProxy.registerService(NamingProxy.java:191)
    at com.alibaba.nacos.client.naming.NacosNamingService.registerInstance(NacosNamingService.java:207)
    at com.alibaba.cloud.nacos.registry.NacosServiceRegistry.register(NacosServiceRegistry.java:64)
    at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.register(AbstractAutoServiceRegistration.java:239)
    at com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration.register(NacosAutoServiceRegistration.java:76)
    at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.start(AbstractAutoServiceRegistration.java:138)
    at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.bind(AbstractAutoServiceRegistration.java:101)
    at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.onApplicationEvent(AbstractAutoServiceRegistration.java:88)
    at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.onApplicationEvent(AbstractAutoServiceRegistration.java:47)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:403)
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:360)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:165)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
    at com.atguigu.guli.service.edu.ServiceEduApplication.main(ServiceEduApplication.java:15)

 

2. Solution
1.Check whether nacos is open
2.Check whether nacos is open properly
3.  nacos and eureka are not the same, eureka is part of the project, nacos is more like a partner relationship.

         ,--.
       ,--.'|
   ,--,:  : |                                         
,`--.'`|  ' :                       ,---.               
|   :  :  | |                      '   ,'\   .--.--.   
:   |   \ | :  ,--.--.     ,---. / /  |//   '   
|   : '  '; |/      \  /    \.   ; ,. :|  :  /`./   
'   ' ;.    ;.--.  .-. |/  /''   | |: :|  :  ;_
|   | | \   | \__\/: . ..    '/'   | .; : \  \    `.      
'   : |  ; .' ," .--.; |'   ; :__|   :    |  `----.   \
|   | '`--' // ,.  |'   | '.'|\   \ // /`--'  /
'   : |     ;  :   .'   \   :    : `----'  '--'.     /
;   |.'     |  ,     .-./\   \ /           `--'---'
'---'        `--`---'     `----'

4. If Nacos starts normally, restart the project, or not

(1) Check annotation @enablediscoveryclient

(2) Check configuration file

# Note the space position
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 # nacos server location

Read More: