🏘️ μΉ΄ν”„μΉ΄ ν΄λŸ¬μŠ€ν„° ꡬ좕

덀벨둜퍼·2024λ…„ 3μ›” 22일
0

κ°œμΈκ³΅λΆ€

λͺ©λ‘ 보기
11/11

1. vi docker-compose.yml

λ‘œμ»¬μ— λ‹€μš΄λ°›μ€ μΉ΄ν”„μΉ΄ ν΄λ”μ—μ„œ yml 파일 νŽΈμ§‘

version: '2.1'

services:
  zoo1:
    image: confluentinc/cp-zookeeper:latest
    hostname: zoo1
    container_name: zoo1
    ports:
      - "2182:2181"
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_SERVER_ID: 1
      ZOOKEEPER_SERVERS: zoo1:2888:3888

  kafka1:
    image: confluentinc/cp-kafka:latest
    hostname: kafka1
    container_name: kafka1
    ports:
      - "9093:9093"
      - "29093:29093"
    environment:
      KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka1:19093,EXTERNAL://localhost:9093,DOCKER://host.docker.internal:29093
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT,DOCKER:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL
      KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181"
      KAFKA_BROKER_ID: 1
      KAFKA_LOG4J_LOGGERS: "kafka.controller=INFO,kafka.producer.async.DefaultEventHandler=INFO,state.change.logger=INFO"
      KAFKA_AUTHORIZER_CLASS_NAME: kafka.security.authorizer.AclAuthorizer
      KAFKA_ALLOW_EVERYONE_IF_NO_ACL_FOUND: "true"
    depends_on:
      - zoo1

  kafka2:
    image: confluentinc/cp-kafka:latest
    hostname: kafka2
    container_name: kafka2
    ports:
      - "9094:9094"
      - "29094:29094"
    environment:
      KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka2:19094,EXTERNAL://localhost:9094,DOCKER://host.docker.internal:29094
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT,DOCKER:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL
      KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181"
      KAFKA_BROKER_ID: 2
      KAFKA_LOG4J_LOGGERS: "kafka.controller=INFO,kafka.producer.async.DefaultEventHandler=INFO,state.change.logger=INFO"
      KAFKA_AUTHORIZER_CLASS_NAME: kafka.security.authorizer.AclAuthorizer
      KAFKA_ALLOW_EVERYONE_IF_NO_ACL_FOUND: "true"
    depends_on:
      - zoo1

  kafka3:
    image: confluentinc/cp-kafka:latest
    hostname: kafka3
    container_name: kafka3
    ports:
      - "9095:9095"
      - "29095:29095"
    environment:
      KAFKA_ADVERTISED_LISTENERS: INTERNAL://kafka3:19095,EXTERNAL://localhost:9095,DOCKER://host.docker.internal:29095
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INTERNAL:PLAINTEXT,EXTERNAL:PLAINTEXT,DOCKER:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: INTERNAL
      KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181"
      KAFKA_BROKER_ID: 3
      KAFKA_LOG4J_LOGGERS: "kafka.controller=INFO,kafka.producer.async.DefaultEventHandler=INFO,state.change.logger=INFO"
      KAFKA_AUTHORIZER_CLASS_NAME: kafka.security.authorizer.AclAuthorizer
      KAFKA_ALLOW_EVERYONE_IF_NO_ACL_FOUND: "true"
    depends_on:
      - zoo1

둜컬 2182 포트둜 주킀퍼 접속 (λ‚΄λΆ€μ μœΌλ‘  2181)
μΉ΄ν”„μΉ΄ 1 : 9093 포트
μΉ΄ν”„μΉ΄ 2 : 9094 포트
μΉ΄ν”„μΉ΄ 3 : 9095 포트

    ports:
      - "9095:9095"
      - "29095:29095"

9095:9095: 이 뢀뢄은 호슀트의 9095 포트λ₯Ό μ»¨ν…Œμ΄λ„ˆμ˜ 9095 포트둜 맀핑
-> ν˜ΈμŠ€νŠΈμ—μ„œ Kafka λΈŒλ‘œμ»€μ— 접속할 λ•Œ 9095 포트λ₯Ό μ‚¬μš©ν•œλ‹€λŠ” 의미
29095:29095: Docker λ‚΄λΆ€ λ„€νŠΈμ›Œν¬μ—μ„œμ˜ 포트 맀핑을 μ •μ˜
-> μ»¨ν…Œμ΄λ„ˆ λ‚΄λΆ€μ—μ„œ Kafka λΈŒλ‘œμ»€μ— 접속할 λ•Œ μ‚¬μš©ν•  포트 지정.
μ»¨ν…Œμ΄λ„ˆ λ‚΄λΆ€μ—μ„œ 29095 포트λ₯Ό μ‚¬μš©ν•˜μ—¬ Kafka λΈŒλ‘œμ»€μ— 접속할 수 있음.

1. μ‹€ν–‰

$docker-compose -f docker-compose.yml up -d # yml에 ν¬ν•¨λœ μ»¨ν…Œμ΄λ„ˆλ“€ μ˜¬λ¦¬λŠ” λͺ…λ Ήμ–΄
$docker ps # docker ps λͺ…λ Ήμ–΄λ‘œ 잘 λŒμ•„κ°€λ‚˜ 확인

