Giter Club home page Giter Club logo

gokafka's Introduction

GOKafka

Easy and flexible Kafka Library for GO.

Software License

Introduction

GOKafka provides a simple, straight-forward implementation for working with Kafka inside GO applications.

Requirements

  • GO >= 1.17

Installation

You can install the library via GO get:

$ go get github.com/leroy-merlin-br/gokafka

And add the env`s for with:

KAFKA_BROKERS="example.southamerica-east1.gcp.confluent.cloud:9092"
KAFKA_VERSION=2.1.1
KAFKA_GROUP="kafka-group-example"
KAFKA_TOPICS="EXAMPLE-TOPIC-V1"
KAFKA_ASSIGNOR="range"
KAFKA_AUTHENTICATION_TYPE="sasl_ssl"

# to use sasl_ssl authentication
KAFKA_USERNAME=
KAFKA_PASSWORD=

# to use ssl authentication
KAFKA_AUTHENTICATION_TYPE=
KAFKA_AUTHENTICATION_CA=
KAFKA_AUTHENTICATION_KEY=
KAFKA_AUTHENTICATION_CERTIFICATE=

If you will use Avro Schema, you must add this env`s too:

AVRO_SCHEMA_URL="https://sr-southamerica-east1.streaming.data.cloud"
AVRO_SCHEMA_USERNAME=
AVRO_SCHEMA_PASSWORD=

Avro Schema Quick Usage Guide

Create kafka_adapter.go

import (
	"github.com/leroy-merlin-br/gokafka"
	"github.com/linkedin/goavro"
	"github.com/rs/zerolog/log"
	"lmbr/saleorder/domain"
)

type KafkaAdapter struct {
}

type User struct {
	Id   string
	Name string
}

// Adapts a message action to Kafka Action
func (adapter *KafkaAdapter) Receive(saleOrderAction domain.Action) error {
	action := func(record *goavro.Record) error {
		decodedRecord := adapter.Transform(record)

		log.Print(decodedRecord)

		return nil
	}

	consumer := gokafka.Consumer{
		Ready:  make(chan bool),
		Action: action,
	}

	return gokafka.Handle(consumer)
}

// Adapts a message (kafka Record in this case) to a User
func (adapter *KafkaAdapter) Transform(record *goavro.Record) User {
	id, _ := record.Get("id")
	name, _ := record.Get("name")

	user := User{
		Id:   id.(string),
		Name: name.(string),
	}

	return user
}

Create worker.go

func main() {
    messageService := KafkaAdapter{}
    err := messageService.Receive(saveAction)
    if err != nil {
        log.Error().Err(err).Msg("Receive handler error occurs.")
        return err
    }
    
    return nil
}

License

GOKafka is free software distributed under the terms of the MIT license

Additional information

GOKafka was proudly built by the Leroy Merlin Brazil team. See all the contributors.

gokafka's People

Contributors

djonasm avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.