Skip to content

Commit 9fc18ca

Browse files
committed
test(be): parition 균등 분배를 위한 파티션 직접 지정
1 parent 7618802 commit 9fc18ca

File tree

1 file changed

+7
-3
lines changed
  • src/backend/chat_server/src/main/java/com/jootalkpia/chat_server/service

1 file changed

+7
-3
lines changed

src/backend/chat_server/src/main/java/com/jootalkpia/chat_server/service/KafkaProducer.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.jootalkpia.chat_server.dto.ChatMessageToKafka;
55
import lombok.RequiredArgsConstructor;
66
import lombok.extern.slf4j.Slf4j;
7+
import org.apache.kafka.clients.producer.ProducerRecord;
78
import org.springframework.beans.factory.annotation.Value;
89
import org.springframework.kafka.core.KafkaTemplate;
910
import org.springframework.stereotype.Service;
@@ -23,10 +24,13 @@ public class KafkaProducer {
2324

2425
public void sendChatMessage(ChatMessageToKafka chatMessageToKafka, Long channelId) {
2526
try {
26-
String randomKey = String.valueOf(ThreadLocalRandom.current().nextInt(1, 4));
27-
27+
int partitionNumber = ThreadLocalRandom.current().nextInt(0, 3);
2828
String jsonChatMessage = objectMapper.writeValueAsString(chatMessageToKafka);
29-
kafkaTemplate.send(topicChat, randomKey, jsonChatMessage)
29+
30+
ProducerRecord<String, String> record =
31+
new ProducerRecord<>(topicChat, partitionNumber, null, jsonChatMessage);
32+
33+
kafkaTemplate.send(record)
3034
.whenComplete((result, ex) -> { //키 값 설정으로 순서 보장, 실시간성이 떨어짐, 고민해봐야 할 부분
3135
if (ex == null) {
3236
log.info("Kafka message sent: {}", result.toString());

0 commit comments

Comments
 (0)