在CentOS7上安装RabbitMQ详解


Installing on RPM-based Linux (RHEL, CentOS, Fedora, openSUSE)

rpm -Uvh http://download.Fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch.rpm
yum install erlang

安装过程中会有提示,一路输入“y”即可。

目前创新互联已为千余家的企业提供了网站建设、域名、网站空间网站托管、企业网站设计、鄂托克前网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

完成后安装RabbitMQ:

先下载rpm:

wget http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.6/rabbitmq-server-3.6.6-1.el7.noarch.rpm

下载完成后安装:

yum install rabbitmq-server-3.6.6-1.el7.noarch.rpm 

完成后启动服务:

service rabbitmq-server start

可以查看服务状态:

service rabbitmq-server status

这里可以看到log文件的位置,转到文件位置,打开文件:

这里显示的是没有找到配置文件,我们可以自己创建这个文件

cd /etc/rabbitmq/
vi rabbitmq.config

编辑内容如下:

[{rabbit, [{loopback_users, []}]}].

这里的意思是开放使用,rabbitmq默认创建的用户guest,密码也是guest,这个用户默认只能是本机访问,localhost或者127.0.0.1,从外部访问需要添加上面的配置。

保存配置后重启服务:

service rabbitmq-server stop
service rabbitmq-server start

此时就可以从外部访问了,但此时再看log文件,发现内容还是原来的,还是显示没有找到配置文件,可以手动删除这个文件再重启服务,不过这不影响使用

rm rabbit\@mythsky.log 
service rabbitmq-server stop
service rabbitmq-server start

开放5672端口:

firewall-cmd --zone=public --add-port=5672/tcp --permanent
firewall-cmd --reload 

在Windows上进行测试:

新建.net core控制台项目,引用RabbitMQ.Client包:

Install-Package RabbitMQ.Client

测试代码:

public static void Main(string[] args)
        {
            ConnectionFactory factory = new ConnectionFactory();
            factory.UserName = "guest";
            factory.Password = "guest";
            factory.VirtualHost = "/";
            factory.HostName = "localhost";
            //factory.HostName = "10.255.19.111";
            try
            {
                IConnection conn = factory.CreateConnection();
                IModel model = conn.CreateModel();
                string exchangeName = "test";
                string queueName = "testq";
                string routingKey = "first";

                model.ExchangeDeclare(exchangeName, ExchangeType.Direct);
                model.QueueDeclare(queueName, false, false, false, null);
                model.QueueBind(queueName, exchangeName, routingKey, null);
                byte[] messageBodyBytes = System.Text.Encoding.UTF8.GetBytes("Hello, world!");
                model.BasicPublish(exchangeName, routingKey, null, messageBodyBytes);
                Console.WriteLine("message sended.");

                bool noAck = false;
                BasicGetResult result = model.BasicGet(queueName, noAck);
                if (result == null)
                {
                    Console.Write("no message.");
                }
                else
                {
                    IBasicProperties props = result.BasicProperties;
                    byte[] body = result.Body;
                    model.BasicAck(result.DeliveryTag, false);
                    string message = System.Text.Encoding.UTF8.GetString(body);
                    Console.Write(message);
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
        }

 也可以使用官网的例子(这里更清晰):

http://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html

发送端:

using System;
using RabbitMQ.Client;
using System.Text;

class Send
{
    public static void Main()
    {
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using(var connection = factory.CreateConnection())
        using(var channel = connection.CreateModel())
        {
            channel.QueueDeclare(queue: "hello",
                                 durable: false,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

            string message = "Hello World!";
            var body = Encoding.UTF8.GetBytes(message);

            channel.BasicPublish(exchange: "",
                                 routingKey: "hello",
                                 basicProperties: null,
                                 body: body);
            Console.WriteLine(" [x] Sent {0}", message);
        }

        Console.WriteLine(" Press [enter] to exit.");
        Console.ReadLine();
    }
}

接收端:

using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;

class Receive
{
    public static void Main()
    {
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using(var connection = factory.CreateConnection())
        using(var channel = connection.CreateModel())
        {
            channel.QueueDeclare(queue: "hello",
                                 durable: false,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

            var consumer = new EventingBasicConsumer(channel);
            consumer.Received += (model, ea) =>
            {
                var body = ea.Body;
                var message = Encoding.UTF8.GetString(body);
                Console.WriteLine(" [x] Received {0}", message);
            };
            channel.BasicConsume(queue: "hello",
                                 noAck: true,
                                 consumer: consumer);

            Console.WriteLine(" Press [enter] to exit.");
            Console.ReadLine();
        }
    }
}

在Windows上发送,在CentOS上接收,效果如图:

 

开启管理UI:

rabbitmq-plugins enable rabbitmq_management
firewall-cmd --zone=public --add-port=15672/tcp --permanent
firewall-cmd --reload

在Windows下打开地址:

http://10.255.19.111:15672

用户名和密码都是 guest

这样就可以方便管理RabbitMQ了。

分享题目:在CentOS7上安装RabbitMQ详解
本文地址:http://www.shufengxianlan.com/qtweb/news41/101441.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联