Skip to content

Repository files navigation

core-processor

The project provides a framework for consuming Kafka.

It aims to simplify the logic of data consumption and transmission, and actively provide a configurable and efficient way.

With core and core-processor, we can do this:

package main
import (
"context""encoding/json""fmt""github.com/DoNewsCode/core"
processor "github.com/DoNewsCode/core-processor""github.com/DoNewsCode/core/di""github.com/DoNewsCode/core/otkafka""github.com/segmentio/kafka-go"
)
typeHandlerstruct {
}
funcNewHandlerOut() processor.Out {
returnprocessor.NewOut(
&Handler{},
)
}
typeDatastruct {
IDint`json:"id"`Namestring`json:"name"`
}
func (h*Handler) Info() *processor.Info {
return&processor.Info{
Name: "default", // the reader name is defaultBatchSize: 3,
}
}
func (h*Handler) Handle(ctx context.Context, msg*kafka.Message) (interface{}, error) {
e:=&Data{}
iferr:=json.Unmarshal(msg.Value, &e); err!=nil {
returnnil, err
}
returne, nil
}
func (h*Handler) Batch(ctx context.Context, data []interface{}) error {
for_, e:=rangedata {
fmt.Println(e.(*Data))
}
returnnil
}
funcmain() {
// prepare config and dependenciesc:=core.New(
core.WithInline("kafka.reader.default.brokers", []string{"127.0.0.1:9092"}),
core.WithInline("kafka.reader.default.topic", "test"),
core.WithInline("kafka.reader.default.groupID", "test"),
core.WithInline("kafka.reader.default.startOffset", kafka.FirstOffset),
)
deferc.Shutdown()
c.ProvideEssentials()
c.Provide(otkafka.Providers())
c.AddModuleFunc(processor.New)
// provide your handlersc.Provide(di.Deps{
NewHandlerOut,
})
// start servererr:=c.Serve(context.Background())
iferr!=nil {
panic(err)
}
}

After the above, we just need to add handlers and provide new methods for core. We can use processor.Info to flexibly adjust the operation of the processor.

Have fun!

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages