Hystrix

发布时间 2023-06-15 19:56:28作者: 人在代码在

    Spring Cloud Hystrix是一款优秀的服务容错和保护组件,也是Spring Cloud的重要组件之一。

 

 

 

@HystrixCommand(fallbackMethod = "prop",commandProperties =
{
@HystrixProperty(name = "circuitBreaker.enabled",value = "true"), //开启熔断器
@HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds",value ="1000"), //统计时间窗
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"), //统计时间窗内请求次数
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"), //休眠时间窗口期
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"), 在统计时间窗口期以内,请求失败率达到 60% 时进入熔断状态

})

@Override
public User getUserById(int id) {
if(id<0)
{
throw new RuntimeException("ID为负数,用户ID不存在");
}
User user = userMapper.getUserById(id);
if(null == user)
{
throw new RuntimeException("该用户不存在");
}
return user;
}

public User prop(int id)
{
return new User().setId(id).setUsername("用户ID不存在,请您确认好信息").setAlias(null);
}