Home > Backend Development > Golang > Write an efficient messaging system using Go language

Write an efficient messaging system using Go language

PHPz
Release: 2023-06-15 12:36:12
Original
1453 people have browsed it

With the development of the Internet, messaging systems are increasingly used in various fields. The messaging system can implement asynchronous communication to improve system performance and reliability, and can also achieve decoupling to facilitate system expansion and maintenance. The Go language has the characteristics of coroutines and channels, making it highly efficient and flexible in implementing message systems. This article will introduce how to use Go language to write an efficient messaging system.

1. Understand the basic architecture of the message system

The basic architecture of the message system consists of three parts: message publisher, message consumer and message queue. The message publisher sends the message to the message queue for storage, and the message consumer obtains the message from the message queue for consumption. The message queue plays the role of buffering and decoupling, which can make the processing capabilities of message publishers and message consumers inconsistent, cache messages during peak periods, and ensure the reliability and sequence of messages.

2. Use Go language to create a message system

  1. Install the message queue RabbitMQ

Since RabbitMQ is an open source, reliable, efficient and scalable Message broker, so we choose to use this message queue to implement our messaging system here. You can download RabbitMQ from the official website https://www.rabbitmq.com/.

  1. Create message producers and message consumers

It is very simple to write message producers and message consumers in Go language. The following is a sample code for a simple message producer:

package main

import (
    "log"
    "github.com/streadway/amqp"
)

func failOnError(err error, msg string) {
    if err != nil {
        log.Fatalf("%s: %s", msg, err)
    }
}

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()
    failOnError(err, "Failed to open a channel")
    defer ch.Close()

    q, err := ch.QueueDeclare(
        "hello", // queue name
        false,   // durable
        false,   // delete when unused
        false,   // exclusive
        false,   // no-wait
        nil,     // arguments
    )
    failOnError(err, "Failed to declare a queue")

    body := "Hello World!"
    err = ch.Publish(
        "",     // exchange
        q.Name, // routing key
        false,  // mandatory
        false,  // immediate
        amqp.Publishing {
            ContentType: "text/plain",
            Body:        []byte(body),
        })
    failOnError(err, "Failed to publish a message")
}
Copy after login

The above code connects to the RabbitMQ server, creates a queue named "hello", and sends a message "Hello World!" to the queue.

The following is a sample code for a simple message consumer:

package main

import (
    "log"
    "github.com/streadway/amqp"
)

func failOnError(err error, msg string) {
    if err != nil {
        log.Fatalf("%s: %s", msg, err)
    }
}

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()
    failOnError(err, "Failed to open a channel")
    defer ch.Close()

    q, err := ch.QueueDeclare(
        "hello", // queue name
        false,   // durable
        false,   // delete when unused
        false,   // exclusive
        false,   // no-wait
        nil,     // arguments
    )
    failOnError(err, "Failed to declare a queue")

    msgs, err := ch.Consume(
        q.Name, // queue
        "",     // consumer
        true,   // auto-ack
        false,  // exclusive
        false,  // no-local
        false,  // no-wait
        nil,    // arguments
    )
    failOnError(err, "Failed to register a consumer")

    forever := make(chan bool)

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

    log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
    <-forever
}
Copy after login

The above code connects to the RabbitMQ server, creates a queue named "hello", and obtains messages from the queue. Consumption. As long as there is a message in the queue, the message consumer can consume it immediately.

  1. Use coroutines and channels to implement concurrent processing

The features of coroutines and channels in the Go language can help us implement concurrent processing in the message system. A coroutine is like a lightweight thread that can achieve high concurrent processing. Channels can serve as communication bridges between coroutines to achieve concurrent transmission of data.

The following is a sample code that uses coroutines and channels to implement concurrent processing:

package main

import (
    "log"
    "math/rand"
    "time"
    "github.com/streadway/amqp"
)

func failOnError(err error, msg string) {
    if err != nil {
        log.Fatalf("%s: %s", msg, err)
    }
}

func publish(i int) {
    conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
    failOnError(err, "Failed to connect to RabbitMQ")
    defer conn.Close()

    ch, err := conn.Channel()
    failOnError(err, "Failed to open a channel")
    defer ch.Close()

    q, err := ch.QueueDeclare(
        "hello", // queue name
        false,   // durable
        false,   // delete when unused
        false,   // exclusive
        false,   // no-wait
        nil,     // arguments
    )
    failOnError(err, "Failed to declare a queue")

    body := "Hello World " + strconv.Itoa(i) + "!"
    err = ch.Publish(
        "",     // exchange
        q.Name, // routing key
        false,  // mandatory
        false,  // immediate
        amqp.Publishing {
            ContentType: "text/plain",
            Body:        []byte(body),
        })
    failOnError(err, "Failed to publish a message")
}

func consume() {
    conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
    failOnError(err, "Failed to connect to RabbitMQ")
    defer conn.Close()

    ch, err := conn.Channel()
    failOnError(err, "Failed to open a channel")
    defer ch.Close()

    q, err := ch.QueueDeclare(
        "hello", // queue name
        false,   // durable
        false,   // delete when unused
        false,   // exclusive
        false,   // no-wait
        nil,     // arguments
    )
    failOnError(err, "Failed to declare a queue")

    msgs, err := ch.Consume(
        q.Name, // queue
        "",     // consumer
        true,   // auto-ack
        false,  // exclusive
        false,  // no-local
        false,  // no-wait
        nil,    // arguments
    )
    failOnError(err, "Failed to register a consumer")

    forever := make(chan bool)

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

    log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
    <-forever
}

func main() {
    rand.Seed(time.Now().UnixNano())
    
    for i := 0; i < 10; i++ {
        go publish(i)
    }

    go consume()

    forever := make(chan bool)
    <-forever
}
Copy after login

In the above code, we created 10 coroutines to send messages to the message queue at the same time, and created another Coroutine to obtain messages for consumption. This greatly improves the concurrent processing capability of the message system.

3. Summary

In this article, we introduced how to use Go language to write an efficient messaging system. By using the features of RabbitMQ message brokers, coroutines, and channels, we can easily implement a high-concurrency, high-reliability messaging system. If you need to implement asynchronous message communication in your current project, then Go language is a good choice.

The above is the detailed content of Write an efficient messaging system using Go language. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template