saramafx

package module
v0.0.0-...-870a2cc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 12, 2024 License: MIT Imports: 8 Imported by: 2

README

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Module = fx.Module("saramafx",
	fx.Provide(

		New,
	),

	fx.Invoke(hook),
)

Module srarma fx module to be provided

Functions

func Consume

func Consume(kc Client)

Consume background thread for the consumer to run

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client for sarama fx

func New

func New(params Params) (*Client, error)

New sarama fx client

func (Client) SendMessage

func (kc Client) SendMessage(req SendMessageRequest) error

SendMessage sends a message to the specified kafka topic

type Config

type Config struct {
	Version         string   `yaml:"version"`
	Brokers         []string `yaml:"brokers"`
	Topics          []string `yaml:"topics"`
	ConsumerGroupID string   `yaml:"consumer_group_id" mapstructure:"consumer_group_id"`
}

Config for the saramafx client

type Params

type Params struct {
	fx.In

	Lifecycle      fx.Lifecycle
	ConfigProvider config.Provider
	// ConsumerGroupHandler needs to be provided by the user of the library
	Handler sarama.ConsumerGroupHandler `optional:"true"`
}

Params to create the client

type SendMessageRequest

type SendMessageRequest struct {
	Topic   string
	Message []byte
}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