Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use nsq message Middleware

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to use nsq message middleware", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use nsq message middleware.

Composition

Nsq is a lightweight messaging middleware. Check the explanation given on the official website of nsq to find out the composition and division of nsq:

Nsqd is the daemon that receives, queues, and delivers messages to clients.

It can be run standalone but is normally configured in a cluster with nsqlookupd instance (s) (in which case it will announce topics and channels for discovery).

It listens on two TCP ports, one for clients and another for the HTTP API. It can optionally listen ona third port for HTTPS.

As can be seen from the above:

Nsqd is a daemon that deals with the client and caches messages from the client

Nsqd can run alone as a single instance, usually with the cooperation of nsqlookupd instances to form a cluster (in cluster scenarios, nsqd can be used to discover topics and channels)

Nsqd listens on two TCP ports, one for the client (default 4150) and the other for HTTP API (default 4151), and the port for HTTPS

Nsqlookupd is the daemon that manages topology information. Clients query nsqlookupd to discover nsqd producers for a specific topic and nsqd nodes broadcasts topic and channel information.

There are two interfaces: A TCP interface which is used by nsqd for broadcasts and an HTTP interface for clients to perform discovery and administrative actions.

As can be seen from the above:

Nsqlookupd is a daemon that manages topology information and allows clients to query nsqd nodes (nsqd broadcast topics and channels information)

Nsqlookupd provides two interfaces, the TCP interface (default 4160) is used by nsqd to send broadcasts, and the HTTP interface (default 4161) is used by clients to discover nsqd and connect to nsqadmin

Nsqadmin is a Web UI to view aggregated cluster stats in realtime and perform various administrative tasks.

As can be seen from the above:

Nsqadmin is a background managed Web process that can browse cluster status in real time and initiate a variety of management tasks (nsqadmin relies on nsqlookupd to handle user operations)

Installation

Here, for quick construction, use docker compose to install (see attachment for docker-compose.yaml)

Copy the docker-compose.yaml to the virtual machine with the following commands:

Start nsqlookupd/nsqadmin/nsqd respectively, corresponding to three containers and port mappings

Http://192.168.1.91:32770 can be opened in the browser to access nsqadmin (virtual machine IP is 192.168.1.91)

test

Package mainimport ("bufio"fmt"github.com/bitly/go-nsq"nsq-demo/src/config"os") var producer * nsq.Producerfunc InitProducer (addr string) {var err error producer, err = nsq.NewProducer (addr, nsq.NewConfig ()) if err! = nil {panic (err)} fmt.Println ("connect to") Producer.String ()} func Publish (topic, msg string) error {if producer = = nil {/ / check producer return fmt.Errorf ("producer is nil")} if msg = "" {/ / void empty msg return nil} return producer.Publish (topic [] byte (msg) / / publish msg} func main () {InitProducer (config.Nsqd01) running: = true reader: = bufio.NewReader (os.Stdin) for running {data, _ _: = reader.ReadLine () command: = string (data) if command = = "stop" {running = false} for err: = Publish (config.Topic, command) Err! = nil; err = Publish (config.Topic, command) {config.ExchangeNsqdIPs () InitProducer (config.Nsqd01)}} producer.Stop ()}

/ / producer directly connects to the nsqd, receives the input from the console, and then sends the message to the nsqd

Package mainimport ("fmt"github.com/bitly/go-nsq"nsq-demo/src/config"time") type MyConsumer struct {} func (* MyConsumer) HandleMessage (msg * nsq.Message) error {/ / implementation Handler interface fmt.Println ("receive from", msg.NSQDAddress, "msg:", string (msg.Body)) return nil} func InitConsumer (topic, channel) Addr string) {conf: = nsq.NewConfig () conf.LookupdPollInterval = time.Second c, err: = nsq.NewConsumer (topic, channel, conf) if err! = nil {panic (err)} c.SetLogger (nil, 0) / set system log c.AddHandler (& MyConsumer {}) / / set Hander to handle msg / / if err: = c.ConnectToNSQLookupd (addr) Err! = nil {/ / panic (err) / /} / / if err: = c.ConnectToNSQDs (config.GetNsqdIPs ()); err! = nil {/ / panic (err) / /} if err: = c.ConnectToNSQD (config.Nsqd01) Err! = nil {panic (err)}} func main () {InitConsumer (config.Topic, config.Channel, config.Lookupd) select {}}

/ / consumer after directly connecting to nsqd, use custom Handler to process messages

Appendix

Version: '3'services: nsqlookupd: image: nsqio/nsq command: / nsqlookupd ports:-"4160" # for the nsqd-"4161" # for the nsqadmin nsqd: image: nsqio/nsq command: / nsqd-- lookupd-tcp-address=nsqlookupd:4160 # connect to nsqlookupd depends_on:-nsqlookupd ports:-"4150" # for clients-"4151" # for the HTTP API Nsqadmin: image: nsqio/nsq command: / nsqadmin-- lookupd-http-address=nsqlookupd:4161 # connect to nsqlookupd depends_on:-nsqlookupd ports:-"4171"

# docker-compose.yaml of simple nsq

At this point, I believe you have a deeper understanding of "how to use nsq message middleware". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report