>  기사  >  웹 프론트엔드  >  아파치 카프카 시작하기

아파치 카프카 시작하기

PHPz
PHPz원래의
2024-08-05 22:36:121024검색

Getting Started With Apache Kafka

Apache Kafka는 하루에 수조 개의 이벤트를 처리할 수 있는 강력한 분산 이벤트 스트리밍 플랫폼입니다. 원래 LinkedIn에서 개발하고 2011년 초에 오픈 소스로 공개된 Kafka는 많은 최신 데이터 아키텍처의 중앙 백본으로 발전했습니다. 이 가이드에서는 아키텍처 이해부터 설정 및 기본 작업 수행에 이르기까지 Apache Kafka를 시작하는 데 필요한 모든 것을 안내합니다.

아파치 카프카 소개

Apache Kafka는 실시간 데이터 피드를 처리하도록 설계되었습니다. 데이터 스트림을 처리하기 위한 처리량이 높고 대기 시간이 짧은 플랫폼으로 작동합니다. Kafka는 실시간 스트리밍 데이터 파이프라인과 데이터 스트림에 적응하는 애플리케이션을 구축하는 데 자주 사용됩니다. 일반적인 사용 사례로는 로그 집계, 실시간 분석, 스트림 처리 등이 있습니다.

주요 개념 및 용어

설정 및 작업을 시작하기 전에 Kafka의 몇 가지 주요 개념과 용어를 이해하는 것이 중요합니다.

  • Producer: Kafka 주제에 메시지를 보내는 애플리케이션
  • 소비자: Kafka 주제에서 메시지를 읽는 애플리케이션
  • 주제: 프로듀서가 메시지를 보내는 카테고리 또는 피드 이름입니다.
  • 브로커: Kafka 토픽을 저장하고 서비스하는 Kafka 서버입니다.
  • 파티션: 확장성과 병렬 처리를 위한 주제 분할
  • 오프셋: 파티션 내 각 메시지의 고유 식별자입니다.

아파치 카프카 설정

Apache Kafka 설정에는 필요한 소프트웨어 다운로드, 구성, 서비스 시작 등 여러 단계가 포함됩니다. 이 섹션에서는 Kafka 환경을 원활하게 구축하고 실행할 수 있도록 자세한 연습을 제공합니다.

전제 조건

Kafka 설정을 시작하기 전에 시스템이 다음 전제 조건을 충족하는지 확인하세요.

  1. JDK(Java Development Kit): Kafka에는 Java 8 이상이 필요합니다. 다음 명령을 사용하여 Java 버전을 확인할 수 있습니다.

    java -version
    

    Java가 설치되지 않은 경우 Oracle 웹사이트에서 다운로드하여 설치하거나 Debian 기반 시스템의 경우 apt, macOS의 경우 Brew와 같은 패키지 관리자를 사용할 수 있습니다.

    # For Debian-based systems
    sudo apt update
    sudo apt install openjdk-11-jdk
    
    # For macOS
    brew install openjdk@11
    
  2. Apache ZooKeeper: Kafka는 ZooKeeper를 사용하여 분산 구성 및 동기화를 관리합니다. ZooKeeper는 Kafka와 함께 번들로 제공되므로 별도로 설치할 필요가 없습니다.

카프카 다운로드 및 설치

  1. Kafka 다운로드: 공식 Apache Kafka 다운로드 페이지를 방문하여 최신 버전의 Kafka를 다운로드하세요. 글을 쓰는 시점에서는 Kafka 2.8.0이 최신 안정 릴리스입니다.

    wget https://downloads.apache.org/kafka/2.8.0/kafka_2.13-2.8.0.tgz
    
  2. 다운로드한 파일 추출: 원하는 디렉터리에 tar 파일을 추출합니다.

    tar -xzf kafka_2.13-2.8.0.tgz
    cd kafka_2.13-2.8.0
    
  3. ZooKeeper 시작: Kafka를 실행하려면 ZooKeeper가 필요합니다. 제공된 구성 파일을 사용하여 ZooKeeper 서비스를 시작하세요.

    bin/zookeeper-server-start.sh config/zookeeper.properties
    

    ZooKeeper는 기본 포트 2181에서 시작되어야 합니다. ZooKeeper가 실행 중임을 나타내는 로그 메시지가 표시되어야 합니다.

  4. Kafka Broker 시작: 새 터미널 창을 열고 제공된 구성 파일을 사용하여 Kafka 브로커를 시작합니다.

    bin/kafka-server-start.sh config/server.properties
    

    Kafka는 기본 포트 9092에서 시작되어야 합니다. Kafka 브로커가 실행 중임을 나타내는 로그 메시지가 표시되어야 합니다.

