網站首頁 編程語言 正文
消費者
package main import ( "fmt" "github.com/streadway/amqp" ) func failOnError(err error, msg string) { if err != nil { fmt.Println("%s: %s", msg, err) } } // 只能在安裝 rabbitmq 的服務器上操作 func main() { conn, err := amqp.Dial("amqp://mquser:123456@127.0.0.1/toutiao") failOnError(err, "Failed to connect to RabbitMQ") defer conn.Close() ch, err := conn.Channel() failOnError(err, "Failed to open a channel") defer ch.Close() q, err := ch.QueueDeclare( "toutiao.web.test", // 隊列名字 false, // durable false, // delete when unused false, // exclusive false, // no-wait nil, // arguments ) failOnError(err, "Failed to declare a queue") msgs, err := ch.Consume( q.Name, // queue "toutiao", // consumer true, // auto-ack,true消費了就消失 false, // exclusive false, // no-local false, // no-wait nil, // args ) failOnError(err, "Failed to register a consumer") forever := make(chan bool) go func() { for d := range msgs { fmt.Println(fmt.Sprintf("返回的消息:%s",d.Body)) } }() fmt.Println("[*] Waiting for messages. To exit press CTRL+C") <-forever }
生產者
body:消息體
package main import ( "github.com/streadway/amqp" "log" ) func failOnError(err error, msg string) { if err != nil { log.Fatalf("%s: %s", msg, err) } } // 只能在安裝 rabbitmq 的服務器上操作 func main() { conn, err := amqp.Dial("amqp://mquser:123456@127.0.0.1/toutiao") failOnError(err, "Failed to connect to RabbitMQ") defer conn.Close() ch, err := conn.Channel() failOnError(err, "Failed to open a channel") defer ch.Close() q, err := ch.QueueDeclare( "toutiao.web.test", // name false, // durable false, // delete when unused false, // exclusive false, // no-wait nil, // arguments ) failOnError(err, "Failed to declare a queue") body := "Hello World!" //發送的消息 err = ch.Publish( "", // exchange q.Name, // routing key false, // mandatory false, // immediate amqp.Publishing{ ContentType: "text/plain", Body: []byte(body), }) log.Printf(" [x] Sent %s", body) failOnError(err, "Failed to publish a message") }
原文鏈接:https://www.cnblogs.com/guyouyin123/p/14768971.html
相關推薦
- 2023-01-17 React?Hooks核心原理深入分析講解_React
- 2022-05-09 Redis擊穿穿透雪崩產生原因分析及解決思路面試_Redis
- 2023-06-17 Python構建區塊鏈的方法詳解_python
- 2022-05-10 VSCode中 “Error:Can‘t find Python executable “pytho
- 2022-09-24 ASP.NET?MVC使用jQuery?Template實現批量更新_實用技巧
- 2022-07-24 Golang?CSP并發機制及使用模型_Golang
- 2023-11-15 Latex如何加粗文字;表格中如何加粗文字而不讓文本變寬
- 2022-06-22 Git的撤銷、修改和回退命令_其它綜合
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支