Skip to content

Commit

Permalink
some fixes and add locustfile.py
Browse files Browse the repository at this point in the history
  • Loading branch information
andifalk committed Nov 9, 2023
1 parent 33eb38a commit 6fe041e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
18 changes: 13 additions & 5 deletions lab2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,13 @@ In this step we will use the _retry_ feature with a special API endpoint at [htt

* 200 (OK)
* 400 (Bad Request)
* 408 (Request Timeout)
* 500 (Internal Server Error)
* 503 (Service Unavailable)

Next, we will add a new route including the _Retry_ filter.

Please open the file `src/main/resources/application.yml` in the _/lab2/initial/api-gateway_ project and add the following entries at the end of the _routes_ path:
Please open the file `src/main/resources/application.yml` in the _/lab2/initial/api-gateway_ project and add the following entries at the end of the `routes` path:

__application.yml:__

Expand All @@ -110,7 +112,7 @@ spring:
- name: Retry
args:
retries: 5
statuses: BAD_REQUEST, INTERNAL_SERVER_ERROR
statuses: REQUEST_TIMEOUT, SERVICE_UNAVAILABLE
methods: GET
backoff:
firstBackoff: 10ms
Expand All @@ -124,7 +126,7 @@ This defines a route from [localhost:9090/api/v1/customers/retry (gateway)](http
The retry filter is configured as follows:
* __retries__: The number of retries that should be attempted. In our scenario the request is tried to execute 5 times.
* __statuses__: The HTTP status codes that should be retried, in our sample only `400 (Bad request)` and `500 (Internal server error)` is retried
* __statuses__: The HTTP status codes that should be retried, in our sample only `408 (REQUEST_TIMEOUT)` and `503 SERVICE_UNAVAILABLE)` is retried
* __methods__: The HTTP methods that should be retried, we only want `GET` requests to be retried.
* __backoff__: The configured exponential backoff for the retries. Retries are performed after a backoff interval of `firstBackoff * (factor ^ n)`, where _n_ is the iteration. If _maxBackoff_ is configured, the maximum backoff applied is limited to _maxBackoff_. If _basedOnPreviousValue_ is `true`, the _backoff_ is calculated by using `prevBackoff * factor`.

Expand All @@ -138,7 +140,7 @@ In the logs of the api-gateway you will notice that it might have some entries f
```shell
o.s.c.g.f.f.RetryGatewayFilterFactory : setting new iteration in attr 0
o.s.c.g.f.f.RetryGatewayFilterFactory : exceedsMaxIterations false, iteration 0, configured retries 5
o.s.c.g.f.f.RetryGatewayFilterFactory : retryableStatusCode: true, statusCode 400 BAD_REQUEST, configured statuses [400 BAD_REQUEST, 500 INTERNAL_SERVER_ERROR], configured series [SERVER_ERROR]
o.s.c.g.f.f.RetryGatewayFilterFactory : retryableStatusCode: true, statusCode 503 SERVICE_UNAVAILABLE, configured statuses [403 REQUEST_TIMEOUT, 503 SERVICE_UNAVAILABLE], configured series [SERVER_ERROR]
o.s.c.g.f.f.RetryGatewayFilterFactory : retryableMethod: true, httpMethod GET, configured methods [GET]
o.s.c.g.f.f.RetryGatewayFilterFactory : disposing response connection before next iteration
```
Expand Down Expand Up @@ -357,7 +359,7 @@ To test the rate limiter you need a mechanism to create multiple requests in sho
You may use one of these client tools:

* __Apache Bench__: On Linux and macOS operating systems you may use [Apache Bench (ab)](https://httpd.apache.org/docs/2.4/programs/ab.html). This is a tool from the Apache organization for benchmarking a Hypertext Transfer Protocol (HTTP) web server. With this tool, you can quickly know how many requests per second your web server is capable of serving.
* __Rate Limiter Client__: This workshop also provides a simple client to issue multiple requests to the product service. You find the project for the Rate Limiter Client in the directory _/rate-limiter-client_).
* __Rate Limiter Client__: This workshop also provides a simple client to issue multiple requests to the product service. You find the project for the Rate Limiter Client in the directory _/rate-limiter-client_.

With Apache Bench you can try to perform this command:

Expand All @@ -367,6 +369,12 @@ ab -c 2 -m GET -n 10 -v 3 http://localhost:9090/api/v1/products

This executes 2 concurrent requests at a time (-c 2), uses the GET HTTP method (-m GET), issues 10 requests (-n 10) and sets a verbose level of `3` to receive HTTP status results.

You could also use [Locust](https://github.com/locustio/locust) for this.
If you already have installed python3 then you just have to perform a `pip install locust` to install locust.

Then use the provided file `locustfile.py` and run `locust -f locustfile.py`
In the [Locust web UI](http://0.0.0.0:8089/) specify the target server http://localhost:9090 of the gateway.

If you cannot install the Apache Bench tool then you may use the _rate-limiter-client_ as an alternative.
Navigate to the directory _/rate-limiter-client_ and start the class `com.example.client.RateLimiterClientApplication`.

Expand Down
13 changes: 13 additions & 0 deletions lab2/locustfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from locust import HttpUser, task, between

class ApiGatewayDemo(HttpUser):
wait_time = between(2,4)

@task
def community_page(self):

headers = {
"Authorization": "Bearer eyJraWQiOiI1N2RiZjkxMS0yNDRhLTQ3ZDAtYTM1Zi0zNjM1OWE5MGIzMjciLCJ0eXAiOiJqd3QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJjNTJiZjdkYi1kYjU1LTRmODktYWM1My04MmI0MGU4YzU3YzIiLCJ3ZWJzaXRlIjoiaHR0cHM6Ly9leGFtcGxlLmNvbSIsInpvbmVpbmZvIjoiRXVyb3BlL0JlcmxpbiIsImVtYWlsX3ZlcmlmaWVkIjp0cnVlLCJwcm9maWxlIjoiaHR0cHM6Ly9leGFtcGxlLmNvbS9id2F5bmUiLCJyb2xlcyI6WyJVU0VSIl0sImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6OTAwMCIsInByZWZlcnJlZF91c2VybmFtZSI6ImJ3YXluZSIsImdpdmVuX25hbWUiOiJCcnVjZSIsImxvY2FsZSI6ImRlLURFIiwiYXVkIjoiZGVtby1jbGllbnQtcGtjZSIsIm5iZiI6MTY5ODE1ODc2NSwidXBkYXRlZF9hdCI6IjE5NzAtMDEtMDFUMDA6MDA6MDBaIiwic2NvcGUiOlsib3BlbmlkIiwicHJvZmlsZSIsImVtYWlsIl0sIm5hbWUiOiJCcnVjZSBXYXluZSIsIm5pY2tuYW1lIjoiYndheW5lIiwiZXhwIjoxNjk4MTU5NjY1LCJpYXQiOjE2OTgxNTg3NjUsImZhbWlseV9uYW1lIjoiV2F5bmUiLCJlbWFpbCI6ImJydWNlLndheW5lQGV4YW1wbGUuY29tIn0.EmMam-jx8wBJSvktPrtey9IG09gfFGoci3VRqBhS_eO12fjgzGpdH-rHDzCl2IZpvF_eOm5Mtix2x8fzAYuMq1dBy9dAYpJ85_NWL-M1fU0y3t8_0q-lt6sfn6i9p8ica46r1vV_CfX_Mje6hTdsM6rS3lSwcVQtRtaiAO7jJ52eq6GIhCyftqFRQMVK8h6mc4lJZoaCkzZPqSmi-Nz7n0XrXBCKG_qFvbRw-bHhhQKQhs6rMgIOhArnUpeANpBjycDezPQxKo9yySmHri004-PpSyDyBgXgq1Kw09OMxMu6_vgFJshTwHgIACKOJZPAv18Vn370cl0jrSsMXR67Fw"
}

self.client.get('/api/v1/products', headers=headers)
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ spring:
- name: Retry
args:
retries: 5
statuses: BAD_REQUEST, INTERNAL_SERVER_ERROR
statuses: REQUEST_TIMEOUT, SERVICE_UNAVAILABLE
methods: GET
backoff:
firstBackoff: 10ms
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ void init() {
statusCodes = new HashSet<>();
statusCodes.add(HttpStatusCode.valueOf(200));
statusCodes.add(HttpStatusCode.valueOf(400));
statusCodes.add(HttpStatusCode.valueOf(408));
statusCodes.add(HttpStatusCode.valueOf(500));
statusCodes.add(HttpStatusCode.valueOf(503));

random = new Random();
}
Expand Down Expand Up @@ -59,9 +61,9 @@ String hiddenEndpoint(@CookieValue(name = HIDDEN_API_COOKIE, required = false, d
ResponseEntity<String> retryEndpoint() {
HttpStatusCode httpStatusCode = randomStatus();
if (HttpStatusCode.valueOf(200).isSameCodeAs(httpStatusCode)) {
return new ResponseEntity<>("Request Success", httpStatusCode);
return new ResponseEntity<>("Request Success: " + httpStatusCode, httpStatusCode);
} else {
return new ResponseEntity<>("Request Failure", httpStatusCode);
return new ResponseEntity<>("Request Failure: " + httpStatusCode, httpStatusCode);
}
}

Expand Down

0 comments on commit 6fe041e

Please sign in to comment.