카프카 구성

기본 구성은 개발 및 테스트에 적합하지만 프로덕션 환경에 맞게 설정을 맞춤설정해야 할 수도 있습니다. 일부 주요 구성 파일은 다음과 같습니다.

  • server.properties: 이 파일에는 브로커 ID, 로그 디렉터리, 리스너 등 Kafka 브로커에 대한 구성이 포함되어 있습니다.
  • zookeeper.properties: 이 파일에는 데이터 디렉터리, 클라이언트 포트 등 ZooKeeper 구성이 포함되어 있습니다.

이러한 구성 파일을 필요에 맞게 편집할 수 있습니다. 예를 들어, 로그 디렉터리를 변경하려면 server.properties 파일에서 log.dirs 속성을 편집하면 됩니다.

log.dirs=/path/to/your/kafka-logs

시스템 서비스 파일 생성

특히 Linux 서버에서 관리를 쉽게 하기 위해 ZooKeeper 및 Kafka용 시스템 서비스 파일을 생성할 수 있습니다. 이를 통해 systemctl을 사용하여 이러한 서비스를 시작, 중지 및 다시 시작할 수 있습니다.

  1. ZooKeeper 서비스 파일: /etc/systemd/system/ 디렉터리에 Zookeeper.service라는 파일을 생성합니다.

    [Unit]
    Description=Apache ZooKeeper
    After=network.target
    
    [Service]
    Type=simple
    ExecStart=/path/to/kafka/bin/zookeeper-server-start.sh /path/to/kafka/config/zookeeper.properties
    ExecStop=/path/to/kafka/bin/zookeeper-server-stop.sh
    Restart=on-abnormal
    
    [Install]
    WantedBy=multi-user.target
    
  2. Kafka 서비스 파일: /etc/systemd/system/ 디렉터리에 kafka.service라는 파일을 만듭니다.

    [Unit]
    Description=Apache Kafka
    After=zookeeper.service
    
    [Service]
    Type=simple
    ExecStart=/path/to/kafka/bin/kafka-server-start.sh /path/to/kafka/config/server.properties
    ExecStop=/path/to/kafka/bin/kafka-server-stop.sh
    Restart=on-abnormal
    
    [Install]
    WantedBy=multi-user.target
    
  3. Enable and Start Services: Enable and start the services using systemctl:

    sudo systemctl enable zookeeper
    sudo systemctl start zookeeper
    
    sudo systemctl enable kafka
    sudo systemctl start kafka
    

    You can now manage ZooKeeper and Kafka using standard systemctl commands (start, stop, status, restart).

Verifying the Installation

To verify that your Kafka setup is working correctly, you can perform some basic operations such as creating a topic, producing messages, and consuming messages.

  1. Creating a Topic:

    bin/kafka-topics.sh --create --topic test-topic --bootstrap-server localhost:9092 --partitions 1 --replication-factor 1
    

    You should see a confirmation message indicating that the topic has been created successfully.

  2. Producing Messages:

    bin/kafka-console-producer.sh --topic test-topic --bootstrap-server localhost:9092
    

    Type a few messages in the console and press Enter after each message.

  3. Consuming Messages:
    Open a new terminal window and run:

    bin/kafka-console-consumer.sh --topic test-topic --from-beginning --bootstrap-server localhost:9092
    

    You should see the messages you produced in the previous step.

By following these steps, you should have a fully functional Apache Kafka environment set up on your system. This setup forms the foundation for developing and deploying real-time data streaming applications using Kafka.

Conclusion

Getting started with Apache Kafka can seem daunting, but with the right guidance, you can quickly get up to speed. This guide provided a comprehensive introduction to Kafka, from installation to basic operations and building simple producers and consumers. As you continue to explore Kafka, you will uncover its full potential for building robust, real-time data pipelines.

By following this guide, you’ve taken the first steps in mastering Apache Kafka. Happy streaming!

위 내용은 아파치 카프카 시작하기의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.