Home Backend Development Golang Using RabbitMQ in Go: A Complete Guide

Using RabbitMQ in Go: A Complete Guide

Jun 19, 2023 am 08:10 AM
go language guide rabbitmq

As modern applications increase in complexity, messaging has become a powerful tool. In this area, RabbitMQ has become a very popular message broker that can be used to deliver messages between different applications.

In this article, we will explore how to use RabbitMQ in Go language. This guide will cover the following:

  • Introduction to RabbitMQ
  • RabbitMQ Installation
  • Basic RabbitMQ Concepts
  • Getting Started with RabbitMQ in Go Language
  • Advanced usage of RabbitMQ and Go language

Introduction to RabbitMQ

RabbitMQ is an open source message broker middleware. It is a reliable, highly available, scalable, easy-to-use broker that helps you handle messaging between applications with ease.

RabbitMQ supports multiple messaging protocols, such as AMQP, MQTT and STOMP. It also has many advanced features such as message distribution, message persistence, message acknowledgment and message queuing.

RabbitMQ Installation

Before using RabbitMQ, you need to install it using the following command:

$ sudo apt-get install rabbitmq-server
Copy after login

You can also install RabbitMQ as a Docker container. For more information, please visit the official website.

RabbitMQ basic concepts

Before starting to use RabbitMQ, let us understand some basic concepts.

  • Producer: The application that sends messages.
  • Message Queue (Queue): A container used by RabbitMQ to store messages.
  • Consumer: The application that receives messages.
  • Exchange: The component used by RabbitMQ to receive and route messages.
  • Binding: The process of associating switches and queues.
  • Routing Key: A string used to route messages to matching queues.
  • Message Pattern: Rules that specify how messages are routed, including Direct, Fanout, Topic and Headers.

Getting started with RabbitMQ in Go language

Let us write a simple RabbitMQ producer and consumer using Go language.

First, you need to install the RabbitMQ client for Go language:

$ go get github.com/streadway/amqp
Copy after login

Next, we will use the following code as a simple producer for RabbitMQ:

package main

import (
    "log"

    "github.com/streadway/amqp"
)

func main() {
    // 连接RabbitMQ服务器
    conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
    if err != nil {
        log.Fatalf("Failed to connect to RabbitMQ: %v", err)
    }
    defer conn.Close()

    // 建立通道
    ch, err := conn.Channel()
    if err != nil {
        log.Fatalf("Failed to open a channel: %v", err)
    }
    defer ch.Close()

    // 声明一个队列
    q, err := ch.QueueDeclare(
        "hello", // 队列名称
        false,  // 是否持久化
        false,  // 是否自动删除
        false,  // 是否具有排他性
        false,  // 是否阻塞处理
        nil,    // 额外的参数
    )
    if err != nil {
        log.Fatalf("Failed to declare a queue: %v", err)
    }

    // 发送两条消息
    for _, body := range []string{"Hello", "World"} {
        err = ch.Publish(
            "",    // 交换机名称
            q.Name,  // 队列名称
            false, // 是否强制遵循新的基于名称的路由规则
            false, // 是否立即将消息传递给消费者
            amqp.Publishing{
                ContentType: "text/plain",
                Body:        []byte(body),
            },
        )
        if err != nil {
            log.Fatalf("Failed to publish a message: %v", err)
        }
        log.Printf("Sent a message: %v", body)
    }
}
Copy after login

In this example , we connect to a queue named "hello" and use a for loop to send two messages to the queue. Each message is a simple string wrapped in a Publishing structure. ch.Publish()The method is used to publish messages to the queue.

Now, let's create a consumer to receive these messages:

package main

import (
    "log"

    "github.com/streadway/amqp"
)

func main() {
    // 连接RabbitMQ服务器
    conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
    if err != nil {
        log.Fatalf("Failed to connect to RabbitMQ: %v", err)
    }
    defer conn.Close()

    // 建立通道
    ch, err := conn.Channel()
    if err != nil {
        log.Fatalf("Failed to open a channel: %v", err)
    }
    defer ch.Close()

    // 声明一个队列
    q, err := ch.QueueDeclare(
        "hello", // 队列名称
        false,  // 是否持久化
        false,  // 是否自动删除
        false,  // 是否具有排他性
        false,  // 是否阻塞处理
        nil,    // 额外的参数
    )
    if err != nil {
        log.Fatalf("Failed to declare a queue: %v", err)
    }

    // 将通道设置为接收模式
    msgs, err := ch.Consume(
        q.Name, // 队列名称
        "",     // 消费者名称
        true,   // 自动确认消息
        false,  // 是否具有排他性
        false,  // 是否阻塞处理
        false,  // 额外参数
    )
    if err != nil {
        log.Fatalf("Failed to register a consumer: %v", err)
    }

    // 使用通道消费消息
    forever := make(chan bool)

    go func() {
        for d := range msgs {
            log.Printf("Received a message: %v", string(d.Body))
        }
    }()

    log.Printf("Waiting for messages...")

    <-forever
}
Copy after login

