Golang Connection RABBITMQ

Source: Internet
Author: User
Tags rabbitmq

Installing RABBITMQ in 1.docke

Docker Pull Rabbitmq

2. Running RABBITMQ

Docker run-d--name rabbitmq-p 5671:5671-p 5672:5672-p 4369:4369-p 25672:25672-p 15671:15671-p 15672:15672 RABBITMQ : Latest

3. Download RABBITMQ Golang Client

Go get GITHUB.COM/STREADWAY/AMQP

4.send.go

1 Package Main2 3 Import (4     "Log"5 6     "GITHUB.COM/STREADWAY/AMQP"7 )8 9Func FailOnError (err error, MSGstring) {Ten     ifErr! =Nil { OneLog. Fatalf ("%s:%s", MSG, err) A     } - } -  the //can only operate on servers that have RABBITMQ installed - Func Main () { -Conn, err: = Amqp. Dial ("amqp://guest:guest@localhost:5672/") -FailOnError (Err,"Failed to connect to RabbitMQ") + defer Conn. Close () -  +CH, err: =Conn. Channel () AFailOnError (Err,"Failed to open a channel") at defer ch. Close () -  -Q, Err: =Ch. Queuedeclare ( -         "Hello",//name -         false,//Durable -         false,//Delete When unused in         false,//Exclusive -         false,//no-wait toNil//arguments +     ) -FailOnError (Err,"Failed to declare a queue") the  *Body: ="Hello world!" $Err =Ch. Publish (Panax Notoginseng         "",//Exchange -Q.name,//Routing Key the         false,//Mandatory +         false,//Immediate A AMQP. publishing{ theContentType:"Text/plain", +Body: []byte(body), -         }) $Log. Printf ("[x] Sent%s", body) $FailOnError (Err,"Failed to publish a message") -}

5.recv.go

1 Package Main2 3 Import (4     "Log"5 6     "GITHUB.COM/STREADWAY/AMQP"7 )8 9Func FailOnError (err error, MSGstring) {Ten     ifErr! =Nil { OneLog. Fatalf ("%s:%s", MSG, err) A     } - } -  the //can only operate on servers that have RABBITMQ installed - Func Main () { -Conn, err: = Amqp. Dial ("amqp://guest:guest@localhost:5672/") -FailOnError (Err,"Failed to connect to RabbitMQ") + defer Conn. Close () -  +CH, err: =Conn. Channel () AFailOnError (Err,"Failed to open a channel") at defer ch. Close () -  -Q, Err: =Ch. Queuedeclare ( -         "Hello",//name -         false,//Durable -         false,//Delete When unused in         false,//Exclusive -         false,//no-wait toNil//arguments +     ) -FailOnError (Err,"Failed to declare a queue") the  *Msgs, err: =Ch. Consume ( $Q.name,//QueuePanax Notoginseng         "",//Consumer -         true,//Auto-ack the         false,//Exclusive +         false,//no-local A         false,//no-wait theNil//args +     ) -FailOnError (Err,"Failed to register a consumer") $  $Forever: = Make (chanBOOL) -  - go func () { the          forD: =Range Msgs { -Log. Printf ("Received a message:%s", D.body)Wuyi         } the     }() -  WuLog. Printf ("[*] waiting for messages. To exit Press CTRL + C") -<-Forever About}

6. Execute in two windows, respectively

Go run./send.go

2018/08/22 15:48:13 [x] Sent Hello world!

Go run./recv.go

2018/08/22 15:48:16 [*] waiting for messages. To exit Press CTRL + C
2018/08/22 15:48:16 Received a Message:hello world!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.