44 lines
987 B
Go
44 lines
987 B
Go
package broker
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/segmentio/kafka-go"
|
|
)
|
|
|
|
func SetUpKafkaConnecter() (*kafka.Conn, error) {
|
|
topic := "styx"
|
|
partition := 0
|
|
conn, err := kafka.DialLeader(context.Background(), "tcp", "localhost:9092", topic, partition)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
conn.SetDeadline(time.Now().Add(10 * time.Second))
|
|
|
|
return conn, nil
|
|
}
|
|
|
|
func ProduceEvent(conn *kafka.Conn, message string) error {
|
|
// SetWriteDeadline sets the deadline for future Write calls and any
|
|
// currently-blocked Write call. Even if write times out, it may return n > 0,
|
|
// indicating that some of the data was successfully written. A zero value for t
|
|
// means Write will not time out.
|
|
conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
|
|
|
|
_, err := conn.WriteMessages(
|
|
kafka.Message{Value: []byte("one!")},
|
|
kafka.Message{Value: []byte("two!")},
|
|
kafka.Message{Value: []byte("three!")},
|
|
)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func consumeEvent() {
|
|
|
|
}
|