写点什么

Dubbo 的超时和重试

  • 2022 年 5 月 09 日
  • 本文字数:2325 字

    阅读完需:约 8 分钟

<dubbo:consumer timeout="4000" > 全局配置


<dubbo:reference timeout="3000" ...> 接口类配置


<dubbo:method timeout="2000" ...> 方法配置


  • 基于注解方式的超时配置

  • 服务提供者注解方式配置


@Slf4j


@DubboService(timeout = 5000)


public class TestDubboTimeOutFacadeImpl implements TestDubboTimeOutFacade {


@Override


public void sendMessage(String param) {


try {


Thread.sleep(6000);


} catch (InterruptedException e) {


e.printStackTrace();


}


System.out.println("_________________服务提供者设置的超时时间是 5s");


}


}


  • 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 服务消费者注解方式配置


@DubboReference(timeout = 3000)


private TestDubboTimeOutFacade testDubboTimeOutFacade;


[](()服务超时设置的代码实例


======================================================================


[](()场景一:服务消费者设置超时时间 3 秒,服务提供者设置超时时间 5 秒




服务提供者注解方式配置


@Slf4j


@DubboService(timeout = 5000)


public class TestDubboTimeOutFacadeImpl implements TestDubboTimeOutFacade {


@Override


public void sendMessage(String param) {


try {


Thread.sleep(6000);


} catch (InterruptedException e) {


e.printStackTrace();


}


System.out.println("_________________服务提供者设置的超时时间是 5s");


}


}


服务消费者注解方式配置


@DubboReference(timeout = 3000)


private TestDubboTimeOutFacade testDubboTimeOutFacade;


执行结果如下:


消费者端报错:


Caused by: java.util.concurrent.ExecutionException: org.apache.dubbo.remoting.TimeoutException: Waiting server-side response timeout by scan timer. start time: 2022-01-06 15:06:59.482, end time: 2022-01-06 15:07:02.513, client elapsed: 0 ms, server elapsed: 3031 ms, timeout: 3000 ms, request: Request [id=92, version=2.0.2, twoway=true, event=false, broken=false, data=null], channel: /127.0.0.1:50076 -> /127.0.0.1:7890


at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:357)


at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1928)


at org.apache.dubbo.rpc.AsyncRpcResult.get(AsyncRpcResult.java:181)


at org.apache.dubbo.rpc.protocol.AsyncToSyncInvoker.invoke(AsyncToSyncInvoker.java:61)


at org.apache.dubbo.rpc.listener.ListenerInvokerWrapper.invoke(ListenerInvokerWrapper.java:78)


at com.dahai.video.dubbotrace.DubboTransferTraceIdFilter.invoke(DubboTransferTraceIdFilter.java:43)


at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83)


at org.apache.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:89)


at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83)


at org.apache.dubbo.rpc.protocol.dubbo.filter.FutureFilter.invoke(FutureFilter.java:51)


at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83)


at com.dahai.video.dubbotrace.DubboTransferUserLidFilter.invoke(DubboTransferUserLidFilter.java:33)


at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83)


at org.apache.dubbo.rpc.filter.ConsumerContextFilter.invoke(ConsumerContextFilter.java:69)


at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:83)


at org.apache.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:56)


at org.apache.dubbo.rpc.cluster.support.FailoverClusterInvoker.doInvoke(FailoverClusterInvoker.java:82)


... 107 more


Caused by: org.apache.dubbo.remoting.TimeoutException: Waiting server-side response timeout by scan timer. start time: 2022-01-06 15:06:59.482, end time: 2022-01-06 15:07:02.513, client elapsed: 0 ms, server elapsed: 3031 ms, timeout: 3000 ms, request: Request [id=92, version=2.0.2, twoway=true, event=false, broken=false, data=null], channel: /127.0.0.1:50076 -> /127.0.0.1:7890


at org.apache.dubbo.remoting.exchange.support.DefaultFuture.doReceived(DefaultFuture.java:210)


at org.apache.dubbo.remoting.exchange.support.DefaultFuture.received(DefaultFuture.java:175)


at org.apache.dubbo.remoting.exchange.support.DefaultFuture$TimeoutCheckTask.notifyTimeout(DefaultFuture.java:298)


at org.apache.dubbo.remoting.exchange.support.DefaultFuturerun$0(DefaultFuture.java:285)


at org.apache.dubbo.common.threadpool.ThreadlessExecutor.waitAndDrain(ThreadlessExecutor.java:93)


at org.apache.dubbo.rpc.AsyncRpcResult.get(AsyncRpcResult.java:179)


... 121 more


服务提供者被重试 3 次



[](()场景二:服务消费者设置超时时间 5 秒,服务提供者设置超时时间 3 秒




服务提供者注解方式配置


@Slf4j


@DubboService(timeout = 3000)


public class TestDubboTimeOutFacadeImpl implements TestDubboTimeOutFacade {


@Override


public void sendMessage(String param) {


try {


Thread.sleep(6000);


} catch (InterruptedException e) {


e.printStackTrace();


}


System.out.println("_________________服务提供者设置的超时时间是 5s");


}


}

用户头像

还未添加个人签名 2022.04.13 加入

还未添加个人简介

评论

发布
暂无评论
Dubbo的超时和重试_Java_爱好编程进阶_InfoQ写作社区