RabbitMQ 默认有一个根VHOST 当然一般情况下我们都会创建属于某个应用程序的vhost 去方便管理。在个节点当中vhost 与 vhost 之间是互相隔离的,所以在多个不同的vhost当中可以创建相同名称的 交换器(exchange) 队列(queue)。这个概念就像是数据库一样,mysql当中有个多个不同的数据库,多个数据库当中可以使用相同的表名的概念一样。
接下来我们就来开始写一下如何创建vhost~
创建Vhost MacBook-Pro:rabbitmq Tony$ /usr/local/sbin/rabbitmqctl add_vhost APP_A Creating vhost "APP_A" ... 删除Vhost MacBook-Pro:rabbitmq Tony$ /usr/local/sbin/rabbitmqctl delete_vhost APP_A Deleting vhost "APP_A" ... 查看节点上的Vhost MacBook-Pro:rabbitmq Tony$ /usr/local/sbin/rabbitmqctl list_vhosts Listing vhosts ... / APP_A APP_B Java客户端中链接相应的vhost ConnectionFactory factory = new ConnectionFactory(); factory.setHost("127.0.0.1"); //服务器地址 factory.setUsername("guest"); //默认用户名 factory.setPassword("guest"); //默认密码 factory.setVirtualHost("APP_A"); //vhost名称 factory.setPort(5672); //默认端口启动和停止RabbitMQ RabbitMQ启动的方式比较简单,但是关闭会有几种方式去个关闭,而且关闭的情况各有不同。
启动RabbitMQ MacBook-Pro:~ Tony$ /usr/local/sbin/rabbitmq-server RabbitMQ 3.5.6. Copyright (C) 2007-2015 Pivotal Software, Inc. ## ## Licensed under the MPL. See ## ## ########## Logs: /usr/local/var/log/rabbitmq/rabbit@localhost.log ###### ## /usr/local/var/log/rabbitmq/rabbit@localhost-sasl.log ########## Starting broker... completed with 10 plugins.我不建议,直接在这里使用ctrl+C的方法直接结束应用程序,我这里推荐以下方式去关闭RabbitMQ。使用这种方式有效干净关闭,并且保护好持久化的队列。
关闭RabbitMQ MacBook-Pro:rabbitmq Tony$ /usr/local/sbin/rabbitmqctl stop Stopping and halting node rabbit@localhost ...当然也可以使用 -n rabbit@[hostname]来关闭其他远程节点。这个会在介绍群集的时候在贴出代码
查看RabbitMQ 状态 MacBook-Pro:lib Tony$ /usr/local/sbin/rabbitmqctl status Status of node rabbit@localhost ... [{pid,10780}, {running_applications, [{rabbitmq_management_visualiser,"RabbitMQ Visualiser","3.5.6"}, {rabbitmq_management,"RabbitMQ Management Console","3.5.6"}, {rabbitmq_web_dispatch,"RabbitMQ Web Dispatcher","3.5.6"}, {webmachine,"webmachine","1.10.3-rmq3.5.6-gite9359c7"}, {mochiweb,"MochiMedia Web Server","2.7.0-rmq3.5.6-git680dba8"}, {rabbitmq_mqtt,"RabbitMQ MQTT Adapter","3.5.6"}, {rabbitmq_stomp,"Embedded Rabbit Stomp Adapter","3.5.6"}, {rabbitmq_management_agent,"RabbitMQ Management Agent","3.5.6"}, {rabbitmq_amqp1_0,"AMQP 1.0 support for RabbitMQ","3.5.6"}, {rabbit,"RabbitMQ","3.5.6"}, {os_mon,"CPO CXC 138 46","2.3.1"}, {inets,"INETS CXC 138 49","5.10.6"}, {mnesia,"MNESIA CXC 138 12","4.12.5"}, {amqp_client,"RabbitMQ AMQP Client","3.5.6"}, {xmerl,"XML parser","1.3.7"}, {sasl,"SASL CXC 138 11","2.4.1"}, {stdlib,"ERTS CXC 138 10","2.4"}, {kernel,"ERTS CXC 138 10","3.2"}]}, {os,{unix,darwin}}, {erlang_version, "Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:4:4] [async-threads:64] [hipe] [kernel-poll:true]\n"}, {memory, [{total,43180016}, {connection_readers,0}, {connection_writers,0}, {connection_channels,0}, {connection_other,5616}, {queue_procs,19712}, {queue_slave_procs,0}, {plugins,868704}, {other_proc,13887864}, {mnesia,72792}, {mgmt_db,234672}, {msg_index,47824}, {other_ets,1275216}, {binary,19872}, {code,20760734}, {atom,711569}, {other_system,5275441}]}, {alarms,[]}, {listeners, [{clustering,25672,"::"}, {amqp,5672,"127.0.0.1"}, {stomp,61613,"::"}, {mqtt,1883,"::"}]}, {vm_memory_high_watermark,0.4}, {vm_memory_limit,1401384140}, {disk_free_limit,50000000}, {disk_free,8082620416}, {file_descriptors, [{total_limit,156},{total_used,5},{sockets_limit,138},{sockets_used,3}]}, {processes,[{limit,1048576},{used,198}]}, {run_queue,0}, {uptime,8267}] 再看看 RabbitMQ的配置文件