It is possible to return custom data to Kafka Producer

Viewed 1865

I am learning kafka and I want to split my app to 2 microservices. First save all incoming messages from KafkaConsumer to database and select entity by given id. Second provide REST api to save and get entities. Interaction between them provided with kafka. How can I receive stored ID from db in REST api with kafka? Here is sample code of producer which calls on POST request.

 public void sendToKafka(MyObject myobject) throws ExecutionException, InterruptedException {
    LOGGER.info("sending payload='{}' to topic='{}'", myobject, myTopic);
    byte[] bytes = parseObjectToByte(myobject);
    ListenableFuture<SendResult<String, byte[]>> resultFuture = kafkaTemplate.send(topicSave, bytes);
    SendResult<String, byte[]> result = resultFuture.get();
    LOGGER.info(result.toString());
}

and Consumer, which save myObject to database

@KafkaListener(topics = "${kafka.topic.mytopic}")
public void saveMyObject(byte[] value) {
    MyObject myobject = parseToMyObject(value);
    LOGGER.info("received myobject='{}'", myobject);
    MyObject myobjectSaved = myObjectRepository.insert(myobject);
}

I'm using spring-kafka with spring-boot. Rest api has 2 methods: POST - save myObject Get - return saved object by id. It is possible do it with kafka or i must connect this microservices directly? Thank you.

1 Answers
Related