|
| 1 | +package com.loopers.confg.kafka; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import org.apache.kafka.clients.consumer.ConsumerConfig; |
| 5 | +import org.springframework.boot.autoconfigure.kafka.KafkaProperties; |
| 6 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 7 | +import org.springframework.context.annotation.Bean; |
| 8 | +import org.springframework.context.annotation.Configuration; |
| 9 | +import org.springframework.kafka.annotation.EnableKafka; |
| 10 | +import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; |
| 11 | +import org.springframework.kafka.core.*; |
| 12 | +import org.springframework.kafka.listener.ContainerProperties; |
| 13 | +import org.springframework.kafka.support.converter.BatchMessagingMessageConverter; |
| 14 | +import org.springframework.kafka.support.converter.ByteArrayJsonMessageConverter; |
| 15 | + |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +@EnableKafka |
| 20 | +@Configuration |
| 21 | +@EnableConfigurationProperties(KafkaProperties.class) |
| 22 | +public class KafkaConfig { |
| 23 | + public static final String BATCH_LISTENER = "BATCH_LISTENER_DEFAULT"; |
| 24 | + |
| 25 | + public static final int MAX_POLLING_SIZE = 3000; // read 3000 msg |
| 26 | + public static final int FETCH_MIN_BYTES = (1024 * 1024); // 1mb |
| 27 | + public static final int FETCH_MAX_WAIT_MS = 5 * 1000; // broker waiting time = 5s |
| 28 | + public static final int SESSION_TIMEOUT_MS = 60 * 1000; // session timeout = 1m |
| 29 | + public static final int HEARTBEAT_INTERVAL_MS = 20 * 1000; // heartbeat interval = 20s ( 1/3 of session_timeout ) |
| 30 | + public static final int MAX_POLL_INTERVAL_MS = 2 * 60 * 1000; // max poll interval = 2m |
| 31 | + |
| 32 | + @Bean |
| 33 | + public ProducerFactory<Object, Object> producerFactory(KafkaProperties kafkaProperties) { |
| 34 | + Map<String, Object> props = new HashMap<>(kafkaProperties.buildProducerProperties()); |
| 35 | + return new DefaultKafkaProducerFactory<>(props); |
| 36 | + } |
| 37 | + |
| 38 | + @Bean |
| 39 | + public ConsumerFactory<Object, Object> consumerFactory(KafkaProperties kafkaProperties) { |
| 40 | + Map<String, Object> props = new HashMap<>(kafkaProperties.buildConsumerProperties()); |
| 41 | + return new DefaultKafkaConsumerFactory<>(props); |
| 42 | + } |
| 43 | + |
| 44 | + @Bean |
| 45 | + public KafkaTemplate<Object, Object> kafkaTemplate(ProducerFactory<Object, Object> producerFactory) { |
| 46 | + return new KafkaTemplate<>(producerFactory); |
| 47 | + } |
| 48 | + |
| 49 | + @Bean |
| 50 | + public ByteArrayJsonMessageConverter jsonMessageConverter(ObjectMapper objectMapper) { |
| 51 | + return new ByteArrayJsonMessageConverter(objectMapper); |
| 52 | + } |
| 53 | + |
| 54 | + @Bean(name = BATCH_LISTENER) |
| 55 | + public ConcurrentKafkaListenerContainerFactory<Object, Object> defaultBatchListenerContainerFactory( |
| 56 | + KafkaProperties kafkaProperties, |
| 57 | + ByteArrayJsonMessageConverter converter |
| 58 | + ) { |
| 59 | + Map<String, Object> consumerConfig = new HashMap<>(kafkaProperties.buildConsumerProperties()); |
| 60 | + consumerConfig.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, MAX_POLLING_SIZE); |
| 61 | + consumerConfig.put(ConsumerConfig.FETCH_MIN_BYTES_CONFIG, FETCH_MIN_BYTES); |
| 62 | + consumerConfig.put(ConsumerConfig.FETCH_MAX_WAIT_MS_CONFIG, FETCH_MAX_WAIT_MS); |
| 63 | + consumerConfig.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, SESSION_TIMEOUT_MS); |
| 64 | + consumerConfig.put(ConsumerConfig.HEARTBEAT_INTERVAL_MS_CONFIG, HEARTBEAT_INTERVAL_MS); |
| 65 | + consumerConfig.put(ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG, MAX_POLL_INTERVAL_MS); |
| 66 | + |
| 67 | + ConcurrentKafkaListenerContainerFactory<Object, Object> factory = new ConcurrentKafkaListenerContainerFactory<>(); |
| 68 | + factory.setConsumerFactory(new DefaultKafkaConsumerFactory<>(consumerConfig)); |
| 69 | + factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL); // 수동 커밋 |
| 70 | + factory.setBatchMessageConverter(new BatchMessagingMessageConverter(converter)); |
| 71 | + factory.setConcurrency(3); |
| 72 | + factory.setBatchListener(true); |
| 73 | + return factory; |
| 74 | + } |
| 75 | +} |
0 commit comments