一、问题
在一次项目架构中,使用SpringCloud
微服务架构,其中将Alibaba-Cloud
配置SpringCloud
组件使用,服务发现和配置中心使用的是阿里巴巴开源的组件nacos
,nacos-server
部署在linux
下的centos7.0
环境上,每个服务启动时都会去连接nacos-server
端,在nacos-server
部署在Linux
环境下时,由于网络波动等原因,框架bug
就产生了,Windows
下无此问题,部署在Linux
下,客户端连接配置中心时会报如下错误,错误日志如下:
2020-04-14 09:51:15.874 ERROR [jiuyv-gateway,,,] 15364 --- [252.80.198_8848] c.a.n.c.config.http.ServerHttpAgent : [NACOS SocketTimeoutException httpPost] currentServerAddr: http://130.252.80.198:8848, err : Read timed out
2020-04-14 09:51:15.874 ERROR [jiuyv-gateway,,,] 15364 --- [252.80.198_8848] c.a.n.c.config.http.ServerHttpAgent : no available server, currentServerAddr : http://130.252.80.198:8848
2020-04-14 09:51:16.049 ERROR [jiuyv-gateway,,,] 15364 --- [252.80.198_8848] c.a.n.client.config.impl.ClientWorker : [fixed-130.252.80.198_8848] [check-update] get changed dataId exception
java.net.ConnectException: no available server, currentServerAddr : http://130.252.80.198:8848
at com.alibaba.nacos.client.config.http.ServerHttpAgent.httpPost(ServerHttpAgent.java:170) ~[nacos-client-1.1.4.jar:na]
at com.alibaba.nacos.client.config.http.MetricsHttpAgent.httpPost(MetricsHttpAgent.java:64) ~[nacos-client-1.1.4.jar:na]
at com.alibaba.nacos.client.config.impl.ClientWorker.checkUpdateConfigStr(ClientWorker.java:377) [nacos-client-1.1.4.jar:na]
at com.alibaba.nacos.client.config.impl.ClientWorker.checkUpdateDataIds(ClientWorker.java:352) [nacos-client-1.1.4.jar:na]
at com.alibaba.nacos.client.config.impl.ClientWorker$LongPollingRunnable.run(ClientWorker.java:512) [nacos-client-1.1.4.jar:na]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_25]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_25]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_25]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.8.0_25]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_25]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_25]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_25]
2020-04-14 09:51:16.052 ERROR [jiuyv-gateway,,,] 15364 --- [252.80.198_8848] c.a.n.client.config.impl.ClientWorker : longPolling error :
java.net.ConnectException: no available server, currentServerAddr : http://130.252.80.198:8848
at com.alibaba.nacos.client.config.http.ServerHttpAgent.httpPost(ServerHttpAgent.java:170) ~[nacos-client-1.1.4.jar:na]
at com.alibaba.nacos.client.config.http.MetricsHttpAgent.httpPost(MetricsHttpAgent.java:64) ~[nacos-client-1.1.4.jar:na]
at com.alibaba.nacos.client.config.impl.ClientWorker.checkUpdateConfigStr(ClientWorker.java:377) ~[nacos-client-1.1.4.jar:na]
at com.alibaba.nacos.client.config.impl.ClientWorker.checkUpdateDataIds(ClientWorker.java:352) ~[nacos-client-1.1.4.jar:na]
at com.alibaba.nacos.client.config.impl.ClientWorker$LongPollingRunnable.run(ClientWorker.java:512) ~[nacos-client-1.1.4.jar:na]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_25]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_25]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_25]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.8.0_25]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_25]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_25]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_25]
控制台截图如下:
二、原因
原因是由于在远程服务器上的配置,网络有些延迟,二nacos
框架中将超时时间设置得特别短,在段时间内就一直报超时异常,虽然不影响使用,但是问题还是很明显,框架存在bug
,如果将同样的配置放在本地nacos
配置中心上启动,则没有遇见该问题。
三、解决方式
1. 查源码,提Issue
在得出这个异常是框架的bug
之后,查找nacos
源码,查看Issue
: https://github.com/alibaba/nacos/issues/2206 ,看看是否有类似的问题,在GitHub
上查到了这种超时问题,但是没有明确解决方式,只是说在未来的1.2.0
版本之后会解决掉,而且有部分人nacos
的client
和server
端都升级1.2.0
版本,依然有此问题,于是我提了个Issue
,官方GitHub
上的回答如下:
Issue: https://github.com/alibaba/nacos/issues/2206
2. 解决方式: pom加入nacos-client端依赖
<!-- Alibaba-nacos服务发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Alibaba-nacos配置中心-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.2.0</version>
</dependency>
3. 我使用的版本
- Spring Boot Version: 2.1.6.RELEASE
- Spring Cloud Version: Spring Cloud Greenwich
- Spring Cloud Alibaba Version: 2.1.1.RELEASE
- Nacos Version: 1.1.4
- Java Version: 1.8
通过以上将nacos
集成到微服务项目中作为服务注册与发现和配置中心的实践,还是有点小bug
,相信在后续更新中,nacos
框架会更加完善,到时候我们再体验他更加具有体验性的功能吧。
如果有问题,请评论联系,我很愿意探讨。