Hystrix生产者熔断

本节内容

配置生产者的Hystrix熔断

改造生产者

控制器,添加一段延时,加入熔断属性

@RestController
public class ProducerControllerImpl implements ProducerController {
	@Override
	@RequestMapping("/p")
	@HystrixCommand(fallbackMethod = "fun2", commandProperties = {
			//启用熔断
			@HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
			//超时时间
			@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000"),
			//10秒内异常次数,1次就触发熔断
			@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "1"),
			//恢复时间
			@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000")
	})
	public Student fun1(@RequestParam("age") Integer age) {
		System.out.println(age);
		try {
			TimeUnit.SECONDS.sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		if (age % 5 == 0) {
			throw new RuntimeException("人工异常");
		}
		return new Student("张三", age);
	}

	//方法声明要完全一致
	public Student fun2(@RequestParam("age") Integer age) {
		return new Student("生产者的降级", age);
	}
}

测试

只打开生产者:http://localhost:9101/p?age=1
Hystrix生产者熔断
刷新,发现会秒回,这说明熔断了。
等待一会儿之后再刷新,重新进入了延时,这说明熔断已恢复。

成功!

上一篇:php-fpm request_terminate_timeout 和php.ini的max_execution_time


下一篇:MySQL + PHP+Nginx相连接