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!