refactor(infra-monitoring)
refactor(infra-gateway)
This commit is contained in:
+24
-43
@@ -1,57 +1,38 @@
|
||||
package at.mocode.infrastructure.messaging.config
|
||||
|
||||
import org.apache.kafka.clients.consumer.ConsumerConfig
|
||||
import org.apache.kafka.clients.producer.ProducerConfig
|
||||
import org.apache.kafka.common.serialization.StringDeserializer
|
||||
import org.apache.kafka.common.serialization.StringSerializer
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.kafka.core.DefaultKafkaProducerFactory
|
||||
import org.springframework.kafka.core.KafkaTemplate
|
||||
import org.springframework.kafka.core.ProducerFactory
|
||||
import org.springframework.kafka.support.serializer.JsonDeserializer
|
||||
import org.springframework.kafka.support.serializer.JsonSerializer
|
||||
|
||||
@Configuration
|
||||
/**
|
||||
* Central Kafka producer configuration used across modules.
|
||||
*
|
||||
* This class can be instantiated programmatically (as done in tests) or
|
||||
* registered as a Spring @Configuration with @Bean methods in an application context.
|
||||
*/
|
||||
class KafkaConfig {
|
||||
|
||||
// KORREKTUR: Von lateinit zu einer public var mit Standardwert, um Tests zu ermöglichen
|
||||
@Value($$"${spring.kafka.bootstrap-servers:localhost:9092}")
|
||||
/**
|
||||
* Comma-separated list of host:port pairs used for establishing the initial connection to the Kafka cluster.
|
||||
*/
|
||||
var bootstrapServers: String = "localhost:9092"
|
||||
|
||||
@Value("\${spring.kafka.consumer.group-id:meldestelle-group}")
|
||||
private lateinit var consumerGroupId: String
|
||||
/**
|
||||
* Common producer properties with sensible defaults (String keys, JSON values).
|
||||
*/
|
||||
fun producerConfigs(): Map<String, Any> = mapOf(
|
||||
ProducerConfig.BOOTSTRAP_SERVERS_CONFIG to bootstrapServers,
|
||||
ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG to StringSerializer::class.java,
|
||||
ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG to JsonSerializer::class.java,
|
||||
// Avoid adding type info headers; keeps payloads simple and interoperable.
|
||||
JsonSerializer.ADD_TYPE_INFO_HEADERS to false
|
||||
)
|
||||
|
||||
@Bean
|
||||
fun producerFactory(): ProducerFactory<String, Any> {
|
||||
val configProps = mapOf(
|
||||
ProducerConfig.BOOTSTRAP_SERVERS_CONFIG to bootstrapServers,
|
||||
ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG to StringSerializer::class.java,
|
||||
ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG to JsonSerializer::class.java,
|
||||
ProducerConfig.ACKS_CONFIG to "all",
|
||||
ProducerConfig.RETRIES_CONFIG to 3,
|
||||
ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG to true,
|
||||
ProducerConfig.MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION to 1
|
||||
)
|
||||
return DefaultKafkaProducerFactory(configProps)
|
||||
}
|
||||
|
||||
@Bean
|
||||
fun kafkaTemplate(producerFactory: ProducerFactory<String, Any>): KafkaTemplate<String, Any> {
|
||||
return KafkaTemplate(producerFactory)
|
||||
}
|
||||
|
||||
// NEU: Stellt eine zentrale Map mit den Basis-Konfigurationen für alle Consumer bereit.
|
||||
@Bean
|
||||
fun kafkaConsumerConfiguration(): Map<String, Any> {
|
||||
return mapOf(
|
||||
ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG to bootstrapServers,
|
||||
ConsumerConfig.GROUP_ID_CONFIG to consumerGroupId,
|
||||
ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG to StringDeserializer::class.java,
|
||||
ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG to JsonDeserializer::class.java,
|
||||
ConsumerConfig.AUTO_OFFSET_RESET_CONFIG to "earliest", // Beginne davon am Anfang, wenn kein Offset existiert
|
||||
JsonDeserializer.TRUSTED_PACKAGES to "*" // Erlaube Deserialisierung aller unserer Klassen
|
||||
)
|
||||
}
|
||||
/**
|
||||
* Strongly typed producer factory to avoid unchecked casts in consumers/tests.
|
||||
*/
|
||||
fun producerFactory(): DefaultKafkaProducerFactory<String, Any> =
|
||||
DefaultKafkaProducerFactory(producerConfigs())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user