专栏首页陌无崖知识分享微服务系列笔记之RabbitMQ的入门使用

微服务系列笔记之RabbitMQ的入门使用

不忘初心,砥砺前行

作者 | 陌无崖

转载请联系授权

导语

上一篇文章我们讲到了broker模式,其实在Micro框架中已经为我们提供了一个rabbitMQ插件,我们可以借助这个插件来实现我们的生产与消费

启动RabbitMQ

这里推荐使用docker启动,比较迅速 rabbitmq是容器的名字

docker start rabbitmq

如果你之前从未启动过rabbitmq容器,你需要使用docker实例化一个容器

docker run -d -p 15672:15672  -p  5672:5672  -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin --name rabbitmq --hostname=rabbitmqhostone  rabbitmq:latest

15672 :表示 RabbitMQ 控制台端口号,可以在浏览器中通过控制台来执行 RabbitMQ 的相关操作。 5672 : 表示 RabbitMQ 所监听的 TCP 端口号,应用程序可通过该端口与 RabbitMQ 建立 TCP 连接,完成后续的异步消息通信 RABBITMQ_DEFAULT_USER:用于设置登陆控制台的用户名,这里我设置 admin RABBITMQ_DEFAULT_PASS:用于设置登陆控制台的密码,这里我设置 admin 现在我们可以用浏览器进行访问

编写客户端

定义一个主题

var (
    // 定义一个主题
    topic = "mu.micro.book.topic.payment.done"
)

发布消息

func pub() {
    tick := time.NewTicker(time.Second)
    i := 0
    for range tick.C {
        msg := &broker.Message{
            Header: map[string]string{
                "id": fmt.Sprintf("%d", i),
            },
            Body: []byte(fmt.Sprintf("%d:%s", i, time.Now().String())),
        }
        if err := broker.Publish(topic, msg); err != nil {
            log.Printf("[pub] 发布消息失败: %v", err)
        } else {
            fmt.Println("[pub] 发布消息:", string(msg.Body))
        }
        i++
    }
}

订阅消息

func sub() {
    _, err := broker.Subscribe(topic, func(p broker.Event) error {
        fmt.Printf("[sub] Received Body: %s, Header: %s\n", string(p.Message().Body), p.Message().Header)
        return nil
    }, broker.Queue("mu.micro.book.topic.queue"))
    if err != nil {
        fmt.Println(err)
    }

}

初始化broker

func main() {
    cmd.Init()

    if err := broker.Init(); err != nil {
        log.Fatalf("Broker 初始化错误:%v", err)
    }
    if err := broker.Connect(); err != nil {
        log.Fatalf("Broker 连接错误:%v", err)
    }

    go pub()
    go sub()

    <-time.After(time.Second * 20)

}

以上的代码和之前的一模一样,看不懂代码的可以看之前的关于broker的文章,这里为了可以使用rabbitMQ,我们还需要另外的一个包就是_ "github.com/micro/go-plugins/broker/rabbitmq"

启动测试

go run main.go --broker=rabbitmq --broker_address=amqp://admin:admin@192.168.10.252:5672

控制台打印

