Files
open-im-server/pkg/common/kafka/producer.go
T

79 lines
3.0 KiB
Go
Raw Normal View History

2021-05-26 19:37:10 +08:00
package kafka
import (
2023-03-02 12:00:31 +08:00
"context"
2022-06-08 17:17:55 +08:00
"errors"
2023-03-16 10:46:06 +08:00
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/constant"
log "github.com/OpenIMSDK/Open-IM-Server/pkg/common/log"
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/tracelog"
"github.com/OpenIMSDK/Open-IM-Server/pkg/utils"
2022-09-15 01:22:20 +08:00
2021-05-26 19:37:10 +08:00
"github.com/Shopify/sarama"
"github.com/golang/protobuf/proto"
2022-09-15 01:22:20 +08:00
2023-03-16 10:46:06 +08:00
prome "github.com/OpenIMSDK/Open-IM-Server/pkg/common/prome"
2021-05-26 19:37:10 +08:00
)
type Producer struct {
topic string
addr []string
config *sarama.Config
producer sarama.SyncProducer
}
func NewKafkaProducer(addr []string, topic string) *Producer {
p := Producer{}
2022-06-15 18:02:48 +08:00
p.config = sarama.NewConfig() //Instantiate a sarama Config
p.config.Producer.Return.Successes = true //Whether to enable the successes channel to be notified after the message is sent successfully
p.config.Producer.Return.Errors = true
2021-07-08 11:56:39 +08:00
p.config.Producer.RequiredAcks = sarama.WaitForAll //Set producer Message Reply level 0 1 all
p.config.Producer.Partitioner = sarama.NewHashPartitioner //Set the hash-key automatic hash partition. When sending a message, you must specify the key value of the message. If there is no key, the partition will be selected randomly
2022-09-16 16:51:12 +08:00
if config.Config.Kafka.SASLUserName != "" && config.Config.Kafka.SASLPassword != "" {
2022-09-16 11:57:01 +08:00
p.config.Net.SASL.Enable = true
2022-09-16 16:51:12 +08:00
p.config.Net.SASL.User = config.Config.Kafka.SASLUserName
p.config.Net.SASL.Password = config.Config.Kafka.SASLPassword
2022-09-16 11:57:01 +08:00
}
2021-05-26 19:37:10 +08:00
p.addr = addr
p.topic = topic
2021-07-08 11:56:39 +08:00
producer, err := sarama.NewSyncProducer(p.addr, p.config) //Initialize the client
2021-05-26 19:37:10 +08:00
if err != nil {
2021-11-26 14:20:50 +08:00
panic(err.Error())
2021-05-26 19:37:10 +08:00
return nil
}
p.producer = producer
return &p
}
2023-03-08 16:35:18 +08:00
func (p *Producer) SendMessage(ctx context.Context, key string, m proto.Message) (int32, int64, error) {
2023-03-02 12:00:31 +08:00
operationID := tracelog.GetOperationID(ctx)
log.Info(operationID, "SendMessage", "key ", key, m.String(), p.producer)
2021-05-26 19:37:10 +08:00
kMsg := &sarama.ProducerMessage{}
kMsg.Topic = p.topic
2022-06-08 17:24:22 +08:00
kMsg.Key = sarama.StringEncoder(key)
2021-05-26 19:37:10 +08:00
bMsg, err := proto.Marshal(m)
if err != nil {
log.Error(operationID, "", "proto marshal err = %s", err.Error())
2021-05-26 19:37:10 +08:00
return -1, -1, err
}
2022-06-08 17:17:55 +08:00
if len(bMsg) == 0 {
log.Error(operationID, "len(bMsg) == 0 ")
2022-06-16 10:26:37 +08:00
return 0, 0, errors.New("len(bMsg) == 0 ")
2022-06-08 17:17:55 +08:00
}
2021-05-26 19:37:10 +08:00
kMsg.Value = sarama.ByteEncoder(bMsg)
log.Info(operationID, "ByteEncoder SendMessage begin", "key ", kMsg, p.producer, "len: ", kMsg.Key.Length(), kMsg.Value.Length())
2022-06-15 18:02:48 +08:00
if kMsg.Key.Length() == 0 || kMsg.Value.Length() == 0 {
log.Error(operationID, "kMsg.Key.Length() == 0 || kMsg.Value.Length() == 0 ", kMsg)
2022-06-15 18:02:48 +08:00
return -1, -1, errors.New("key or value == 0")
}
2023-03-02 12:00:31 +08:00
kMsg.Metadata = ctx
2023-03-03 17:42:26 +08:00
kMsg.Headers = []sarama.RecordHeader{{Key: []byte(constant.OperationID), Value: []byte(operationID)}}
2023-02-23 17:28:57 +08:00
partition, offset, err := p.producer.SendMessage(kMsg)
log.Info(operationID, "ByteEncoder SendMessage end", "key ", kMsg.Key.Length(), kMsg.Value.Length(), p.producer)
2023-02-23 17:28:57 +08:00
if err == nil {
2023-02-24 11:13:16 +08:00
prome.Inc(prome.SendMsgCounter)
2022-09-15 01:22:20 +08:00
}
2023-02-23 17:28:57 +08:00
return partition, offset, utils.Wrap(err, "")
2021-05-26 19:37:10 +08:00
}