doubb超时_dubbo超时重试

dubbo超时重试概述

consumer端和provider端都可以设置timeout。

超时优先级:consumer方法级>provider方法级>consumer接口级>provider接口级>consumer级>provider级

当consumer端调用超时,会触发重试调用。

重试对应的配置属性是retries。默认的重试次数是2。就是说,当调用超时,会最多重试2次,如果仍然失败,会提示异常。

对于查询或删除来说,接口重试是幂等的。

对于新增数据,如果retries>0,则要做幂等处理,否则会造成重复数据入库而产生bug。安全起见,可单独设置retries=0。

【说明】在直连的情况下,是不会触发重试的。

代码

Provider application.yml dubbo配置:

dubbo:

application:

name: zhanggz-dubbodemo

registry:

# address: N/A

address: zookeeper://127.0.0.1:2181

protocol:

port:28088name: dubbo

scan:

base-packages: dubbodemo.provider

provider:

timeout:2200retries:3

View Code

Provider代码:

packagedubbodemo.provider;importdubbodemo.contract.HelloWord;importlombok.extern.slf4j.Slf4j;importorg.apache.commons.lang3.RandomUtils;importorg.apache.dubbo.config.annotation.Service;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;

@Slf4j

@Service(retries= 3)

@SpringBootApplicationpublic class ProviderApplication implementsHelloWord {public static voidmain(String[] args) {

SpringApplication.run(ProviderApplication.class);

}publicString add(String str) {

log.info("add入参:{}", str);long start =System.currentTimeMillis();try{//int i=1/0;

try{int r = RandomUtils.nextInt(1000, 2000);

log.info("sleep time={}", r);

Thread.sleep(r);

}catch(InterruptedException e) {

e.printStackTrace();

}return "test retry:" +str;

}finally{

log.info("duration={}", System.currentTimeMillis() -start);

}

}publicString say() {return ("hello");

}

}

View Code

Consumer application.yml dubbo配置:

dubbo:

application:

name: zhanggz-dubbodemo-consumer

registry:

address: zookeeper://127.0.0.1:2181

# address: N/A

protocol:

name: dubbo

consumer:

timeout:1000# retries:0

View Code

Consumer代码:

packagedubbodemo.consumer;importdubbodemo.contract.HelloWord;importlombok.extern.slf4j.Slf4j;importorg.apache.dubbo.config.annotation.Reference;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.context.junit4.SpringRunner;

@Slf4j

@RunWith(SpringRunner.class)

@SpringBootTestpublic classDubboConsumerTest {

@Reference(/*url = "dubbo://localhost:28088",*/)privateHelloWord helloWord;

@Testpublic voidtestAddRetry() {long start =System.currentTimeMillis();

log.info("请求开始...");try{

String str= helloWord.add("str");

log.info("返回值={}", str);

}finally{

log.info("duration=={}", System.currentTimeMillis() -start);

}

}

}

View Code

接口定义:

packagedubbodemo.contract;public interfaceHelloWord {

String say();

String add(String str);

}

测试结论

--------consumer不设置timeout(默认值=1000ms),改变provider的timeout值

【provider】

provider:

timeout: 1000

2020-07-23 18:53:57.427 INFO 13356 --- [:20880-thread-2] dubbodemo.provider.ProviderApplication : duration=2000

2020-07-23 18:53:57.432 WARN 13356 --- [:20880-thread-2] o.apache.dubbo.rpc.filter.TimeoutFilter : [DUBBO] invoke time out. method: add arguments: [str] , url is dubbo://192.168.40.69:20880/dubbodemo.contract.HelloWord?anyhost=true&application=zhanggz-dubbodemo&bean.name=ServiceBean:dubbodemo.contract.HelloWord&bind.ip=192.168.40.69&bind.port=20880&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=dubbodemo.contract.HelloWord&methods=add,say&pid=13356&qos.enable=false&register=true&release=2.7.3&side=provider&timeout=1000&timestamp=1595501612605, invoke elapsed 2005 ms., dubbo version: 2.7.3, current host: 192.168.40.69

【provider】

provider:

timeout: 2200

2020-07-23 18:57:16.949 INFO 12280 --- [:20880-thread-2] dubbodemo.provider.ProviderApplication : duration=2000

(没有提示invoke timeout)

【consumer都会收到timeout异常】

2020-07-23 18:53:56.273 INFO 13216 --- [ main] dubbodemo.consumer.DubboConsumerTest : duration==1040

