feat kafka 改版 1

This commit is contained in:
2026-01-29 16:20:29 +08:00
parent be7b922cbf
commit a2251b9cb7
3 changed files with 31 additions and 10 deletions

View File

@@ -2,6 +2,7 @@ package kafka
import (
"context"
"encoding/json"
"github.com/IBM/sarama"
"go.opentelemetry.io/otel"
otelcodes "go.opentelemetry.io/otel/codes"
@@ -14,10 +15,11 @@ func NewProducer() *Producer {
type Producer struct {
}
func (c *Producer) Produce(ctx context.Context, topic, value string) {
func (c *Producer) Produce(ctx context.Context, data Topic) {
marshal, _ := json.Marshal(data)
msg := &sarama.ProducerMessage{
Topic: topic,
Value: sarama.StringEncoder(value),
Topic: data.Name(),
Value: sarama.ByteEncoder(marshal),
Headers: NewCarrier().Inject(ctx),
}
client.producer.Input() <- msg