GO-NSQ Use Summary

Source: Internet
Author: User

an environmental dependency:

Golang Development Environment (version >= 1.2) under source code, configure environment variables, execute installation script

GPM Dependency Package Manager Ubantu:sudo Apt-get intall GPM

two NSQ installation:
    1. git get Source: mkdir-p $GOPATH/src/github.com/nsqio;cd $GOPATH/src/github.com/nsqio;git clone https://github.com/nsqio/ NSQ.GIT;CD NSQ
    2. Install dependency Package: gpm Install
    3. Installation Nsq:go install./...

three open nsq:
    1. NSQD node Maintenance process: Nsqlookupd &
    2. NSQD node Process: NSQD--lookupd-tcp-address=127.0.0.1:4160 &
    3. Message production See process: Nsqadmin--lookupd-http-address=127.0.0.1:4161 &

PS:NSQLOOKUPD and nsqadmin are auxiliary processes and can work without the use of direct nsqd.

The processes that are opened here use the default port

Four tool testing:
    • curl-d ' Hello World 1 ' http://127.0.0.1:4151/put?topic=test '//produce a topic for "test" Message with message content "Hello World"
    • Nsq_to_file--topic=test--output-dir=./tmp--lookupd-http-address=127.0.0.1:4161//Writes the message topic for "Test". In a file under the TMP directory

Five code tests:
1 Package Main2 3 Import (4     "FMT"5     " Time"6 7     "github.com/nsqio/go-nsq"8 )9 Ten //NSQ Publishing Messages One func Producer () { AP, Err: = nsq. Newproducer ("127.0.0.1:4150", NSQ. Newconfig ())//new producer -     ifErr! =Nil { - Panic (Err) the     }    -      -     ifERR: = P.publish ("Test", []byte("Hello NSQ!!!")); Err! =Nil {//Post message - Panic (Err) +     }    - } +  A //NSQ Subscription Message atType Consumertstruct{} -  -Func (*consumert) handlemessage (MSG *nsq. Message) Error { -Fmt. Println (string(Msg. Body)) -     returnNil - } in  - func Consumer () { toC, err: = nsq. Newconsumer ("Test","Test-channel", NSQ. Newconfig ())//Create a new consumer +     ifErr! =Nil { - Panic (Err) the     }    *C.addhandler (&consumert{})//Add message processing $     ifERR: = C.CONNECTTONSQD ("127.0.0.1:4150"); Err! =Nil {//Establish connectionPanax Notoginseng Panic (Err) -     }    the } + //Main function A Func Main () { the Producer () + Consumer () -Time. Sleep (time. Second *3) $ } $ //the run will print: Hello NSQ!!!
Six use summary:

Single use conditions, synchronous publishing message speed is also very fast (10W/S), the release of the message end is basically no more cache encapsulation. The message processing at the receiving end should take as short a time as possible, avoid the accumulation of messages, and when the number of cached messages accumulated to the NSQ will write extra messages to the file, it will slow down the sending speed.

Therefore, the receiver can use the channel and go routine to do a simple package processing. If a topic message is produced too fast or too much, it can be handled separately using one NSQD to avoid the accumulation of messages affecting other message delivery and reception.

NSQ Official Use Introduction

NSQ Source Address

NSQ Client Code Address

GO-NSQ Use Summary

Related Article

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.