Kafka
This section will introduce how to use the Kafka component.
Introduction
The Kafka component is a simple wrapper around the segmentio/kafka-go
library, allowing developers to ignore connection details.
When Go-Sail starts, if enabled, it will automatically initialize the Kafka component. After that, developers can directly call it using the sail
keyword.
import (
"github.com/keepchen/go-sail/v3/sail"
)
func main() {
connections := sail.GetKafkaConnections()
instance := sail.GetKafkaInstance()
reader := sail.GetKafkaReader()
writer := sail.GetKafkaWriter()
}
Usage
Others
For more native methods, please refer to the official documentation of segmentio/kafka-go.
Advanced
New Instance
In some specific scenarios, developers may need to create a new Kafka instance separately. In this case, you can use the syntactic sugar provided by Go-Sail to create a new instance.
import (
"github.com/keepchen/go-sail/v3/lib/kafka"
"github.com/keepchen/go-sail/v3/sail"
)
func main() {
conf := kafka.Conf{....}
sail.NewKafkaConnections(conf)
}
The new instance will no longer be managed by Go-Sail, so developers need to manage its lifecycle themselves, such as closing or releasing connections.