现象:
1、云主机本机服务以docker bridge网络运行,本机服务能注册到nacos。但是不利于多服务器集群。
2、云主机本机服务直接运行,或者以docker host网络运行,本机服务不能注册到本机nacos。
3、如果nacos运行在服务器A,其他服务运行在服务器B,那么注册服务都正常。
4、机房服务器,无论怎么折腾,都正常。
5、云主机和机房服务器,都试过centos7,centos8。现象一致未改变。
报错:
启动日志:
09:28:50.459 [main] ERROR c.a.n.c.r.c.g.GrpcClient - [printIfErrorEnabled,99] - Server check fail, please check server 10.100.0.19 ,port 9848 is available , error ={}
java.util.concurrent.TimeoutException: Waited 3000 milliseconds (plus 610940 nanoseconds delay) for com.alibaba.nacos.shaded.io.grpc.stub.ClientCalls$GrpcFuture@4d49af10[status=PENDING, info=[GrpcFuture{clientCall={delegate={delegate=ClientCallImpl{method=MethodDescriptor{fullMethodName=Request/request, type=UNARY, idempotent=false, safe=false, sampledToLocalTracing=true, requestMarshaller=com.alibaba.nacos.shaded.io.grpc.protobuf.lite.ProtoLiteUtils$MessageMarshaller@279ad2e3, responseMarshaller=com.alibaba.nacos.shaded.io.grpc.protobuf.lite.ProtoLiteUtils$MessageMarshaller@58134517, schemaDescriptor=com.alibaba.nacos.api.grpc.auto.RequestGrpc$RequestMethodDescriptorSupplier@4450d156}}}}}]]
at com.alibaba.nacos.shaded.com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:508)
at com.alibaba.nacos.common.remote.client.grpc.GrpcClient.serverCheck(GrpcClient.java:146)
at com.alibaba.nacos.common.remote.client.grpc.GrpcClient.connectToServer(GrpcClient.java:268)
at com.alibaba.nacos.common.remote.client.RpcClient.start(RpcClient.java:394)
at com.alibaba.nacos.client.config.impl.ClientWorker$ConfigRpcTransportClient.ensureRpcClient(ClientWorker.java:941)
at com.alibaba.nacos.client.config.impl.ClientWorker$ConfigRpcTransportClient.getOneRunningClient(ClientWorker.java:1104)
at com.alibaba.nacos.client.config.impl.ClientWorker$ConfigRpcTransportClient.queryConfig(ClientWorker.java:996)
at com.alibaba.nacos.client.config.impl.ClientWorker.getServerConfig(ClientWorker.java:407)
at com.alibaba.nacos.client.config.NacosConfigService.getConfigInner(NacosConfigService.java:166)
at com.alibaba.nacos.client.config.NacosConfigService.getConfig(NacosConfigService.java:94)
at com.alibaba.cloud.nacos.client.NacosPropertySourceBuilder.loadNacosData(NacosPropertySourceBuilder.java:85)
at com.alibaba.cloud.nacos.client.NacosPropertySourceBuilder.build(NacosPropertySourceBuilder.java:73)
at com.alibaba.cloud.nacos.client.NacosPropertySourceLocator.loadNacosPropertySource(NacosPropertySourceLocator.java:199)
at com.alibaba.cloud.nacos.client.NacosPropertySourceLocator.loadNacosDataIfPresent(NacosPropertySourceLocator.java:186)
at com.alibaba.cloud.nacos.client.NacosPropertySourceLocator.loadNacosConfiguration(NacosPropertySourceLocator.java:158)
at com.alibaba.cloud.nacos.client.NacosPropertySourceLocator.loadSharedConfiguration(NacosPropertySourceLocator.java:116)
at com.alibaba.cloud.nacos.client.NacosPropertySourceLocator.locate(NacosPropertySourceLocator.java:101)
at org.springframework.cloud.bootstrap.config.PropertySourceLocator.locateCollection(PropertySourceLocator.java:51)
at org.springframework.cloud.bootstrap.config.PropertySourceLocator.locateCollection(PropertySourceLocator.java:47)
at org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.initialize(PropertySourceBootstrapConfiguration.java:95)
at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:638)
at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:402)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:336)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1336)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1325)
at com.spt.edsp.basedata.BasedataApplication.main(BasedataApplication.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:108)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)
问题很明显,不是端口问题,而是localhost的网络问题。
解决过程:
1、临时关闭防火墙,未解决。
[root@10-100-0-19 ~]# setenforce 0
setenforce: SELinux is disabled
2、彻底关闭防火墙,未解决。
[root@10-100-0-19 ~]# systemctl disable firewalld
[root@10-100-0-19 ~]# systemctl stop firewalld
[root@10-100-0-19 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead) since 四 2021-12-16 14:49:37 CST; 3 months 24 days ago
Docs: man:firewalld(1)
Main PID: 1197 (code=exited, status=0/SUCCESS)
12月 15 17:55:08 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
12月 15 17:55:09 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
12月 15 17:55:09 localhost.localdomain firewalld[1197]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be remo...g it now.
12月 16 11:31:27 10-100-0-19 firewalld[1197]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a ...ng it now.
12月 16 11:53:51 10-100-0-19 firewalld[1197]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a ...ng it now.
12月 16 14:49:36 10-100-0-19 systemd[1]: Stopping firewalld - dynamic firewall daemon...
12月 16 14:49:37 10-100-0-19 systemd[1]: Stopped firewalld - dynamic firewall daemon.
Hint: Some lines were ellipsized, use -l to show in full.
3、更改hosts,解决。
[root@10-100-0-19 ~]# vim /etc/hosts
127.0.0.1 localhost 10-100-0-19 # 添加这一行
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
在文件/etc/hosts,添加一行,localhost和主机名对应起来。
4、分析原因。
注册服务异常的机器,hostname -i 没有对应到 127.0.0.1。主机名没有和127.0.0.1对应起来。
[root@10-100-0-18 file]# hostname -i
fe80::b9a:d281:e84b:1f6a%ens12 fe80::1018:1b6e:6103:6d95%ens13 10.216.115.211 10.100.0.18
[root@10-100-0-18 file]# hostname
10-100-0-18
注册服务正常的机器:
[root@10-100-0-21 ~]# hostname
10-100-0-21
[root@10-100-0-21 ~]# hostname -i
127.0.0.1
接着查看:
[root@10-100-0-19 ~]# hostnamectl
Static hostname: localhost.localdomain
Transient hostname: 10-100-0-19
Icon name: computer-vm
Chassis: vm
Machine ID: 46a19cb5f7064fbeb634f9668f22d158
Boot ID: 07aadce6349c4d8eb55a855f8b31c58e
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-1160.49.1.el7.x86_64
Architecture: x86-64
10-100-0-19为主机名,Transient hostname 临时主机名。而原本hosts文件已经有了localhost对应Static hostname: localhost.localdomain。看来nacos注册服务,需要的是Transient hostname来替代localhost。
从nacos的配置文件,也有点类似的参数,不知道是否针对此问题。
[root@10-100-0-19 ~]# vim /opt/edsp/nacos/conf/application.properties
.....................................................................
#*************** Spring Boot Related Configurations ***************#
### Default web context path:
server.servlet.contextPath=/nacos
### Default web server port:
#*************** Network Related Configurations ***************#
### If prefer hostname over ip for Nacos server addresses in cluster.conf:
# nacos.inetutils.prefer-hostname-over-ip=false
### Specify local server's IP:
# nacos.inetutils.ip-address=
.............................................
参考:
https://github.com/alibaba/nacos/issues/404
https://www.cnblogs.com/zzugyl/p/16135255.html