IT, 개발/서버

Kafka 우분투에 설치 방법(kafka 설치, topic 생성, 통신 테스트)

개발자스터디 2022. 9. 6.
728x90
반응형

 

 

 

 

kafka

Kafka는 서버 클러스터 내에서 데이터 스트림을 레코드로 유지하는 방식으로 작동하는 브로커 기반 솔루션입니다. 

오늘은 서버에 Kafka 설치 및 세팅하는 방법과 간단한 테스트 예제를 확인해보도록 하겠습니다.

 

 

1. Kafka 다운로드

 

https://www.apache.org/dyn/closer.cgi?path=/kafka/3.2.1/kafka_2.13-3.2.1.tgz

 

Apache Downloads

We suggest the following site for your download: https://dlcdn.apache.org/kafka/3.2.1/kafka_2.13-3.2.1.tgz Alternate download locations are suggested below. It is essential that you verify the integrity of the downloaded file using the PGP signature (.asc

www.apache.org

 

위 링크를 통해 다운로드할 수 있습니다.

 

 

2. 서버 세팅

 

다운로드한 파일을 서버에서 압축 풀어줍니다.

$ tar -xzf 다운로드받은파일.tgz

 

압축을 푼 폴더 내부에서 우리가 확인할 부분은 bin 폴더와 config 폴더입니다.

 

외부와 통신을 하고자 한다면 properties 파일을 수정해야 합니다.

$ vi config/server.properties

파일 내용 중 일부분

# Hostname and port the broker will advertise to producers and consumers. If not set,
# it uses the value for "listeners" if configured.  Otherwise, it will use the value
# returned from java.net.InetAddress.getCanonicalHostName().
#advertised.listeners=PLAINTEXT://your.host.name:9092

 

위의 문서 일부분에서 마지막 부분 주석 풀고 your.host.name 부분에 IP 주소를 입력합니다.

예) advertised.listeners=PLAINTEXT://123.456.789:9092

 

외부 통신을 위해서는 포트를 열어줘야 하는데 이 부분은 다음에 자세하게 확인해보도록 하겠습니다.

이번에는 서버 내부에서 통신하는 것부터 확인하겠습니다.

 

 

728x90

 

3. Kafka 실행

 

세팅이 끝났다면 Kafka를 실행하고 테스트해보도록 하겠습니다.

 

먼저 zookeeper와 kafka를 실행합니다.

(아래 명령어에서 bin, config 폴더는 아까 압축 풀었던 kafka폴더 내부의 폴더입니다.)

 

zookeeper 실행

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

 

kafka 실행

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

 

실행 후 토픽을 생성합니다.

 

Topic 생성

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

TOPICNAME 부분에 자신이 원하는 토픽 이름을 입력합니다.

 

Topic 목록 확인

$ sh ./bin/kafka-topics.sh --list --bootstrap-server localhost:9092

 

토픽을 생성했다면 잘 생성되었는지 확인해보겠습니다.

토픽 세부내용은 아래 명령어로 확인할 수 있습니다.

$ sh ./bin/kafka-topics.sh --describe --topic TOPICNAME --bootstrap-server localhost:9092

 

 

4. Kafka 통신 테스트

 

토픽까지 생성했으니 Producer/Consumer를 테스트해보도록 하겠습니다.

 

터미널 창 두 개를 열어서 한쪽에는 Producer, 다른 한쪽에는 Consumer를 실행시켜서 테스트하면 됩니다.

 

Producer

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

 

Consumer

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

 

 

Producer 실행한 쪽에서 메시지를 입력하여 Enter키를 눌렀을 때 Consumer 쪽에 메시지가 출력되는지 확인해보시기 바랍니다.

 

 

 

 

 

 

728x90
반응형