Producers
package main import ( "fmt" "github.com/nsqio/go-nsq" ) func main() { nsqdTcpAddress := "172.16.30.129:4150" config := nsq.NewConfig() tPro, err := nsq.NewProducer(nsqdTcpAddress, config) if err != nil { fmt.Println(err) } // topic topic := "topic_test" // message tCommand := "new message!" //发布消息 err = tPro.Publish(topic, []byte(tCommand)) if err != nil { fmt.Println(err) } }
consumer
main Package Import ( " github.com/nsqio/go-nsq " " FMT " " Sync " ) // implement interface method HandleMessage type NsqHandler struct { // The field information defined in their business needs } // Message message processing func ( * NsqHandler S) HandleMessage (message * nsq.Message) error { // process the message, where only print fmt.Println ( " message content: " , String (message.Body)) return nil } FUNC main () { // Initial configuration config: =nsq.NewConfig () // create consumer, designated Topic, Channel topicName: = " topic_test " channelName: = " test_channel " COM, ERR: = nsq.NewConsumer (topicName, channelName, config) IF ERR =! nil { fmt. println (ERR) } // adding process callback com.AddHandler (& NsqHandler {}) // connected to the corresponding nsqd nsqdTcpAddress: = " 172.16.30.129:4150 " ERR = com.ConnectToNSQD (nsqdTcpAddress) IF ERR =! nil { fmt.Println (ERR) } // in order not the end of this process, there is no sense var WG = & sync.WaitGroup {} wg.Add ( . 1 ) wg.Wait () }