nsq小试牛刀-0.3.0 API变更

NSQ是由知名短链接服务商bitly用Go语言开发的实时消息处理系统,具有高性能、高可靠、无视单点故障等优点,是一个非常不错的新兴的消息队列解决方案。

nsg易于配置和部署,所有参考都通过命令行指定,编译好的二进制文件,没有其它依赖项。而且支持多种消息格式。

关于nsq的介绍看这里:http://www.oschina.net/translate/day-22-a-journey-into-nsq

官网:http://nsq.io/overview/quick_start.html

相比之前,API更好用了。

对我来说,主要是部署简单,方便扩展。

直接上代码。

package main

import (
    "github.com/bitly/go-nsq"
    "fmt"
    "time"
)

type Handle struct{
    msgchan chan *nsq.Message
    stop bool
}

func (h *Handle) HandleMsg(m *nsq.Message) error {
    if !h.stop{
        h.msgchan <- m    
    }    
    return nil
}

func (h *Handle) Process(){
    h.stop = false
    for{
        select{
            case m := <-h.msgchan:
                fmt.Println(string(m.Body));
            case <-time.After(time.Second):
                if h.stop{
                    close(h.msgchan)
                    return
                }        
        }
    }
}

func (h *Handle) Stop(){
    h.stop = true
}

func main(){
    config := nsq.NewConfig()
    consumer, err := nsq.NewConsumer("test", "my", config)
    if err != nil{
        panic(err)
    }
    h := new(Handle)
    consumer.AddHandler(nsq.HandlerFunc(h.HandleMsg))
    h.msgchan = make(chan *nsq.Message, 1024)
    err = consumer.ConnectToNSQD("127.0.0.1:4150")
    if err != nil{
        panic(err)
    }
    h.Process()
}

 

另外,nsq的 topic和channel的关系:

http://word.bitly.com/post/38385370762/spray-some-nsq-on-it

 

Brief tangent: NSQ has the concept of topics and channels. Basically, think of a topic as a unique stream of messages (like our stream of API events above). Think of a channel as a copy of that stream of messages for a given set of consumers. Topics and channels are both independent queues, too. These properties enable NSQ to support both multicast (a topic copying each message to N channels) and distributed (a channel equally dividing its messages among N consumers) message delivery.

 

在代码里面:

 consumer, err := nsq.NewConsumer("test", "my", config)

channel == my

如果有多个consumer是同一个channel的时候,消息只能被其中的一个consumer吃掉。如果多个consumer采用不同的channel,那么每个consumer都可以收到producer生产的同一个消息,这样很方便的支持了多播和分发。

=============================================================

NSQ 和 RabiitMQ的对比

 

一、RabbitMQ

对于一个数据从Producer到Consumer的正确传递,还有三个概念需要明确:exchanges, queues and bindings。

        Exchanges are where producers publish their messages.

        Queuesare where the messages end up and are received by consumers

        Bindings are how the messages get routed from the exchange to particular queues.

   还有几个概念是上述图中没有标明的,那就是Connection(连接),Channel(通道,频道)。

   Connection: 就是一个TCP的连接。Producer和Consumer都是通过TCP连接到RabbitMQ Server的。以后我们可以看到,程序的起始处就是建立这个TCP连接。

   Channels: 虚拟连接。它建立在上述的TCP连接中。数据流动都是在Channel中进行的。也就是说,一般情况是程序起始建立TCP连接,第二步就是建立这个Channel。

 

二、NSQ

 

NSQ组合比较自由。

可以单独使用nsqd,Producer和Consumer自己直接连接nsqd。

也可以利用nsqlookupd来管理nsqd(根据心跳,挑选出可用的nsqd),Producer和Consumer跟nsqlookupd来交互。

======================================

RabbitMQ里面的Exchange类似nsqd,queen类似channel。

关于RabbitMQ里面的详细介绍可参考:http://www.diggerplus.org/archives/3110

 

转载于:https://www.cnblogs.com/zhangqingping/p/4150549.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值