dirtyhand-tester ChaosToolkit in Spring Boot 项目使用介绍

simonpatrick · May 21, 2019 · 322 hits

Spring/Springboot 是使用非常广泛的JAVA框架,所以通过使用chaostoolkit的插件chaostoolkit-spring
来进行springboot restful api的混沌工程实验.

Install Chaostoolkit-Spring

pip install -U chaostoolkit-spring

Chaostookit在Springboot 项目中的使用

首先这个实验依赖于chaos-monkey-spring-boot 这个项目,chaos toolkit通过具有这个lib报的springboot项目交换。
这个交互主要是通过spring boot actuator HTTP endpoints进行。

所以在进行chaos实验之前,我们先添加一个springboot的项目来做模拟:创建spring 项目不具体展开, 几个关键点是:

  • pom.xml 添加依赖/或者启动jar包的时候把chaos-monkey-spring-boot添加
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>chaos-monkey-spring-boot</artifactId>
<version>2.0.2</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</exclusion>
</exclusions>
</dependency>
  • application.properties
spring.profiles.active=chaos-monkey
chaos.monkey.enabled=true

chaos.monkey.watcher.controller=false
chaos.monkey.watcher.restController=true
chaos.monkey.watcher.service=true
chaos.monkey.watcher.repository=false
  • 创建helloworld controller
@GetMapping("/HelloWorld/{id}")
public BaseResponse getHelloWorld(@PathVariable Long id) {
return BaseResponse.OK().data(HelloWorldDTO.builder().id(id).msg("Hello World").build());

}


@PostMapping("/HelloWorld")
public BaseResponse<HelloWorldDTO> createHelloWorld(@RequestBody HelloWorldDTO requestBody) {
return BaseResponse.OK().data(requestBody);
}

@PutMapping("/HelloWorld/{id}")
public BaseResponse<HelloWorldDTO> updateHelloWorld(@PathVariable Long id, @RequestBody HelloWorldDTO updateData) {

return BaseResponse.OK().data(updateData);
}

@DeleteMapping("/HelloWorld/{id}")
public BaseResponse<HelloWorldDTO> deleteHelloWorld(@PathVariable Long id) {
return BaseResponse.OK().data(HelloWorldDTO.builder().id(id).msg("deleted!").build());
}
  • 启动springboot 服务,访问actuator接口

GET: http://localhost:8080/actuator/chaosmonkey

如果有返回说明设置成功了:

{
"chaosMonkeyProperties": {
"enabled": true
},
"assaultProperties": {
"level": 5,
"latencyRangeStart": 1000,
"latencyRangeEnd": 3000,
"latencyActive": true,
"exceptionsActive": false,
"exception": {
"type": null,
"arguments": null
} ......
}
}

choas experiment 设置

spring experiment:

{
"version": "1.0.0",
"title": "springboot-chaos-monkey try",
"description": "springboot chaos monkey usage",
"tags": [
"springboot"
],
"steady-state-hypothesis": {
"title": "Application responds",
"probes": [

{
"type": "probe",
"name": "call localhost url hello world",
"tolerance": 200,
"provider": {
"type": "http",
"timeout": 3,
"url": "http://localhost:8080/HelloWorld/1"
}
}
]
},
"method": [
{
"name": "configure_assaults",
"provider": {
"arguments": {
"base_url": "http://localhost:8080/actuator",
"assaults_configuration": {
"level": 5,
"latencyRangeStart": 2000,
"latencyRangeEnd": 5000,
"latencyActive": true,
"exceptionsActive": false,
"killApplicationActive": true,
"restartApplicationActive": false
}
},
"func": "change_assaults_configuration",
"module": "chaosspring.actions",
"type": "python"
},
"type": "action"
}
],
"rollbacks": [
{
"name": "disable_chaosmonkey",
"provider": {
"arguments": {
"base_url": "http://localhost:8080/actuator"
},
"func": "disable_chaosmonkey",
"module": "chaosspring.actions",
"type": "python"
},
"type": "action"
}
]
}

结果:

(chaos-toolkits)   chaos-toolkits git:(master)  chaos run spring-experiment.json
[2019-05-21 14:38:00 INFO] Validating the experiment's syntax
[2019-05-21 14:38:00 INFO] Experiment looks valid
[2019-05-21 14:38:00 INFO] Running experiment: springboot-chaos-monkey try
[2019-05-21 14:38:00 INFO] Steady state hypothesis: Application responds
[2019-05-21 14:38:00 INFO] Action: enable_chaosmonkey
[2019-05-21 14:38:00 CRITICAL] Steady state probe 'enable_chaosmonkey' is not in the given tolerance so failing this experiment
[2019-05-21 14:38:00 INFO] Let's rollback...
[2019-05-21 14:38:00 INFO] Rollback: disable_chaosmonkey
[2019-05-21 14:38:00 INFO] Action: disable_chaosmonkey
[2019-05-21 14:38:00 INFO] Experiment ended with status: failed

chaos-spring 源码说明

chaos-spring 代码分析, 实际上chaos-spring 代码不多,很容易理解

  tree .
.
├── __init__.py
├── actions.py
├── api.py
└── probes.py

实际上总过就是这三个文件.

api: 文件是访问api chaos-monkey的接口调用
probes: 用来定义probes的方法,下面的例子这个就可以在experiment.json中定义了 assaults_configuration可以使用在

```python

def assaults_configuration(base_url: str,
headers: Dict[str, Any] = None,
timeout: float = None,
configuration: Configuration = None,
secrets: Secrets = None) -> Dict[str, Any]:
"""
Get the current assaults configuraton from the specified service.
"""


response = api.call_api(base_url=base_url,
api_endpoint="chaosmonkey/assaults",
headers=headers,
timeout=timeout,
configuration=configuration,
secrets=secrets)

if response.status_code != codes.ok:
raise FailedActivity(
"ChaosMonkey assaults enquiry failed: {m}".format(m=response.text))

return response.json()
  • actions: 基本上和probes一样,也是方法名和变量组合成一次函数调用

简单使用spring的这个介绍就到这里,后续会进行说明如何使用actions/probes原理的介绍.

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
No Reply at the moment.
需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up