ZhangZhihui's Blog  

 

 

 

 

docker-compose.yml:

services:
  rabbitmq:
    image: 'rabbitmq:3.13-management'
    container_name: rabbitmq
    ports:
      - '5672:5672'
      - '15672:15672'
    environment:
      AMQP_URL: 'amqp://rabbitmq?connection_attempts=5&retry_delay=5'
      RABBITMQ_DEFAULT_USER: "guest"
      RABBITMQ_DEFAULT_PASS: "guest"
    networks:
      - rabbitmq

networks:
  rabbitmq:
    driver: bridge

 

zzh@ZZHPC:/zdata/Github/ztest$ docker compose up -d
[+] Running 11/11
 ✔ rabbitmq Pulled                                                                                                                                                              250.2s 
   ✔ 7646c8da3324 Pull complete                                                                                                                                                  91.3s 
   ✔ 04462dba3f37 Pull complete                                                                                                                                                 130.4s 
   ✔ ed4510fcf79f Pull complete                                                                                                                                                 130.8s 
   ✔ 5281fd20b957 Pull complete                                                                                                                                                 130.8s 
   ✔ 742d698ad83e Pull complete                                                                                                                                                 131.6s 
   ✔ 9f2ed69a577b Pull complete                                                                                                                                                 171.2s 
   ✔ fe13fdbb3584 Pull complete                                                                                                                                                 171.2s 
   ✔ 0df16e38d242 Pull complete                                                                                                                                                 171.2s 
   ✔ 0ee882b7bc03 Pull complete                                                                                                                                                 171.2s 
   ✔ 31b112e936b6 Pull complete                                                                                                                                                 184.3s 
[+] Running 2/2
 ✔ Network ztest_rabbitmq  Created                                                                                                                                                0.1s 
 ✔ Container rabbitmq      Started

 

send_mq.go:

package main

import (
    "context"
    "fmt"

    amqp "github.com/rabbitmq/amqp091-go"
)

func main() {
    fmt.Println("RabbitMQ producer")

    conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
    if err != nil {
        fmt.Println("amqp.Dial():", err)
        return
    }
    defer conn.Close()

    ch, err := conn.Channel()
    if err != nil {
        fmt.Println(err)
        return
    }
    defer ch.Close()

    q, err := ch.QueueDeclare("Go", false, false, false, false, nil)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println("Queue:", q)

    message := "Writing to RabbitMQ!"
    err = ch.PublishWithContext(context.TODO(), "", "Go", false, false, 
        amqp.Publishing{ContentType: "text/plain", Body: []byte(message)},
    )
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Println("Message published to Queue!")
}

 

zzh@ZZHPC:/zdata/Github/ztest/a$ go run send_mq.go
RabbitMQ producer
Queue: {Go 0 0}
Message published to Queue!
zzh@ZZHPC:/zdata/Github/ztest/a$ go run send_mq.go
RabbitMQ producer
Queue: {Go 1 0}
Message published to Queue!

 

read_mq.go:

package main

import (
    "fmt"

    amqp "github.com/rabbitmq/amqp091-go"
)

func main() {
    fmt.Println("RabbitMQ consumer")

    conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
    if err != nil {
        fmt.Println("Failed Initializing Broker Connection")
        panic(err)
    }
    defer conn.Close()

    ch, err := conn.Channel()
    if err != nil {
        panic(err)
    }
    defer ch.Close()

    msgs, err := ch.Consume("Go", "", true, false, false, false, nil)
    if err != nil {
        fmt.Println(err)
    }

    forever := make(chan bool)
    go func() {
        for d := range msgs {
            fmt.Printf("Received: %s\n", d.Body)
        }
    }()

    fmt.Println("Connected to the RabbitMQ server!")
    <-forever
}

 

zzh@ZZHPC:/zdata/Github/ztest/b$ go run read_mq.go
RabbitMQ consumer
Connected to the RabbitMQ server!
Received: Writing to RabbitMQ!
Received: Writing to RabbitMQ!
^Csignal: interrupt

 

func main() {
    forever := make(chan bool)  // block the program and prevent it from exiting
    go func() {
        for i := 0; i < 10; i++ {
            time.Sleep(time.Second)
            println("slept 1 second")
        }
    }()
    <-forever  // block the program and prevent it from exiting
}

 

zzh@ZZHPC:/zdata/Github/ztest$ go run main.go
slept 1 second
slept 1 second
slept 1 second
slept 1 second
slept 1 second
slept 1 second
slept 1 second
slept 1 second
slept 1 second
slept 1 second
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan receive]:
main.main()
        /zdata/Github/ztest/main.go:13 +0x39
exit status 2

 

posted on 2024-06-14 15:50  ZhangZhihuiAAA  阅读(4)  评论(0编辑  收藏  举报