rabbitmq中文控制台_微服务系列笔记之RabbitMQ的入门使用

本文介绍了如何使用RabbitMQ作为微服务中的消息中间件,包括启动RabbitMQ容器、编写客户端代码进行消息的发布与订阅。通过示例展示了如何使用Go语言连接RabbitMQ并实现异步通信。
摘要由CSDN通过智能技术生成

导语

上一篇文章我们讲到了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

现在我们可以用浏览器进行访问

c484af9da7691878193ab676764b4f61.png

image

编写客户端

定义一个主题

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()

}

以上的代码和之前的一模一样,看不懂代码的可以看之前的关于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>

推荐阅读

本文欢迎转载,转载请联系作者,谢谢!

打开微信扫一扫,关注微信公众号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值