rabbitMQ安装与简单demo

安装

mac安装有了brew很方便,windows的可参考 win10 安装rabbitMQ详细步骤

bash 复制代码
brew install rabbitmq

启动

bash 复制代码
brew services start rabbitmq

关闭

bash 复制代码
brew services stop rabbitmq

出了问题之后可以重启一下

bash 复制代码
brew services restart rabbitmq

some issue

某些库下载超时

比如我安装过程中,发现libpng总是下不来

bash 复制代码
brew --cache -s libpng

输出如下

bash 复制代码
/Users/andydennis/Library/Caches/Homebrew/downloads/52af0b46c4798c4335195c9e01af657e58b8cb7f3d0fbcad1a753c3aff1ecc93--libpng-1.6.43.tar.xz

手动装一下

bash 复制代码
mv libpng-1.6.43.tar.xz "$(brew --cache -s libpng)"

然后

bash 复制代码
brew install libpng

Failed to connect to raw.githubusercontent.com port 443: Connection refused

报错信息

bash 复制代码
Fetching openssl@3
==> Downloading https://raw.githubusercontent.com/Homebrew/homebrew-core/32addad

curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused
Error: rabbitmq: Failed to download resource "openssl@3.rb"

可能是ip解析问题,查询一下 https://www.ipaddress.com/ ,

发现地址是 185.199.108.133, 185.199.109.133, 185.199.110.133185.199.111.133, 于是打开/etc/hosts 文件

bash 复制代码
sudo vim /etc/hosts

添加下面语句

bash 复制代码
185.199.108.133 raw.githubusercontent.com
185.199.109.133 raw.githubusercontent.com
185.199.110.133 raw.githubusercontent.com
185.199.111.133 raw.githubusercontent.com

不得不说,mac比win慢多了,最后终于装成功了

bash 复制代码
==>rabbitmq
Management UI: http://localhost:15672
Homebrew-specific docs: https://rabbitmq.com/install-homebrew.html

To start rabbitmq now and restart at login:
  brew services start rabbitmq
Or, if you don't want/need a background service you can just run:
  CONF_ENV_FILE="/opt/homebrew/etc/rabbitmq/rabbitmq-env.conf" /opt/homebrew/opt/rabbitmq/sbin/rabbitmq-server

配置

激活一下组件 rabbitmq-plugins enable rabbitmq_management

bash 复制代码
andydennis@andydennisdeMBP ~ % rabbitmq-plugins enable rabbitmq_management
Enabling plugins on node rabbit@localhost:
rabbitmq_management
The following plugins have been configured:
  rabbitmq_amqp1_0
  rabbitmq_management
  rabbitmq_management_agent
  rabbitmq_mqtt
  rabbitmq_stomp
  rabbitmq_stream
  rabbitmq_web_dispatch
Applying plugin configuration to rabbit@localhost...
Plugin configuration unchanged.

开启 brew services start rabbitmq, 访问 http://localhost:15672

初始用户名和密码都是guest

判断有无开启成功可查看端口5672 (默认端口,若修改了则按修改的端口) 有无无监听

bash 复制代码
andydennis@andydennisdeMBP ~ % lsof -i :5672
COMMAND    PID       USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
beam.smp 68981 andydennis   62u  IPv4 0x226aed36751b928f      0t0  TCP localhost:amqp (LISTEN)

code

依赖

在一个新文件夹,先 go mod init go-rabbitmqgo mod tidy下更新一下依赖。

编写代码

consumer.go

go 复制代码
package main

import "go-rabbitmq/rabbitmq"

func main() {

	rabbitMQ := rabbitmq.NewSimpleRabbitMQ("test-one")

	rabbitMQ.Consumer()
}

producer.go

go 复制代码
package main

import (
	"fmt"
	"strconv"
	"time"

	"go-rabbitmq/rabbitmq"
)

func main() {
	rabbitMQ := rabbitmq.NewSimpleRabbitMQ("test-one")

	for i := 0; i < 100000; i++ {
		time.Sleep(1000 * time.Millisecond)
		rabbitMQ.Publish("新消息 " + strconv.Itoa(i))
		fmt.Println("发送成功")
	}
}