In this consumer example, we pull the message from the queue named "hello" and then Perform echo. In this example, we consume messages from the queue using the channel's ch.Consume() method.

Advanced usage of RabbitMQ and Go language

In this section, we will explore the advanced usage of RabbitMQ and Go language.

First, let us explore how to use RabbitMQ and Go language to implement message confirmation. Message acknowledgments are typically used to ensure that messages have been processed correctly.

First, we need to turn on confirmation mode on the consumer side:

msgs, err := ch.Consume(
    q.Name, // 队列名称
    "",     // 消费者名称
    false,  // 自动确认消息
    false,  // 是否具有排他性
    false,  // 是否阻塞处理
    false,  // 额外参数
)
Copy after login

In manual confirmation mode, we need to explicitly confirm each message on the consumer side:

for d := range msgs {
    log.Printf("Received a message: %v", string(d.Body))

    // 确认消息
    d.Ack(false)
}
Copy after login

We can also use RabbitMQ's RPC mode to implement distributed RPC calls. In RPC mode, the client application sends a request to RabbitMQ, RabbitMQ forwards the request to the appropriate server, and the server processes the request and returns a response.

First, we need to declare a switch to receive RPC requests:

ch, err := conn.Channel()
if err != nil {
    log.Fatalf("Failed to open a channel: %v", err)
}
defer ch.Close()

// 声明一个Direct类型的交换机
err = ch.ExchangeDeclare(
    "rpc_exchange", // 交换机名称
    "direct",       // 交换机类型
    true,           // 是否持久化
    false,          // 是否自动删除
    false,          // 是否具有排他性
    false,          // 是否阻塞处理
    nil,            // 额外参数
)
if err != nil {
    log.Fatalf("Failed to declare a exchange: %v", err)
}

// 声明一个接收RPC请求的队列
q, err := ch.QueueDeclare("", false, false, true, false, nil)
if err != nil {
    log.Fatalf("Failed to declare a queue: %v", err)
}

// 将队列绑定到交换机
err = ch.QueueBind(
    q.Name,         // 队列名称
    "rpc_routing",  // 路由键
    "rpc_exchange", // 交换机名称
    false,          // 是否强制遵循新的基于名称的路由规则
    nil,            // 额外参数
)
if err != nil {
    log.Fatalf("Failed to bind a queue: %v", err)
}
Copy after login

Next, we will create a function to handle RPC requests:

func rpcHandler(body []byte) []byte {
    log.Printf("Received RPC request: %s", string(body))

    // 处理请求
    result := []byte("Hello, World!")

    return result
}
Copy after login

Then, we need to write A consumer to receive RPC requests:

msg, ok, err := ch.Get(q.Name, true)
if err != nil {
    log.Fatalf("Failed to handle RPC request: %v", err)
}

if !ok {
    log.Printf("No messages available")
    return
}

// 处理RPC请求
response := rpcHandler(msg.Body)

// 发送RPC响应
err = ch.Publish(
    "",              // 交换机名称
    msg.ReplyTo,     // 回调队列名称
    false,           // 是否强制遵循新的基于名称的路由规则
    false,           // 是否立即将消息传递给消费者
    amqp.Publishing{ // 发布消息
        ContentType: "text/plain",
        CorrelationId: msg.CorrelationId,
        Body:        response,
    },
)
if err != nil {
    log.Fatalf("Failed to publish a message: %v", err)
}

log.Printf("Sent RPC response: %v", string(response))
Copy after login

In this example, we use the ch.Get() method to pull the message from the queue and send it to the rpcHandler () method for processing. Once processing is complete, we send the response back to the client using the ch.Publish() method.

Conclusion

RabbitMQ is a powerful tool that helps you handle messaging between applications easily. In this guide, we cover the basics and advanced usage of RabbitMQ in Go. Now you can apply this knowledge to your own projects for efficient messaging with RabbitMQ!

The above is the detailed content of Using RabbitMQ in Go: A Complete Guide. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

What should I do if the custom structure labels in GoLand are not displayed? What should I do if the custom structure labels in GoLand are not displayed? Apr 02, 2025 pm 05:09 PM

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

In Go, why does printing strings with Println and string() functions have different effects? In Go, why does printing strings with Println and string() functions have different effects? Apr 02, 2025 pm 02:03 PM

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

Which libraries in Go are developed by large companies or provided by well-known open source projects? Which libraries in Go are developed by large companies or provided by well-known open source projects? Apr 02, 2025 pm 04:12 PM

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What is the difference between `var` and `type` keyword definition structure in Go language? What is the difference between `var` and `type` keyword definition structure in Go language? Apr 02, 2025 pm 12:57 PM

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

Why is it necessary to pass pointers when using Go and viper libraries? Why is it necessary to pass pointers when using Go and viper libraries? Apr 02, 2025 pm 04:00 PM

Go pointer syntax and addressing problems in the use of viper library When programming in Go language, it is crucial to understand the syntax and usage of pointers, especially in...

See all articles