This exercise is about to showcase a reactive microservice ecosystem with Kafka distributed event streaming platform.
Using JDK 17 with Spring framework components (Core, Boot, Web, MVC, Cloud, WebFlux, Actuator, Data JPA).
For the JPA based implementations I use H2 in memory DB but later I plan to replace it with Redis to access data reactively as well.
REST API is created with Spring WebFlux, which is a fully non-blocking web stack - it can do more work with fewer resources - and supports back pressure.
At the moment only the server side is implemented in a reactive way but later I will do the same for the client side with the help of WebClient.
See more about reactive programming: https://reactivemanifesto.org/
Until this moment we have an order-service which exposes below REST interfaces and it's going to consume the order-details-service (https://github.com/szintia/order-details-service) in order to validate the order.
POST /order create a new order in case it's valid
GET /orders retrieve all orders
Local port is set to 8081
After running the application, you can find
- OpenApi descriptions at: http://localhost:8081/api-docs
- Swagger UI at: http://localhost:8081/swagger-ui/index.html#/
-
Download Kafka (includes Zookeeper): https://www.apache.org/dyn/closer.cgi?path=/kafka/
-
After extracting, start Zookeeper service via zookeeper-server-start.bat command. For local development 1 node will be sufficient.
D:\kafka\kafka_2.13-3.6.1>.\bin\windows\zookeeper-server-start.bat .\config\zookeeper.properties
By default it listens to port 2181.[2023-12-14 16:15:31,174] INFO clientPortAddress is 0.0.0.0:2181 (org.apache.zookeeper.server.quorum.QuorumPeerConfig)
-
Start Kafka via kafka-server-start.bat command in a new cmd:
D:\kafka\kafka_2.13-3.6.1>.\bin\windows\kafka-server-start.bat .\config\server.properties
It's connected to Zookeeper:
[2023-12-14 16:17:38,748] INFO Connecting to zookeeper on localhost:2181 (kafka.server.KafkaServer)
[2023-12-14 16:17:40,014] INFO Registered broker 0 at path /brokers/ids/0 with addresses: PLAINTEXT://3B1811282:9092, czxid (broker epoch): 25 (kafka.zk.KafkaZkClient)
Default port is 9092. In the command prompt we can see all the configurations.
-
Let's create a topic, open a new cmd and use the kafka-topics.bat command:
D:\kafka\kafka_2.13-3.6.1>.\bin\windows\kafka-topics.bat --create --topic orders --bootstrap-server localhost:9092
Created topic orders.
-
To get some info about the partition, replication, use kafka-topics.bat command with --describe:
D:\kafka\kafka_2.13-3.6.1>.\bin\windows\kafka-topics.bat --describe --topic orders --bootstrap-server localhost:9092
Topic: orders TopicId: F2JeuSm2Rw2qquuGL7RWgg PartitionCount: 1 ReplicationFactor: 1 Configs: Topic: orders Partition: 0 Leader: 0 Replicas: 0 Isr: 0
-
Testing Producer via kafka-console-producer.bat command
D:\kafka\kafka_2.13-3.6.1>.\bin\windows\kafka-console-producer.bat --topic orders --bootstrap-server localhost:9092
After that we can add some events.
>Testing producer with an event
-
Testing Consumer side, let's see what we have:
D:\kafka\kafka_2.13-3.6.1>.\bin\windows\kafka-console-consumer.bat --topic orders --from-beginning --bootstrap-server localhost:9092
Testing producer with an event
More info can be found on the following Apache Kafka site: https://kafka.apache.org/quickstart#quickstart_send
After the setup of Kafka we can now send messages to the topic called "orders" via Swagger UI - POST HTTP method.
To create our own Docker image, the executable artifact from the order service, we need to
-
build the Dockerfile (committed to the project) with a tag 1.0 and with a name "my-app":
docker build -t my-app:1.0 .
Image is built:
-
verify our order service starts successfully
docker run -d -p 8081:8081 my-app:1.0
-> run Docker image = so basically to run our app in a Docker container with port binding, I set the app to listen to port 8081 on host, same as the container's port.
docker ps
-> list running containers
Swagger UI is now available on port 8081:
So until now we are able to run our order service in a Docker container but we will need also Kafka with Zookeeper.
This will result in a multi-container application --> Let's use Docker Compose to define the services I need.
TO BE CONTINUED.