2016-11-08 196 views
3

我正在使用grpc在Java服務器和節點客戶端之間進行通信。當他們中的任何一方死亡時,另一方死亡。在grpc java/node上處理客戶端和服務器錯誤

這裏是拋出的Java服務器上的異常,當節點的客戶端死機 -

Nov 08, 2016 11:28:03 AM io.grpc.netty.NettyServerHandler onConnectionError 
WARNING: Connection Error 
java.io.IOException: An existing connection was forcibly closed by the remote host 
    at sun.nio.ch.SocketDispatcher.read0(Native Method) 
    at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:43) 
    at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223) 
    at sun.nio.ch.IOUtil.read(IOUtil.java:192) 
    at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:380) 
    at io.netty.buffer.PooledUnsafeDirectByteBuf.setBytes(PooledUnsafeDirectByteBuf.java:288) 
    at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:1100) 
    at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:349) 
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:112) 
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:571) 
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:512) 
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:426) 
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:398) 
    at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:877) 
    at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:144) 
    at java.lang.Thread.run(Thread.java:745) 

這裏是扔在節點異常時,Java的死亡:

events.js:141 
     throw er; // Unhandled 'error' event 
    ^
Error: {"created":"@1478623914.082000000","description":"An existing connection was forcibly closed by the remote  host.\r\n","file":"..\src\core\lib\iomgr\tcp_windows.c","file_line":171,"grpc_status":14}  
    at ClientReadableStream._emitStatusIfDone ( C:\HarshalDev\Live_TDFX\TDSL0007-TDFXPlatfo rmsDesignandBuild\tdfxlive\node_modules\grpc\src\node\src\client.js:189:19) 
    at ClientReadableStream._receiveStatus (C:\ HarshalDev\Live_TDFX\TDSL0007-TDFXPlatformsDesignandBuild\tdfxlive\node_modules\grpc\src\node\src\client.js:169:8) 
    at C:\HarshalDev\Live_TDFX\TDSL0007-TDFXPlatformsDesignandBuild\tdfxlive\node_modules\grpc\src\node\src\client.js:577:14 
[nodemon] app crashed - waiting for file changes before starting... 

的問題 - 如何處理這些異常?

我嘗試沒有任何成功添加try/catch併爲線程添加未捕獲的異常處理程序。

Java代碼來初始化 -

ServerBuilder<?> serverBuilder = ServerBuilder.forPort(getPort()); 
server = serverBuilder 
     .addService(createBwayStreamingService()) 
     .addService(ServerInterceptors.intercept(createBwayOrderService(), authServerInterceptor)) 
     .addService(createBwayInstrumentService()) 
     .addService(createBwaySettlementService()) 
     .addService(createBwayDateTimeService()) 
     .addService(ServerInterceptors.intercept(createBwayConfService(), authServerInterceptor)) 
     .addService(ServerInterceptors.intercept(createBwayTradeService(), authServerInterceptor)) 
     .addService(ServerInterceptors.intercept(createBwayAuthService(), authServerInterceptor)) 
     .build(); 
Preconditions.checkNotNull(server); 
server.start(); 
System.out.println("Server started, listening on " + getPort()); 
Runtime.getRuntime().addShutdownHook(new Thread() { 
    @Override 
    public void run() { 
     System.out.println("Shutting down gRPC server"); 
     TocGateway.this.stop(); 
     System.out.println("Server shut down"); 
    } 
}); 
server.awaitTermination(); 

節點客戶端處理程序(的服務之一,所有其他服務都使用相同的模式) -

let protoDescriptorStreaming = grpc.load((process.env.FX_LIVE_PROTO_DIR || '../tocGateway/src/main/proto') + '/streaming.proto'); 
let streamingService = new protoDescriptorStreaming.tds.fxlive.bway.BwayStreamService(process.env.TOC_GATEWAY_ADDRESS || 'localhost:8087', grpc.credentials.createInsecure()); 

回答

2

Java的警告是無害的。發生異常後,應用程序應繼續正常運行。將日誌級別降低到減少logspam會很好,但它也不應該影響應用程序的正確性。

+0

在Node方面,我和OP一樣苦惱,但答案是我沒有處理錯誤事件。修復後,我的Node應用程序將在錯誤發生後繼續運行。 –

+0

如果您在學習grpc時遇到此問題,那是因爲您沒有優雅地終止客戶端 - .channel.shutdown.awaitTermination(5,TimeUnit.SECONDS)請參閱http://comments.gmane.org/gmane.comp.lib。 GRPC/2474 –

相關問題