2.4 Using the Lab

Starting and using the Lab environment.

Starting the Lab

Open a shell and change your directory to the /code/monolith/docker folder.

Start the microservices with

1
2
3
4
5
6
7
8
$ docker-compose up -d
Creating network "docker_monolith" with driver "bridge"
Creating monolith-jaeger         ... done
Creating monolith-grafana        ... done
Creating monolith-alertmanager   ... done
Creating monolith-application-db ... done
Creating monolith-prometheus     ... done
Creating monolith-application    ... done

Now your environment is up and running.

Using the Lab

Available Endpoints

EndpointContainerPurpose
http://localhost:8080/shop-ordersapplicationReturn and create new orders
http://localhost:8080/article-stocksapplicationList article stock count
http://localhost:8080/chaos-monkeyapplicationLets you add a ChaosMonkey

Sample Requests

In the folder requests within the monolith lab folder there is an IntelliJ IDEA compatible HTTP Request file named api.http. You may run the requests directly from this file or build your own requests.

Get the article stock information

1
curl --request GET --url http://localhost:8080/article-stocks

Get order information

1
curl --request GET --url http://localhost:8080/shop-orders

Create new order

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
curl --request POST \
  --url http://localhost:8080/shop-orders \
  --header 'Content-Type: application/json' \
  --data '{
    "articleOrders" : [
        {
            "articleId" : 1,
            "amount" : 1
        }
    ]
}'

Task 2.4.1 - Testing scenario

Run the following queries:

  • Check the article-stock count
  • Create an order for one item of article 1
  • Check the orders and article-stock again

Answer the questions:

  • Is the article-stock count correct?
  • What happens if you are running out of stock?

ChaosMonkey

There is a basic implementation of a ChaosMonkey. It will let you inject errors to methods annotated with @ChaosMonkey. You may configure the ChaosMonkey using the provided RESTful API /chaos-monkey.

The following error types are available:

Error TypeExecutedDescription
RateLimitBefore methodRate limit in calls/sec. Default: Long.MAX_VALUE
LatencyAfter methodIssues a Thread.sleep() in ms. Default: 0
ErrorRateAfter methodLets you specify a percentage of Errors thrown (0 to 1). Default: 0
ExceptionAfter methodAlways triggers an exception if enabled. Default: false

Sample request for configuring the ChaosMonkey which will throw an exception:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
curl --request POST \
  --url http://127.0.0.1:8080/chaos-monkey \
  --header 'Content-Type: application/json' \
  --data '[
  {
    "clazzName": "ArticleStockService",
    "methodName": "orderArticles",
    "enabled": true,
    "errorRate": 0.0,
    "latencyMs": 0,
    "permitsPerSec": 9.223372036854776E18,
    "rateLimiterType": "BLOCK",
    "throwException": true
  }
]'

Task 2.4.2 - Injecting an error

Enable a ChaosMonkey for the method orderArticles in the ArticleStockService.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
curl --request POST \
  --url http://127.0.0.1:8080/chaos-monkey \
  --header 'Content-Type: application/json' \
  --data '[
  {
    "clazzName": "ArticleStockService",
    "methodName": "orderArticles",
    "enabled": true,
    "throwException": true
  }
]'

This will throw an InternalServerErrorException after execution of the method orderArticles.

  • Ask yourself what to expect if an exception is thrown?

Issue the request from the Task 2.4.1.

  • What did you observe? Is there any difference?
  • Is the data still consistent?
  • What’s the role of a transaction in this case?
Task Hint

Since the application throws an InternalServerErrorException the order is not created and the stock count remains the same.

This is due to the fact that the code runs in a single transaction. Throwing a RuntimeException from an active transaction triggers a rollback of the transaction.

Last modified February 12, 2024: fix debezium lab version (9e6013e)