Install and use

  • Address: www.rabbitmq.com/
  • You need to choose different Erlangs for different versions

Install Erlang

To install RabbitMQ, install the Erlang environment and configure the yum source according to the README in Erlang on GitHub

Write the above content to the redlined file to save and exit, and then install Erlang

Yum update -y yum install -y erlang-23.3.4Copy the code

Install the RabbitMQ

Download the RabbitMQ installation package and run the following command to install it

Yum install the rabbitmq server - 3.8.19-1. El7. Noarch. RPMCopy the code

Once the installation is complete, you can see that there are six related executables

The core is rabbitMq-server

The use of the RabbitMQ

Start the

Rabbitmq-server start & #Copy the code

The RabbitMQ GUI is started

RabbitMQ GUI mode is not enabled and requires manual activation. Rabbitmq-plugins are tools for managing plug-ins

Rabbitmq-plugins enable rabbitmq_management enable rabbitmq-plugins disable Rabbitmq_management # Disable the graphical interfaceCopy the code

Access the GUI management page

Open the GUI plug-in to access RabbitMQ from the local browser: http://127.0.0.1:15672/ username and password are guest

If you want to access this page from another machine, you need to configure the user name, password, and permission

Rabbitmqctl add_user Guest Guest # Add user rabbitmqctl set_user_tags Guest administrator # Assign role rabbitmqctl to this user Set_permissions -p/Guest ". * "" *" "*" # given access to the userCopy the code

This allows you to access the RabbitMQ admin interface on another machine

The following through Go language to achieve a simple message queue application

consumer.go

package main

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

/ / consumer

func main(a) {
   //1. Establish a connection
   connection, err := amqp.Dial("It: / / Guest: [email protected]:5672")
   iferr ! =nil {
      panic(err)
   }
   defer connection.Close()
   //2. Set the channel
   ch, err := connection.Channel()
   iferr ! =nil {
      panic(err)
   }
   / / 3. Definition of exchange
   //err = ch.ExchangeDeclare("exchange-name", "direct", false, false, false, false, nil)
   err = ch.ExchangeDeclare("exchange-name-topic"."topic".false.false.false.false.nil)
   iferr ! =nil {
      panic(err)
   }
   //4. Define the queue
   queue, err := ch.QueueDeclare("".false.false.false.false.nil)
   iferr ! =nil {
      panic(err)
   }
   / / 5. Binding to bind
   //err = ch.QueueBind(queue.Name, "direct_key", "exchange-name", false, nil)
   //topic fuzzy matching
   err = ch.QueueBind(queue.Name, "topic.#"."exchange-name-topic".false.nil)
   iferr ! =nil {
      panic(err)
   }
   //6. Receive messages
   consume_msg, err := ch.Consume(queue.Name, "".false.false.false.false.nil)
   iferr ! =nil {
      panic(err)
   }
   //7. Print messages
   //msg := <-consume_msg
   //fmt.Printf("received msg: %s\n", msg.Body)
   for msg := range consume_msg {
      fmt.Printf("received msg: %s\n", msg.Body)
   }
}
Copy the code

producer.go

package main

import "github.com/streadway/amqp"

/ / producer

func main(a) {
   //1. Establish a connection
   connection, err := amqp.Dial("It: / / Guest: [email protected]:5672")
   iferr ! =nil {
      panic(err)
   }
   defer connection.Close()
   //2. Set the channel
   ch, err := connection.Channel()
   iferr ! =nil {
      panic(err)
   }
   / / 3. Definition of exchange
   //err = ch.ExchangeDeclare("exchange-name", "direct", false, false, false, false, nil)
   err = ch.ExchangeDeclare("exchange-name-topic"."topic".false.false.false.false.nil)
   iferr ! =nil {
      panic(err)
   }
   //4. Define the queue
   //queue, err := ch.QueueDeclare("", false, false,false,false, nil)
   //if err ! = nil {
   // panic(err)
   / /}
   / / 5. Binding to bind
   //6. Define published messages
   //msg := "hello"
   //7. Release information
   //err = ch.Publish("exchange-name", "direct_key", false, false, amqp.Publishing{Body: []byte(msg)})
   err = ch.Publish("exchange-name-topic"."topic.first".false.false, amqp.Publishing{Body: []byte("topic1")})
   err = ch.Publish("exchange-name-topic"."TOPIC.eg".false.false, amqp.Publishing{Body: []byte("topic-eg")})
   err = ch.Publish("exchange-name-topic"."topic.last".false.false, amqp.Publishing{Body: []byte("topic2")})
   iferr ! =nil {
      panic(err)
   }
}
Copy the code