카프카 퀵 스타트 해보면서 진행 과정 이슈들을 정리해보려 한다.

1. 카프카 다운로드
https://www.apache.org/dyn/closer.cgi?path=/kafka/3.0.0/kafka_2.13-3.0.0.tgz
여기서 다운받을 수 있다. 

$ wget https://dlcdn.apache.org/kafka/3.0.0/kafka_2.13-3.0.0.tgz  
$ tar -xzf kafka_2.13-3.0.0.tgz
$ cd kafka_2.13-3.0.0


2. 카프카 환경 설정
NOTE: Your local environment must have Java 8+ installed. 
카프카 서버 실행 전에 zookeeper 부터 먼저 동작 시켜야 한다. zookeeper 가 먼저 켜지고 kafka 서버가 실행된다.

# Start the ZooKeeper service
# Note: Soon, ZooKeeper will no longer be required by Apache Kafka.
$ bin/zookeeper-server-start.sh config/zookeeper.properties

# Start the Kafka broker service
$ bin/kafka-server-start.sh config/server.properties

Zookeeper 실행

 

카프카 서버가 실행되면 각종 로그 기록이 저장된다.

3. 카프카 토픽 생성

카프카 브로커에서 토픽 단위로 관리한다. 토픽이 폴더라면, 이벤트를 폴더 안에 파일로 생각할 수 있다. 

$ bin/kafka-topics.sh --create --topic quickstart-events --bootstrap-server localhost:9092

** 혹시 위 명령어 쳤을 때 아래 이슈가 발생한다면, 

Missing required argument "[partitions]"

 

server.properties 에서 아래 partition number를 지정해줄 수 있다. 

# The default number of log partitions per topic. More partitions allow greater
# parallelism for consumption, but this will also result in more files across
# the brokers.
num.partitions=2

또, Missing required argument "[replication-factor]" 가 나오면 CLI 명령어로 직접 수행할 수 있다. 

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

이슈해결 이후 혹은 정상적으로 토픽이 생성되었다면 아래 명령어를 수행하게 되면 topic에 대한 파티션 상태를 확인할 수 있다. 

$ bin/kafka-topics.sh --describe --topic quickstart-events --bootstrap-server localhost:9092
Topic:quickstart-events  PartitionCount:1    ReplicationFactor:1 Configs:
    Topic: quickstart-events Partition: 0    Leader: 0   Replicas: 0 Isr: 0

**토픽 생성할 때 아래처럼 timeout이 발생한다면

Kafka topic creation: Timed out waiting for a node assignment

카프카를 설치한 폴더에서 config > server.properties 로 이동해서 주석 처리되어 있는 listeners 를 풀어주면 된다. 설정 파일을 수정하면 zookeeper 를 다시 restart 한다. ( 이때 kafka 서버도 중지되니 다시 켜줘야 한다. 포트가 중복되어 kill -9 처리해준다.)

listeners=PLAINTEXT://localhost:9092
or
listeners=PLAINTEXT://127.0.0.1:9092

 

4. 토픽에 이벤트 쓰기

카프카 토픽은 파티션 단위로 브로커에게 분산된다. 브로커는 메시지 중간 역할을 수행하며, 프로듀서가 메시지를 만들거나 혹은 전달받으면 구독자에게 메시지를 전달하게 된다. 

https://always-kimkim.tistory.com/entry/kafka101-broker

프로듀서를 이용해 토픽에 이벤트를 발행할 수 있다. 발행한 이벤트(메시지)는 브로커를 통해 관리된다. 

$ bin/kafka-console-producer.sh --topic quickstart-events --bootstrap-server localhost:9092
This is my first event
This is my second event

 

5. 토픽 에서 이벤트 읽어오기

발행한 이벤트를 읽어올 때 컨슈머를 통해 읽어오면 된다. 프로듀서가 발행한 순서로 이벤트를 가져와 콘솔에서 확인할 수 있다. 

$ bin/kafka-console-consumer.sh --topic quickstart-events --from-beginning --bootstrap-server localhost:9092
This is my first event
This is my second event

 

6. 카프카 종료

모든 종료는 ctrl+c 를 이용한다. 카프카 브로커를 종료하고 마지막으로 zookeeper를 종료한다. 그리고 test-events 토픽이 발행했던 메시지는 zookeeper 와 카프카가 종료되어도 남아있다. 카프카 종료와 함께 저장된 이벤트 제거하고 싶다면 아래 명령어를 수행한다.

$ rm -rf /tmp/kafka-logs /tmp/zookeeper


참고

https://ckddn9496.tistory.com/67

 

카프카 시작하기 - (1) (Kafka QuickStart - (1))

Kafka 설치부터 간단한 spring-boot 애플리케이션과 연결하여 동작시키는 과정을 정리한 글입니다. Apache Kafka QuickStart 페이지와 CONFLUNET 예제를 참고하였습니다. 1. Kafka 다운로드 Kafka를 다운로드합니.

ckddn9496.tistory.com

https://kafka.apache.org/quickstart

 

Apache Kafka

Apache Kafka: A Distributed Streaming Platform.

kafka.apache.org

https://stackoverflow.com/questions/48986780/kafka-create-topic-with-default-number-of-partitions

 

Kafka create topic with default number of partitions

I am trying to create a new Kafka topic from the command line $ kafka-topics --create --zookeeper localhost:2181 --topic def-test I get the error Missing required argument "[partitions]" From...

stackoverflow.com

 

'카프카' 카테고리의 다른 글

Kafka 개요  (0) 2024.04.29
카프카01 :: 아파치 카프카 개요  (0) 2020.08.12