PS F:\micolearn\day02\rabbitmq> go run main.go plugin.go --broker=rabbitmq --broker_address=amqp://admin:admin@192.168.10.252:5672
[pub] 发布消息: 0:2019-08-14 14:59:05.1994723 +0800 CST m=+1.033071601[sub] Received Body: 0:2019-08-14 14:59:05.1994723 +0800 CST m=+1.033071601, Header: map[id:0]
[pub] 发布消息: 1:2019-08-14 14:59:06.1993723 +0800 CST m=+2.032971601
[sub] Received Body: 1:2019-08-14 14:59:06.1993723 +0800 CST m=+2.032971601, Header: map[id:1]
[pub] 发布消息: 2:2019-08-14 14:59:07.1993304 +0800 CST m=+3.032929701
[sub] Received Body: 2:2019-08-14 14:59:07.1993304 +0800 CST m=+3.032929701, Header: map[id:2]
[pub] 发布消息: 3:2019-08-14 14:59:08.2006233 +0800 CST m=+4.034222601
[sub] Received Body: 3:2019-08-14 14:59:08.2006233 +0800 CST m=+4.034222601, Header: map[id:3]
[pub] 发布消息: 4:2019-08-14 14:59:09.2006825 +0800 CST m=+5.034281801
[sub] Received Body: 4:2019-08-14 14:59:09.2006825 +0800 CST m=+5.034281801, Header: map[id:4]
[pub] 发布消息: 5:2019-08-14 14:59:10.2011823 +0800 CST m=+6.034781601
[sub] Received Body: 5:2019-08-14 14:59:10.2011823 +0800 CST m=+6.034781601, Header: map[id:5]
[pub] 发布消息: 6:2019-08-14 14:59:11.2000509 +0800 CST m=+7.033650201
[sub] Received Body: 6:2019-08-14 14:59:11.2000509 +0800 CST m=+7.033650201, Header: map[id:6]
exit status 2
PS F:\micolearn\day02\rabbitmq> go run main.go plugin.go --broker=rabbitmq --broker_address=amqp://admin:admin@192.168.10.252:5672
[pub] 发布消息: 0:2019-08-14 15:16:51.9162607 +0800 CST m=+1.037709901
[sub] Received Body: 0:2019-08-14 15:16:51.9162607 +0800 CST m=+1.037709901, Header: map[id:0]
[pub] 发布消息: 1:2019-08-14 15:16:52.9155533 +0800 CST m=+2.037002501
[sub] Received Body: 1:2019-08-14 15:16:52.9155533 +0800 CST m=+2.037002501, Header: map[id:1]
[pub] 发布消息: 2:2019-08-14 15:16:53.9160826 +0800 CST m=+3.037531801
[sub] Received Body: 2:2019-08-14 15:16:53.9160826 +0800 CST m=+3.037531801, Header: map[id:2]
[pub] 发布消息: 3:2019-08-14 15:16:54.9156399 +0800 CST m=+4.037089101
[sub] Received Body: 3:2019-08-14 15:16:54.9156399 +0800 CST m=+4.037089101, Header: map[id:3]
[pub] 发布消息: 4:2019-08-14 15:16:55.9155386 +0800 CST m=+5.036987801
[sub] Received Body: 4:2019-08-14 15:16:55.9155386 +0800 CST m=+5.036987801, Header: map[id:4]
[pub] 发布消息: 5:2019-08-14 15:16:56.9171838 +0800 CST m=+6.038633001
[sub] Received Body: 5:2019-08-14 15:16:56.9171838 +0800 CST m=+6.038633001, Header: map[id:5]
[pub] 发布消息: 6:2019-08-14 15:16:57.9154466 +0800 CST m=+7.036895801
[sub] Received Body: 6:2019-08-14 15:16:57.9154466 +0800 CST m=+7.036895801, Header: map[id:6]
[pub] 发布消息: 7:2019-08-14 15:16:58.9167641 +0800 CST m=+8.038213301
[sub] Received Body: 7:2019-08-14 15:16:58.9167641 +0800 CST m=+8.038213301, Header: map[id:7]
[pub] 发布消息: 8:2019-08-14 15:16:59.9160373 +0800 CST m=+9.037486501
[sub] Received Body: 8:2019-08-14 15:16:59.9160373 +0800 CST m=+9.037486501, Header: map[id:8]
[pub] 发布消息: 9:2019-08-14 15:17:00.9154771 +0800 CST m=+10.036926301
[sub] Received Body: 9:2019-08-14 15:17:00.9154771 +0800 CST m=+10.036926301, Header: map[id:9]
[pub] 发布消息: 10:2019-08-14 15:17:01.9155573 +0800 CST m=+11.037006501
[sub] Received Body: 10:2019-08-14 15:17:01.9155573 +0800 CST m=+11.037006501, Header: map[id:10]
[pub] 发布消息: 11:2019-08-14 15:17:02.9151966 +0800 CST m=+12.036645801
[sub] Received Body: 11:2019-08-14 15:17:02.9151966 +0800 CST m=+12.036645801, Header: map[id:11]
[pub] 发布消息: 12:2019-08-14 15:17:03.9162072 +0800 CST m=+13.037656401
[sub] Received Body: 12:2019-08-14 15:17:03.9162072 +0800 CST m=+13.037656401, Header: map[id:12]
[pub] 发布消息: 13:2019-08-14 15:17:04.9153942 +0800 CST m=+14.036843401
[sub] Received Body: 13:2019-08-14 15:17:04.9153942 +0800 CST m=+14.036843401, Header: map[id:13]
[pub] 发布消息: 14:2019-08-14 15:17:05.9153799 +0800 CST m=+15.036829101
[sub] Received Body: 14:2019-08-14 15:17:05.9153799 +0800 CST m=+15.036829101, Header: map[id:14]
[pub] 发布消息: 15:2019-08-14 15:17:06.915357 +0800 CST m=+16.036806201
[sub] Received Body: 15:2019-08-14 15:17:06.915357 +0800 CST m=+16.036806201, Header: map[id:15]
[pub] 发布消息: 16:2019-08-14 15:17:07.9155691 +0800 CST m=+17.037018301
[sub] Received Body: 16:2019-08-14 15:17:07.9155691 +0800 CST m=+17.037018301, Header: map[id:16]
[pub] 发布消息: 17:2019-08-14 15:17:08.9153224 +0800 CST m=+18.036771601
[sub] Received Body: 17:2019-08-14 15:17:08.9153224 +0800 CST m=+18.036771601, Header: map[id:17]
[pub] 发布消息: 18:2019-08-14 15:17:09.9280693 +0800 CST m=+19.049518501
[sub] Received Body: 18:2019-08-14 15:17:09.9280693 +0800 CST m=+19.049518501, Header: map[id:18]
PS F:\micolearn\day02\rabbitmq>

