初窥熔断器
熔断器的作用
- 对来自依赖的延迟和故障进行防护和控制——这些依赖通常都是通过网络访问的
- 阻止故障的连锁反应
- 快速失败并迅速恢复
- 回退并优雅降级
- 提供近实时的监控与告警())
Hystrix的方法
execute()和queue() 适用于HystrixCommand对象,而observe()和toObservable()适用于HystrixObservableCommand对象
- excute:以同步堵塞方式执行run()
- queue:以异步非阻塞方式执行run()
- oberve:这个是订阅模式。调用observe()自动触发执行run()/construct(),无论是否存在订阅者(异步模式)可以发送多条数据
- toObervable:这个是订阅模式。调用toObservable()不会立即触发执行run()/construct(),必须有订阅者订阅Observable时才会执行。(异步模式)可以发送多条数据
代码示例
java代码
import com.netflix.hystrix.*;
import java.util.Random;
public class MyHystrixDemo extends HystrixCommand<String> {
public static String groupKey = "HystrixCommand";
public static String threadKey = "ThreadPool";
protected MyHystrixDemo(String commandKey) {
/**
* 设置熔断相关
*CircuitBreaker是熔断器,所有相关配置项都是设置这个的
*withExecutionTimeoutInMilliseconds这个是设置的超时时间,限流的一部分
* 最最重要的withCircuitBreakerSleepWindowInMilliseconds设置断路器的回复时间,
*/
// HystrixCommandProperties.Setter commandPorperties=HystrixCommandProperties.Setter().withCircuitBreakerErrorThresholdPercentage(50)
// .withExecutionTimeoutEnabled(true)
// .withExecutionTimeoutInMilliseconds(5000)
// .withCircuitBreakerSleepWindowInMilliseconds(5000);
// HystrixThreadPoolProperties.Setter threadPoolProperties=HystrixThreadPoolProperties.Setter().withCoreSize(10);
super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(groupKey))
.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey))
.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey(threadKey))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
// .withCircuitBreakerErrorThresholdPercentage(50)
.withExecutionTimeoutEnabled(true)
.withExecutionTimeoutInMilliseconds(2000)
.withCircuitBreakerRequestVolumeThreshold(1)
// .withCircuitBreakerErrorThresholdPercentage(1)
.withCircuitBreakerSleepWindowInMilliseconds(50000))
.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter().withCoreSize(10)));
}
@Override
public String getFallback() {
return "Error:熔断";
}
@Override
protected String run() throws Exception {
Random random = new Random();
Integer time = random.nextInt(10);
System.out.println("Time :" + time.toString());
Thread.sleep(time * 1000);
return "Hello World!";
}
}
spring的代码
//注解实现Hystrix功能
@EnableHystrix
//注解实现熔断
@Component
public class HelloApiProxy {
// @HystrixCommand(commandProperties={
// @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="500"),
// @HystrixProperty(name="execution.timeout.enabled",value="true")
// }, fallbackMethod = "fallback")
@HystrixCommand(commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "500")
},threadPoolProperties = {
@HystrixProperty(name = "coreSize", value = "10"),
@HystrixProperty(name = "maxQueueSize", value = "20"),
@HystrixProperty(name = "keepAliveTimeMinutes", value = "0"),
@HystrixProperty(name = "queueSizeRejectionThreshold", value = "15"),
@HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "12"),
@HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "1440")
}, fallbackMethod = "fallback")
public String getString(){
Random random=new Random();
Integer time = random.nextInt(10);
try {
Thread.sleep(time*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Time:"+time.toString()+" helloApiProxy";
}
public String fallback(){
return "fallback";
}
}
protected String run() throws Exception {
Random random = new Random();
Integer time = random.nextInt(10);
System.out.println("Time :" + time.toString());
Thread.sleep(time * 1000);
return "Hello World!";
}
}
注解的实现原理
@AutoConfiguration扫描到Hystrix的包,然后通过factory的包去自动化注入Configure;在configure中有关于HystrixCommand的切面,然后通过反射去实现了相关的功能
Hystrix注解相关的官网:https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica#configuration
乱言:
这礼拜很忙,学了很多;但是能写下来的很少。
这礼拜学习了一下aliyun相关的服务,edas(之前用过),云效(之前没用过现在熟悉一下),rocketMQ也看了一下但是还是不是很理解。
spring boot相关的内容:理解了一下jvm的内存模型,看了一下内存溢出和内存泄漏的相关内容,Hystrix
我去,感觉这礼拜很忙学了很多;为啥写下来也就这么点,还要继续努力了。哎。。。任重而道远,全怪阿里爸爸把东西封装了那么多,导致能学的很少,只要用就行了;感觉不努力就随时失业了。