NSQ Server installation using with Golang clients

Source: Internet
Author: User
Tags http post
This is a creation in Article, where the information may have evolved or changed.

NSQ is using the source code to compile the installation.

Compile and install using source code to build a separate gopath to prevent confusion with the original program

The environment used is UBUNTU14

1. Install git:apt-get installed git first
2, go environment and Gopath set directly skip the self-Baidu
3. Install Godep:go get GITHUB.COM/KR/GODEP
4. Install Assert:go get Github.com/bmizerany/assert
5, I am directly nsq the source code to download down, Gopath under: src/github.com/nsqio/nsq under
6, generally use GODEP installation is not successful, you need to remove godeps files to regenerate (here feel the main role is to step-by-step to solve the nsq needs of the dependency package): RM godeps
7, regenerate, will prompt the missing package then need to use go get the corresponding package download to come down: GODEP save.
8, if the hint is missing golang.org/under the package need to go to https://github.com/golang down to download down to Github.com/golang under: Git clone https://github.com/golang/sys.git
9, rely on all look good, not with GODEP installation, but with: Make&make install
10, will be in the root directory of the source code to build a build, in this directory has a service command, but also automatically installed under the/usr/local/bin directory


To build a NSQ messaging server:
NSQLOOKUPD: Mainly used to manage NSQD and topic information. Selective startup, direct start: Nohup./nsqlookupd >> nsqlookupd.log 2>&1 &

NSQD: A service for message processing that is used to receive forwarded messages. Can be started directly, the general will be specified through the-lookupd-tcp-address parameter nsqlookupd to complete the management. The-deflate parameter specifies whether to discard some specific messages when the default is true,true, some information is lost, and is generally manually specified as false. Start NSQD Service: nohup./NSQD--lookupd-tcp-address=127.0.0.1:4160-deflate=false >> nsqd.log 2>&1 &

Nsqadmin: Service Web Management interface, need to specify LOOKUPD; start mode: Nohup./nsqadmin-lookupd-http-address=127.0.0.1:4161 >> Nsqd.log 2> &1 &


Default information for the NSQ messaging service:
NSQLOOKUPD:
-http-address: The HTTP protocol port of the listener, the default is: 0.0.0.0:4161
-tcp-address: The TCP protocol port that is listening, by default: 0.0.0.0:4160

NSQD:
-deflate: Whether or not to discard some specific messages by default is True, and if set to false guarantees no loss of messages. For example, when a program that sends a message is started, the message is stored, and the message is sent to the appropriate consumer when the program that receives the message starts.
-data-path: Data File storage location
-http-address: HTTP listener port for message service, default is: 0.0.0.0:4151
-http-client-connect-timeout: Set HTTP connection timeout length, default 2 seconds
-http-client-request-timeout: Set HTTP request timeout duration, default 5 Seconds
-https-address: Set HTTPS address for NSQD listener, default is: 0.0.0.0:4152
-lookupd-tcp-address: Specifies the TCP listener address of the NSQLOOKUPD that manages NSQD
-tcp-address: Specifies the TCP listening port of the message server, by default: 0.0.0.0:4150

Nsqadmin:
-http-address: Specifies the address and port of the Management NSQD Web service, by default: 0.0.0.0:4171
-http-client-connect-timeout: Set HTTP connection timeout length, default 2 seconds
-http-client-request-timeout: Set HTTP request timeout duration, default 5 Seconds
-lookupd-http-address: Specify address for NSQLOOKUPD management

NSQD Action Command:
1, create a topic (post submission): Http://172.16.10.79:4151/topic/create?topic=name
2, delete a topic (post submission) : Http://172.16.10.79:4151/topic/delete?topic=name
3, create a channel under topic (Post submission): http://172.16.10.79:4151/ Channel/create?topic=name&channel=name
4, delete a channel under topic (Post submission): http://172.16.10.79:4151/channel/ Delete?topic=name&channel=name
5, empty a topic (post submission): Http://172.16.10.79:4151/topic/empty?topic=name
6 , emptying a channel (post submission) under topic: Http://172.16.10.79:4151/channel/empty?topic=name&channel=name

NSQD HTTP commands use Curl to have a limit, if there are more than two parameters after the,& symbol information will not be recognized, generally I use the program through the HTTP POST request to complete. As follows:
str: = "Http://172.16.10.79:4151/channel/create?topic=dubing&channel=binge"
RESP, err: = http. Post (str,
"Application/x-www-form-urlencoded",
Nil
If err! = Nil {
Fmt. PRINTLN (ERR)
}

Defer resp. Body.close ()
Body, err: = Ioutil. ReadAll (resp. Body)
If err! = Nil {
Handle error
}

Fmt. Println (String (body))

Golang client to complete sending and receiving messages (using "GITHUB.COM/NSQIO/GO-NSQ" package)
Func Producer () {
P, Err: = nsq. Newproducer ("172.16.10.79:4150", nsq. Newconfig ())
If err! = Nil {
Fmt. PRINTLN (ERR)
Panic (ERR)
}
for {
If err: = P.publish ("Test", []byte ("Hello NSQ!!!" +time. Now (). String ())); Err! = Nil {
Fmt. PRINTLN (ERR)
Panic (ERR)
}
Time. Sleep (time. Second * 1)
}
}

Type Consumert struct{}

Func (self *consumert) handlemessage (msg *nsq. Message) Error {
    fmt. Println (String (Msg. Body)
    return Nil
}
Func Consumer () {
    c, err: = nsq. Newconsumer ("Test", "ch", nsq. Newconfig ())
    if Err! = Nil {
        fmt. PRINTLN (Err)
        panic (err)
    }

You can also use functions directly here
/*c.addhandler (NSQ. Handlerfunc (func (msg *nsq). Message) Error {
Fmt. Println (String (Msg. Body))
return Nil
}))*/
C.addhandler (&consumert{})
If err: = C.connecttonsqd ("172.16.10.79:4150"); Err! = Nil {
Fmt. PRINTLN (ERR)
Panic (ERR)
}

<-c.stopchan
}

Func Main () {
Waiter: = Sync. waitgroup{}
Waiter. ADD (1)
Go Consumer ()
Go Producer ()

Waiter. Wait ()
}

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.