关于 jetcd 在 etcd 服务端挂掉又重启的情况下, watch 会抛出异常,有什么优雅的解决方案吗

2020-07-20 00:43:05 +08:00
 0576coder

环境 jectd 版本 0.5

etcd 版本 3.4

本人泥腿子 java 对于使用第三方包,假如是第三方包抛出异常,是不是没办法捕获解决。

如果我手动关闭 etcd 服务端,就会这样,这个异常是 jetcd 包里抛出的

00:22:31.569 [grpc-default-executor-1] INFO com.example.demo.EtcdClient - Watching for key=a
00:22:31.577 [grpc-default-executor-1] INFO com.example.demo.EtcdClient - type=PUT, key=a, value=24444
00:22:58.621 [grpc-nio-worker-ELG-1-2] DEBUG io.grpc.netty.NettyClientHandler - [id: 0x138691d4, L:/127.0.0.1:58759 - R:/127.0.0.1:2379] INBOUND DATA: streamId=3 padding=0 endStream=false length=52 bytes=000000002f0a1a08b298eaf1948386fccd0110cdd2918bd2b881cf8e01181120035a11120f0a01611010181120022a0433333333
00:22:58.622 [grpc-default-executor-1] INFO com.example.demo.EtcdClient - Watching for key=a
00:22:58.622 [grpc-default-executor-1] INFO com.example.demo.EtcdClient - type=PUT, key=a, value=3333
00:23:23.851 [grpc-nio-worker-ELG-1-2] DEBUG io.grpc.netty.NettyClientHandler - [id: 0x138691d4, L:/127.0.0.1:58759 - R:/127.0.0.1:2379] INBOUND GO_AWAY: lastStreamId=2147483647 errorCode=0 length=0 bytes=
00:23:23.856 [grpc-nio-worker-ELG-1-2] DEBUG io.grpc.netty.NettyClientHandler - [id: 0x138691d4, L:/127.0.0.1:58759 - R:/127.0.0.1:2379] INBOUND PING: ack=false bytes=73747577769755401
00:23:23.856 [grpc-nio-worker-ELG-1-2] DEBUG io.grpc.netty.NettyClientHandler - [id: 0x138691d4, L:/127.0.0.1:58759 - R:/127.0.0.1:2379] OUTBOUND PING: ack=true bytes=73747577769755401
00:23:23.857 [grpc-nio-worker-ELG-1-2] DEBUG io.grpc.netty.NettyClientHandler - [id: 0x138691d4, L:/127.0.0.1:58759 - R:/127.0.0.1:2379] OUTBOUND GO_AWAY: lastStreamId=0 errorCode=2 length=24 bytes=436f6e6e656374696f6e2072657365742062792070656572
00:23:23.861 [grpc-nio-worker-ELG-1-2] DEBUG io.netty.handler.codec.http2.Http2ConnectionHandler - [id: 0x138691d4, L:/127.0.0.1:58759 ! R:/127.0.0.1:2379] Sending GOAWAY failed: lastStreamId '0', errorCode '2', debugData 'Connection reset by peer'. Forcing shutdown of the connection.
java.io.IOException: Broken pipe
	at sun.nio.ch.FileDispatcherImpl.writev0(Native Method)
	at sun.nio.ch.SocketDispatcher.writev(SocketDispatcher.java:51)
	at sun.nio.ch.IOUtil.write(IOUtil.java:148)
	at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:504)
	at io.netty.channel.socket.nio.NioSocketChannel.doWrite(NioSocketChannel.java:423)

然后我重启 etcd 服务端,继续 put key 还是能继续 watch 到的:

00:23:50.692 [grpc-nio-worker-ELG-1-3] DEBUG io.grpc.netty.NettyClientHandler - [id: 0x56feb4a4, L:/127.0.0.1:58801 - R:/127.0.0.1:2379] INBOUND DATA: streamId=3 padding=0 endStream=false length=71 bytes=00000000420a1a08b298eaf1948386fccd0110cdd2918bd2b881cf8e01181320045a11120f0a01611010181220032a04333333335a11120f0a01611010181320...
00:23:50.692 [grpc-default-executor-2] INFO com.example.demo.EtcdClient - Watching for key=a
00:23:50.693 [grpc-default-executor-2] INFO com.example.demo.EtcdClient - type=PUT, key=a, value=3333
00:23:50.693 [grpc-default-executor-2] INFO com.example.demo.EtcdClient - type=PUT, key=a, value=4444

假如我在线上要用这个 java 的 etcd 客户端,我肯定起个线程来 watch,但是问题来了,假如 etcd 挂了 抛出了这个异常 然后这个异常导致线程退出,我难道还要线程退出的时候重新开始 watch 吗,这样感觉就麻烦了。话说面对这样的情况 该如何优雅的解决,诚心请教各位大佬了

项目里跑的 demo 参考 jetcd 的 watch demo

1946 次点击
所在节点    程序员
7 条回复
0576coder
2020-07-20 11:50:00 +08:00
有人解答一下吗- -
brucewuio
2020-07-20 17:09:10 +08:00
写个 while 循环不就好了,循环内 catch
0576coder
2020-07-21 00:09:48 +08:00
@brucewuio
三方包内抛出的异常 catch 不住的
nidalee
2021-01-17 00:19:51 +08:00
LZ 最后怎么处理这个?我单起一个线程起监听,发现一段时间后,监听就无征兆的掉了,跟你说的问题有点类似
0576coder
2021-01-29 00:46:49 +08:00
@nidalee 新的 etcd 包自带重连功能了
0576coder
2021-01-29 00:47:12 +08:00
@nidalee 你去更新下 jectd
nidalee
2021-02-23 20:35:05 +08:00
@0576coder 好,我测试一下,这是 jetcd 0.5 版本之后加的么?我看 github 上都没 jetcd0.5 之后版本的新特性介绍

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/691425

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX