<p class="has-text-align-justify wp-block-paragraph">In this article, we will discuss an out of box library &#8220;<strong><em>Spring Boot Chaos Monkey</em></strong>&#8221; to provide a Chaos Monkey for Spring Boot applications and this lib will try to check the resiliency of your running spring boot applications. This library is inspired by <strong><a href="https://principlesofchaos.org/" target="_blank" rel="noreferrer noopener nofollow">PRINCIPLES OF CHAOS ENGINEERING</a></strong>, with a focus on Spring Boot to test applications better during operation.</p>



<p class="wp-block-paragraph">According to the <strong>PRINCIPLES OF CHAOS ENGINEERING</strong>&#8211;</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Chaos Engineering is the discipline of experimenting on a system in order to build confidence in the system’s capability to withstand turbulent conditions in production.</p></blockquote>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Everything from getting started to advanced usage is explained in the <a href="https://codecentric.github.io/chaos-monkey-spring-boot/latest/" target="_blank" rel="noreferrer noopener">Documentation for Chaos Monkey for Spring Boot</a>.</p></blockquote>



<h2 class="wp-block-heading">Need of Chaos Engineering for Spring Boot applications</h2>



<p class="has-text-align-justify wp-block-paragraph">Netflix has started Chaos Engineering to check their APIs in recent years and also it contributed significantly to the growing importance of Chaos Engineering in distributed systems.</p>



<p class="wp-block-paragraph">The below lines by Fire Chief <strong>Mike Burtch</strong> but quite interesting I found.</p>



<figure class="wp-block-pullquote"><blockquote><p>You don’t choose the moment, the moment chooses you!</p><p>You only choose how prepared you are when it does.</p></blockquote></figure>



<p class="has-text-align-justify wp-block-paragraph">As application developers, we focus on developing a product that must be stable, secure and bug-free software. To achieve this, we write a lot of unit tests and integration tests so that we can capture unexpected behaviour and ensure that the patterns we test do not lead to errors. But what I observe in my career of software development after writing many units and integration tests, we might maximum achieve a code coverage from 70% to 80%, sometimes more but not 100%, we can&#8217;t be happy with this unpleasant feeling, how our application behaves in production?</p>



<p class="wp-block-paragraph">Many more questions may arise to mind such as </p>



<ul class="wp-block-list"><li><em>How our code will work on the production?</em></li><li><em>What will happen if one service went down?</em></li><li><em>How does the application behave with network latency?</em></li><li><em>Will our fallback scenarios work?</em></li><li><em>Will Service Discovery work?</em></li><li><em>Is our Client-Side-Load-Balancing also working?</em></li></ul>



<p class="has-text-align-justify wp-block-paragraph">Today&#8217;s applications are adopting microservice architectures and distributed applications creation. These architectures contain many components that can’t all be fully covered with unit and integration tests.</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>If you want more familiar with the principles of chaos engineering, you can check out <a href="https://blog.codecentric.de/en/2018/07/chaos-engineering/" target="_blank" rel="noreferrer noopener nofollow">this blog post</a> for chaos engineering.</p></blockquote>



<p class="wp-block-paragraph">Let&#8217;s integrate Chaos Monkey for Spring Boot applications.</p>



<h2 class="wp-block-heading">Adding Chaos Monkey in Spring Boot application</h2>



<p class="has-text-align-justify wp-block-paragraph">In a spring boot application, just add <em>Spring Boot Chaos Monkey</em> on your classpath of the application and activate with the profile name, it will automatically hook into your spring boot application.</p>



<p class="wp-block-paragraph">Add Chaos Monkey for Spring Boot as a dependency for your project using MAVEN.</p>



<pre class="wp-block-code"><code><;dependency>;

 <;groupId>;de.codecentric<;/groupId>;

 <;artifactId>;chaos-monkey-spring-boot<;/artifactId>;

 <;version>;2.5.4<;/version>;

<;/dependency>;</code></pre>



<p class="wp-block-paragraph"><meta charset="utf-8">Add Chaos Monkey for Spring Boot as a dependency for your project using GRADLE.</p>



<pre class="wp-block-code"><code>implementation 'de.codecentric:chaos-monkey-spring-boot:2.5.4'</code></pre>



<p class="has-text-align-justify wp-block-paragraph">Let start your Spring Boot Application with the <code>chaos-monkey</code> spring profile enabled. You can also pass some other properties to assault services with latency. Let&#8217;s see the following properties as I have defined run time as java arguments.</p>



<pre class="wp-block-code"><code>java -jar my-springboot-app.jar --spring.profiles.active=chaos-monkey --chaos.monkey.enabled=true --chaos.monkey.watcher.service=true --chaos.monkey.assaults.latencyActive=true</code></pre>



<p class="has-text-align-justify wp-block-paragraph">You can also specify the above properties in your <code>application.properties</code> or <code>application.yml</code> file. Let&#8217;s see the following <code>application.properties</code> file with the minimum required configuration for the Chaos Monkey for Spring Boot application.</p>



<p class="wp-block-paragraph">File <code>application.properties</code></p>



<pre class="wp-block-code"><code>spring.profiles.active=chaos-monkey

chaos.monkey.enabled=true

 

chaos.monkey.watcher.service=true

chaos.monkey.assaults.latencyActive=true</code></pre>



<p class="wp-block-paragraph">File <code>application.yml</code></p>



<pre class="wp-block-code"><code>spring:

 profiles:

 active: chaos-monkey

chaos:

 monkey:

 enabled: true

 watcher:

 service: true

 assaults:

 latencyActive: true</code></pre>



<p class="has-text-align-justify wp-block-paragraph">Now you can activate watchers, which look for classes to assault. There are also runtime assaults, which attack your whole application. Let&#8217;s see the following diagram how does it work?</p>



<div class="wp-block-image is-style-rounded"><figure class="aligncenter size-large"><img src="https://dineshonjava.com/wp-content/uploads/2022/01/chaos-engineering-with-spring-boot-application-1024x426.png" class="wp-image-4580"/><figcaption>@Image Source-codecentric.github.com </figcaption></figure></div>



<p class="has-text-align-justify wp-block-paragraph">In the above diagram, we can see that Chaos Monkey provides watchers and assaults. In the Spring boot applications, we have controllers, repositories, and services and Spring Boot Chaos Monkey has a corresponding watcher for each resource. For example, @Controller watched by controller watcher, @Respository watched by repository watchers etc. And each watcher can generate multiple assaults for each resource.</p>



<h3 class="wp-block-heading">Watcher</h3>



<p class="wp-block-paragraph">A watcher is a Chaos Monkey for Spring Boot component, that will scan your app for beans based on one of the conditions described in Watcher Types.</p>



<p class="wp-block-paragraph">These watchers find your beans based on the following annotations:</p>



<ul class="wp-block-list"><li>@Controller</li><li>@RestController</li><li>@Service</li><li>@Repository</li><li>@Component</li></ul>



<p class="wp-block-paragraph">We can also define custom watchers as well.</p>



<h3 class="wp-block-heading">Assault</h3>



<p class="wp-block-paragraph">Assaults are the heart of Monkey’s Chaos and he makes use of them based on your configuration. There are the following types of assault provided by Chaos Monkey.</p>



<p class="wp-block-paragraph"><strong>Request Assaults</strong></p>



<p class="wp-block-paragraph">These assaults attack some point of your application and are triggered by a <strong>/actuator/chaosmonkey/watchers</strong> endpoint.</p>



<ul class="wp-block-list"><li>Latency Assault </li><li>Exception Assault</li></ul>



<p class="wp-block-paragraph"><strong>Runtime Assaults</strong></p>



<ul class="wp-block-list"><li>AppKiller Assault</li><li>Memory Assault</li><li>CPU Assault</li></ul>



<p class="has-text-align-justify wp-block-paragraph">The above minimum configuration is enough to test your spring boot application resiliency but if you pass these properties by using the configuration files either using <code>application.properties</code> or <code>application.yml</code> file, you have to start your application on production. Starting the production application to test your resiliency is not recommended way you have to take a lot of approval from upper management, so you can avoid this restart if you use the Spring Boot Actuator and HTTP/JMX. </p>



<h3 class="wp-block-heading">Spring Boot Actuator Endpoints</h3>



<p class="has-text-align-justify wp-block-paragraph">Chaos Monkey for Spring Boot offers you some built-in endpoints exposed via JMX or HTTP. This allows you to change the configuration at runtime.</p>



<p class="wp-block-paragraph"><meta charset="utf-8">File <code>application.properties</code></p>



<pre class="wp-block-code"><code>management.endpoint.chaosmonkey.enabled=true

management.endpoint.chaosmonkeyjmx.enabled=true

 

# include all endpoints

management.endpoints.web.exposure.include=*

 

# include specific endpoints

management.endpoints.web.exposure.include=health,info,chaosmonkey</code></pre>



<p class="wp-block-paragraph"><meta charset="utf-8">File <code>application.yml</code></p>



<pre class="wp-block-code"><code>management:

 endpoint:

 chaosmonkey:

 enabled: true

 chaosmonkeyjmx:

 enabled: true

 

 endpoints:

 web:

 exposure:

 # include all endpoints

 include: "*"

 # include specific endpoints

 include:

 - health

 - info

 - chaosmonkey</code></pre>



<p class="wp-block-paragraph">Let&#8217;s start the Spring Boot application test all endpoints as below:</p>



<p class="wp-block-paragraph"><strong>GET Chaos Monkey Configuration</strong></p>



<p class="wp-block-paragraph">/<strong><em>actuator/chaosmonkey</em></strong></p>



<p class="wp-block-paragraph">Response:</p>



<pre class="wp-block-code"><code>{
 "chaosMonkeyProperties": {
 "enabled": true,
 "togglePrefix": "chaos.monkey"
 },
 "assaultProperties": {
 "level": 1,
 "deterministic": false,
 "latencyRangeStart": 1000,
 "latencyRangeEnd": 3000,
 "latencyActive": true,
 "exceptionsActive": false,
 "exception": {
 "type": null,
 "arguments": null
 },
 "killApplicationActive": false,
 "memoryActive": false,
 "memoryMillisecondsHoldFilledMemory": 90000,
 "memoryMillisecondsWaitNextIncrease": 1000,
 "memoryFillIncrementFraction": 0.15,
 "memoryFillTargetFraction": 0.25,
 "cpuActive": false,
 "cpuMillisecondsHoldLoad": 90000,
 "cpuLoadTargetFraction": 0.9,
 "runtimeAssaultCronExpression": "OFF"
 },
 "watcherProperties": {
 "controller": false,
 "restController": false,
 "service": true,
 "repository": false,
 "component": false,
 "restTemplate": false,
 "webClient": false,
 "actuatorHealth": false,
 "beans": &#91;]
 }
}</code></pre>



<p class="wp-block-paragraph"><strong>GET Chaos Monkey Status</strong></p>



<p class="wp-block-paragraph"><strong><em>/actuator/chaosmonkey/status</em></strong></p>



<p class="wp-block-paragraph">Response:</p>



<pre class="wp-block-code"><code>Ready to be evil!</code></pre>



<p class="wp-block-paragraph"><strong>POST Chaos Monkey disable</strong></p>



<p class="wp-block-paragraph"><strong><em>/actuator/chaosmonkey/disable</em></strong></p>



<p class="wp-block-paragraph">Response:</p>



<pre class="wp-block-code"><code>{
 "status": "Chaos Monkey is disabled",
 "disabledAt": "2022-01-04T16:41:40.426+05:30"
}</code></pre>



<p class="wp-block-paragraph"><strong>POST Chaos Monkey enable</strong></p>



<p class="wp-block-paragraph"><strong><em>/actuator/chaosmonkey/enable</em></strong></p>



<p class="wp-block-paragraph">Response:</p>



<pre class="wp-block-code"><code>{
 "status": "Chaos Monkey is enabled",
 "enabledAt": "2022-01-04T16:44:35.909+05:30"
}</code></pre>



<p class="wp-block-paragraph"><strong>GET Watchers</strong></p>



<p class="wp-block-paragraph"><strong>/actuator/chaosmonkey/watchers</strong></p>



<p class="wp-block-paragraph">Response:</p>



<pre class="wp-block-code"><code>{
 "controller": false,
 "restController": false,
 "service": true,
 "repository": false,
 "component": false,
 "restTemplate": false,
 "webClient": false,
 "actuatorHealth": false,
 "beans": &#91;]
}</code></pre>



<p class="wp-block-paragraph"><strong>POST Watchers</strong><br>Request to enable/disable Watchers</p>



<p class="wp-block-paragraph">POST<strong><em> /actuator/chaosmonkey/watchers</em></strong></p>



<p class="wp-block-paragraph">Request Body:</p>



<pre class="wp-block-code"><code>{
 "controller": true,
 "restController": true,
 "service": true,
 "repository": true,
 "component": false,
 "restTemplate": false,
 "webClient": false,
 "actuatorHealth": false
}</code></pre>



<p class="wp-block-paragraph">Response:</p>



<pre class="wp-block-code"><code>Watcher config has changed</code></pre>



<p class="wp-block-paragraph">Check again watchers</p>



<p class="wp-block-paragraph"><strong>GET Watchers</strong></p>



<p class="wp-block-paragraph"><strong>/actuator/chaosmonkey/watchers</strong></p>



<p class="wp-block-paragraph">Response:</p>



<pre class="wp-block-code"><code>{
 "controller": true,
 "restController": true,
 "service": true,
 "repository": true,
 "component": false,
 "restTemplate": false,
 "webClient": false,
 "actuatorHealth": false,
 "beans": &#91;]
}</code></pre>



<p class="wp-block-paragraph"><strong>GET Assaults</strong></p>



<p class="wp-block-paragraph"><em>GET /actuator/chaosmonkey/assaults</em></p>



<p class="wp-block-paragraph">Response:</p>



