|
| 1 | +# golang-rabbitmq-sample |
| 2 | + |
| 3 | +This is a example for use rabbitmq with golang |
| 4 | + |
| 5 | +## asynchronize message pattern |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +## use package |
| 10 | + |
| 11 | +https://github.com/rabbitmq/amqp091-go |
| 12 | + |
| 13 | +## install |
| 14 | + |
| 15 | +```shell |
| 16 | +go get github.com/rabbitmq/amqp091-go |
| 17 | +go get github.com/gin-gonic/gin |
| 18 | +``` |
| 19 | + |
| 20 | +## producer logic |
| 21 | + |
| 22 | +```golang |
| 23 | +package main |
| 24 | + |
| 25 | +import ( |
| 26 | + "fmt" |
| 27 | + "log" |
| 28 | + "net/http" |
| 29 | + "os" |
| 30 | + |
| 31 | + "github.com/gin-gonic/gin" |
| 32 | + "github.com/leetcode-golang-classroom/golang-rabbitmq-sample/internal" |
| 33 | + amqp "github.com/rabbitmq/amqp091-go" |
| 34 | +) |
| 35 | + |
| 36 | +func main() { |
| 37 | + // connect to rabbitmq |
| 38 | + conn, err := amqp.Dial(internal.AppConfig.RABBITMQ_URL) |
| 39 | + if err != nil { |
| 40 | + log.Fatal(err) |
| 41 | + } |
| 42 | + defer conn.Close() |
| 43 | + // open a channel |
| 44 | + channel, err := conn.Channel() |
| 45 | + if err != nil { |
| 46 | + log.Fatal(err) |
| 47 | + } |
| 48 | + defer channel.Close() |
| 49 | + // declare queue |
| 50 | + _, err = channel.QueueDeclare(internal.AppConfig.QUEUE_NAME, true, false, false, false, nil) |
| 51 | + if err != nil { |
| 52 | + log.Fatal(err) |
| 53 | + } |
| 54 | + |
| 55 | + // create a web server with gin |
| 56 | + router := gin.Default() |
| 57 | + // add route for access request |
| 58 | + router.GET("/send", func(ctx *gin.Context) { |
| 59 | + msg := ctx.Query("msg") |
| 60 | + if msg == "" { |
| 61 | + ctx.JSON(http.StatusBadRequest, gin.H{"error": "Message is required"}) |
| 62 | + return |
| 63 | + } |
| 64 | + // create a message to publish |
| 65 | + message := amqp.Publishing{ |
| 66 | + ContentType: "text/plain", |
| 67 | + Body: []byte(msg), |
| 68 | + } |
| 69 | + // publish message to queue |
| 70 | + err = channel.Publish("", internal.AppConfig.QUEUE_NAME, false, false, message) |
| 71 | + if err != nil { |
| 72 | + // log.Printf("failed to publish message: %w", err) |
| 73 | + fmt.Fprintf(os.Stderr, "failed to publish message: %v", err) |
| 74 | + ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to publish message"}) |
| 75 | + return |
| 76 | + } |
| 77 | + ctx.JSON(http.StatusOK, gin.H{"message": msg, "status": "success"}) |
| 78 | + }) |
| 79 | + |
| 80 | + log.Fatal(router.Run(fmt.Sprintf(":%v", internal.AppConfig.PORT))) |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +## consumer logic |
| 85 | +```golang |
| 86 | +package main |
| 87 | + |
| 88 | +import ( |
| 89 | + "log" |
| 90 | + "os" |
| 91 | + "os/signal" |
| 92 | + "syscall" |
| 93 | + |
| 94 | + "github.com/leetcode-golang-classroom/golang-rabbitmq-sample/internal" |
| 95 | + amqp "github.com/rabbitmq/amqp091-go" |
| 96 | +) |
| 97 | + |
| 98 | +func main() { |
| 99 | + // connect to rabbitmq |
| 100 | + conn, err := amqp.Dial(internal.AppConfig.RABBITMQ_URL) |
| 101 | + if err != nil { |
| 102 | + log.Fatal(err) |
| 103 | + } |
| 104 | + defer conn.Close() |
| 105 | + // open a channel |
| 106 | + channel, err := conn.Channel() |
| 107 | + if err != nil { |
| 108 | + log.Fatal(err) |
| 109 | + } |
| 110 | + defer channel.Close() |
| 111 | + // subscribe the message to the queue |
| 112 | + messages, err := channel.Consume(internal.AppConfig.QUEUE_NAME, "", true, false, false, false, nil) |
| 113 | + if err != nil { |
| 114 | + log.Fatal(err) |
| 115 | + } |
| 116 | + signChan := make(chan os.Signal, 1) |
| 117 | + signal.Notify(signChan, syscall.SIGINT, syscall.SIGTERM) |
| 118 | + |
| 119 | + for { |
| 120 | + select { |
| 121 | + case message := <-messages: |
| 122 | + log.Printf("Message: %s\n", message.Body) |
| 123 | + case <-signChan: |
| 124 | + log.Println("Interrupt detected") |
| 125 | + os.Exit(0) |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | +``` |
0 commit comments