END

本文分享自微信公众号 - golang技术杂文(gh_ebbdb61f463e),作者:无崖子天下无敌

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2019-08-14

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 计算机操作系统——进程和线程

    为什么需要进程 在传统的计算机运行程序时,往往一次只能运行一个程序,这样对于CPU是极度不公平的,有可能一个程序需要进行计算仅仅需要简单的运算,而大部分都会进行...

    陌无崖
  • 微服务系列笔记之Broker模式入门篇

    API模式笔记告一段了,这篇文章总结了Micro中的消息订阅模式,在之前的系列文章中讲解了RabbitMQ消息队列,使用的是原生代码进行编写,在Micro中,其...

    陌无崖
  • Go实现字符串全排列字典序排列详解

    在数学中,字典或词典顺序(也称为词汇顺序,字典顺序,字母顺序或词典顺序)是基于字母顺序排列的单词按字母顺序排列的方法

    陌无崖
  • Qart轻松制作个性二维码

    雨尘
  • 【读者投稿】几年安全学习经验杂谈

    我属于11年左右才开始入行的小菜鸟,听着前辈们经常讲着在10年之前,注入分分钟拿站,到10年开始慢慢出现waf,作为一个新人,waf当年的确是个不错的ideas...

    信安之路
  • 小知识:TFA收集日志报错空间不足

    今天在某客户环境下分析某节点驱逐的故障,发现有安装TFA,所以使用一键收集包含故障时刻的日志

    Alfred Zhao
  • 获取手机nfc的MW version 和 FW version 记录

    /vendor/nxp/opensource/commonsys/packages/apps/Nfc/nci/jni/NativeNfcManager.cpp ...

    用户7557625
  • js的语法糖?

    前端多写了个+号导致的bug,网址后面多一个0。虽然不知道是什么原因,但是感觉是js的隐式替换

    ydymz
  • Node.js + WebSocket 实现的简易聊天室

    Firefox 7-9 (Old) (Protocol Version 8) Firefox 10+ (Protocol Version 13) ...

    meteoric
  • ASP.NET MVC的Model元数据与Model模板:预定义模板

    通过ModelMetadata表示的Model元数据的一个主要的作用在于为定义在HtmlHelper和HtmlHelper<TModel>中的模板方法(这些模板...

    蒋金楠

扫码关注云+社区

领取腾讯云代金券