fix: add kafka compress type and producer ack params (#1310)

Signed-off-by: rfyiamcool <rfyiamcool@163.com>
pull/1371/head
fengyun.rui 1 year ago committed by GitHub
parent a580c15f6a
commit d1af343b13
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -85,10 +85,12 @@ type configStruct struct {
} `yaml:"redis"` } `yaml:"redis"`
Kafka struct { Kafka struct {
Username string `yaml:"username"` Username string `yaml:"username"`
Password string `yaml:"password"` Password string `yaml:"password"`
Addr []string `yaml:"addr"` ProducerAck string `yaml:"producerAck"`
TLS *struct { CompressType string `yaml:"compressType"`
Addr []string `yaml:"addr"`
TLS *struct {
CACrt string `yaml:"caCrt"` CACrt string `yaml:"caCrt"`
ClientCrt string `yaml:"clientCrt"` ClientCrt string `yaml:"clientCrt"`
ClientKey string `yaml:"clientKey"` ClientKey string `yaml:"clientKey"`

@ -15,8 +15,10 @@
package kafka package kafka
import ( import (
"bytes"
"context" "context"
"errors" "errors"
"strings"
"time" "time"
"github.com/OpenIMSDK/protocol/constant" "github.com/OpenIMSDK/protocol/constant"
@ -49,8 +51,23 @@ func NewKafkaProducer(addr []string, topic string) *Producer {
p.config = sarama.NewConfig() // Instantiate a sarama Config 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.Successes = true // Whether to enable the successes channel to be notified after the message is sent successfully
p.config.Producer.Return.Errors = true p.config.Producer.Return.Errors = true
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 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
var producerAck = sarama.WaitForAll // default: WaitForAll
switch strings.ToLower(config.Config.Kafka.ProducerAck) {
case "no_response":
producerAck = sarama.NoResponse
case "wait_for_local":
producerAck = sarama.WaitForLocal
case "wait_for_all":
producerAck = sarama.WaitForAll
}
p.config.Producer.RequiredAcks = producerAck
var compress = sarama.CompressionNone // default: no compress
_ = compress.UnmarshalText(bytes.ToLower([]byte(config.Config.Kafka.CompressType)))
p.config.Producer.Compression = compress
if config.Config.Kafka.Username != "" && config.Config.Kafka.Password != "" { if config.Config.Kafka.Username != "" && config.Config.Kafka.Password != "" {
p.config.Net.SASL.Enable = true p.config.Net.SASL.Enable = true
p.config.Net.SASL.User = config.Config.Kafka.Username p.config.Net.SASL.User = config.Config.Kafka.Username

Loading…
Cancel
Save