Kafka로 메시지와 이벤트 처리하기 - (1) Kafka 세팅하기 Message and Event Processing with Kafka (1) - Kafka Setup
이번 글은 카프카, 데이터 플랫폼의 최강자 라는 책을 보면서 간단히 작성 해 봤다. 간단한 실행 및 예제 프로그래밍은 책보다 공식홈페이지에 있는 문서 를 참고했다.
Apache Kafka

Apache Kafka는 실시간으로 기록 스트림을 게시, 구독, 저장 및 처리할 수 있는 분산 데이터 스트리밍 플랫폼이다. 이는 여러 소스에서 데이터 스트림을 처리하고 여러 사용자에게 전달하도록 설계가 되었다. 간단히 말해, A지점에서 B지점까지 이동하는 것뿐만 아니라 A지점에서 B지점을 비롯해 필요한 모든 곳에서 대규모 데이터를 동시에 이동할 수 있습니다.
Kafka를 사용 할 때 짚고 넘어 갈 간단한 개념
-
ZooKeeper
Apache Zookeeper는 분산 코디네이션 서비스를 제공하는 오픈 소스.
어플리케이션에서 스케쥴링 및 작업 조율을 직접 하지 않고 zookeeper가 조율을 도와준다. 안정성 확보를 위해서 클러스터로 구축이 되며 클러스터는 보통 홀수개로 구축.
-
Topic
데이터의 ‘주제’ 라고 생각하면 쉽게 이해 할 수 있다. 데이터의 주제 / 이벤트를 Topic으로 생성하고 해당하는 Topic에 데이터를 보내고 읽을 수 있다.
예를 들어 Topic을 temperature로 설정하고, temperature Topic에 관련된 데이터를 보내고 읽을 수 있다.
-
Producer
데이터를 제공하는 쪽. “떠드는 쪽” 이라고 해도 괜찮을 것 같다. 데이터를 생성하고, 이벤트를 생성하는 쪽 입니다. 설정 된 Topic에 데이터를 제공한다.
-
Consumer
데이터를 소비하는 쪽. 즉, 데이터를 필요로 하는 쪽. 데이터나 이벤트의 발생을 보고 분석하거나 저장하는 쪽으로 이어주는 역할을 할 수 도 있다.
Kafka 설치 및 실행하기
실행시키고 확인 해야 할 서비스가 많아서.. 터미널을 좀 많이 켜야겠네요 하핳…
설치 (Download)
https://www.apache.org/dyn/closer.cgi?path=/kafka/2.8.0/kafka_2.13-2.8.0.tgz
위의 페이지에서 Kafka를 받고, 적절한 위치로 이동시켜 주자.
Unzip
tar -xzf kafka_2.13-2.8.0.tgz
cd kafka_2.13-2.8.0
Kafka 실행을 위한 Zookeeper, Broker 실행하기
// kafka_2.13-2.8.0 폴더 내의 위치에서 실행하자
// zookeeper
$ bin/zookeeper-server-start.sh config/zookeeper.properties
// broker service
$ bin/kafka-server-start.sh config/server.properties
메시지 발송을 위한 Topic 생성하기
// Kafka myevent Topic 생성
$ bin/kafka-topics.sh --create --topic myevent --bootstrap-server localhost:9092
// myevent Topic 구독 현황 확인하기
$ bin/kafka-topics.sh --describe --topic myevent --bootstrap-server localhost:9092
Topic에 메시지 발송하기 (command line interface에서)
$ bin/kafka-console-producer.sh --topic myevent --bootstrap-server localhost:9092
>
kafka-console-producer.sh 를 실행하면, 메시지를 계속 생성하고 보내는 역할을 한다. 때문에 콘솔창은 input이 가능한 형태로 계속 열려있다.
Topic에 발송 한 메시지 읽어오기(command line interface에서)
bin/kafka-console-consumer.sh --topic myevent --from-beginning --bootstrap-server localhost:9092
위의 kafka-console-producer.sh 에서 작성 한 데이터가 출력된다.
This post was written while reading the book Kafka, The Definitive Guide. For simple execution and example programming, I referred to the official documentation rather than the book.
Apache Kafka

Apache Kafka is a distributed data streaming platform that can publish, subscribe, store, and process record streams in real-time. It is designed to handle data streams from multiple sources and deliver them to multiple consumers. In simple terms, it can move large amounts of data simultaneously not just from point A to point B, but from point A to point B and everywhere else it’s needed.
Key Concepts to Understand When Using Kafka
-
ZooKeeper
Apache Zookeeper is an open-source project that provides distributed coordination services.
Instead of applications handling scheduling and task coordination directly, Zookeeper helps with coordination. Clusters are built for stability, and clusters are typically built with an odd number of nodes.
-
Topic
Think of it as the ‘subject’ of data. You create a data subject/event as a Topic and can send and read data from that Topic.
For example, you can set a Topic as ‘temperature’ and send and read data related to the temperature Topic.
-
Producer
The side that provides data. You could also call it “the talker.” It’s the side that generates data and creates events. It provides data to the configured Topic.
-
Consumer
The side that consumes data. In other words, the side that needs the data. It can also serve as a bridge to analyze or store data and events as they occur.
Installing and Running Kafka
Since there are many services to run and verify… I’ll need to open quite a few terminals, haha…
Download
https://www.apache.org/dyn/closer.cgi?path=/kafka/2.8.0/kafka_2.13-2.8.0.tgz
Download Kafka from the page above and move it to an appropriate location.
Unzip
tar -xzf kafka_2.13-2.8.0.tgz
cd kafka_2.13-2.8.0
Running Zookeeper and Broker for Kafka Execution
// Run from inside the kafka_2.13-2.8.0 folder
// zookeeper
$ bin/zookeeper-server-start.sh config/zookeeper.properties
// broker service
$ bin/kafka-server-start.sh config/server.properties
Creating a Topic for Message Delivery
// Create Kafka myevent Topic
$ bin/kafka-topics.sh --create --topic myevent --bootstrap-server localhost:9092
// Check myevent Topic subscription status
$ bin/kafka-topics.sh --describe --topic myevent --bootstrap-server localhost:9092
Sending Messages to a Topic (from command line interface)
$ bin/kafka-console-producer.sh --topic myevent --bootstrap-server localhost:9092
>
Running kafka-console-producer.sh continues to generate and send messages. Therefore, the console window stays open in an input-ready state.
Reading Messages Sent to a Topic (from command line interface)
bin/kafka-console-consumer.sh --topic myevent --from-beginning --bootstrap-server localhost:9092
The data written in kafka-console-producer.sh above will be displayed.
댓글남기기