服务熔断Hystrix-3(服务消费者Hystrix测试)
·
服务熔断Hystrix-3(服务消费者Hystrix测试)
hystrix 默认超过时间是1000毫秒,如果你后端的响应时间超过此时间,就会触发断路器。
测试环境 2个提供者 1个消费者 一个注册中心
1.修改hystrix 默认的超时时间: 3秒我这里修改的
@RequestMapping("/web/hystrix")
//加入熔断器注解 对这个方法
// fallbackMethod 如果出现短路 就返回error方法
@HystrixCommand(fallbackMethod = "error",commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
})
public String hystrix(){
//调用springCloud 服务者 提供的服务
/**
* 使用url进行调用但是使用了 Eureka后 使用服务名来调用
*/
return restTemplate.getForEntity("http://01-SPRINGCLOUD-SERVICE-PROVIDER/service/hello",String.class).getBody();
}
2.在1个服务提供者中 添加睡眠
@RequestMapping("/service/hello")
public String hello(){
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("服务提供者---- 1");
return "test";
}
3.测试结果 (负载均衡使用轮转默认策略)
第一次测试

第二次测试 (3秒后出现error)

更多推荐
所有评论(0)