This demo project aims to be an introduction to developing Reactive Microservices based on the Spring framework. For a complete guide check our related article on Dzone here: https://dzone.com/articles/spring-reactive-microservices-a-showcase
We are going to implement a simplified One Time Password (OTP) service, offering the following capabilities:
- Generate OTP
- Validate (use) OTP
- Resend OTP
- Get OTP status
- Get all OTPs of a given number
Our application will consist of the following microservices:
- otp-service: which will provide the functionality above by orchestrating calls to local and remote services
- customer-service: will keep a catalogue of registered users to our service with information like: account id, MSISDN, e-mail etc.
A number of remote (external) services will be invoked. We assume that our application is authorized to use them will access them via their REST API. Of course these will be mocked for simplicity. These "3rd-party" services are:
- number-information: takes a phone number as input and verifies that it belongs to a Telecoms operator and is currently active
- notification-service: delivers the generated OTPs to the designated number or channel (phone, e-mail, messenger etc.)
In order to build and test the application, the prerequisites are:
- Java 11 and above
- Maven
- Docker (because we use TestContainers during our Integration tests)
Then simply execute a mvn clean verify
The easiest way is to run the microservices using Docker and Docker Compose:
docker-compose up --build
When the containers are up and running, you can visit consul's UI to see the active services:
Below you may find curl
commands for invoking the various endpoints via our API Gateway:
Generate OTP
curl --location --request POST 'localhost:8000/otp-service/v1/otp' \
--header 'Content-Type: application/json' \
--data-raw '{
"msisdn": "00306933177321"
}'
Validate OTP
curl --location --request POST 'http://localhost:8000/otp-service/v1/otp/36/validate?pin=356775' \
Resend OTP
curl --location --request POST 'localhost:8000/otp-service/v1/otp/2?via=AUTO,EMAIL,VOICE&[email protected]' \
--header 'Content-Type: application/json' \
Get All OTPs
curl --location --request GET 'localhost:8000/otp-service/v1/otp?number=00306933177321'
OTP Status
curl --location --request GET 'localhost:8000/otp-service/v1/otp/1'
- WebClient simple usage
- Parallel calls to the same endpoint
- Parallel calls to the different endpoint
- .zip
- .zipWhen
- .zipDelayError
- .doOnNext
- .doOnSuccess VS .doOnError
- Chaining of calls (Sequential execution)
- Service-to-service communication
- Database interaction (r2dbc/postgresql)