zoukankan      html  css  js  c++  java
  • golang 中操作nsq队列数据库

    首先先在本地将服务跑起来,我用的是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

  • 相关阅读:
    hdu 4114 Disney's FastPass 状压dp
    lightoj 1381
    bzoj 2428: [HAOI2006]均分数据 随机化
    bzoj 3969: [WF2013]Low Power 二分
    套题:wf2013 (1/8)
    hdu 4119 Isabella's Message 模拟题
    hdu 4118 Holiday's Accommodation 树形dp
    UESTC 2015dp专题 N 导弹拦截 dp
    UESTC 2015dp专题 j 男神的约会 bfs
    UESTC 2015dp专题 H 邱老师选妹子 数位dp
  • 原文地址:https://www.cnblogs.com/jackluo/p/7448599.html
Copyright © 2011-2022 走看看