Senin, 08 April 2019

NSQ messaging Installation


Installation

This is pretty quick for your local setup
Untar the tarball into ~/bin folder Upon untarring ensure that the ~/bin folder has all the files named nsq_

$ wget https://s3.amazonaws.com/bitly-downloads/nsq/nsq-0.2.31.darwin-amd64.go1.3.1.tar.gz
$ tar xvfz nsq-0.2.31.darwin-amd64.go1.3.1.tar.gz
$ sudo mkdir -p /usr/local/nsq/bin
$ sudo mv nsq-0.2.31.darwin-amd64.go1.3.1/bin/* /usr/local/nsq/bin

Setup NSQ and PATH environment variables with root privileges:

# echo 'export NSQROOT=/usr/local/nsq' | tee -a /etc/profile
# echo 'export PATH=$PATH:/usr/local/nsq/bin' | tee -a /etc/profile

Start the following daemons

$ source /etc/profile
$ nsqlookupd &
$ nsqd --lookupd-tcp-address=127.0.0.1:4160
$ nsqadmin --lookupd-http-address=127.0.0.1:4161

If done successfully you will be able to view a web UI that looks like this:




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

requires the golang for the next step, if there is no golang on your system you can install with the tutorial at the following link


Writing Your Go Program

I like creating the consumer first so I can see the handler in action after pushing a message with a producer (see next section).

please create directory src in your home directory and in src directory create consumer and producer directory

$ mkdir src
$ cd src
$ mkdir consumer producer

Get  go client library

$ go get -u -v github.com/bitly/go-nsq
$ go get -u -v github.com/nsqio/go-nsq

Creating a consumer

create file like below in ~/src/consumer/consumer01.go

package main

import (
    "log"
    "sync"

    "github.com/nsqio/go-nsq"
)

func main() {
    wg := &sync.WaitGroup{}
    wg.Add(1)

    decodeConfig := nsq.NewConfig()
    c, err := nsq.NewConsumer("NSQ_Topic", "NSQ_Channel", decodeConfig)
    if err != nil {
        log.Panic("Could not create consumer")
    }
    //c.MaxInFlight defaults to 1

    c.AddHandler(nsq.HandlerFunc(func(message *nsq.Message) error {
        log.Println("NSQ message received:")
        log.Println(string(message.Body))
        return nil
    }))

    err = c.ConnectToNSQD("127.0.0.1:4150")
    if err != nil {
        log.Panic("Could not connect")
    }
    log.Println("Awaiting messages from NSQ topic \"NSQ Topic\"...")
    wg.Wait()
}

Now run this consumer program:

$ go run consumer01.go

You’ll get this output:


2019/04/08 03:41:33 INF    1 [NSQ_Topic/NSQ_Channel] (127.0.0.1:4150) connecting to nsqd
2019/04/08 03:41:33 Awaiting messages from NSQ topic "NSQ Topic"...

This should hang there waiting to receive a NSQ message from a topic you specify. Nothing will happen just yet since there aren’t any queued up messages for this particular topic.
Leave this program running in a terminal window for now. In the next step we’ll push a message to it.

Creating a Producer

You can publish a message with a producer with some simple code like this:

create file like below in ~/src/producer/producer01.go

package main

import (
  "log"

  "github.com/nsqio/go-nsq"
)

func main() {
    config := nsq.NewConfig()
    p, err := nsq.NewProducer("127.0.0.1:4150", config)
    if err != nil {
        log.Panic(err)
    }
    err = p.Publish("NSQ_Topic", []byte("sample NSQ message"))
    if err != nil {
        log.Panic(err)
    }
}

Now run this publisher program:

$ go run producer01.go

In this terminal window, you’ll only see this message indicating your message was published to NSQ:

go run producer01.go
2019/04/08 03:43:35 INF    1 (127.0.0.1:4150) connecting to nsqd

If you look at your consumer terminal window that you left running from the previous step, you’ll now see this additional output:

2019/04/08 03:41:33 Awaiting messages from NSQ topic "NSQ Topic"...
2019/04/08 03:43:35 NSQ message received:
2019/04/08 03:43:35 sample NSQ message topic

Dont  forget add  firewall rule

# firewall-cmd --zone=public --add-port=4171/tcp
# firewall-cmd --zone=public --add-port=4171/tcp --permanent
# firewall-cmd --zone=public --add-port=4150/tcp
# firewall-cmd --zone=public --add-port=4150/tcp --permanent
# firewall-cmd --zone=public --add-port=4151/tcp
# firewall-cmd --zone=public --add-port=4151/tcp --permanent
# firewall-cmd --zone=public --add-port=4160/tcp
# firewall-cmd --zone=public --add-port=4160/tcp --permanent
# firewall-cmd --zone=public --add-port=4161/tcp
# firewall-cmd --zone=public --add-port=4161/tcp --permanent

Congrats - you just pushed and received your first NSQ message!

If you go back to your web UI console you’ll see your newly-created topic. If you drill into this topic, you can also see the channel that you consumed the message to, with the message counter at 1:


Tidak ada komentar: