Springboot連接nacos報錯總結
springboot配置連接nacos分為五步:
-
導入依賴
如果發現以下的報錯信息請檢查:配置依賴時需要清楚配置的nacos版本和springboot版本是否互相支持。
版本不一致會出no server available 沒有可用的服務器報錯信息:
nacos registry, cloud-user-service register failed…NacosRegistration{nacosDiscoveryProperties=NacosDiscoveryProperties{serverAddr=’’, endpoint=’’, namespace=’’, watchDelay=30000, logName=’’, service=‘cloud-user-service’, weight=1.0, clusterName=‘DEFAULT’, namingLoadCacheAtStart=‘false’, metadata={preserved.register.source=SPRING_CLOUD}, registerEnabled=true, ip=‘192.168.109.1’, networkInterface=’’, port=8001, secure=false, accessKey=’’, secretKey=’’}},
java.lang.IllegalArgumentException: no server available at com.alibaba.nacos.client.naming.net.NamingProxy.reqAPI(NamingProxy.java:438) ~[nacos-client-1.1.1.jar:na] at com.alibaba.nacos.client.naming.net.NamingProxy.reqAPI(NamingProxy.java:386) ~[nacos-client-1.1.1.jar:na] at com.alibaba.nacos.client.naming.net.NamingProxy.registerService(NamingProxy.java:188) ~[nacos-client-1.1.1.jar:na] at com.alibaba.nacos.client.naming.NacosNamingService.registerInstance(NacosNamingService.java:205) ~[nacos-client-1.1.1.jar:na] at com.alibaba.nacos.client.naming.NacosNamingService.registerInstance(NacosNamingService.java:184) ~[nacos-client-1.1.1.jar:na] at com.alibaba.cloud.nacos.registry.NacosServiceRegistry.register(NacosServiceRegistry.java:61) ~[spring-cloud-alibaba-nacos-discovery-2.1.0.RELEASE.jar:2.1.0.RELEASE] at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.register(AbstractAutoServiceRegistration.java:239) [spring-cloud-commons-2.2.2.RELEASE.jar:2.2.2.RELEASE] at com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration.register(NacosAutoServiceRegistration.java:74) [spring-cloud-alibaba-nacos-discovery-2.1.0.RELEASE.jar:2.1.0.RELEASE] at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.start(AbstractAutoServiceRegistration.java:138) [spring-cloud-commons-2.2.2.RELEASE.jar:2.2.2.RELEASE] at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.bind(AbstractAutoServiceRegistration.java:101) [spring-cloud-commons-2.2.2.RELEASE.jar:2.2.2.RELEASE] at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.onApplicationEvent(AbstractAutoServiceRegistration.java:88) [spring-cloud-commons-2.2.2.RELEASE.jar:2.2.2.RELEASE] at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.onApplicationEvent(AbstractAutoServiceRegistration.java:47) [spring-cloud-commons-2.2.2.RELEASE.jar:2.2.2.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) [spring-context-5.2.4.RELEASE.jar:5.2.4.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) [spring-context-5.2.4.RELEASE.jar:5.2.4.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) [spring-context-5.2.4.RELEASE.jar:5.2.4.RELEASE] at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:403) [spring-context-5.2.4.RELEASE.jar:5.2.4.RELEASE] at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:360) [spring-context-5.2.4.RELEASE.jar:5.2.4.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.finishRefresh(ServletWebServerApplicationContext.java:165) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:553) [spring-context-5.2.4.RELEASE.jar:5.2.4.RELEASE] at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE] at com.wuyou.springcloud.UserService.main(UserService.java:11) [classes/:na]
修改前的springboot版本
查看了當前的nacos版本:
參照GitHub上的nacos和springboot對應版本信息進行了修改:
-
在啟動類中加入
@EnableDiscovery
(由於springboot默認微服務都要注冊,不添加也會注冊) -
在application.yml中配置微服務的注冊信息
在微服務添加注冊信息的時候需要留意配置的nacos路徑是否正確
-
在nacos導入配置文件
需要獲取的信息
-
配置文件所在的命名空間
-
配置文件所在的分組
-
配置文件的名稱
-
-
在bootstrap中讀取nacos存放的配置文件相關信息(建議最好直接從nacos中粘貼下來)
需要注意的是,這個兩個配置不要搞混:
spring.cloud.nacos.config.server-addr=127.0.0.1:8848 //在 bootstrap.properties 中配置 Nacos server 的地址和應用名 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 //在 application.properties 中配置 Nacos server 的地址: