|
| 1 | +package io.f1.backend.global.config; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import org.apache.kafka.clients.consumer.ConsumerConfig; |
| 7 | +import org.apache.kafka.clients.producer.ProducerConfig; |
| 8 | +import org.apache.kafka.common.serialization.StringDeserializer; |
| 9 | +import org.apache.kafka.common.serialization.StringSerializer; |
| 10 | +import org.springframework.beans.factory.annotation.Value; |
| 11 | +import org.springframework.context.annotation.Bean; |
| 12 | +import org.springframework.context.annotation.Configuration; |
| 13 | +import org.springframework.kafka.annotation.EnableKafka; |
| 14 | +import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; |
| 15 | +import org.springframework.kafka.core.ConsumerFactory; |
| 16 | +import org.springframework.kafka.core.DefaultKafkaConsumerFactory; |
| 17 | +import org.springframework.kafka.core.DefaultKafkaProducerFactory; |
| 18 | +import org.springframework.kafka.core.KafkaTemplate; |
| 19 | +import org.springframework.kafka.core.ProducerFactory; |
| 20 | +import org.springframework.kafka.listener.ContainerProperties; |
| 21 | + |
| 22 | +@EnableKafka |
| 23 | +@Configuration |
| 24 | +public class KafkaConfig { |
| 25 | + |
| 26 | + @Value("${spring.kafka.bootstrap-servers}") |
| 27 | + private String bootstrapServers; |
| 28 | + |
| 29 | + @Bean |
| 30 | + public ProducerFactory<String, String> producerFactory() { |
| 31 | + Map<String, Object> properties = new HashMap<>(); |
| 32 | + |
| 33 | + properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); |
| 34 | + properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); |
| 35 | + properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class); |
| 36 | + properties.put(ProducerConfig.ACKS_CONFIG, "all"); |
| 37 | + properties.put(ProducerConfig.RETRIES_CONFIG, 3); |
| 38 | + properties.put(ProducerConfig.BATCH_SIZE_CONFIG, 16384); |
| 39 | + properties.put(ProducerConfig.LINGER_MS_CONFIG, 5); |
| 40 | + |
| 41 | + return new DefaultKafkaProducerFactory<>(properties); |
| 42 | + } |
| 43 | + |
| 44 | + @Bean |
| 45 | + public KafkaTemplate<String, String> kafkaTemplate() { |
| 46 | + return new KafkaTemplate<>(producerFactory()); |
| 47 | + } |
| 48 | + |
| 49 | + @Bean |
| 50 | + public ConsumerFactory<String, String> consumerFactory() { |
| 51 | + Map<String, Object> properties = new HashMap<>(); |
| 52 | + |
| 53 | + properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); |
| 54 | + properties.put(ConsumerConfig.GROUP_ID_CONFIG, "stat-sync-group"); |
| 55 | + properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); |
| 56 | + properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, false); |
| 57 | + properties.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, 100); |
| 58 | + properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); |
| 59 | + properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); |
| 60 | + |
| 61 | + return new DefaultKafkaConsumerFactory<>(properties); |
| 62 | + } |
| 63 | + |
| 64 | + @Bean |
| 65 | + public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() { |
| 66 | + ConcurrentKafkaListenerContainerFactory<String, String> factory = |
| 67 | + new ConcurrentKafkaListenerContainerFactory<>(); |
| 68 | + factory.setConsumerFactory(consumerFactory()); |
| 69 | + factory.getContainerProperties().setAckMode(ContainerProperties.AckMode.MANUAL_IMMEDIATE); |
| 70 | + return factory; |
| 71 | + } |
| 72 | +} |
0 commit comments