본문 바로가기
개발

[kafka] NotLeaderForPartitionException 해결 방법

by eun2ce 2022. 8. 6.

카프카를 외부에서 접근할 수 있도록 설정 한 뒤, 접근하려고 할 때 아래와 같은 이슈가 발생했습니다.

Received invalid metadata error in produce request on partition ${TOPIC} due to org.apache.kafka.common.errors.NotLeaderForPartitionException: This server is not the leader for that topic-partition.. Going to request metadata update now

 

Kafka 에서 각 토픽은 여러 브로커에 의해 제공이 되는데, 한 브로커는 리더이고 나머지는 팔로워가 됩니다.

 

producer 의 경우, 내부적으로 모든 팔로워에게 데이터를 복제하는 리더 브로커에 새 메시지를 보내야하기 때문에,

클라이언트가 올바른 브로커에 연결되지 않고 리더 대신 팔로워에 연결 될 경우, 팔로워가 요청을 거부한다고 응답할 수 있습니다.

 

아래와 같이 props 를 추가하여 이 문제를 해결할 수 있습니다.

props.put(ProducerConfig.RETRIES_CONFIG, 10);  //increase to 10 from default of 0
props.put(ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG, Integer.toString(Ineger.MAX_VALUE)); // increase to infinity from default of 300 s