Files
spring-boot-demo/src/main/kotlin/com/example/demo/config/KafkaConsumerConfig.kt
Denis Savosin 3fbc94553d move validation config to root-level key
some refactoring
2024-10-11 13:28:04 +07:00

49 lines
1.9 KiB
Kotlin

package com.example.demo.config
import com.example.demo.config.properties.KafkaProperties
import com.example.demo.services.database.city.CityService
import com.example.demo.services.kafka.Consumer
import io.micrometer.core.instrument.MeterRegistry
import org.apache.kafka.clients.consumer.ConsumerConfig
import org.apache.kafka.common.serialization.StringDeserializer
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory
import org.springframework.kafka.core.ConsumerFactory
import org.springframework.kafka.core.DefaultKafkaConsumerFactory
@Configuration
class KafkaConsumerConfig(
@Autowired val kafkaProperties: KafkaProperties
) {
@Bean
fun consumer(
@Autowired cityService: CityService,
@Autowired metricRegistry: MeterRegistry
): Consumer = Consumer(
cityService = cityService,
metricRegistry = metricRegistry,
)
@Bean
fun consumerFactory(): ConsumerFactory<String, String> {
val configs = mapOf(
ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG to kafkaProperties.bootstrapServers,
ConsumerConfig.GROUP_ID_CONFIG to kafkaProperties.consumer.groupId,
ConsumerConfig.AUTO_OFFSET_RESET_CONFIG to kafkaProperties.consumer.autoOffsetReset,
ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG to StringDeserializer::class.java,
ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG to StringDeserializer::class.java
)
return DefaultKafkaConsumerFactory(configs)
}
@Bean
fun kafkaListenerContainerFactory(): ConcurrentKafkaListenerContainerFactory<String, String> {
val factory = ConcurrentKafkaListenerContainerFactory<String, String>()
factory.consumerFactory = consumerFactory()
return factory
}
}