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

73 lines
2.5 KiB
Go
Raw Normal View History

2021-05-26 19:37:10 +08:00
package kafka
import (
2022-09-16 11:57:01 +08:00
"Open_IM/pkg/common/config"
log "Open_IM/pkg/common/log"
2022-06-16 10:26:37 +08:00
"Open_IM/pkg/utils"
2022-06-08 17:17:55 +08:00
"errors"
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
promePkg "Open_IM/pkg/common/prometheus"
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
}
2022-06-09 13:12:46 +08:00
func (p *Producer) SendMessage(m proto.Message, key string, operationID string) (int32, int64, error) {
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")
}
2022-06-09 13:12:46 +08:00
a, b, c := p.producer.SendMessage(kMsg)
log.Info(operationID, "ByteEncoder SendMessage end", "key ", kMsg.Key.Length(), kMsg.Value.Length(), p.producer)
2022-09-15 01:22:20 +08:00
if c == nil {
promePkg.PromeInc(promePkg.SendMsgCounter)
}
2022-06-16 10:26:37 +08:00
return a, b, utils.Wrap(c, "")
2021-05-26 19:37:10 +08:00
}