我在fabric8 kubernerties client版本4.10.2和Watcher中遇到了java.net.SocketException: Socket closed异常。
代码如下:

KubernetesClient client = createClient();
client.pods().watch(createLogWatcher("pod"));
client.apps().deployments().watch(createLogWatcher("deployment"));
client.services().watch(createLogWatcher("service"));
client.extensions().ingresses().watch(createLogWatcher("ingress"));

// do stuff with client
当我的应用程序关闭时,我收到以下异常
08:10:37.180 [OkHttp https://test-api.ib.dnsbego.de:6443/...] WARN  i.f.k.c.d.i.WatchConnectionManager - Exec Failure
javax.net.ssl.SSLException: Socket closed
    at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:127)
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:320)
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:263)
    at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:258)
    at java.base/sun.security.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1315)
    at java.base/sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:839)
    at okio.Okio$2.read(Okio.java:140)
    at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)
    at okio.RealBufferedSource.request(RealBufferedSource.java:72)
    at okio.RealBufferedSource.require(RealBufferedSource.java:65)
    at okio.RealBufferedSource.readByte(RealBufferedSource.java:78)
    at okhttp3.internal.ws.WebSocketReader.readHeader(WebSocketReader.java:117)
    at okhttp3.internal.ws.WebSocketReader.processNextFrame(WebSocketReader.java:101)
    at okhttp3.internal.ws.RealWebSocket.loopReader(RealWebSocket.java:273)
    at okhttp3.internal.ws.RealWebSocket$1.onResponse(RealWebSocket.java:209)
    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)
    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
    Suppressed: java.net.SocketException: Socket closed
        at java.base/java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)
        at java.base/java.net.SocketOutputStream.write(SocketOutputStream.java:150)
        at java.base/sun.security.ssl.SSLSocketOutputRecord.encodeAlert(SSLSocketOutputRecord.java:81)
        at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:351)
        ... 18 common frames omitted
Caused by: java.net.SocketException: Socket closed
    at java.base/java.net.SocketInputStream.socketRead0(Native Method)
    at java.base/java.net.SocketInputStream.socketRead(SocketInputStream.java:115)
    at java.base/java.net.SocketInputStream.read(SocketInputStream.java:168)
    at java.base/java.net.SocketInputStream.read(SocketInputStream.java:140)
    at java.base/sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:448)
    at java.base/sun.security.ssl.SSLSocketInputRecord.bytesInCompletePacket(SSLSocketInputRecord.java:68)
    at java.base/sun.security.ssl.SSLSocketImpl.readApplicationRecord(SSLSocketImpl.java:1104)
    at java.base/sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:823)
    ... 14 common frames omitted

最佳答案

我自己弄清楚了这一点,但由于我找不到关于此的任何讨论,因此我想将其发布给下一个人。
每个watcher(...)调用都会返回一个Watch实例,可以将其关闭,因此我可以通过以下方法解决此问题:

KubernetesClient client = createClient();
List<Watch> watches = new ArrayList<>();
watches.add(client.pods().watch(createLogWatcher("pod")));
watches.add(client.apps().deployments().watch(createLogWatcher("deployment")));
watches.add(client.services().watch(createLogWatcher("service")));
watches.add(client.extensions().ingresses().watch(createLogWatcher("ingress")));
try {
   // do stuff
} finally {
   watches.forEach(Watch::close);
}

10-07 20:38