EzyKafka Configuration

Updated at 1699803613000

1. Configuration List

EzyKafka provides a list of configurations in bellow table:

PropertyDefault ValueDescription
1kafka.consumersnullList of kafka consumers
2kafka.producersnullList of kafka producers

For every producer, EzyKafka provides bellow properties:

PropertyDefault ValueDescription
1topicproducer nameName of the kafka topic
2bootstrap.serverslocalhost:9092Kafka bootstrap servers
3client.idproducer nameKafka client id

You can find out the all properties of kafka producer in the ProducerConfig class For every consumer, EzyKafka provides bellow properties:

PropertyDefault ValueDescription
1topicconsumer nameName of the kafka topic
2bootstrap.serverslocalhost:9092Kafka bootstrap servers
3client.idconsumer nameKafka client id for consumer
4group.idrandom uuidKafka group id for consumers

You can find out the all properties of kafka consumer in the ConsumerConfig class

Let's careful with group.id

Let's take a look bellow image:

You can see, in a group, there is only one consumer can poll message at a time. Example, you have Consumer Group 2 with Consumer1 and Consumer2, now Topic T1 has 2 messages: Message1, Message2, so maybe Consumer1 or Consumer2 will poll the both messages or Consumer1 poll Message1 and Consumer2 poll Message2. So, if you want to broadcast a message to the all consumer, you will need set value for group.id property is difference for each consumer, or you don't need provide any value, EzyKafka will random an UUID for you.

2. An Example

Let's we neen create a producer and a consumer named hello-world, you can put bellow configuration to your application.yaml file:

    # for application.yaml
    kafka:
      producers:
        hello-world:
          hello-world: test
          bootstrap.servers: localhost:9092
          client.id: KafkaProducerExample
      consumers:
        hello-world:
          topic: hello-world
          group.id: KafkaConsumerExample

Or for application.properties file:

    # for application.properties
    kafka.producers.hello-world.topic=hello-world
    kafka.producers.hello-world.bootstrap.servers=localhost:9092
    kafka.producers.hello-world.client.id=KafkaProducerExample
    kafka.consumers.hello-world.topic=test
    kafka.consumers.hello-world.group.id=KafkaConsumerExample

3. Configure for producer

You can configure for producer like this:

    EzyKafkaProxy kafkaProxy = EzyKafkaProxy.builder()
        .scan("com.tvd12.ezymq.example.kafka")
        .build();
    EzyKafkaProducer producer = kafkaProxy.getProducer("hello-world");

4. Configure for consumer

You can configure for consumer like this:

    EzyKafkaProxy.builder()
        .addSingleton(someSingleton)
        .scan("com.tvd12.ezymq.example.kafka")
        .build();
 

Please take a look ezymq-kafka for entire example.

Next step

You can see how to build a push message system.