Centos7.0 Rabbitmq 环境搭建及.NET 简单应用(二)
一、下载erlang 和rabbitmq
二、安装环境
1、安装Erlang,安装完成测试安装结果
[root@localhost Rabbitmq]# rpm -ivh erlang-19.0.4-1.el7.centos.x86_64.rpm
准备中... ################################# [100%]
正在升级/安装...
1:erlang-19.0.4-1.el7.centos ################################# [100%]
[root@localhost Rabbitmq]# erl
Erlang/OTP 19 [erts-8.0.3] [source] [64-bit] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false]
pvuf
Eshell V8.0.3 (abort with ^G)
1> 5+6
.
11
2> halt().
2、安装Rabbitmq并测试安装结果,
[root@localhost Rabbitmq]# rpm -ivh rabbitmq-server-3.6.6-1.el7.noarch.rpm
警告:rabbitmq-server-3.6.6-1.el7.noarch.rpm: 头V4 RSA/SHA1 Signature, 密钥 ID 6026dfca: NOKEY
错误:依赖检测失败:
socat 被 rabbitmq-server-3.6.6-1.el7.noarch 需要
[root@localhost Rabbitmq]# yum install socat
已加载插件:fastestmirror, langpacks
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
base | 3.6 kB 00:00:00
docker-main-repo | 2.9 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:02
(1/3): extras/7/x86_64/primary_db | 184 kB 00:00:01
(2/3): docker-main-repo/primary_db | 34 kB 00:00:08
(3/3): updates/7/x86_64/primary_db | 7.1 MB 00:04:02
Loading mirror speeds from cached hostfile
* base: centos.ustc.edu.cn
* extras: centos.ustc.edu.cn
* updates: mirrors.zju.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 socat.x86_64.0.1.7.2.2-5.el7 将被 安装
--> 解决依赖关系完成
[root@localhost Rabbitmq]# rpm -ivh rabbitmq-server-3.6.6-1.el7.noarch.rpm
警告:rabbitmq-server-3.6.6-1.el7.noarch.rpm: 头V4 RSA/SHA1 Signature, 密钥 ID 6026dfca: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:rabbitmq-server-3.6.6-1.el7 ################################# [100%]
[root@localhost Rabbitmq]# cd /sbin/
[root@localhost sbin]# ./rabbitmq-plugins list
Configured: E = explicitly enabled; e = implicitly enabled
| Status: [failed to contact rabbit@localhost - status not shown]
[root@localhost sbin]# /sbin/service rabbitmq-server start
Redirecting to /bin/systemctl start rabbitmq-server.service
[root@localhost sbin]# ./rabbitmqctl status
Status of node rabbit@localhost ...
[{pid,5084},
{running_applications,[{rabbit,"RabbitMQ","3.6.6"},
{mnesia,"MNESIA CXC 138 12","4.14"},
{rabbit_common,[],"3.6.6"},
{os_mon,"CPO CXC 138 46","2.4.1"},
{ranch,"Socket acceptor pool for TCP protocols.",
"1.2.1"},
................
3、增加用户信息
[root@localhost sbin]# ./rabbitmqctl add_user admin admin #创建管理用户
Creating user "admin" ...
[root@localhost sbin]# ./rabbitmqctl set_user_tags admin adminstrator #设置管理员
Setting tags for user "admin" to [adminstrator] ...
[root@localhost sbin]# ./rabbitmqctl set_permissions admin ".*" ".*" ".*" #设置权限
[root@localhost sbin]# ./rabbitmqctl list_users
Listing users ...
admin [adminstrator]
guest [administrator]
4、增加Rabbitmq配置文件rabbimq.config,配置防火墙
[root@localhost rabbitmq]# vi rabbitmq.config
写入内容:
[
{rabbit, [{tcp_listeners, [5672]}, {loopback_users, ["admin"]}]}
].
说明:
loopback_users:设置RabbitMq访问服务的用户。
tcp_listeners:设置RabbitMQ监听的IP地址与端口。
重启Rabbitmq服务
[root@localhost rabbitmq]# service rabbitmq-server stop
Redirecting to /bin/systemctl stop rabbitmq-server.service
[root@localhost rabbitmq]# service rabbitmq-server start
Redirecting to /bin/systemctl start rabbitmq-server.service
配置防火墙
[root@localhost rabbitmq]# firewall-cmd --zone=public --add-port=5672/tcp --permanent
success
[root@localhost rabbitmq]# firewall-cmd --zone=public --add-port=15672/tcp --permanent
success
[root@localhost rabbitmq]# firewall-cmd --reload
success
[root@localhost sbin]# ./rabbitmq-plugins enable rabbitmq_management #启用Web监控功能
The following plugins have been enabled:
mochiweb
webmachine
rabbitmq_web_dispatch
amqp_client
rabbitmq_management_agent
rabbitmq_management
Applying plugin configuration to rabbit@localhost... started 6 plugins.
5、其他命令
[root@localhost sbin]# ./rabbitmqctl delete_user XXX
[root@localhost sbin]# ./rabbitmqctl change_passwork username password
[root@localhost sbin]# ./rabbitmqctl list_users
[root@localhost sbin]# ./rabbitmqctl list_queues
三、.NET环境简单应用
在.NET中使用RabbitMQ需要下载RabbitMQ的客户端程序集,可以使用Nuget下载使用,这就是RabbitMQ的客户端。
RabbitMQ是一个消息代理。他从消息生产者(producers)那里接收消息,然后把消息送给消息消费者(consumer)在发送和接受之间,他能够根据设置的规则进行路由,缓存和持久化。
1、发送信息代码
1 string queueName = "Test"; 2 var factory = new ConnectionFactory(); 3 factory.HostName = "195.1.1.90"; 4 factory.UserName = "admin"; 5 factory.Password = "admin"; 6 7 using (var connection = factory.CreateConnection()) 8 { 9 using (var channel = connection.CreateModel()) 10 { 11 channel.QueueDeclare(queueName, false, false, false, null); 12 string message = "XXXXXXXXXXXXXXXXX"; 13 var body = Encoding.UTF8.GetBytes(message); 14 15 //设置消息持久化 16 IBasicProperties properties = channel.CreateBasicProperties(); 17 properties.DeliveryMode = 2; 18 19 channel.BasicPublish("", queueName, properties, body); 20 Console.WriteLine(" Send Message: {0}", message); 21 } 22 }
2、接收信息代码
1 string queueName = "Test"; 2 var factory = new ConnectionFactory(); 3 factory.HostName = "195.1.1.90"; 4 factory.UserName = "admin"; 5 factory.Password = "admin"; 6 7 using (var connection = factory.CreateConnection()) 8 { 9 using (var channel = connection.CreateModel()) 10 { 11 //定义一个持久化队列,名称相同不会重复创建 12 channel.QueueDeclare(queueName, false, false, false, null); 13 14 //输入1,如果接收一个消息,在没有应答之前,客户端不会收到下一个消息 15 channel.BasicQos(0, 1, false); 16 17 var consumer = new QueueingBasicConsumer(channel); 18 channel.BasicConsume(queueName, false, consumer); 19 20 Console.WriteLine(" waiting for message."); 21 while (true) 22 { 23 //阻塞函数,获取队列中的消息 24 var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue(); 25 26 var body = ea.Body; 27 var message = Encoding.UTF8.GetString(body); 28 Console.WriteLine("Received {0}", message); 29 30 //回复确认 31 channel.BasicAck(ea.DeliveryTag, false); 32 } 33 } 34 }