A simple .net core webapi using Kafka as a streaming API.
The webapi is the entry point to generate an order (i.e. to order a book). The Api sends the request (after serializing the object to a json string) to a topic in Kafka called 'orderBookRequests'.
-
Endpoint: http://localhost:5000/api/order
-
Action: POST
-
Body:
{"Id":1234, "Book":{"Title":"Origin", "Isbn": "9123456123456"}, "Quantity":3}
The console app is used as a running service for mapping (from OrderRequest to DeliverRequest) and distributing. As soon as the app is started it acts as a Kafka consumer with a subscription to the topic 'orderBookRequests'. When receiving the messages, they are desirialized from a json string and mapped into a new object called 'DeliveryOrder'. This object will be sent to a producer which writes messages to the Kafka topic 'deliverBookRequests'.
This class library has two producers. One producer writes to the Kafka topic 'orderBookRequests', the other producer writes to the Kafka topic 'deliverBookRequests'.
Install Kafka (latest version) on your local machine: [https://kafka.apache.org/downloads]
bin\windows\zookeeper-server-start.bat config/zookeeper.properties
bin\windows\kafka-server-start.bat config/server.properties
git clone https://github.com/confluentinc/examples/tree/5.3.1-post/cp-all-in-one
Make sure you have at least 8192MB Memory allocated in docker (Settings, Advanced).
cd cp-all-in-one
docker-compose up -d --build
docker-compose ps
All states should have the value 'Up'
Go to a webbrowser and start Confluent Control Center: localhost:9021
Under 'topics' you can create two new topics called 'orderBookRequests' and 'deliverBookRequests' with 1 partitions each.
cd StreamingDistributor
dotnet run args[-topic] (i.e. dotnet run -topic orderBookRequests
)
The result should be like that:
- Press Ctrl+C to exit
- Topic: orderBookRequests
- Group: test-group
cd StreamingApi
dotnet run
The result should be like that:
- info: Microsoft.Hosting.Lifetime[0]
-
Now listening on: https://localhost:5001
- info: Microsoft.Hosting.Lifetime[0]
-
Now listening on: http://localhost:5000
- info: Microsoft.Hosting.Lifetime[0]
-
Application started. Press Ctrl+C to shut down.
- info: Microsoft.Hosting.Lifetime[0]
-
Hosting environment: Development
- info: Microsoft.Hosting.Lifetime[0]
https: https://localhost:5001
http: http://localhost:5000
Action: POST
Header: application/json
Body: {"Id":1234, "Book":{"Title":"Origin", "Isbn": "9123456123456"}, "Quantity":3}
The result in the console (where the WebApi is running) should be like that:
Delivered '{"Id":1234,"Book":{"Isbn":"9123456123456","Title":"Origin"},"Quantity":3,"status":0}' to: orderBookRequests [[0]] @47