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:
- 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
- Install dependency Package: gpm Install
- Installation Nsq:go install./...
three open nsq:
- NSQD node Maintenance process: Nsqlookupd &
- NSQD node Process: NSQD--lookupd-tcp-address=127.0.0.1:4160 &
- 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