2. 토픽생성

# 둜컬 μΉ΄ν”„μΉ΄ ν΄λ”λ‚΄μ—μ„œ
./bin/kafka-topics.sh --create \
    --replication-factor 3 \
    --partitions 3 \
    --topic app-push-topic \
    --bootstrap-server localhost:9093,localhost:9094,localhost:9095
  • "app-push-topic"μ΄λΌλŠ” 토픽을 μƒμ„±ν•˜λ©°, 각 νŒŒν‹°μ…˜μ—λŠ” 3개의 볡제본이 있고, νŒŒν‹°μ…˜ μˆ˜λŠ” 3개
  • μ‹€ν–‰ν•˜λ©΄ Kafka ν΄λŸ¬μŠ€ν„° λ‚΄μ—μ„œ ν•΄λ‹Ή 토픽이 μƒμ„±λ˜κ³ , κ΄€λ ¨λœ 브둜컀 간에 νŒŒν‹°μ…˜ λ³΅μ œκ°€ 섀정됨
# ν† ν”½ 상세 쑰회
./bin/kafka-topics.sh --describe \
    --topic app-push-topic \
    --bootstrap-server localhost:9093,localhost:9094,localhost:9095

#κ²°κ³Ό
Topic: app-push-topic	TopicId: C__MUpdpTv-Ej1s8Empw8A	PartitionCount: 3	ReplicationFactor: 3	Configs:
	Topic: app-push-topic	Partition: 0	Leader: 1	Replicas: 1,2,3	Isr: 2,3,1
	Topic: app-push-topic	Partition: 1	Leader: 2	Replicas: 2,3,1	Isr: 2,3,1
	Topic: app-push-topic	Partition: 2	Leader: 3	Replicas: 3,1,2	Isr: 3,2,1

  • Partition: 0, 1, 2: 각 νŒŒν‹°μ…˜μ˜ 번호
  • Leader: 1, 2, 3: 각 νŒŒν‹°μ…˜μ˜ 리더 브둜컀의 ID
  • Replicas: 1,2,3, 2,3,1, 3,1,2: 각 νŒŒν‹°μ…˜μ— λŒ€ν•œ 볡제본(Replica)의 브둜컀 ID λͺ©λ‘. μˆœμ„œλŠ” μ€‘μš”ν•˜μ§€ μ•ŠμŒ!
  • Isr: 2,3,1, 2,3,1, 3,2,1: ν˜„μž¬ 인-μ‹œν¬(In-Sync Replica) λͺ©λ‘. 각 νŒŒν‹°μ…˜μ˜ 리더 브둜컀λ₯Ό ν¬ν•¨ν•œ ν˜„μž¬ 볡제본의 λͺ©λ‘

πŸ“ŒΒ μΈ-μ‹œν¬λž€?
Kafkaμ—μ„œ 각 νŒŒν‹°μ…˜μ€ μ—¬λŸ¬ 개의 λ³΅μ œλ³Έμ„ κ°€μ§ˆ 수 있음, 이 μ€‘μ—μ„œ ν•˜λ‚˜λŠ” 리더이며, λ‚˜λ¨Έμ§€λŠ” νŒ”λ‘œμ›Œ 볡제본. 리더 νŒŒν‹°μ…˜μ€ λ©”μ‹œμ§€λ₯Ό μ“°κ³  μ†ŒλΉ„ν•˜λŠ” 역할을 μˆ˜ν–‰ν•˜λ©°, νŒ”λ‘œμ›Œ λ³΅μ œλ³Έλ“€μ€ 리더와 λ™κΈ°ν™”λ˜μ–΄ μžˆμ–΄ 리더에 μž₯μ• κ°€ λ°œμƒν–ˆμ„ λ•Œ λŒ€μ²΄ κ°€λŠ₯.
β†’ 인-μ‹œν¬λŠ” ν˜„μž¬ λ©”μ‹œμ§€λ₯Ό λ”°λΌκ°€λŠ” λ™κΈ°ν™”λœ μƒνƒœμ— μžˆλŠ” λ³΅μ œλ³Έλ“€μ„ λ‚˜νƒ€λƒ„. 즉, 인-μ‹œν¬μ— μ†ν•œ λ³΅μ œλ³Έλ“€μ€ 리더와 λ™μΌν•œ λ©”μ‹œμ§€λ₯Ό 가지고 있으며, λ°μ΄ν„°μ˜ 일관성을 보μž₯ν•˜λŠ”κ²ƒ
β†’ λ§Œμ•½ 리더 νŒŒν‹°μ…˜μ—μ„œ λ©”μ‹œμ§€λ₯Ό μ†‘μ‹ ν•˜κ³  μžˆλŠ”λ° νŒ”λ‘œμ›Œ 쀑 ν•˜λ‚˜κ°€ λ™κΈ°ν™”λ˜μ§€ λͺ»ν•œ μƒνƒœλΌλ©΄ ν•΄λ‹Ή λ³΅μ œλ³Έμ€ 인-μ‹œν¬μ—μ„œ μ œμ™Έλ¨.

