使用SpringBoot整合Nacos集群报错,Nacos的集群是部署在docker中
错误实例
# 创建一个自定义网络
docker network ls
docker network create --driver bridge --subnet=172.19.0.0/16 --gateway=172.19.0.1 mynetwork
docker network inspect mynetwork
# 启动Nacos容器 192.168.220.220是虚拟机(ubuntu)的地址,docker安装这个虚拟机中
## nacos-server8848
docker run -d--rm \
-e PREFER_HOST_MODE=123 \
-e MODE=cluster \
-e NACOS_SERVER_PORT=8848 \
-e NACOS_SERVER_IP=192.168.220.220 \
-e NACOS_SERVERS="192.168.220.220:8848 192.168.220.220:8849 192.168.220.220:8850" \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=192.168.220.220 \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=123456 \
-e MYSQL_SERVICE_DB_NAME=nacos-cluster \
-p 8848:8848 --net=mynetwork \
--name nacos-server8848 \
bdf60dc2ada3
## nacos-server8849
docker run -d --rm \
-e PREFER_HOST_MODE=123 \
-e MODE=cluster \
-e NACOS_SERVER_PORT=8849 \
-e NACOS_SERVER_IP=192.168.220.220 \
-e NACOS_SERVERS="192.168.220.220:8848 192.168.220.220:8849 192.168.220.220:8850" \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=192.168.220.220 \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=123456 \
-e MYSQL_SERVICE_DB_NAME=nacos-cluster \
-p 8849:8848 --net=mynetwork \
--name nacos-server8849 \
bdf60dc2ada3
## nacos-server8850
docker run -d --rm \
-e PREFER_HOST_MODE=123 \
-e MODE=cluster \
-e NACOS_SERVER_PORT=8850 \
-e NACOS_SERVER_IP=192.168.220.220 \
-e NACOS_SERVERS="192.168.220.220:8848 192.168.220.220:8849 192.168.220.220:8850" \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=192.168.220.220 \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=123456 \
-e MYSQL_SERVICE_DB_NAME=nacos-cluster \
-p 8850:8848 --net=mynetwork \
--name nacos-server8850 \
bdf60dc2ada3
这样部署会发现没有任何问题,进入页面nacos的主页也正常
我们会发现整个流程不会出现一点问题。
但是,一旦使用springboot去整合Nacos集群就会报错
错误信息
2021-12-08 18:44:40.677 ERROR 23032 --- [ main] c.a.c.n.registry.NacosServiceRegistry : nacos registry, stock-service register failed...NacosRegistration{nacosDiscoveryProperties=NacosDiscoveryProperties{serverAddr='192.168.220.220:8848', endpoint='', namespace='public', watchDelay=30000, logName='', service='stock-service', weight=1.0, clusterName='DEFAULT', group='DEFAULT_GROUP', namingLoadCacheAtStart='false', metadata={preserved.register.source=SPRING_CLOUD}, registerEnabled=true, ip='192.168.189.1', networkInterface='', port=8080, secure=false, accessKey='', secretKey='', heartBeatInterval=null, heartBeatTimeout=null, ipDeleteTimeout=null, failFast=true}},
com.alibaba.nacos.api.exception.NacosException: failed to req API:/nacos/v1/ns/instance after all servers([192.168.220.220:8848]) tried: ErrCode:503, ErrMsg:server is DOWNnow, detailed error message: Optional[Distro protocol is not initialized]
at com.alibaba.nacos.client.naming.net.NamingProxy.reqApi(NamingProxy.java:556) ~[nacos-client-1.4.2.jar:na]
at com.alibaba.nacos.client.naming.net.NamingProxy.reqApi(NamingProxy.java:498) ~[nacos-client-1.4.2.jar:na]
at com.alibaba.nacos.client.naming.net.NamingProxy.reqApi(NamingProxy.java:493) ~[nacos-client-1.4.2.jar:na]
at com.alibaba.nacos.client.naming.net.NamingProxy.registerService(NamingProxy.java:246) ~[nacos-client-1.4.2.jar:na]
at com.alibaba.nacos.client.naming.NacosNamingService.registerInstance(NacosNamingService.java:212) ~[nacos-client-1.4.2.jar:na]
at com.alibaba.cloud.nacos.registry.NacosServiceRegistry.register(NacosServiceRegistry.java:74) ~[spring-cloud-starter-alibaba-nacos-discovery-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.register(AbstractAutoServiceRegistration.java:239) [spring-cloud-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration.register(NacosAutoServiceRegistration.java:78) [spring-cloud-starter-alibaba-nacos-discovery-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.start(AbstractAutoServiceRegistration.java:138) [spring-cloud-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.bind(AbstractAutoServiceRegistration.java:101) [spring-cloud-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.onApplicationEvent(AbstractAutoServiceRegistration.java:88) [spring-cloud-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.onApplicationEvent(AbstractAutoServiceRegistration.java:47) [spring-cloud-commons-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) [spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) [spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) [spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:404) [spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:361) [spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.boot.web.servlet.context.WebServerStartStopLifecycle.start(WebServerStartStopLifecycle.java:46) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:182) [spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:53) [spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:360) [spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:158) [spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:122) [spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:895) [spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:554) [spring-context-5.2.8.RELEASE.jar:5.2.8.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
at com.rush.stock.StackApplication.main(StackApplication.java:9) [classes/:na]
pom文件的配置信息
server:
port: 8083
# 应用名称 (nacos会将该名称当作服务名称)
spring:
application:
name: stock-service
cloud:
nacos:
server-addr: 192.168.220.220:8848,192.168.220.220:8849,192.168.220.220:8850 # nacos的服务地址
discovery:
username: nacos # 登录到管理平台的一个名称,默认是nacos
password: nacos # 登录密码
namespace: public # 默认是public
ephemeral: false # 设置为永久实例
我们会发现stock-service register failed,在浏览器上访问这三个的Nacos服务器是没有问题,在这之前Nacos单机版整合也是没有问题的,问题只能出现的docker启动的Nacos容器上。会发现创建的Nacos容器填写的ip都是外部ip:port,但是创建的网段却是docker内部的网段,我们需要只能在同一网段的ip
正确实例
# 创建一个自定义网络
docker network ls
docker network create --driver bridge --subnet=172.19.0.0/16 --gateway=172.19.0.1 mynetwork
docker network inspect mynetwork
#创建Nacos容器
docker run -d --rm \
-e PREFER_HOST_MODE=123 \
-e MODE=cluster \
-e NACOS_SERVER_PORT=8848 \
-e NACOS_SERVER_IP=172.19.0.2 \
-e NACOS_SERVERS="172.19.0.2:8848 172.19.0.3:8848 172.19.0.4:8848" \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=192.168.220.220 \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=123456 \
-e MYSQL_SERVICE_DB_NAME=nacos-cluster \
-p 8848:8848 --net=mynetwork \
--name nacos-server8848 \
--ip 172.19.0.2 \
bdf60dc2ada3
docker run -d --rm \
-e PREFER_HOST_MODE=123 \
-e MODE=cluster \
-e NACOS_SERVER_PORT=8848 \
-e NACOS_SERVER_IP=172.19.0.3 \
-e NACOS_SERVERS="172.19.0.2:8848 172.19.0.3:8848 172.19.0.4:8848" \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=192.168.220.220 \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=123456 \
-e MYSQL_SERVICE_DB_NAME=nacos-cluster \
-p 8849:8848 --net=mynetwork \
--name nacos-server8849 \
--ip 172.19.0.3 \
bdf60dc2ada3
docker run -d --rm \
-e PREFER_HOST_MODE=123 \
-e MODE=cluster \
-e NACOS_SERVER_PORT=8848 \
-e NACOS_SERVER_IP=172.19.0.4 \
-e NACOS_SERVERS="172.19.0.2:8848 172.19.0.3:8848 172.19.0.4:8848" \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=192.168.220.220 \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_USER=root \
-e MYSQL_SERVICE_PASSWORD=123456 \
-e MYSQL_SERVICE_DB_NAME=nacos-cluster \
-p 8850:8848 --net=mynetwork \
--name nacos-server8850 \
--ip 172.19.0.4 \
bdf60dc2ada3
发现访问nacos服务器的页面也是没有问题的
springboot整合也解决了