Producer
KP exposes a producer that can produce avro formatted messages, this ensures backwards compatibility through schema registry.
tip
Initializing a producer also publishes the schema to schema registry, this allows you to fail the startup of your application if the schema has a breaking change.
Example
The following example sends a message to a Kafka topic if the type UserLoggedIn
is backwards compatiable.
tip
Please check this page for detailed configuration option
package main
import (
"context"
"github.com/honestbank/kp/v2/producer"
)
type UserLoggedIn struct {
UserID string `avro:"user_id"`
}
func main() {
p, err := producer.New[UserLoggedIn]("topic-name", getConfig())
defer p.Flush()
if err != nil {
panic(err)
}
err = p.Produce(context.Background(), UserLoggedIn{})
if err != nil {
panic(err)
}
}
func getConfig() any {
return nil // return your config
}
If you need to produce Kafka messages with keys, you can use context to pass MessageKey
:
package main
import (
"context"
"github.com/honestbank/kp/v2/producer"
)
type UserLoggedIn struct {
UserID string `avro:"user_id"`
}
func doSomething(p producer.Producer[UserLoggedIn]) error {
return p.Produce(context.WithValue(context.Background(), producer.MessageKey, []byte("my-key")), UserLoggedIn{})
}