ewolff / microservice-kafka

Demo to show how Apache Kafka can be used for communication between microservices

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How does Kafka Converts Order info to Invoice

pavankjadda opened this issue · comments

Hello,
I was going through the code and trying to understand it. In OrderService class, method fireOrderCreatedEvent has kafkaTemplate.send("order", order.getId() + "created", order); statement, which sends Order object to Kafka messaging system.

private void fireOrderCreatedEvent(Order order) {
		kafkaTemplate.send("order", order.getId() + "created", order);
	}

And in OrderKafkaListener class you have the following method

@KafkaListener(topics = "order")
	public void order(Invoice invoice, Acknowledgment acknowledgment) {
		log.info("Revceived invoice " + invoice.getId());
		invoiceService.generateInvoice(invoice);
		acknowledgment.acknowledge();
	}

How did Order object serialize or converted to Invoice object? I am kind of confused here. Does Kafka implement this? or we need to manually do it here.

I saw your updated documentation. However, I still have some questions

  1. JSON serialization is flexible. So when an Order is deserialized into Invoice and Delivery just the needed data is read, How is this possible? I know they use the same topic, but no relation exists between Order and Invoice i.e foreign key
  2. Things become complex when if no such relation exists. Order data on Kafka consumed by Invoice and Shipping. Since there is no relation between Shipping and Invoice, Invoice may consume one Order object and shipping may consume different Order object. When the user pulls his order information, he may not see correct information.
    I looked at other implementations, they deserialized Order (in their case Car) object on another microservice. Am I missing something here?

1 - The attributes in Order, Invoice and Delivery have the same names. So a serialized Order can be interpreted as an Invoice or a Delivery. The additional attributes are ignored.
2 - The Order has an ID that is also present in Invoice and Delivery. So there is a relation.

Got it. In large enterprise applications do you think it's a good idea to have the same id for all the Objects? Let's just say we have custom order id: ORD112-34-44847477 for Order and as per your implementation Invoice and Delivery will get the same ID which has ORD in it, which confuses users. I think it's better to have a unique id (invoice_id, delivery_id) for each object based on object/microservice name and have the ID attribute for relation. What do you think?

You need to correlate orders to invoices and deliveries. I am not sure how that should be done if the invoice and delivery do not contain the id of the order. Note that there might still be an additional invoice or delivery id.