68 lines
2.3 KiB
Go
68 lines
2.3 KiB
Go
|
package kafka
|
||
|
|
||
|
import (
|
||
|
"finclip-app-manager/infrastructure/config"
|
||
|
"finclip-app-manager/infrastructure/logger"
|
||
|
"github.com/Shopify/sarama"
|
||
|
cluster "github.com/bsm/sarama-cluster"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
log = logger.GetLogger()
|
||
|
producer sarama.SyncProducer
|
||
|
consumerConfig *cluster.Config
|
||
|
)
|
||
|
|
||
|
func Start() {
|
||
|
|
||
|
if config.GetConfig().KafkaAddr == "" {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
version, err := sarama.ParseKafkaVersion(config.GetConfig().KafkaVersion)
|
||
|
if err != nil {
|
||
|
log.Errorln("kafka err:" + err.Error())
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
kafkaConfig := sarama.NewConfig()
|
||
|
// 等待服务器所有副本都保存成功后的响应
|
||
|
kafkaConfig.Producer.RequiredAcks = sarama.WaitForAll
|
||
|
// 随机的分区类型:返回一个分区器,该分区器每次选择一个随机分区
|
||
|
kafkaConfig.Producer.Partitioner = sarama.NewRandomPartitioner
|
||
|
// 是否等待成功和失败后的响应
|
||
|
kafkaConfig.Producer.Return.Successes = true
|
||
|
kafkaConfig.Producer.Return.Errors = true
|
||
|
kafkaConfig.Version = version
|
||
|
|
||
|
if config.GetConfig().KafkaUser != "" && config.GetConfig().KafkaPwd != "" {
|
||
|
kafkaConfig.Net.SASL.Enable = true
|
||
|
kafkaConfig.Net.SASL.User = config.Cfg.KafkaUser
|
||
|
kafkaConfig.Net.SASL.Password = config.Cfg.KafkaPwd
|
||
|
kafkaConfig.Net.SASL.Handshake = true
|
||
|
if config.GetConfig().KafkaMechanism == sarama.SASLTypeSCRAMSHA256 {
|
||
|
kafkaConfig.Net.SASL.SCRAMClientGeneratorFunc = func() sarama.SCRAMClient { return &XDGSCRAMClient{HashGeneratorFcn: SHA256} }
|
||
|
kafkaConfig.Net.SASL.Mechanism = sarama.SASLTypeSCRAMSHA256
|
||
|
} else if config.GetConfig().KafkaMechanism == sarama.SASLTypeSCRAMSHA512 {
|
||
|
kafkaConfig.Net.SASL.SCRAMClientGeneratorFunc = func() sarama.SCRAMClient { return &XDGSCRAMClient{HashGeneratorFcn: SHA512} }
|
||
|
kafkaConfig.Net.SASL.Mechanism = sarama.SASLTypeSCRAMSHA512
|
||
|
} else {
|
||
|
kafkaConfig.Net.SASL.Mechanism = sarama.SASLMechanism(config.GetConfig().KafkaMechanism)
|
||
|
}
|
||
|
}
|
||
|
// 使用给定代理地址和配置创建一个同步生产者
|
||
|
producer, err = sarama.NewSyncProducer([]string{config.GetConfig().KafkaAddr}, kafkaConfig)
|
||
|
if err != nil {
|
||
|
log.Errorln("kafka err:" + err.Error())
|
||
|
panic(err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 消费者共同配置
|
||
|
consumerConfig = cluster.NewConfig()
|
||
|
consumerConfig.Consumer.Return.Errors = true
|
||
|
consumerConfig.Group.Return.Notifications = true
|
||
|
consumerConfig.Consumer.Offsets.Initial = sarama.OffsetOldest
|
||
|
consumerConfig.Version = version
|
||
|
}
|