FX wrapper for the sarama go client for Apache Kafka
Checkout the examples directory in the repo.
Add the module to your FX application
fx.New(
saramafx.Module,
).Run()
The module uses viper for config management.
It will look for a node called kafka with the following structure:
kafka:
# kafka version running
version: 3.6.0
# consumer group id
consumer_group_id: test-group
# list of brokers
brokers:
- localhost:9092
# list of topics
topics:
- test
Provide a group consumer handler to consume messages in your application
fx.New(
fx.Provide(
NewKafkaHandler,
),
saramafx.Module,
).Run()
// implement the sarama.ConsumerGroupHandler interface
// check out the full example under examples/consumer
func NewKafkaHandler() sarama.ConsumerGroupHandler {
...
}
Inject the saramafx client wherever you need it and use the SendMessage function to produce a message
// full example under examples/producer
kp.client.SendMessage(saramafx.SendMessageRequest{
Topic: "test",
Message: []byte("message from server"),
})