美文网首页
go语言使用nsq(producer)

go语言使用nsq(producer)

作者: 王炎杰 | 来源:发表于2018-04-20 20:27 被阅读0次

    NSQ(https://github.com/bitly/nsq)是一个基于Go语言的分布式实时消息平台。
    下面是我使用的一个nsq producer的源码例子。希望帮到需要的朋友。

    package main
    
    import (
    
        "fmt"
        "flag"
    
        "github.com/crackcomm/nsqueue/producer"
    )
    
    var (
        nsqdAddr        = flag.String("nsqd", "127.0.0.1:4150", "nsqd http address")
    )
    
    // UploadMsg represent the msg sent to download task.
    type UploadMsg struct {
        Bucket       string
        Object       string
        ContentType  string
        Author       string
        Size         int64
    
        SrcFilePath     string   //源路径
        DstFilePath    string   //目标路径
    }
    
    // SendTask - to send upload task to nsq.
    func SendTask(msg *UploadMsg) error {
        myProducer := producer.New()
        myProducer.Connect(*nsqdAddr)
    
        myProducer.PublishJSON("latency-test", msg)
        myProducer.Stop()
        return nil
    }
    
    func main() {
        flag.Parse()
        var msg UploadMsg
        msg.Bucket = "new_bucket"
        msg.Object = "new_object"
        msg.ContentType = "type"
        msg.Author = "bary"
        msg.Size = 666
        msg.SrcFilePath = "/tmp/qidong"
        msg.DstFilePath = "/raid/qidong"
        err := SendTask(&msg)
        if err != nil {
            fmt.Println(err.Error())
        }
        fmt.Println("Done !")
    }
    

    相关文章

      网友评论

          本文标题:go语言使用nsq(producer)

          本文链接:https://www.haomeiwen.com/subject/liizkftx.html