Home > Java > javaTutorial > body text

Starting from scratch: Springboot guide to quickly build kafka integrated environment

王林
Release: 2024-02-01 09:29:15
Original
480 people have browsed it

Starting from scratch: Springboot guide to quickly build kafka integrated environment

Springboot Integrated Kafka Overview

Apache Kafka is a distributed streaming service that allows you to produce, consume and store data with extremely high throughput. It is widely used to build a wide variety of applications such as log aggregation, metric collection, monitoring, and transactional data pipelines.

Springboot is a framework for simplifying Spring application development. It provides out-of-the-box autowiring and conventions to easily integrate Kafka into Spring applications.

Build the environment required for Kafka to integrate Springboot

1. Install Apache Kafka

  • Download the Apache Kafka distribution.
  • Unzip the distribution and start the Kafka service.
  • Check the Kafka service log to make sure it is running normally.

2. Install Springboot

  • Download the Springboot distribution.
  • Extract the distribution and add it to your system's path.
  • Create a Springboot application.

Code Example

1. Create Springboot application

public class SpringbootKafkaApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootKafkaApplication.class, args);
    }
}
Copy after login

2. Add Kafka dependency

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-kafka</artifactId>
</dependency>
Copy after login

3. Configure Kafka producer

@Bean
public ProducerFactory<String, String> senderFactory() {
    Map<String, Object> config = new LinkedHashMap<>();
    config.put(ProducerConfig.BOOTSTRAP_ certification_URL_setConfig, "kafka://127.0.0.1:9092");
    config.put(ProducerConfig.KEY_SERIALIZER_setClass_Config, StringDeserializer.class);
    config.put(ProducerConfig.KEY_SERIALIZER_setClass_Config, StringDeserializer.class);
    return new SimpleKafkaProducerFactory<>(config);
}
Copy after login

4. Configure Kafka consumer

@Bean
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
    ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
    factory.setBrokerAddresses("127.0.0.1:9092");
    factory.setKeyDeserializer(new StringDeserializer());
    factory.setKeyDeserializer(new StringDeserializer());
    return factory;
}
Copy after login

5. Create Kafka producer service

@Service
public class ProducerService {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    public void sendMessage(String message) {
        kafkaTemplate.send("test-kafka", message);
    }
}
Copy after login

6. Create Kafka consumer service

@Service
public class ReceiverService {

    @KafkaListener(topics = "test-kafka", id = "kafka-consumer-1")
    public void receiveMessage(String message) {
        System.out.println("Message received: " + message);
    }
}
Copy after login

Test

  1. Start the Kafka service.
  2. Start the Springboot application.
  3. Use ProducerService to send a message.
  4. Check the Kafka service log to make sure it has received the information correctly.
  5. Check the Springboot application log to make sure it has consumed the information correctly.

Summary

This article demonstrates how to use Springboot to integrate Kafka into a Spring application. We first gave an overview of Kafka and Springboot, and explained how to build the environment required for Kafka to integrate Springboot. Next, we provide a detailed Springboot application example that demonstrates how to use Springboot to produce and consume Kafka information.

The above is the detailed content of Starting from scratch: Springboot guide to quickly build kafka integrated environment. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template