go实现kafka推送

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
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值