Producers and consumers of NSQ

Source: Internet
Author: User

Resources:

1. In-depth NSQ tour: http://www.oschina.net/translate/day-22-a-journey-into-nsq

2. NSQ Source: https://github.com/bitly/nsq/

3. NSQ Source Interpretation: http://www.baiyuxiong.com/?p=873

After reading the above several articles, to nsq have a certain understanding, very want to implement a message queue, I would like to refer to other people based on the implementation of NSQ message queue, but after a search, did not find directly through the code to speak, are some theoretical article around. So calm down to read NSQ data and source code.

Based on the site (http://www.baiyuxiong.com/?p=873) for installation and simple demonstration of the use of NSQ, but how to use Golang language how to simulate the production of a large number of producers of messages, generating Message Queuing, so that consumers from the message queue to take out messages for consumption?

Producer's Code:

1 Package Main2 3 Import (4     "Flag"5     "FMT"6     "github.com/bitly/go-nsq"7     "Log"8 )9 Ten Func Main () { One     varNSQADDR = flag. String ("NSQD","127.0.0.1:4150","NSQD TCP Address") A flag. Parse () -Config: =nsq. Newconfig () -  theProducer, err: = nsq. Newproducer (*nsqaddr, config) -     ifErr! =Nil { - log. Fatal (ERR) -         return +     } -  +     //Producer produces 1 million messages A      forI: =0; I <1000000; i++ { at         //Finally, it is necessary to confirm whether the characters read by consumers are garbled. -Body: = FMT. Sprintf (' {"name":%s,"Tid":%d} ',"He-tom", i) -         //Post message, topic=tom;body={"name":-tom, "Tid": 1~1000000} -Producer. Publish ("Tom", []byte(body)) -     } -}

Run producer Producer.go, and look at the resulting Message Queuing effect through Nsqadmin:

The consumer code is as follows:

1 Package Main2 3 Import (4     "FMT"5     "github.com/bitly/go-nsq"6     "Log"7 )8 9 Func Main () {TenConsumer, err: = nsq. Newconsumer ("Tom","Consumer", NSQ. Newconfig ()) One     ifErr! =Nil { A log. Fatal (ERR) -         return -     } the  -Consumer. AddHandler (NSQ. Handlerfunc (Func (MSG *nsq. Message) Error { -         //gets the message from the message queue. -Fmt. Println ("Consumer:",string(Msg. Body)) +         returnNil -     })) +err = consumer. CONNECTTONSQD ("127.0.0.1:4150") A     ifErr! =Nil { at log. Fatal (ERR) -         return -     } -<-make (chanBOOL) -}

After running the consumer:

Imagine how bad it would be to write 1 million messages to the database, how slow it would be to consume more resources. It seems that Message Queuing is a good choice for processing large amounts of data.

Producers and consumers of NSQ

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.