Hystrix降级时间与ribbon超时时间设置

一、项目情况

1、调用方shop-business的rest接口

@ApiOperation(value = "商品详情", httpMethod = "GET")
@RequestMapping(value="/get/{id}", method = RequestMethod.GET)
public String get(@PathVariable String id) {
		try {
			System.out.println(LocalTime.now()+"---AA开始---"+id);
			String us = userFeignService.getUser(id);
			System.out.println(LocalTime.now()+"---AA结束---"+us);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return id+"";
	}

1.1 UserCenterService,对应第三方系统的接口,需要处理加降级的接口

public interface UserCenterService {
      @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
      public String getUser(@PathVariable("id") String id);
}

1.2 feign接口UserCenterFeignService

@FeignClient(name="shop-user-center", fallback =   
 UserCenterFeignFallback.class)
 public interface UserCenterFeignService extends UserCenterService{
 }

1.3 UserCenterFeignFallback 降级处理类实现

@Component
public class UserCenterFeignFallback implements UserCenterFeignService{
	@Override
	public String getUser(String id) {
		System.out.println(LocalTime.now() + "---AA降级---" + id);
		return "getUser降级";
	}
}

1.4 配置文件

#降级触发时间(方法调用3秒后,还未返回,则执行降级)
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=3000
#ribbon读取数据的超时时间,如果超时就会触发重试
shop-user-center.ribbon.ReadTimeout=4000
shop-user-center.ribbon.ConnectTimeout=3000

2、被调用方:
2.1 shop-user-center的rest接口

@RequestMapping(value = "/get/{id}" ,method = RequestMethod.GET)
	public String getUser(@PathVariable Integer id) {
		try {
			System.out.println(LocalTime.now()+"---BB开始----" + id);
			TimeUnit.MILLISECONDS.sleep(5300L);
			System.out.println(LocalTime.now()+"---BB结束----" + id);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return id+"";
	}

二、测试情况

1.执行测试

http://localhost:8001/api/v1/business/add/2133333

2.调用方shop-business的日志情况

15:39:16.203---AA开始---11
15:39:19.207---AA降级---11
15:39:19.208---AA结束---降级

3.被调用方shop-user-center的日志情况

15:39:16.208---BB开始----11
15:39:20.214---BB开始----11
15:39:21.509---BB结束----11
15:39:25.516---BB结束----11

4、按时间把两个服务的日志排序

15:39:16.203---AA开始---11
15:39:16.208---BB开始----11
15:39:19.207---AA降级---11
15:39:19.208---AA结束---降级
15:39:20.214---BB开始----11
15:39:21.509---BB结束----11
15:39:25.516---BB结束----11

根据上面的日志可以得出:
a.调用方收到请求的时间,第1个时间:15:39:16.203。
b.调用方开始调用第三方,第三方接口收到的时间,第2个时间:15:39:16.208。
c.在时间达到hystrix的降级时间3秒后(3秒 = 第3个时间 - 第1个时间 ),第3个时间:15:39:19.207触发降级。
d.在到达ribbon超时时间ReadTimeout=4000后(4秒 = 第5个时间 - 第1个时间 ),开始重试,并打印出第5条日志。

三、总结

1、hystrix触发熔断与ribbon的重试在机制上没关系,ribbon该重试还是会重试。
2、如hystrix的超时时间大于ribbon的超时时间,业务调用方会在业务返回前熔断,导致业务处理失败。如果有重试,还会使得被调用系统做无用且重复的业务。
3、ribbon超时时间至少应小于ystrix的超时时间

四、问题

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值