kafka生产者
package pusher
import (
"encoding/json"
"parkingEasy/g"
"sync"
"strings"
"time"
"github.com/Shopify/sarama"
"github.com/astaxie/beego"
)
var (
address []string
driveInOrdertopic string
driveOutOrdertopic string
producer sarama.AsyncProducer
)
const (
PUSH_INTERVAL = 1
)
func init() {
hosts := beego.AppConfig.String("kafka_host")
hostParam := strings.Split(hosts, ",")
if len(hostParam) == 0 {
g.Log.Error("[order_pusher][init]:kafka主机地址配置错误")
return
}
address = hostParam
driveInOrdertopic = beego.AppConfig.String("kafka_drive_in_order_topic")
driveOutOrdertopic = beego.AppConfig.String("kafka_drive_out_order_topic")
initProducer(address)
//开启推送定时器
go startPusherTimer()
}
func initProducer(address []string) {
config := sarama.NewConfig()
config.Producer.Return.Successes = true
config.Producer.Timeout = 5 * time.Second
var err error
producer, err = sarama.NewAsyncProducer(address, config)
if err != nil {
g.Log.Error("创建kafka producer失败:%s", err.Error())
return
}
var wg sync.WaitGroup
var successes, errors int
wg.Add(2) //2 goroutine
// 发送成功message计数
go func() {
defer wg.Done()
for range producer.Successes() {
successes++
}
}()
// 发送失败计数
go func() {
defer wg.Done()
for err := range producer.Errors() {
g.Log.Error("%s 发送失败,err:%s\n", err.Msg, err.Err)
errors++
}
}()
}
func startPusherTimer() {
timer := time.NewTimer(10 * time.Second)
for {
select {
case <-timer.C:
{
//item 为推送的实体,这里就不写业务解析
pusherMessage(driveInOrdertopic, item)
timer.Reset(PUSH_INTERVAL * time.Minute)
}
}
}
}
func pusherMessage(topic string, value interface{}) {
data, err := json.Marshal(value)
if err != nil {
g.Log.Error("[kafka_producer][sendMessage]:%s", err.Error())
return
}
msg := &sarama.ProducerMessage{
Topic: topic,
Value: sarama.ByteEncoder(data),
}
producer.Input() <- msg
}