๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๊ฐœ๋ฐœ ํ™˜๊ฒฝ ์„ค์ •ํ•˜๊ธฐ: Java, Maven/Gradle, Kafka, ๊ทธ๋ฆฌ๊ณ  ์Šคํ”„๋ง ๋ถ€ํŠธ ํ†ตํ•ฉ ๊ฐ€์ด๋“œ

okrestart 2024. 10. 22.

 

๊ฐœ๋ฐœ ํ™˜๊ฒฝ์„ ์ œ๋Œ€๋กœ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์€ ์„ฑ๊ณต์ ์ธ ํ”„๋กœ์ ํŠธ ๊ฐœ๋ฐœ์˜ ์ฒซ ๊ฑธ์Œ์ž…๋‹ˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” Java , Maven/Gradle , Apache Kafka , ๊ทธ๋ฆฌ๊ณ  ์Šคํ”„๋ง ๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ ๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ณผ์ •์„ ๋‹จ๊ณ„๋ณ„๋กœ ์•ˆ๋‚ดํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ๋‹จ๊ณ„๋Š” ์ฝ”๋“œ ์˜ˆ์ œ์™€ ํ•จ๊ป˜ ์ œ๊ณต๋˜์–ด, ๋”ฐ๋ผ ํ•˜๋ฉด์„œ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 


 

 

1. Java์™€ Maven/Gradle ์„ค์น˜

 

Java ์„ค์น˜

Java ๋Š” ์Šคํ”„๋ง ๋ถ€ํŠธ์™€ Kafka์˜ ํ•„์ˆ˜ ๊ธฐ๋ฐ˜์ด ๋˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ž…๋‹ˆ๋‹ค. ์ตœ์‹  JDK(Java Development Kit) ๋ฅผ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ์„ค์น˜ ํ›„, Java ๋ฒ„์ „์„ ํ™•์ธํ•˜๋Š” ๋ช…๋ น์–ด๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค:

java -version

์ด ๋ช…๋ น์–ด๋กœ ํ˜„์žฌ ์„ค์น˜๋œ Java ๋ฒ„์ „์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Java 11 ๋˜๋Š” Java 17 ๋ฒ„์ „์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.

Maven ๋˜๋Š” Gradle ์„ค์น˜

Maven ๊ณผ Gradle ์€ ์ž๋ฐ” ํ”„๋กœ์ ํŠธ์˜ ๋นŒ๋“œ ๋„๊ตฌ ๋กœ, ๊ฐ๊ฐ ํ”„๋กœ์ ํŠธ์˜ ์˜์กด์„ฑ ๊ด€๋ฆฌ์™€ ๋นŒ๋“œ๋ฅผ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค.

 

Maven ์„ค์น˜

Maven ์„ค์น˜ ํ›„, ๋‹ค์Œ ๋ช…๋ น์–ด๋กœ ์„ค์น˜ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

mvn -version

 

Gradle ์„ค์น˜

Gradle์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์„ค์น˜ ํ›„ ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ํ™•์ธํ•ฉ๋‹ˆ๋‹ค:

gradle -v

๋‘˜ ์ค‘ ํ•˜๋‚˜์˜ ๋นŒ๋“œ ๋„๊ตฌ๋ฅผ ์„ ํƒํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์Šคํ”„๋ง ๋ถ€ํŠธ ๋Š” Maven ๊ณผ Gradle ๋ชจ๋‘ ์ง€์›ํ•˜๋ฏ€๋กœ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์— ๋งž์ถฐ ์„ ํƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 


 

2. ๋กœ์ปฌ์— Kafka ์„ค์น˜ ๋ฐ ์‹คํ–‰

 

Kafka ์„ค์น˜

Kafka๋Š” Apache Kafka ๊ณต์‹ ์›น์‚ฌ์ดํŠธ ์—์„œ ๋‹ค์šด๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Kafka๋Š” Zookeeper ์™€ ํ•จ๊ป˜ ๋™์ž‘ํ•˜๋ฏ€๋กœ, Zookeeper๋„ ์„ค์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

# Kafka ๋‹ค์šด๋กœ๋“œ
wget https://downloads.apache.org/kafka/3.0.0/kafka_2.13-3.0.0.tgz
tar -xvzf kafka_2.13-3.0.0.tgz
cd kafka_2.13-3.0.0

Zookeeper ์‹คํ–‰

Kafka๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— Zookeeper ๋ฅผ ๋จผ์ € ์‹คํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

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

Kafka ์‹คํ–‰

