1

I am using Kafka and Springboot in my project, Now here I have external Kafka cluster and SpringBoot microservice project.

Using the yml file I successfully create Kafka Producer and consumer and then both application and kafka communicate perfectly fine.

Now I want to use embedded kafka in my sprintboot project. Something like embedded following Activemq code.

spring:

  # Embedded ActiveMQ Configuration Example
  activemq:
      broker-url: vm://embedded?broker.persistent=false,useShutdownHook=false
      in-memory: true
      non-blocking-redelivery: true

I googled and read lot of article on the same but could not get the clear picture. Here I do not want this embedded server only for junit testing but
want to functional testing without setting any external Kafka component in my Eco System.

I really do not know how to do that , could someone help me on the same.

Thanks in Advance

keepmoving
  • 1,813
  • 8
  • 34
  • 74
  • Did you find how to embed kafka to your application (not test)? I'm interested in it too. – Saram May 19 '20 at 10:15

1 Answers1

1

Add the dependency

    <dependency>
        <groupId>org.springframework.kafka</groupId>
        <artifactId>spring-kafka-test</artifactId>
        <scope>test</scope>
    </dependency>

Create something similar in your test

    public static KafkaEmbedded embeddedKafka = new KafkaEmbedded(2, true, 2, "messages");

and you are ready to use it -- whether functional or unit test should not matter.

You can specify the number of brokers, partitions etc.

You could configure producer and consumer the same way you'd do otherwise.

senseiwu
  • 5,001
  • 5
  • 26
  • 47
  • Hi senseiwu, When I say don't looking for Junit Test, means application will flow something like this. Controller -> Actvice AMQ producer -> AMQ Consumer -> Kafka Producer (here I want to user embedded server) -> Finally Kafka Consumer. Could you please tell me where your I can fit this class or any example would be great help. Thanks – keepmoving Jan 17 '19 at 17:10
  • Your Kafka Producer for `EmbeddedKafka` would be configured just like you would have done with a real Kafka cluster. In your initialization code, you'd call `container.start()` and use `KafkaTemplate template = new KafkaTemplate<>(producerFactory);` If it helps, first try to get the test running with a real external Kafka broker, so that you have your configuration part clear. Then replace with the embedded server like I wrote. _Controller -> Actvice AMQ producer -> AMQ Consumer ->_ part shouldn't have anything to do with embedded or not – senseiwu Jan 17 '19 at 17:14
  • well senseiwu, as I wrote I already configured this with external Kafka cluster and only thing I want to do was embedded to remove dependency of any external component. But I am trying whatever you suggested. Thanks – keepmoving Jan 17 '19 at 17:23