Search code examples
javaspringspring-bootapache-kafkaspring-kafka

Can spring request reply can be implemented with client side using spring kafka API's and server side using apache kafka API?


Can spring request reply can be implemented with client side using spring kafka API's and server side using apache kafka API ?


Solution

  • Spring for Apache Kafka provides request/reply using the ReplyingKafkaTemplate and @KafkaListener with a @SendTo.

    Documentation here.

    EDIT

    When using multiple client instances...

    f you have multiple client instances and you do not configure them as discussed in the preceding paragraph, each instance needs a dedicated reply topic. An alternative is to set the KafkaHeaders.REPLY_PARTITION and use a dedicated partition for each instance. The Header contains a four-byte int (big-endian). The server must use this header to route the reply to the correct partition (@KafkaListener does this). In this case, though, the reply container must not use Kafka’s group management feature and must be configured to listen on a fixed partition (by using a TopicPartitionOffset in its ContainerProperties constructor).

    When configuring with a single reply topic, each instance must use a different group.id. In this case, all instances receive each reply, but only the instance that sent the request finds the correlation ID. This may be useful for auto-scaling, but with the overhead of additional network traffic and the small cost of discarding each unwanted reply. When you use this setting, we recommend that you set the template’s sharedReplyTopic to true, which reduces the logging level of unexpected replies to DEBUG instead of the default ERROR.