一、问题

启动服务报错:nacos连接失败

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)

二、解决

1、检查nacos有没有开启

2、检查nacos开启是否正常

3、nacos和eureka不一样,eureka是项目的一部分,nacos更像是合作伙伴的关系,新手容易忽略

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

4、如果nacos正常开启了,重新启动项目,还是不行

(1)检查注解@EnableDiscoveryClient

(2)检查配置文件

# 注意空格位置
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 # nacos服务地址

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