refactor: Migrate from monolithic to modular architecture
### **Service-Implementation** - [ ] **Tag 1**: Members-Service REST-API implementieren - [ ] **Tag 2**: Database-Migrations und Repository-Layer - [ ] **Tag 3**: Event-Publishing nach Kafka aktivieren - [ ] **Tag 4**: Horses-Service analog implementieren - [ ] **Tag 5**: Integration-Tests für beide Services - [ ] **Tag 6-7**: Events-Service und Masterdata-Service
This commit is contained in:
+24
@@ -0,0 +1,24 @@
|
||||
package at.mocode.infrastructure.messaging.client
|
||||
|
||||
/**
|
||||
* Interface for publishing domain events to message broker.
|
||||
*/
|
||||
interface EventPublisher {
|
||||
|
||||
/**
|
||||
* Publishes an event to the specified topic.
|
||||
*
|
||||
* @param topic The topic to publish to
|
||||
* @param key The message key (optional)
|
||||
* @param event The event to publish
|
||||
*/
|
||||
suspend fun publishEvent(topic: String, key: String? = null, event: Any)
|
||||
|
||||
/**
|
||||
* Publishes multiple events to the specified topic.
|
||||
*
|
||||
* @param topic The topic to publish to
|
||||
* @param events The events to publish with their keys
|
||||
*/
|
||||
suspend fun publishEvents(topic: String, events: List<Pair<String?, Any>>)
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package at.mocode.infrastructure.messaging.client
|
||||
|
||||
import kotlinx.coroutines.future.await
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.kafka.core.KafkaTemplate
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
/**
|
||||
* Kafka implementation of EventPublisher.
|
||||
*/
|
||||
@Component
|
||||
class KafkaEventPublisher(
|
||||
private val kafkaTemplate: KafkaTemplate<String, Any>
|
||||
) : EventPublisher {
|
||||
|
||||
private val logger = LoggerFactory.getLogger(KafkaEventPublisher::class.java)
|
||||
|
||||
override suspend fun publishEvent(topic: String, key: String?, event: Any) {
|
||||
try {
|
||||
logger.debug("Publishing event to topic '{}' with key '{}'", topic, key)
|
||||
|
||||
val sendResult = if (key != null) {
|
||||
kafkaTemplate.send(topic, key, event).get()
|
||||
} else {
|
||||
kafkaTemplate.send(topic, event).get()
|
||||
}
|
||||
|
||||
logger.info("Successfully published event to topic '{}' with key '{}'", topic, key)
|
||||
} catch (exception: Exception) {
|
||||
logger.error("Failed to publish event to topic '{}' with key '{}'", topic, key, exception)
|
||||
throw exception
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun publishEvents(topic: String, events: List<Pair<String?, Any>>) {
|
||||
try {
|
||||
logger.debug("Publishing {} events to topic '{}'", events.size, topic)
|
||||
|
||||
events.forEach { (key, event) ->
|
||||
publishEvent(topic, key, event)
|
||||
}
|
||||
|
||||
logger.info("Successfully published {} events to topic '{}'", events.size, topic)
|
||||
} catch (exception: Exception) {
|
||||
logger.error("Failed to publish events to topic '{}'", topic, exception)
|
||||
throw exception
|
||||
}
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package at.mocode.infrastructure.messaging.config
|
||||
|
||||
import org.apache.kafka.clients.producer.ProducerConfig
|
||||
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.JsonSerializer
|
||||
|
||||
/**
|
||||
* Kafka configuration for event publishing.
|
||||
*/
|
||||
@Configuration
|
||||
class KafkaConfig {
|
||||
|
||||
@Value("\${spring.kafka.bootstrap-servers:localhost:9092}")
|
||||
private lateinit var bootstrapServers: String
|
||||
|
||||
@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(): KafkaTemplate<String, Any> {
|
||||
return KafkaTemplate(producerFactory())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user