golang 中操作nsq队列数据库

jackluo · · 1419 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

首先先在本地将服务跑起来,我用的是docker-compose ,一句话6666

先新建一个docker-compose.yml
version: '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"

  然后整个数据就跑起来了

写个生产消息的

tproducter.go

package main
import(
    "log"
    "github.com/nsqio/go-nsq"
    "encoding/json"
    "strconv"
)

type Person struct {
    Id int
    Name string
    Age int
    NickName string
}

func main() {
    config :=nsq.NewConfig()
    w,err :=nsq.NewProducer("192.168.9.111:4150",config)
    if err !=nil {
        log.Panic("Could not create producer.")
    }
    defer w.Stop()
    for i :=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)
        if err !=nil || jerr !=nil {
            log.Panic("Could not connect.")
        }
    }
    w.Stop()
}

再写个消费的

tconsumer.go

package main

import (
    "log"
    "github.com/nsqio/go-nsq"
    "time"
)

func main() {
    config :=nsq.NewConfig()
    q,err := nsq.NewConsumer("write_test","ch",config)
    if err !=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",string(message.Body))
        time.Sleep(5*time.Second)
        return nil
    }))
    //err = q.ConnectToNSQD("192.168.9.111:32771");
    err = q.ConnectToNSQD("192.168.9.111:4150");
    if err !=nil {
        log.Panic("Could not connect")
    }
    time.Sleep(3600*time.Second)
}

然后就要以6起来了

/usr/local/go/bin/go run /Users/jackluo/Works/golang/src/nsq/tconsumer.go
2017/08/29 15:29:45 INF    1 [write_test/ch] (192.168.9.111:4150) connecting to nsqd
2017/08/29 15:29:45 Got a message: {"Id":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":10,"Name":"Jack10","Age":10,"NickName":"Luo10"}
2017/08/29 15:30:40 Got a message: {"Id":11,"Name":"Jack11","Age":11,"NickName":"Luo11"}
2017/08/29 15:30:45 Got a message: {"Id":12,"Name":"Jack12","Age":12,"NickName":"Luo12"}

可以通过这个地址看得到界面http://192.168.9.111:4171/counter


有疑问加站长微信联系(非本文作者)

本文来自:博客园

感谢作者:jackluo

查看原文:golang 中操作nsq队列数据库

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1419 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传