1. Introduction
1.绪论
In this tutorial, we’re going to talk about Chaos Monkey for Spring Boot.
在本教程中,我们将讨论Spring Boot的Chaos Monkey。
This tool helps us introduce some of the principles of chaos engineering into our Spring Boot web applications by adding latency to our REST endpoints, throwing errors, or even killing an app.
这个工具可以帮助我们将混沌工程的一些原则引入我们的Spring Boot Web应用程序,为我们的REST端点增加延迟,抛出错误,甚至杀死一个应用程序。
2. Setup
2.设置
To add Chaos Monkey to our application, we need a single Maven dependency in our project:
要将Chaos Monkey添加到我们的应用程序中,我们需要在我们的项目中设置一个Maven依赖项。
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>chaos-monkey-spring-boot</artifactId>
<version>2.0.0</version>
</dependency>
3. Configuration
3.配置
Once we have the dependency setup in our project, we need to configure and start our chaos.
一旦我们在项目中设置了依赖关系,我们就需要配置并启动我们的混乱局面。
We can do this in a couple of ways:
我们可以通过几种方式做到这一点。
- At application startup, using chaos-monkey spring profile (recommended)
- Using chaos.monkey.enabled=true property
By starting the application with chaos-monkey spring profile we don’t have to stop and start the application if we want to enable or disable it while our app is running:
通过使用chaos-monkey spring profile启动应用程序,如果我们想在应用程序运行时启用或禁用它,我们不必停止和启动应用程序。
java -jar your-app.jar --spring.profiles.active=chaos-monkey
Another useful property is management.endpoint.chaosmonkey.enabled. Setting this property to true will enable the management endpoint for our Chaos Monkey:
另一个有用的属性是management.endpoint.chaosmonkey.enabled。将这个属性设置为 “true “将为我们的混沌猴启用管理端点。
http://localhost:8080/chaosmonkey
From this endpoint, we can see our library’s status. Here is the full list of endpoints and their description that will help change configuration, enable or disable Chaos Monkey and other more granular controls.
从这个端点,我们可以看到我们库的状态。这里是完整的端点列表及其描述,这将有助于改变配置、启用或禁用Chaos Monkey以及其他更精细的控制。
Using all the available properties, we can have a more fine-grained control over what happens in our generated chaos.
使用所有可用的属性,我们可以对我们生成的混沌中发生的事情进行更精细的控制。
4. How Does It Work
4.它是如何工作的
Chaos Monkey consists of Watchers and Assaults. A Watcher is a Spring Boot component. It makes use of Spring AOP to see when a public method is executed in classes annotated with the following Spring annotations:
Chaos Monkey由Watcher和Assaults组成。观察者是一个Spring Boot组件。它利用Spring AOP来查看带有以下Spring注解的类中的公共方法何时被执行。
- Component
- Controller
- RestController
- Service
- Repository
Based on the configuration in our app properties file, our public methods will be either assaulted or not, by one of the following:
根据我们应用程序属性文件中的配置,我们的公共方法将被攻击或不被攻击,由以下之一。
- Latency Assault – adds random latency to the request
- Exception Assault – throws random Runtime Exception
- AppKiller Assault – um, the app dies
Let’s take a look at how we can configure our watcher and assaults for a more controlled assault.
让我们来看看我们如何配置我们的观察者和攻击者,以实现更可控的攻击。
5. Watcher
5.观察者
By default, Watcher is only enabled for our services. This means that our assaults will be executed only for public methods in our classes annotated with @Service.
默认情况下,Watcher只对我们的services启用。这意味着我们的攻击将只对我们的类中用@Service.注释的公共方法执行。
But we can easily change that by configuring properties:
但我们可以通过配置属性轻松地改变这一点。
chaos.monkey.watcher.controller=false
chaos.monkey.watcher.restController=false
chaos.monkey.watcher.service=true
chaos.monkey.watcher.repository=false
chaos.monkey.watcher.component=false
Keep in mind that once the application started, we cannot dynamically change the watcher using the Chaos Monkey for Spring Boot management port that we talked about earlier.
请记住,一旦应用程序启动,我们就不能使用我们前面谈到的Chaos Monkey for Spring Boot管理端口动态地改变观察者。
6. Assaults
6.攻击行为
Assaults are basically scenarios that we want to test in our application. Let’s take each type of attack and see what it does and how we can configure it.
攻击基本上是我们想在我们的应用程序中测试的情景。让我们来看看每种类型的攻击,看看它的作用以及我们如何配置它。
6.1. Latency Assault
6.1.延迟攻击
This type of attack adds latency to our calls. This way our application responds slower and we can monitor how it behaves when for example the database responds slower.
这种类型的攻击为我们的调用增加了延迟。这样一来,我们的应用程序的响应速度就会变慢,我们可以监控它的行为,例如,当数据库的响应速度变慢时。
We can configure and turn on or of this type of attack using the properties file of our app:
我们可以使用我们应用程序的属性文件来配置和打开或这种类型的攻击。
chaos.monkey.assaults.latencyActive=true
chaos.monkey.assaults.latencyRangeStart=3000
chaos.monkey.assaults.latencyRangeEnd=15000
Another way to configure and switch on and off this type of attack is through the management endpoint of Chaos Monkey.
另一种配置和开关这种类型的攻击的方法是通过Chaos Monkey的管理端点。
Let’s turn on the latency attack and add a range of latency between two and five seconds:
让我们打开延迟攻击,并在2到5秒之间添加一个延迟范围。
curl -X POST http://localhost:8080/chaosmonkey/assaults \
-H 'Content-Type: application/json' \
-d \
'
{
"latencyRangeStart": 2000,
"latencyRangeEnd": 5000,
"latencyActive": true,
"exceptionsActive": false,
"killApplicationActive": false
}'
6.2. Exception Assault
6.2.例外 殴打
This tests how well our application can handle exceptions. Based on configuration it will throw a random Runtime Exception once enabled.
这将测试我们的应用程序处理异常的能力。基于配置,一旦启用,它将随机抛出一个运行时异常。
We can enable it using a curl call similar to our latency assault:
我们可以使用类似于我们的延迟攻击的curl调用来启用它。
curl -X POST http://localhost:8080/chaosmonkey/assaults \
-H 'Content-Type: application/json' \
-d \
'
{
"latencyActive": false,
"exceptionsActive": true,
"killApplicationActive": false
}'
6.3. AppKiller Assault
6.3.AppKiller Assault
This one, well, our app will die at some random point. We can enable or disable it with a simple curl call like the previous two types of assault:
这个,好吧,我们的应用程序会在某个随机点死亡。我们可以像前两种攻击方式一样,通过简单的curl调用来启用或禁用它。
curl -X POST http://localhost:8080/chaosmonkey/assaults \
-H 'Content-Type: application/json' \
-d \
'
{
"latencyActive": false,
"exceptionsActive": false,
"killApplicationActive": true
}'
7. Conclusion
7.结语
In this article, we talked about Chaos Monkey for Spring Boot. We’ve seen that it takes some of the principles of chaos engineering and enables us to apply them to a Spring Boot application.
在这篇文章中,我们谈到了用于 Spring Boot 的 Chaos Monkey。我们看到,它采用了混沌工程的一些原则,并使我们能够将其应用于Spring Boot应用程序。
As always, the full code of the examples can be found over on Github.
一如既往,可以在Github上找到这些例子的完整代码。