org.apache.dubbo.rpc.RpcException: Invoke remote method timeout. method: add, provider: dubbo://localhost:20880/dubbodemo.contract.HelloWord?application=zhanggz-dubbodemo-consumer&interface=dubbodemo.contract.HelloWord&lazy=false&pid=13216&qos.enable=false&register.ip=192.168.40.69&remote.application=&side=consumer&sticky=false, cause: org.apache.dubbo.remoting.TimeoutException: Waiting server-side response timeout by scan timer. start time: 2020-07-23 18:53:55.250, end time: 2020-07-23 18:53:56.272, client elapsed: 106 ms, server elapsed: 916 ms, timeout: 1000 ms, request: Request [id=0, version=2.0.2, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=add, parameterTypes=[class java.lang.String], arguments=[str], attachments={path=dubbodemo.contract.HelloWord, interface=dubbodemo.contract.HelloWord, version=0.0.0}]], channel: /192.168.40.69:1813 -> /192.168.40.69:20880

----------------------------- 服务端停止运行后,客户端无法启动

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dubbodemo.consumer.DubboConsumerTest': Injection of @Reference dependencies is failed; nested exception is org.apache.dubbo.rpc.RpcException: Fail to create remoting client for service(dubbo://localhost:20880/dubbodemo.contract.HelloWord?application=zhanggz-dubbodemo-consumer&codec=dubbo&heartbeat=60000&interface=dubbodemo.contract.HelloWord&lazy=false&pid=5236&qos.enable=false&register.ip=192.168.40.69&remote.application=&side=consumer&sticky=false&timeout=2200): client(url: dubbo://localhost:20880/dubbodemo.contract.HelloWord?application=zhanggz-dubbodemo-consumer&codec=dubbo&heartbeat=60000&interface=dubbodemo.contract.HelloWord&lazy=false&pid=5236&qos.enable=false&register.ip=192.168.40.69&remote.application=&side=consumer&sticky=false&timeout=2200) failed to connect to server localhost/127.0.0.1:20880, error message is:Connection refused: no further information: /192.168.40.69:20880

----------------------------- consumer端设置dubbo.consumer.check=false。则可以启动consumer服务。不过,当调用远程服务的时候,由于远程服务处于停止状态,所以会报错。org.apache.dubbo.rpc.RpcException: Failed to invoke remote method: add, provider: dubbo://localhost:20880/dubbodemo.contract.HelloWord?application=zhanggz-dubbodemo-consumer&check=false&interface=dubbodemo.contract.HelloWord&lazy=false&pid=12504&qos.enable=false&register.ip=192.168.40.69&remote.application=&side=consumer&sticky=false&timeout=2200, cause: message can not send, because channel is closed . url:dubbo://localhost:20880/dubbodemo.contract.HelloWord?application=zhanggz-dubbodemo-consumer&check=false&codec=dubbo&heartbeat=60000&interface=dubbodemo.contract.HelloWord&lazy=false&pid=12504&qos.enable=false&register.ip=192.168.40.69&remote.application=&side=consumer&sticky=false&timeout=2200

----------------------------------

consumer端,设置retries=3

2020-07-24 17:12:57.389 INFO 7468 --- [ main] dubbodemo.consumer.DubboConsumerTest : 请求开始...

2020-07-24 17:13:00.202 INFO 7468 --- [ main] dubbodemo.consumer.DubboConsumerTest : 返回值=test retry:str

2020-07-24 17:13:00.202 INFO 7468 --- [ main] dubbodemo.consumer.DubboConsumerTest : duration==2813

provider端:最多会被执行1+retries=4次

2020-07-24 17:12:57.540 INFO 2372 --- [28088-thread-17] dubbodemo.provider.ProviderApplication : add入参:str

2020-07-24 17:12:57.541 INFO 2372 --- [28088-thread-17] dubbodemo.provider.ProviderApplication : r=1169

2020-07-24 17:12:58.444 INFO 2372 --- [28088-thread-18] dubbodemo.provider.ProviderApplication : add入参:str

2020-07-24 17:12:58.444 INFO 2372 --- [28088-thread-18] dubbodemo.provider.ProviderApplication : r=1934

2020-07-24 17:12:58.711 INFO 2372 --- [28088-thread-17] dubbodemo.provider.ProviderApplication : duration=1170

2020-07-24 17:12:59.461 INFO 2372 --- [28088-thread-19] dubbodemo.provider.ProviderApplication : add入参:str

2020-07-24 17:12:59.461 INFO 2372 --- [28088-thread-19] dubbodemo.provider.ProviderApplication : r=736

2020-07-24 17:13:00.198 INFO 2372 --- [28088-thread-19] dubbodemo.provider.ProviderApplication : duration=737

2020-07-24 17:13:00.379 INFO 2372 --- [28088-thread-18] dubbodemo.provider.ProviderApplication : duration=1935

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值