生产遇到的一个异常如下:
com.alibaba.dubbo.remoting.RemotingException: Failed to send message Response [id=0, version=2.0.0, status=20, event=false, error=null, result=RpcResult [result=Fruit(name=null, color=oops), exception=null]] to /169.254.18.108:63462, cause: null
at com.alibaba.dubbo.remoting.transport.netty.NettyChannel.send(NettyChannel.java:108)
at com.alibaba.dubbo.remoting.transport.AbstractPeer.send(AbstractPeer.java:51)
at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:171)
at com.alibaba.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:52)
at com.alibaba.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:82)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.nio.channels.ClosedChannelException
at org.jboss.netty.channel.socket.nio.NioWorker.cleanUpWriteBuffer(NioWorker.java:643)
at org.jboss.netty.channel.socket.nio.NioWorker.writeFromUserCode(NioWorker.java:370)
at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.handleAcceptedSocket(NioServerSocketPipelineSink.java:137)
at org.jboss.netty.channel.socket.nio.NioServerSocketPipelineSink.eventSunk(NioServerSocketPipelineSink.java:76)
at org.jboss.netty.channel.Channels.write(Channels.java:632)
at org.jboss.netty.handler.codec.oneone.OneToOneEncoder.handleDownstream(OneToOneEncoder.java:70)
at com.alibaba.dubbo.remoting.transport.netty.NettyHandler.writeRequested(NettyHandler.java:99)
at org.jboss.netty.channel.Channels.write(Channels.java:611)
at org.jboss.netty.channel.Channels.write(Channels.java:578)
at org.jboss.netty.channel.AbstractChannel.write(AbstractChannel.java:251)
at com.alibaba.dubbo.remoting.transport.netty.NettyChannel.send(NettyChannel.java:98)
... 7 more
这个异常通常在dubbo服务中发生在服务的提供端,同时这个异常一般还会跟另外一个结对出现,在服务的消费端会发生如何错误:
Exception in thread "pool-1-thread-1" com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method getColor in the service com.alibaba.dubbo.demo.api.DemoService. Tried 1 times of the providers [169.254.18.108:20880] (1/1) from the registry 118.24.157.71:2181 on the consumer 169.254.18.108 using the dubbo version 2.5.3. Last error is: Invoke remote method timeout. method: getColor, provider: dubbo://169.254.18.108:20880/com.alibaba.dubbo.demo.api.DemoService?anyhost=true&application=consumer-of-helloworld-app&check=false&default.reference.filter=consumerTraceFilter&default.service.filter=providerTraceFilter&dubbo=2.5.3&interface=com.alibaba.dubbo.demo.api.DemoService&methods=sayHello,getColor,doValidate&pid=20632&retries=0&side=consumer&timeout=5000×tamp=1539442169002, cause: Waiting server-side response timeout by scan timer. start time: 2018-10-13 22:49:31.965, end time: 2018-10-13 22:49:36.994, client elapsed: 66 ms, server elapsed: 4962 ms, timeout: 5000 ms, request: Request [id=0, version=2.0.0, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=getColor, parameterTypes=[class com.alibaba.dubbo.demo.api.bean.Fruit], arguments=[Fruit(name=null, color=)], attachments={traceId=53feab28645f492baec2b8a185f1a1ca, path=com.alibaba.dubbo.demo.api.DemoService, interface=com.alibaba.dubbo.demo.api.DemoService, version=0.0.0, timeout=5000}]], channel: /169.254.18.108:63462 -> /169.254.18.108:20880
at com.alibaba.dubbo.rpc.cluster.support.FailoverClusterInvoker.doInvoke(FailoverClusterInvoker.java:101)
at com.alibaba.dubbo.rpc.cluster.support.AbstractClusterInvoker.invoke(AbstractClusterInvoker.java:227)
at com.alibaba.dubbo.rpc.cluster.support.wrapper.MockClusterInvoker.invoke(MockClusterInvoker.java:72)
at com.alibaba.dubbo.rpc.proxy.InvokerInvocationHandler.invoke(InvokerInvocationHandler.java:52)
at com.alibaba.dubbo.common.bytecode.proxy0.getColor(proxy0.java)
at com.alibaba.dubbo.demo.consumer.Consumer2.run(Consumer2.java:45)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.alibaba.dubbo.remoting.TimeoutException: Waiting server-side response timeout by scan timer. start time: 2018-10-13 22:49:31.965, end time: 2018-10-13 22:49:36.994, client elapsed: 66 ms, server elapsed: 4962 ms, timeout: 5000 ms, request: Request [id=0, version=2.0.0, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=getColor, parameterTypes=[class com.alibaba.dubbo.demo.api.bean.Fruit], arguments=[Fruit(name=null, color=)], attachments={traceId=53feab28645f492baec2b8a185f1a1ca, path=com.alibaba.dubbo.demo.api.DemoService, interface=com.alibaba.dubbo.demo.api.DemoService, version=0.0.0, timeout=5000}]], channel: /169.254.18.108:63462 -> /169.254.18.108:20880
at com.alibaba.dubbo.remoting.exchange.support.DefaultFuture.returnFromResponse(DefaultFuture.java:188)
at com.alibaba.dubbo.remoting.exchange.support.DefaultFuture.get(DefaultFuture.java:110)
at com.alibaba.dubbo.remoting.exchange.support.DefaultFuture.get(DefaultFuture.java:84)
at com.alibaba.dubbo.rpc.protocol.dubbo.DubboInvoker.doInvoke(DubboInvoker.java:96)
at com.alibaba.dubbo.rpc.protocol.AbstractInvoker.invoke(AbstractInvoker.java:144)
at com.alibaba.dubbo.demo.filter.ConsumerTraceFilter.invoke(ConsumerTraceFilter.java:24)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:75)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.rpc.protocol.dubbo.filter.FutureFilter.invoke(FutureFilter.java:53)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.rpc.filter.ConsumerContextFilter.invoke(ConsumerContextFilter.java:48)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.rpc.listener.ListenerInvokerWrapper.invoke(ListenerInvokerWrapper.java:74)
at com.alibaba.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:53)
at com.alibaba.dubbo.rpc.cluster.support.FailoverClusterInvoker.doInvoke(FailoverClusterInvoker.java:77)
... 8 more
以上代码为服务端的调用超时错误.
产生以上错误的主要原因是,服务端的处理时间过长,超过了客户端调用的超时时间,客户端超时抛出了“com.alibaba.dubbo.remoting.TimeoutException”,关闭了连接通道,当server端处理结束后,再次向客户端发送数据的时候,发现通道已经关闭,则抛出“java.nio.channels.ClosedChannelException”
发生这类错误我们通常处理方式,主要还是要看一下server端超时的时间是否配置合理,检查一下,为什么server端的处理时间会超过我们的预估时间(timeout),保证业务能在超时时间内完成,如果业务确实需要更多时间,可适当延长超时时间
timeout 在consumer,provider两端都可以配置,如果两端都配置的情况下,consumer端的优先级更高,所以有时候,我们看到,server端总的处理花费的时间并没有大于所配置的timeout的时间,还是出现在上述的异常,那么可以检查一下,client端是不是配置了更短的超时时间,覆盖了provider的配置所致。
另外Dubbo官方:“建议由服务提供方设置超时,因为一个方法需要执行多长时间,服务提供方更清楚,如果一个消费方同时引用多个服务,就不需要关心每个服务的超时设置。”
server端 timeout 在配置中为可选项,不配置的情况下缺省值为1000毫秒!
希望对正在解决类似问题的你有帮助!!!