首页 技术 正文
技术 2022年11月9日
0 收藏 401 点赞 2,697 浏览 930 个字

在上一篇moonmq的介绍中(这里),我仅仅简短的罗列了一些moonmq的设计想法,但是对于如何使用并没有详细说明,公司同事无法很好的使用。

对于moonmq的使用,其实很简单,样例代码在这里,我们只需要处理好broker,consumer以及publisher的关系就可以了。

首先,我们需要启动一个broker,因为moonmq现在只支持tcp的自定义协议,所以broker启动的时候需要指定一个listen address。

#启动broker
./simple_broker -addr=127.0.0.1:11182

启动了broker之后,我们就可以向该broker发送消息

#向test这个queue发送 hello msg
./simple_publisher -addr=127.0.0.1:11182 -queue=test -msg=hello

然后在另一个shell里面接收消息

#接收test这个queue的消息
./simple_consumer -addr=127.0.0.1:11182 -queue=test#output get msg: hello

如果没有消息,那么consumer就会一直等待,直到接收到消息。

这里详细说一下consumer的实现,

  • 创建一个与broker的连接

      //create a client for use
    client := NewClient(config) //get a usable connection
    conn, _ := client.Get()
  • 绑定queue

      //bind a queue
    //queue name : test
    //routingKey : ""
    //noAck : true
    ch, _ := conn.Bind("test", "", true)
  • 接收消息

      //receive msg, block to wait until a msg received
    msg := ch.GetMsg()
    println(msg)
  • 回执消息

      //if channel noAck is false, we must ack
    ch.Ack()

从上面的例子可以看出,使用moonmq很方便,后续我准备加入http的支持,使其更容易使用。

moonmq的代码在这里https://github.com/siddontang/moonmq

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297