Search code examples
javaapache-kafkamicroservicesvert.x

How use apache kafka in vertx, both on the server side, as well as the client?


I recently asked what was the best way to communicate my microservices in vertx, I did it in a way which was with a web client with the same library that provides vertx (it was very simple), however I have been reading that use apache kafka is better, but I do not know how to use it, I read the official documentation of kafka, but I could only create a producer and a consumer, I really do not know what to do with them, how can I from a microservice send a method to another miscroservice through kafka? Excuse my ignorance with the subject


Solution

  • Your microservice can do that :

    • Subscribe a Kafka topic and consume the messages from this topic and process that messages (to do that you need to create a consumer with a group.id in your microservice)

    • Send messages in one or more topics to contact other microservices (to do that you need to create one or more producers in your microservice)

    I recommend you to read some articles about microservices architectures using Kafka or old fashion JMS compliant system (like ActiveMQ or RabbitMQ for example).

    You can begin with this one : https://medium.com/@ulymarins/an-introduction-to-apache-kafka-and-microservices-communication-bf0a0966d63

    Here is an example of scenario :

    • Service A: a restfull endpoint to order a burger menu:

      POST /v1/order

      {"type": "cheeseburger", "quantity": 1, "ketchup": true, "notify": ["cooker"]}

      This service use a Kafka producer to send the JSON in a topic called TOPIC_ORDERS

    • Service B: consume the messages of the TOPIC_ORDER and send a SMS to the cooker and insert the order in database and produce the created order with ids in another topic CREATED_ORDERS

    • Service C: another restfull endpoint used by the cooker when the menu is cooked:

      PATCH /v1/order/{id}

      {"ready": true, "notify": ["customer"]}

      This one produce the message in the TOPIC_ORDER in order to call the service B to update the order in database and notify the customer.

    • Service D: consume the messages of CREATED_ORDERS topic to index in a search engine used by some other applications (to make stats for example)