Zookeeper๊ฐ€ ์‹คํ–‰๋˜์—ˆ๋‹ค๋ฉด, ์ด์ œ Kafka ๋ธŒ๋กœ์ปค๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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

Kafka๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์‹คํ–‰๋˜๋ฉด, Kafka ๋ธŒ๋กœ์ปค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ค€๋น„๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

 


 

3. ์Šคํ”„๋ง ๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

 

์Šคํ”„๋ง ๋ถ€ํŠธ ์ดˆ๊ธฐํ™”

์Šคํ”„๋ง ๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ๋Š” Spring Initializr ์›น์‚ฌ์ดํŠธ๋ฅผ ํ†ตํ•ด ์‰ฝ๊ฒŒ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ ์‹œ ์•„๋ž˜์™€ ๊ฐ™์€ ์„ค์ •์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค:
- Project : Maven Project (๋˜๋Š” Gradle)
- Language : Java
- Spring Boot Version : 2.5.6
- Dependencies : Spring Web, Spring Kafka

์›น์‚ฌ์ดํŠธ์—์„œ ์„ค์ •ํ•œ ํ›„ ํ”„๋กœ์ ํŠธ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•œ ๋’ค, ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์—์„œ ํ”„๋กœ์ ํŠธ๋ฅผ ์—ด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ

์Šคํ”„๋ง ๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ๊ฐ€ ์ƒ์„ฑ๋˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ธฐ๋ณธ ๊ตฌ์กฐ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

src/
  main/
    java/
      com.example.kafka/
        KafkaApplication.java
    resources/
      application.yml

Kafka ๊ด€๋ จ ์˜์กด์„ฑ ์ถ”๊ฐ€

pom.xml ๋˜๋Š” build.gradle ํŒŒ์ผ์— Kafka ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” Maven ์„ ๊ธฐ์ค€์œผ๋กœ ํ•œ ์˜์กด์„ฑ ์ถ”๊ฐ€ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค:

<dependency>
    <groupId>org.springframework.kafka</groupId>
    <artifactId>spring-kafka</artifactId>
    <version>2.7.0</version>
</dependency>

Gradle ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ build.gradle ํŒŒ์ผ์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค:

implementation 'org.springframework.kafka:spring-kafka:2.7.0'

 


 

4. Kafka ๊ด€๋ จ ์„ค์ • ์ถ”๊ฐ€

์Šคํ”„๋ง ๋ถ€ํŠธ์—์„œ Kafka ์„ค์ • ์„ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋จผ์ €, application.yml ํŒŒ์ผ์— Kafka ๋ธŒ๋กœ์ปค ์„ค์ •์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

application.yml ์„ค์ • ์˜ˆ์ œ

spring:
  kafka:
    bootstrap-servers: localhost:9092
    consumer:
      group-id: my-group
      auto-offset-reset: earliest
    producer:
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.apache.kafka.common.serialization.StringSerializer

์ด ์„ค์ •์€ Kafka ๋ธŒ๋กœ์ปค์— ์—ฐ๊ฒฐํ•˜์—ฌ ํ”„๋กœ๋“€์„œ ์™€ ์ปจ์Šˆ๋จธ ๊ฐ€ ๋ฉ”์‹œ์ง€๋ฅผ ์ฃผ๊ณ ๋ฐ›์„ ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค๋‹ˆ๋‹ค.

 


 

5. Kafka ๋ฉ”์‹œ์ง€ ์ „์†ก ๋ฐ ์ˆ˜์‹  ์˜ˆ์ œ

 

Kafka ํ”„๋กœ๋“€์„œ ์˜ˆ์ œ

@Service
public class KafkaProducer {
    private static final Logger logger = LoggerFactory.getLogger(KafkaProducer.class);
    private final KafkaTemplate<String, String> kafkaTemplate;

    @Autowired
    public KafkaProducer(KafkaTemplate<String, String> kafkaTemplate) {
        this.kafkaTemplate = kafkaTemplate;
    }

    public void sendMessage(String message) {
        logger.info(String.format("#### -> Producing message -> %s", message));
        this.kafkaTemplate.send("my_topic", message);
    }
}

Kafka ์ปจ์Šˆ๋จธ ์˜ˆ์ œ

@Service
public class KafkaConsumer {
    private static final Logger logger = LoggerFactory.getLogger(KafkaConsumer.class);

    @KafkaListener(topics = "my_topic", groupId = "my-group")
    public void consume(String message) {
        logger.info(String.format("#### -> Consumed message -> %s", message));
    }
}

