使用Spring Boot的混乱猴子的方法
总结
我发现了一个类似于Netflix Chaos Monkey的名为Chaos Monkey for Spring Boot的工具,它可以随机产生错误。于是我决定试用一下。
文件
解说视频
在公式文档中可以找到。
github
解释
Spring Boot的混沌猴能够随机引发以下三种错误
-
- 速度遅延
-
- 例外発生
- Spring Bootの停止
其他主要功能如下:
-
- 障害を発生させる頻度(10%単位)を指定できる
-
- 速度遅延時間の範囲(単位ms)を指定できる
-
- 障害を発生させるBeanの種類(@Controller, @Service, @Componentなど)を指定できる
- Chaos Monkeyの有効/無効や設定は、actuator経由で変更できる
嵌入方式 ɡrù ɡshì)
Spring Boot的混沌猴子可以通过以下三个步骤进行引入。
-
- pomに依存追加
-
- active profilesの指定
- applicationプロパティーの設定追加
添加依赖到pom文件
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>chaos-monkey-spring-boot</artifactId>
<version>2.0.0</version>
</dependency>
如果您使用SNAPSHOT,请参考文档中的Snapshots。
活跃档案的设定
将active profiles指定为chaos-monkey。
如果要在application.properties中指定:
spring.profiles.active=chaos-monkey
如果在Spring Boot启动时需要指定的话,可以使用以下方法:
java -jar your-app.jar --spring.profiles.active=chaos-monkey
添加application属性的设置
可以通过使用actuator来更改,将chaos.monkey.enabled配置到application.properties文件中。
在application.properties文件中:
chaos.monkey.enabled=true
如果是application.xml文件:
chaos:
monkey:
enabled: true
如果通过执行器进行设置的话:
如果要使用actuator,请参考下一章的”使用actuator进行配置确认和更改”。
通过以上三个设置(pom 、active profiles和application.properties),Chaos Monkey将被启用。
请确认启动
当启动Spring Boot时,启动日志中会显示以下的AA。
_____ _ __ __ _
/ ____| | | \/ | | |
| | | |__ __ _ ___ ___ | \ / | ___ _ __ | | _____ _ _
| | | '_ \ / _` |/ _ \/ __| | |\/| |/ _ \| '_ \| |/ / _ | | | |
| |____| | | | (_| | (_) \__ \ | | | | (_) | | | | | __| |_| |
\_____|_| |_|\__,_|\___/|___/ |_| |_|\___/|_| |_|_|\_\___|\__, |
__/ |
_ready to do evil! |___/
:: Chaos Monkey for Spring Boot ::
提示
改变发生障碍的频率
障害的发生频率可以通过一个属性(level)进行设定。障害发生率可以用1-10的数字,每10%为一个单位,进行指定。
- 1は発生率10%、10は発生率100%
请选择发生故障的类型。
-
- 遅延の障害は、遅延範囲開始(latencyRangeStart)と遅延範囲停止(latencyRangeEnd)に指定したミリ秒の数値の範囲でランダムで遅延が発生します。中身はThread.sleepです。
-
- 例外の障害は、RuntimeException(“Chaos Monkey – RuntimeException”)を発生させます。
- アプリケーションダウンは、Spring Boot ApplicationをexitCode0で終了させます。
导致障碍发生的地方
在watcherProperties中可以指定导致故障的位置。我们通过扫描带有以下注解的类来选择导致故障的目标。
如果Watcher的所有设定都是false,那么请注意Chaos Monkey将不会运行。
使用动作器进行设置确认和设置变更。
启用actuator功能
可以通过添加以下配置来通过执行器更改Chaos Monkey的设置。
如果是application.properties的情况下:
management.endpoint.chaosmonkey.enabled=true
management.endpoints.web.exposure.include=chaosmonkey
如果是application.xml文件:
management:
endpoint:
chaosmonkey:
enabled: true
endpoints:
web:
exposure:
include: chaosmonkey
确认一下Chaos Monkey的设置。
指令
curl http://localhost:8080/actuator/chaosmonkey
结果
答案
{
"chaosMonkeyProperties": {
"enabled": false
},
"assaultProperties": {
"level": 5,
"latencyRangeStart": 1000,
"latencyRangeEnd": 3000,
"latencyActive": true,
"exceptionsActive": false,
"killApplicationActive": false
},
"watcherProperties": {
"controller": false,
"restController": false,
"service": true,
"repository": false,
"component": true
}
}
启用混沌猴
如果”chaosMonkeyProperties.enabled”被设置为false,那么Chaos Monkey将被禁用。你可以通过actuator将其修改为true。
{
"chaosMonkeyProperties": {
"enabled": false
},
...
}
命令 (Ming4 ling4)
curl -X POST -H "Content-type: application/json" http://localhost:8080/actuator/chaosmonkey/enable
请注意POST请求的类型
请注意Content-type设置为application/json
结果 –
Alternatively:
最终的成果 – de .
Chaos Monkey is enabled
修改其他项目(袭击)。
其他项目也可以通过执行器进行更改。以下是更改“Assault”的示例。
指令
curl -X POST -H "Content-type: application/json" http://localhost:8080/actuator/chaosmonkey/assaults -d '{
"level": 5,
"latencyRangeStart": 1000,
"latencyRangeEnd": 3000,
"latencyActive": true,
"exceptionsActive": false,
"killApplicationActive": false
}'
结果
As an AI language model, I can provide multiple options for paraphrasing the word “结果” in Chinese:
– 成果
– 完成的效果 de
– 终局
– 输出
– 后果
Please let me know if you would like more options or if you have any specific requirements.
Assault config has changed
总结
上述是Chaos Monkey for Spring Boot的简易使用方法。