Operating NSQ Queue database in Golang

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

First in the local service run up, I use docker-compose, a sentence 6666

First create a new docker-compose.ymlversion: ' 2 ' services:  nsqlookupd:    image:nsqio/nsq    command:/NSQLOOKUPD    Ports:      -"192.168.9.111:4160:4160"      -"192.168.9.111:4161:4161"  nsqd:    image:nsqio/nsq    command :/nsqd--lookupd-tcp-address=nsqlookupd:4160    depends_on:      -nsqlookupd    ports:      -" 192.168.9.111:4150:4150 "      -" 192.168.9.111:4151:4151 "  nsqadmin:    image:nsqio/nsq    Command:/ Nsqadmin--lookupd-http-address=nsqlookupd:4161    depends_on:      -nsqlookupd    ports:      -" 192.168.9.111:4171:4171 "

And then the entire data ran.

Write a production message.

tproducter.gopackage Mainimport ("Log" "GITHUB.COM/NSQIO/GO-NSQ" "Encoding/json" "StrConv") type person struct {IdintName String ageintnickname String}func Main () {config:=nsq. Newconfig () W,err:=nsq. Newproducer ("192.168.9.111:4150", config)ifErr! =Nil {log. Panic ("Could not create producer.")} defer w.stop () forI: =0;i<100;i++{p:=&person{} p.id=I p.name= "Jack" +StrConv. Itoa (i) P.nickname= "Luo" +StrConv. Itoa (i) P.age=I info,jerr:=JSON. Marshal (P) Err:=w.publish ("Write_test", info)ifErr!=nil | | Jerr! =Nil {log. Panic ("Could not connect.")}} w.stop () }

and write a consumer.

tconsumer.gopackage Mainimport ("Log" "GITHUB.COM/NSQIO/GO-NSQ" "Time"Func Main () {config:=nsq. Newconfig () Q,err:= nsq. Newconsumer ("Write_test", "ch", config)ifErr! =nil{Log. Panic ("Could not create consumer.")} defer Q.stop () Q.addhandler (nsq. Handlerfunc (func (Message*nsq. Message) error{log. Printf ("Got a message:%v"a string (message. Body) time. Sleep (5*Time . Second)returnnil})) //err = Q.CONNECTTONSQD ("192.168.9.111:32771");Err = Q.CONNECTTONSQD ("192.168.9.111:4150"); ifErr! =Nil {log. Panic ("Could Not Connect")} time. Sleep (3600*Time . Second)}

And then we're going to get 6.

/usr/local/go/bin/go run/users/jackluo/works/golang/src/nsq/Tconsumer.go2017/08/29 15:29:45 INF 1 [write_test/ch] (192.168.9.111:4150) connecting to Nsqd2017/08/29 15:29:45 Got a message: {"I D ": 0," Name ":" Jack0 "," age ": 0," nickname ":" Luo0 "}2017/08/29 15:29:50 Got A message: {"Id": 1, "Name": "Jack1", "Age": 1, "nickname": "Luo1"}2017/08/29 15:29:55 Got A message: {"Id": 2, "Name": "Jack2", "Age": 2, "nickname": "Luo2"}2017/08/29 15:30:00 Got A message: {"Id": 3, "Name": "Jack3", "Age": 3, "nickname": "Luo3"}2017/08/29 15:30:05 Got A message: {"Id": 4, "Name": "Jack4", "Age": 4, "nickname": "Luo4"}2017/08/29 15:30:10 Got A message: {"Id": 5, "Name": "Jack5", "Age": 5, "nickname": "Luo5"}2017/08/29 15:30:15 Got A message: {"Id": 6, "Name": "Jack6", "Age": 6, "nickname": "Luo6"}2017/08/29 15:30:20 Got A message: {"Id": 7, "Name": "Jack7", "Age": 7, "nickname": "Luo7"}2017/08/29 15:30:25 Got A message: {"Id": 8, "Name": "Jack8", "Age": 8, "nickname": "Luo8"}2017/08/29 15:30:30 Got A message: {"Id": 9, "Name": "Jack9", "Age": 9, "nickname": "Luo9"}2017/08/29 15:30:35 Got A message: {"Id": Ten, "Name": "Jack10", "Age": Ten, "nickname": "Luo10"}2017/08/29 15:30:40 Got A message: {"Id": One, "Name": "Jack11", "age": One, "nickname": "Luo11"}2017/08/29 15:30:45 Got A message: {"Id": "Name": "Jack12", "Age": "nickname": "Luo12"}

This address can be used to see the interface Http://192.168.9.111:4171/counter

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.