Chaos Monkey使用场景
使用场景(举例说明)
Chaos Monkey 是一个测试实例健壮性和弹性的工具。之前版本的 Chaos Monkey,除了支持关闭服务实例之外,还支持其他一些操作系统级别的破坏,例如提高 CPU 占用率、阻塞网络 IO、写满硬盘空间等。Chaos Monkey 2.0 移除了这些功能,只支持关闭服务实例。对于这些功能移除,Netflix 的工程师认为,这些功能应该被放到故障注入服务中进行定向注入,而不是作为 Chaos Monkey 的随机操作之一。关于故障注入,Netflix 也有一些。
以面向 spring boot 的 chaos monkey 为例,assaults 包下是四种攻击方式包括抛出自定义异常、杀掉应用、延迟攻击、内存攻击,分为两类 ChaosMonkeyRequestAssault 和 ChaosMonkeyRuntimeAssault.一个时请求攻击,另一个是应用运行时攻击(可以接口调用,也可以定时任务调用)。
调用了 AssaultException 的 throwExceptionInstance,根据配置通过反射创建异常实例
例如 yml 里配置自定义 exception,就会抛出自定义异常。
assaults:
level: 1
latencyActive: true
exceptionsActive: true
exception:
type: pl.piomin.services.customer.ZuoqiException
arguments[0]:
className: java.lang.String
value: 1
killApplicationActive: false
KillAppAssault 杀掉应用
public void attack() {
try {
LOGGER.info("Chaos Monkey - I am killing your Application!");
if (metricEventPublisher != null) {
metricEventPublisher.publishMetricEvent(MetricType.KILLAPP_ASSAULT);
}
int exit = SpringApplication.exit(context, (ExitCodeGenerator) () -> 0);
Thread.sleep(5000); // wait before kill to deliver some metrics
System.exit(exit);
} catch (Exception e) {
LOGGER.info("Chaos Monkey - Unable to kill the App, I am not the BOSS!");
}
}
LatencyAssault 延迟攻击
public void attack() {
LOGGER.debug("Chaos Monkey - timeout");
// 在配置的延迟攻击范围内随机取值
Int timeout = ThreadLocalRandom
.current()
.nextInt(settings
.getAssaultProperties()
.getLatencyRangeStart(), settings.getAssaultProperties().getLatencyRangeEnd());
atomicTimeoutGauge.set(timeout);
LOGGER.info("Chaos Monkey latency timr" + timeout);
// metrics
if (metricEventPublisher != null) {
//发布事件
metricEventPublisher.publishMetricEvent(MetricType.LATENCY_ASSAULT);
metricEventPublisher.publishMetricEvent(MetricType.LATENCY_ASSAULT, atomicTimeoutGauge);
}
try {
//线程睡眠随机数值
Thread.sleep(timeout);
} catch (InterruptedException ignored) {
}
}