01
introduce
NSQ is a real-time distributed messaging platform (also known as message queue) written in Golang language. It is mainly composed of three daemons: nsqd, nsqlookupd and nsqadmin. Nsqd is the core component, which is responsible for processing client requests, such as production, sorting and consumption messages; Nsqlookupd is responsible for managing cluster topology information and providing a final consistent discovery service. Nsqadmin is a management platform of web interface, which can be used to view cluster information and perform other management operations in real time. A single nsqd can contain many topics, and each topic can contain many channel s.
NSQ supports cross platform and multilingual clients. Readers who use Mac can easily install NSQ using brew. In this article, we mainly introduce go NSQ, the official golang client provided by NSQ. For more information about NSQ, interested readers and friends can refer to the official documents. Limited to space, this article will not repeat it.
To operate NSQ with go NSQ, you need to install the go NSQ library, which provides many functions and methods for operating NSQ.
Installation method:
// Mac installation nsq brew install nsq // Install go NSQ go get -u github.com/nsqio/go-nsq
02
producer
Producer type in go NSQ package, used to send messages to NSQ. First, you need to call the function NewProducer to create a producer instance. The received parameters are addr of string type and nsq.Config of pointer type (NSQ configuration information). The returned result is the address and error of a producer instance.
It should be noted that the function NewConfig must be called to return a pointer type nsq.Config, which contains the default configuration information. The configuration information can be Set by calling the Set method of the instance, and must be Set before it is used for parameter transmission, otherwise the Set configuration information will not take effect.
Producer contains many methods. This article mainly introduces four methods: Ping, String, Publish and Stop. The ping method is used to detect whether the producer is successfully connected and its configured nsqd; The String method returns the address of the nsqd connected by the producer; The Publish method is used to synchronously send messages to the specified topic; The Stop method is used to gracefully Stop the producer.
Example code:
// Default configuration information config := nsq.NewConfig() // Create producer producer, err := nsq.NewProducer("127.0.0.1:4150", config) if err != nil { log.Fatal(err) } // Verify that the generator connection was successful err = producer.Ping() if err != nil { log.Fatal(err) } // Return producer address producerAddr := producer.String() log.Printf("producerAddr:%v", producerAddr) messageBody := []byte("hello") topicName := "topic" // Send messages to the specified topic synchronously err = producer.Publish(topicName, messageBody) if err != nil { log.Fatal(err) } producer.Stop()
Operation results:
2021/10/23 18:58:23 INF 1 (127.0.0.1:4150) connecting to nsqd 2021/10/23 18:58:23 producerAddr:127.0.0.1:4150 2021/10/23 18:58:23 INF 1 stopping 2021/10/23 18:58:23 INF 1 exiting router
03
consumer
Consumer type in go NSQ package, used to consume messages from NSQ. First, you need to call the function NewConsumer to create a consumer instance. The received parameters are topic and channel of string type and nsq.Config (NSQ configuration information) of pointer type. The returned result is the address and error of a consumer instance.
It should be noted that the function NewConfig must be called to return a pointer type nsq.Config, which contains the default configuration information. The configuration information can be Set by calling the Set method of the instance, and must be Set before it is used for parameter transmission, otherwise the Set configuration information will not take effect.
Consumer contains many methods. This article mainly introduces three methods: Stats, AddHandler and ConnectToNSQD. The Stats method is used to retrieve the current connection and message statistics of the consumer; AddHandler is used to set processing functions for messages consumed by consumers. Each processing function runs independently in a goroutine. If you need to start multiple goroutines to run processing functions, you can call AddHandler multiple times; ConnectToNSQD is used to connect the nsqd of the configuration.
Example code:
config := nsq.NewConfig() // Create Consumer consumer, err := nsq.NewConsumer("topic", "channel", config) if err != nil { log.Fatal(err) } consumerStats := consumer.Stats() log.Printf("consumerStats:%+v", consumerStats) // Add processors to the Consumer. You can add multiple processors. Each Handler runs in a separate goroutine consumer.AddHandler(&myMessageHandler{}) // Connect nsqd err = consumer.ConnectToNSQD("127.0.0.1:4150") if err != nil { log.Fatal(err) } <-consumer.StopChan
Operation results:
2021/10/23 18:59:30 consumerStats:&{MessagesReceived:0 MessagesFinished:0 MessagesRequeued:0 Connections:0} 2021/10/23 18:59:30 INF 1 [topic/channel] (127.0.0.1:4150) connecting to nsqd 2021/10/23 18:59:30 hello
04
summary
This paper mainly introduces the go NSQ client of NSQ, a real-time distributed messaging platform written in golang language. It is the NSQ Golang client officially provided by NSQ. It also introduces the simple use methods of Producer and Consumer respectively. You can use go NSQ flexibly according to your business needs. For more information about go NSQ, interested readers can read the official documents.