mklfarha / sarama-fx

FX wrapper for the sarama go client for Apache Kafka

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sarama fx

Go Reference

FX wrapper for the sarama go client for Apache Kafka

Getting started

Checkout the examples directory in the repo.

Quickstart

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

Consuming

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 {
    ...
}

Producing

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"),
	})

About

FX wrapper for the sarama go client for Apache Kafka

License:MIT License


Languages

Language:Go 100.0%