<pre class="wp-block-code"><code>{
 "level": 1,
 "deterministic": false,
 "latencyRangeStart": 1000,
 "latencyRangeEnd": 3000,
 "latencyActive": true,
 "exceptionsActive": false,
 "exception": {
 "type": null,
 "arguments": null
 },
 "killApplicationActive": false,
 "memoryActive": false,
 "memoryMillisecondsHoldFilledMemory": 90000,
 "memoryMillisecondsWaitNextIncrease": 1000,
 "memoryFillIncrementFraction": 0.15,
 "memoryFillTargetFraction": 0.25,
 "cpuActive": false,
 "cpuMillisecondsHoldLoad": 90000,
 "cpuLoadTargetFraction": 0.9,
 "runtimeAssaultCronExpression": "OFF"
}</code></pre>



<p class="wp-block-paragraph"><strong>POST Assaults</strong><br>Request to enable Latency &; Exception Assault</p>



<p class="wp-block-paragraph">POST <strong><em>/actuator/chaosmonkey/assaults</em></strong></p>



<p class="wp-block-paragraph">Request Body:</p>



<pre class="wp-block-code"><code>{
 "level": 5,
 "latencyRangeStart": 2000,
 "latencyRangeEnd": 5000,
 "latencyActive": true,
 "exceptionsActive": true,
 "killApplicationActive": false
}</code></pre>



<p class="wp-block-paragraph">Response:</p>



<pre class="wp-block-code"><code>Assault config has changed</code></pre>



<p class="wp-block-paragraph"><strong>GET Assaults again</strong></p>



<p class="wp-block-paragraph"><em>GET /actuator/chaosmonkey/assaults</em></p>



<p class="wp-block-paragraph">Response:</p>



<pre class="wp-block-code"><code>{
 "level": 5,
 "deterministic": false,
 "latencyRangeStart": 2000,
 "latencyRangeEnd": 5000,
 "latencyActive": true,
 "exceptionsActive": true,
 "exception": {
 "type": null,
 "arguments": null
 },
 "killApplicationActive": false,
 "memoryActive": false,
 "memoryMillisecondsHoldFilledMemory": 90000,
 "memoryMillisecondsWaitNextIncrease": 1000,
 "memoryFillIncrementFraction": 0.15,
 "memoryFillTargetFraction": 0.25,
 "cpuActive": false,
 "cpuMillisecondsHoldLoad": 90000,
 "cpuLoadTargetFraction": 0.9,
 "runtimeAssaultCronExpression": "OFF"
}</code></pre>



<p class="wp-block-paragraph"><strong>Define specific method attacks</strong></p>



<p class="wp-block-paragraph">POST <strong><em>/actuator/chaosmonkey/assaults</em></strong></p>



<p class="wp-block-paragraph">Request Body:</p>



<pre class="wp-block-code"><code>{
 "level": 5,
 "latencyRangeStart": 2000,
 "latencyRangeEnd": 5000,
 "latencyActive": true,
 "exceptionsActive": true,
 "killApplicationActive": false,
 "watchedCustomServices": &#91;
 "com.doj.myapp.controller.HelloController.sayHello",
 "com.doj.myapp.controller.HelloController.sayWelcome"
 ]
}</code></pre>



<p class="wp-block-paragraph">Response:</p>



<pre class="wp-block-code"><code>Assault config has changed</code></pre>



<p class="wp-block-paragraph"><meta charset="utf-8"> <p><strong>GET Assaults again</strong></p></p>



<p class="wp-block-paragraph"><p><em>GET /actuator/chaosmonkey/assaults</em></p></p>



<p class="wp-block-paragraph"><p>Response:</p></p>



<pre class="wp-block-code"><code>{
 "level": 5,
 "deterministic": false,
 "latencyRangeStart": 2000,
 "latencyRangeEnd": 5000,
 "latencyActive": true,
 "exceptionsActive": true,
 "exception": {
 "type": null,
 "arguments": null
 },
 "killApplicationActive": false,
 "memoryActive": false,
 "memoryMillisecondsHoldFilledMemory": 90000,
 "memoryMillisecondsWaitNextIncrease": 1000,
 "memoryFillIncrementFraction": 0.15,
 "memoryFillTargetFraction": 0.25,
 "cpuActive": false,
 "cpuMillisecondsHoldLoad": 90000,
 "cpuLoadTargetFraction": 0.9,
 "runtimeAssaultCronExpression": "OFF",
 "watchedCustomServices": &#91;
 "com.doj.myapp.controller.HelloController.sayHello",
 "com.doj.myapp.controller.HelloController.sayWelcome"
 ]
}</code></pre>



<p class="wp-block-paragraph"> </p>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/spring-batch-read-from-csv-and-write-to-relational-db/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/top-20-mongodb-interview-questions-2022/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '4579'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…