rabbitmq.go

go 复制代码
package rabbitmq

import (
	"fmt"
	"log"

	"github.com/streadway/amqp"
)

const MQURL = "amqp://guest:guest@localhost:5672"

type RabbitMQ struct {
	conn      *amqp.Connection
	channel   *amqp.Channel
	queueName string
	exchange  string
	key       string
	mqurl     string
}

func NewRabbitMQ(queueName string, exchange string, key string) *RabbitMQ {

	rabbitMQ := &RabbitMQ{
		queueName: queueName,
		exchange:  exchange,
		key:       key,
		mqurl:     MQURL,
	}

	dial, err := amqp.Dial(rabbitMQ.mqurl)
	rabbitMQ.failOnErr(err, "创建连接失败")
	rabbitMQ.conn = dial

	rabbitMQ.channel, err = rabbitMQ.conn.Channel()
	rabbitMQ.failOnErr(err, "获取通道失败")

	return rabbitMQ
}

func (r *RabbitMQ) failOnErr(err error, message string) {
	if err != nil {
		log.Fatalf("%s:%s\n", err, message)
		panic(fmt.Sprintf("%s:%s\n", err, message))
	}
}

func (r *RabbitMQ) destory() {
	r.channel.Close()
	r.conn.Close()
}

func NewSimpleRabbitMQ(queueName string) *RabbitMQ {

	return NewRabbitMQ(queueName, "", "")
}

func (r *RabbitMQ) Publish(message string) {

	_, err := r.channel.QueueDeclare(
		r.queueName,
		//是否持久化
		false,
		//是否为自动删除
		false,
		//是否具有排他性
		false,
		//是否阻塞
		false,
		//额外属性
		nil,
	)
	if err != nil {
		panic(err)
	}

	r.channel.Publish(
		r.exchange,
		r.queueName,
		false,
		false,
		amqp.Publishing{
			ContentType: "text/plain",
			Body:        []byte(message),
		})

}

func (r *RabbitMQ) Consumer() {

	_, err := r.channel.QueueDeclare(r.queueName, false, false, false, false, nil)

	if err != nil {
		panic(err)
	}

	//2、接收消息
	msgs, err := r.channel.Consume(
		r.queueName,
		//用来区分多个消费者
		"",
		//是否自动应答
		true,
		//是否具有排他性
		false,
		//如果设置为true,表示不能将同一个connection中发送的消息传递给这个connection中的消费者
		false,
		//消息队列是否阻塞
		false,
		nil,
	)
	if err != nil {
		panic(err)
	}

	forever := make(chan bool)

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

	log.Printf("[*] Waiting for messagees,To exit press CTRL+C")

	<-forever

}

运行

(1) cd consumer 进入消费者目录, 把消费者运行起来 go run consumer.go

(2) cd producer 进入生产者目录,让生产者制造消息 go run producer.go

相关推荐
P.H. Infinity4 小时前
【RabbitMQ】03-交换机
分布式·rabbitmq
龙哥·三年风水6 小时前
群控系统服务端开发模式-应用开发-个人资料
分布式·php·群控系统
funnyZpC8 小时前
quartz集群增强版🎉
java·分布式·开源·集群·定时任务
明达技术9 小时前
工业4.0时代下的分布式IO模块
分布式
天冬忘忧10 小时前
Spark 程序开发与提交:本地与集群模式全解析
大数据·分布式·spark
一叶飘零_sweeeet12 小时前
Dubbo 构建高效分布式服务架构
分布式·架构·dubbo
孤蓬&听雨12 小时前
RabbitMQ自动发送消息工具(自动化测试RabbitMQ)
分布式·测试工具·自动化·rabbitmq·自动发送消息
呼啦啦啦啦啦啦啦啦12 小时前
RabbitMQ 七种工作模式介绍
分布式·rabbitmq
qq_2037694912 小时前
win11安装最新rabbitmq
分布式·rabbitmq·ruby
Amagi.12 小时前
RabbitMQ的解耦、异步、削峰是什么?
分布式·rabbitmq