go语言使用nsq(producer)

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))