【3】JMicro微服务-服务超时,重试,重试间隔

接下来的内容都基于【2】JMicro微服务-Hello World做Demo

微服务中,超时和重试是一个最基本问题下面Demo JMicro如何实现。

  

@Service(maxSpeed=-1,baseTimeUnit=Constants.TIME_SECONDS)
@Component
public class SimpleRpcImpl implements ISimpleRpc {

    @Override
    @SMethod(
        timeout=3000,
        retryInterval=1000,
        retryCnt=3
    )
    public String hello(String name) {
        System.out.println("Server hello: " +name);
              try {
                    Thread.sleep(3000);//让服务超时,看看消费端是什么结果,可以分别设置1000,2000,3000,4000做测试
                        } catch (InterruptedException e) {
                        e.printStackTrace();
                 }

        return "Server say hello to: "+name;
    }

}

在服务提供者中配置SMethod的timeout,retryInterval,retryCnt三个属性即可。

timeout:表示超时时间,单位毫秒,小于或等于0表示不启用超时,大于0表示启用超时;

retryInterval:表示超时后,重试间隔,单位毫秒

retryCnt:表示重试次数,不包括第一次,如设置3,如果第一次超时,第2次成功,则总共请求了2次,同理,全部超时时,总共请求了4次,4次超时后,RPC失败,抛出TimeoutException异常。

注意:

1. 服务启用超时重试,必须确保服务是幂等的,所谓“幂等”就是RPC方法调用N次“效果”是一样的,不会造成系统不一致的问题出现,否则不能启用超时重试机制,即retryCnt只能等于或小时0;

2. 请思考)当服务熔断时,超时重试自动失效?

3. 请思考)服务限流时,如何处理超时重试?

猜你喜欢

转载自www.cnblogs.com/jmicro/p/10573273.html
今日推荐