์ด ์ฝ”๋“œ๋Š” Kafka ํ”„๋กœ๋“€์„œ ๊ฐ€ ๋ฉ”์‹œ์ง€๋ฅผ my_topic ์œผ๋กœ ๋ณด๋‚ด๊ณ , ์ปจ์Šˆ๋จธ ๊ฐ€ ํ•ด๋‹น ๋ฉ”์‹œ์ง€๋ฅผ ์†Œ๋น„ํ•˜๋Š” ๊ณผ์ •์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค. ์ด๋Š” ์‹ค์ œ ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ํ™˜๊ฒฝ์—์„œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 


 

๊ฐœ๋ฐœ ํ™˜๊ฒฝ ์„ค์ • ํŠธ๋ Œ๋“œ ๋ฐ ํ†ต๊ณ„

์ตœ๊ทผ ํ†ต๊ณ„์— ๋”ฐ๋ฅด๋ฉด, ๋งŽ์€ ๊ฐœ๋ฐœ์ž๋“ค์ด ์Šคํ”„๋ง ๋ถ€ํŠธ ์™€ Kafka ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ์•„ํ‚คํ…์ฒ˜ ๋ฅผ ๊ตฌ์ถ•ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 2023๋…„ Stack Overflow ์„ค๋ฌธ์กฐ์‚ฌ ์— ๋”ฐ๋ฅด๋ฉด, ์Šคํ”„๋ง ๋ถ€ํŠธ๋Š” ๊ฐ€์žฅ ์ธ๊ธฐ ์žˆ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ ์ค‘ ํ•˜๋‚˜์ด๋ฉฐ, Kafka๋Š” ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๋ฅผ ์œ„ํ•œ ๋Œ€ํ‘œ์ ์ธ ์†”๋ฃจ์…˜์œผ๋กœ ์ž๋ฆฌ ์žก๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ํŠนํžˆ ๊ธˆ์œต, IT, IoT ์‚ฐ์—… ์—์„œ Kafka์˜ ์‚ฌ์šฉ์ด ๊ธ‰์ฆํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 


 

์ž์ฃผ ๋ฌป๋Š” ์งˆ๋ฌธ (FAQ)

Q1. Kafka๋Š” ์™œ ํ•„์š”ํ•œ๊ฐ€์š”?
A1. Kafka๋Š” ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ๋ฅผ ์‹ค์‹œ๊ฐ„์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์–ด, ์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜ ์‹œ์Šคํ…œ์ด๋‚˜ ๋กœ๊ทธ ์ฒ˜๋ฆฌ์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

Q2. ์Šคํ”„๋ง ๋ถ€ํŠธ์—์„œ Kafka ์„ค์ •์ด ์–ด๋ ต๋‚˜์š”?
A2. ์Šคํ”„๋ง ๋ถ€ํŠธ๋Š” Kafka์™€์˜ ํ†ตํ•ฉ์„ ์œ„ํ•ด ์ž๋™ ์„ค์ •์„ ์ œ๊ณตํ•˜๋ฏ€๋กœ, ์„ค์ •์ด ๋งค์šฐ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.

Q3. Kafka๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด Zookeeper๋„ ์„ค์น˜ํ•ด์•ผ ํ•˜๋‚˜์š”?
A3. ๋„ค, Kafka๋Š” Zookeeper์™€ ํ•จ๊ป˜ ๋™์ž‘ํ•˜๋ฏ€๋กœ, Kafka๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— Zookeeper๋ฅผ ๋จผ์ € ์„ค์น˜ํ•˜๊ณ  ์‹คํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

Q4. Kafka ํ”„๋กœ๋“€์„œ์™€ ์ปจ์Šˆ๋จธ๋Š” ์–ด๋–ค ์—ญํ• ์„ ํ•˜๋‚˜์š”?
A4. ํ”„๋กœ๋“€์„œ๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด๋Š” ์—ญํ• ์„, ์ปจ์Šˆ๋จธ๋Š” ๊ทธ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.

Q5. ์Šคํ”„๋ง ๋ถ€ํŠธ์—์„œ Kafka ํ†ตํ•ฉ์„ ์œ„ํ•œ ํ•„์ˆ˜ ์˜์กด์„ฑ์€ ๋ฌด์—‡์ธ๊ฐ€์š”?
A5. spring-kafka ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์Šคํ”„๋ง ๋ถ€ํŠธ์—์„œ Kafka๋ฅผ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋Œ“๊ธ€