Docker容器

RabbitMQ安装教程(linux与Docker)

本文主要是介绍RabbitMQ安装教程(linux与Docker),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一.架构的演变过程

 

 

二.正确看待异步架构

1.优势

(1).削峰

降低了服务器的压力

(2).响应快

加快了服务器的响应速度,优化了用户体验

(3).降耦合

降低了服务器之间的耦合

(4).便于系统的升级

处理业务的时候,升级系统可以停用处理消息的服务器

 

2.劣势

(1).架构复杂

架构更加复杂,会依赖于队列(容器)

(2).及时性降低了

 

三.常见的队列

1.RabbitMQ

特点:使用频繁,上手快,易学

2.Redis

3.kafka

性能高,主要用于大型系统架构

 

 

四.Linux 内安装 RabbitMQ

1.安装erlang

 

A.检查是否安装了erlang环境
rpm -qa | grep erlang
B.卸载erlang
yum list | grep erlang
yun -y remove erlang-*
rm -rf /usr/lib64/erlang
C.设置仓储
curl -s https://packagecloud.io/install/repositories/rabbitmq/erlang/script.rpm.sh | sudo bash
D.安装erlang
yum install erlang
E.查看erlang版本号
erl

 

2.安装RabbitMQ

A.检查是否安装了RabbitMQ
rpm -qa | grep rabbitmq
B.卸载RabbitMQ
yum list | grep rabbitmq
yum -y remove rabbitmq-server.noarch
C.导入两个Key
rpm --import https://packagecloud.io/rabbitmq/rabbitmq-server/gpgkey
rpm --import https://packagecloud.io/gpg.key
D.下载包
curl -s https://packagecloud.io/install/repositories/rabbitmq/rabbitmq-server/script.rpm.sh | sudo bash
E.下载
wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.5/rabbitmq-server-3.8.5-1.el8.noarch.rpm
F.导入Key
rpm --import https://www.rabbitmq.com/rabbitmq-release-signing-key.asc
G.添加RabbitMQ的依赖项
yum -y install epel-release
yum -y install socat
H.指定安装rabbitmq服务端
rpm -ivh rabbitmq-server-3.8.5-1.el8.noarch.rpm
I. 可视化RabbitMQ工具
rabbitmq-plugins enable rabbitmq_management 

 

(1).可以通过浏览器可视化

rabbitmq可视化端口:15672

rabbitmq链接端口:5672

http://宿主机IP:15672/#/queues
(2).创建管理员账户
rabbitmqctl add_user richard richard
(3).设置注册账号为管理员
rabbitmqctl set_user_tags richard administrator
J.启动
systemctl start rabbitmq-server
K.放开端口
firewall-cmd --add-port=15672/tcp --permanent
firewall-cmd --add-port=5672/tcp --permanent
​
#重启防火墙
firewall-cmd --reload

 

 

五.Docker内安装RabbitMQ

1.Docker仓储查询RabbitMQ的镜像

docker search rabbitmq

2.拉取RabbitMQ镜像

docker pull rabbitmq

3.通过镜像启动RabbitMQ容器

1.第一个映射端口是可视化端口号

2.第二个映射端口是链接端口号

docker run -d -name Yp_rabbitmq -p 15677:15672 -p 5677:5672 镜像ID

4.进入到RabbitMq容器内安装可视化插件

docker ps -a
docker exec -it 容器ID bash
rabbitmq-plugins enable rabbitmq_management
​

5.重启RabbitMQ容器

docker ps -a  
docker restart 容器ID

6.访问RabbitMQ可视化界面

A.地址: http://宿主机的IP:第三步指定可视化端口号

B.默认账号:guest

C.默认密码:guest

 

六.程序链接RabbitMQ

1.创建消息生产者

A.Nuget引入RabbitMQ Client

引入帮助类库

B.创建链接工厂
ConnectionFactory factory= new ConnectionFactory(); //创建链接工厂
factory.HostName = "192.168.1.21";
factory.UserName = "yangpeng";
factory.Password = "yangpeng";
factory.Port = 5672; //默认端口号是:5672
​
C.基于链接工厂创建链接
​
ConnectionFactory factory= new ConnectionFactory(); //创建链接工厂
factory.HostName = "192.168.1.21";
factory.UserName = "yangpeng";
factory.Password = "yangpeng";
factory.Port = 5672; //默认端口号是:5672
​
using (IConnection connection = factory.CreateConnection()) //创建链接
{
    
}
D.创建信道
ConnectionFactory factory= new ConnectionFactory(); //创建链接工厂
factory.HostName = "192.168.1.21";
factory.UserName = "yangpeng";
factory.Password = "yangpeng";
factory.Port = 5672; //默认端口号是:5672
​
using (IConnection connection = factory.CreateConnection())  //基于创建链接
{
    using (IModel channel = connection.CreateModel())  //基于链接创建信道
    {
        
    }
}
E.创建队列和交换机 绑定队列和交换机
ConnectionFactory factory= new ConnectionFactory(); //创建链接工厂
factory.HostName = "192.168.1.21";
factory.UserName = "yangpeng";
factory.Password = "yangpeng";
factory.Port = 5672; //默认端口号是:5672
​
using (IConnection connection = factory.CreateConnection())
{
    using (IModel channel = connection.CreateModel())
    {
        //创建队列
        channel.QueueDeclare(queue: "OnlyProducerMessage", durable: true, exclusive: false, autoDelete: false, arguments: null);
        //创建交换机
        channel.ExchangeDeclare(exchange: "OnlyProducerMessageExChange", type: ExchangeType.Direct, durable: true, autoDelete: false, arguments: null);
        //将队列和交换机绑定
        channel.QueueBind(queue: "OnlyProducerMessage", exchange: "OnlyProducerMessageExChange", routingKey: String.Empty, arguments: null);
    }
}
F.开始发送消息
ConnectionFactory factory= new ConnectionFactory(); //创建链接工厂
factory.HostName = "192.168.1.21";
factory.UserName = "yangpeng";
factory.Password = "yangpeng";
factory.Port = 5672; //默认端口号是:5672
​
using (IConnection connection = factory.CreateConnection())
{
    using (IModel channel = connection.CreateModel())
    {
        //创建队列
        channel.QueueDeclare(queue: "OnlyProducerMessage", durable: true, exclusive: false, autoDelete: false, arguments: null);
        //创建交换机
        channel.ExchangeDeclare(exchange: "OnlyProducerMessageExChange", type: ExchangeType.Direct, durable: true, autoDelete: false, arguments: null);
        //将队列和交换机绑定
        channel.QueueBind(queue: "OnlyProducerMessage", exchange: "OnlyProducerMessageExChange", routingKey: String.Empty, arguments: null);
    
        Console.ForegroundColor=ConsoleColor.Red;
        Console.WriteLine("生产者已经准备就绪");
​
        int i = 1;
​
        while (i <= 100)
        {
            string message = $"生成者发送第{i}条消息;";
            byte[] body=Encoding.UTF8.GetBytes(message);
            channel.BasicPublish(exchange: "OnlyProducerMessageExChange", routingKey: string.Empty, basicProperties: null, body: body);
            Console.WriteLine($"消息已发送{i}条");
            i++;
            Thread.Sleep(500);
        }
    }
}

2.创建消息消费者

 
ConnectionFactory factory= new ConnectionFactory(); //创建链接工厂
factory.HostName = "192.168.1.21";
factory.UserName = "yangpeng";
factory.Password = "yangpeng";
factory.Port = 5672; //默认端口号是:5672
​
C.基于链接工厂创建链接
ConnectionFactory factory= new ConnectionFactory(); //创建链接工厂
factory.HostName = "192.168.1.21";
factory.UserName = "yangpeng";
factory.Password = "yangpeng";
factory.Port = 5672; //默认端口号是:5672
​
using (IConnection connection = factory.CreateConnection()) //创建链接
{
    
}
D.创建信道
ConnectionFactory factory= new ConnectionFactory(); //创建链接工厂
factory.HostName = "192.168.1.21";
factory.UserName = "yangpeng";
factory.Password = "yangpeng";
factory.Port = 5672; //默认端口号是:5672
​
using (IConnection connection = factory.CreateConnection())  //基于创建链接
{
    using (IModel channel = connection.CreateModel())  //基于链接创建信道
    {
        
    }
}
 

 

七.RabbitMQ常见错误

1.解决:Stats in management UI are disabled on this node

docker exec -it 容器ID bash
cd /etc/rabbitmq/conf.d/
echo management_agent.disable_metrics_collector = false > management_agent.disable_metrics_collector.conf
​

2.解决:None of the specified endpoints were reachable

A.使用guest账号 进入web管理网站
B.然后选择Admin
C.点击需要用到的账号名称
D.找到 Set permission
E.点击Set permission 按钮

 

3.解决:The AMQP operation was interrupted: AMQP close-reason, initia

var factory = new ConnectionFactory();
​
factory.AutomaticRecoveryEnabled = true;   //设置端口后自动恢复连接属性即可
这篇关于RabbitMQ安装教程(linux与Docker)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!