μ‰˜ 4개 이제 μ—΄μ–΄μ„œ , 1개 ν”„λ‘œλ“€μ„œ 3개 컨슈머 μ‹€ν–‰

# consumer jar 파일 μžˆλŠ” λ””λ ‰ν† λ¦¬μ—μ„œ
java -jar consumer-2.3.0.RELEASE.jar

# producer jar 파일 μžˆλŠ” λ””λ ‰ν† λ¦¬μ—μ„œ
java -jar producer-2.3.0.RELEASE.jar

postman으둜 TEST



λΆ„μ‚°μ²˜λ¦¬ ν™•μΈμ™„λ£Œ


# 컨슈머 그룹 리슀트 쑰회
./bin/kafka-consumer-groups.sh --list \
    --bootstrap-server localhost:9093,localhost:9094,localhost:9095

app-push-group

# 컨슈머 κ·Έλ£Ή μƒμ„Έμ‘°νšŒ
./bin/kafka-consumer-groups.sh --describe \
    --group app-push-group \
    --bootstrap-server localhost:9093,localhost:9094,localhost:9095

GROUP           TOPIC           PARTITION  CURRENT-OFFSET  LOG-END-OFFSET  LAG             CONSUMER-ID                                                    HOST            CLIENT-ID
app-push-group  app-push-topic  0          264             264             0               consumer-app-push-group-1-a9d6852b-1f81-400b-8337-6466a1f0aab2 /192.168.65.1   consumer-app-push-group-1
app-push-group  app-push-topic  2          248             248             0               consumer-app-push-group-1-dc22572f-eddf-4ed2-8299-d6da8db4edfa /192.168.65.1   consumer-app-push-group-1
app-push-group  app-push-topic  1          270             270             0               consumer-app-push-group-1-c17f0f35-d5a2-48d4-8952-9c9f9cc820fd /192.168.65.1   consumer-app-push-group-1
  • GROUP | Topic | PARTITION : μ‘°νšŒν•œ 컨슈머 그룹이 λ§ˆμ§€λ§‰μœΌλ‘œ μ»€λ°‹ν•œ ν† ν”½κ³Ό νŒŒν‹°μ…˜μ„ λ‚˜νƒ€λƒ„
  • CURRENT-OFFSET : 컨슈머 그룹이 κ°€μ Έκ°„ ν† ν”½μ˜ νŒŒν‹°μ…˜μ— κ°€μž₯ μ΅œμ‹  μ˜€ν”„μ…‹(offset)이 λͺ‡λ²ˆ 인지 λ‚˜νƒ€λƒ„
    • μ˜€ν”„μ…‹ : νŒŒν‹°μ…˜μ˜ 각 λ ˆμ½”λ“œμ— ν• λ‹Ήλœ 번호 , 데이터가 νŒŒν‹°μ…˜μ— λ“€μ–΄μ˜¬λ•Œλ§ˆλ‹€ 1μ”© 증가
  • LOG-END-OFFSET : ν•΄λ‹Ή 컨슈머 그룹의 μ»¨μŠˆλ¨Έκ°€ μ–΄λŠ μ˜€ν”„μ…‹κΉŒμ§€ μ»€λ°‹ν–ˆλŠ”μ§€ μ•Œ 수 μžˆλ‹€
    • CURRENT-OFFSET ≀ LOG-END-OFFSET
  • LAG : 컨슈머 그룹이 ν† ν”½μ˜ νŒŒν‹°μ…˜μ— μžˆλŠ” 데이터λ₯Ό κ°€μ Έκ°€λŠ”λ° μ–Όλ§ˆλ‚˜ 지연이 λ°œμƒν•˜λ‚˜ λ‚˜νƒ€λ‚΄λŠ” μ§€ν‘œ
    • LOG-END-OFFSET(컨슈머 그룹이 μ»€λ°‹ν•œ μ˜€ν”„μ…‹) - CURRENT-OFFSET(ν•΄λ‹Ή νŒŒν‹°μ…˜μ˜ κ°€μž₯ μ΅œμ‹  μ˜€ν”„μ…‹) = LAG
  • CONSUMER-ID : 컨슈머의 할당을 λ‚΄λΆ€μ μœΌλ‘œ κ΅¬λΆ„ν•˜κΈ° μœ„ν•œ id
  • HOST : μ»¨μŠˆλ¨Έκ°€ λ™μž‘ν•˜λŠ” host λͺ… , 이 값을 톡해 컨슈머의 호슀트λͺ… λ˜λŠ” ipλ₯Ό μ•Œμ•„ λ‚Ό 수 μžˆλ‹€.
  • CLIENT-ID : μ»¨μŠˆλ¨Έμ— ν• λ‹Ήλœ id , μ‚¬μš©μžκ°€ 지정가λŠ₯ , μ§€μ •ν•˜μ§€ μ•ŠμœΌλ©΄ μžλ™μƒμ„±λ¨
profile
πŸ’ͺ 점진적 κ³ΌλΆ€ν•˜λ‘œ μ„±μž₯ν•˜λŠ” 개발자

0개의 λŒ“κΈ€