V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
• 请不要在回答技术问题时复制粘贴 AI 生成的内容
0576coder
V2EX  ›  程序员

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

  •  
  •   0576coder · Jul 20, 2020 · 3182 views
    This topic created in 2115 days ago, the information mentioned may be changed or developed.

    环境 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

    7 replies    2021-02-23 20:35:05 +08:00
    0576coder
        1
    0576coder  
    OP
       Jul 20, 2020
    有人解答一下吗- -
    brucewuio
        2
    brucewuio  
       Jul 20, 2020
    写个 while 循环不就好了,循环内 catch
    0576coder
        3
    0576coder  
    OP
       Jul 21, 2020
    @brucewuio
    三方包内抛出的异常 catch 不住的
    nidalee
        4
    nidalee  
       Jan 17, 2021
    LZ 最后怎么处理这个?我单起一个线程起监听,发现一段时间后,监听就无征兆的掉了,跟你说的问题有点类似
    0576coder
        5
    0576coder  
    OP
       Jan 29, 2021
    @nidalee 新的 etcd 包自带重连功能了
    0576coder
        6
    0576coder  
    OP
       Jan 29, 2021
    @nidalee 你去更新下 jectd
    nidalee
        7
    nidalee  
       Feb 23, 2021
    @0576coder 好,我测试一下,这是 jetcd 0.5 版本之后加的么?我看 github 上都没 jetcd0.5 之后版本的新特性介绍
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2690 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 54ms · UTC 02:14 · PVG 10:14 · LAX 19:14 · JFK 22:14
    ♥ Do have faith in what you're doing.