How to publish/subscribe Kafka messages in Springboot 3

Поделиться
HTML-код
  • Опубликовано: 24 янв 2025

Комментарии • 2

  • @fvhiveproductionsbyLadyAga
    @fvhiveproductionsbyLadyAga  10 месяцев назад

    org.springframework.kafka
    spring-kafka

  • @fvhiveproductionsbyLadyAga
    @fvhiveproductionsbyLadyAga  10 месяцев назад

    1. Install Kafka and Zookeeper
    2. Create Springboot App1 - Producer
    3. Create Springboot App2 - Consumer
    4. Test Kafka Messaging
    -----------------
    application.properties for CONSUMER microservice
    spring.kafka.consumer.bootstrap-servers = localhost:9092
    spring.kafka.consumer.group-id= group_id
    spring.kafka.consumer.auto-offset-reset = earliest
    spring.kafka.consumer.key-deserializer= org.apache.kafka.common.serialization.StringDeserializer
    spring.kafka.consumer.value-deserializer = org.apache.kafka.common.serialization.StringDeserializer
    -------------------
    application.properties for PRODUCER microservice
    spring.kafka.producer.bootstrap-servers = localhost:9092
    spring.kafka.producer.key-serializer = org.apache.kafka.common.serialization.StringSerializer
    spring.kafka.producer.value-serializer = org.apache.kafka.common.serialization.StringSerializer
    --------------
    Code Snippet for sending Kafka message:
    @Autowired
    private KafkaTemplate kafkaTemplate;
    public void send() {
    kafkaTemplate.send("testtopic1", "test msg");
    }
    --------------
    Code Snippet for consuming Kafka message:
    @KafkaListener(topics = "testtopic1", groupId = "*")
    public void listenGroupFoo(String message) {
    System.out.println("Received Message in group foo: " + message);
    }

    ------------------
    kafka-install.yml to install Kafka:
    version: '3'
    services:
    zookeeper:
    image: wurstmeister/zookeeper
    container_name: zookeeper
    ports:
    - "2181:2181"
    kafka:
    image: wurstmeister/kafka
    container_name: kafka
    ports:
    - "9092:9092"
    environment:
    KAFKA_ADVERTISED_HOST_NAME: localhost
    KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181