$ apt install rabbitmq-server
这里使用aptitude方式安装,也可以使用apt安装apt install rabbitmq-server
$ apt remove rabbitmq-server
也可以使用apt remove rabbitmq-server
$ service rabbitmq-server start
也可以是:
$ rabbitmq-server
或者:
$ rabbitmq-server start
RabbitMQ安装好后默认是启动的
$ service rabbitmq-server stop
$ service rabbitmq-server restart
$ rabbitmq-plugins enable rabbitmq_management
启动插件后,可以通过http://localhost:15672来访问RabbitMQ的管理系统。
$ rabbitmqctl status
Status of node rabbit@ubuntu
[{pid,19689},
{running_applications,
[{rabbitmq_management,"RabbitMQ Management Console","3.6.10"},
{rabbitmq_management_agent,"RabbitMQ Management Agent","3.6.10"},
{rabbitmq_web_dispatch,"RabbitMQ Web Dispatcher","3.6.10"},
{rabbit,"RabbitMQ","3.6.10"},
{cowboy,"Small, fast, modular HTTP server.","1.0.4"},
{ranch,"Socket acceptor pool for TCP protocols.","1.3.0"},
{ssl,"Erlang/OTP SSL application","8.2.3"},
{public_key,"Public key infrastructure","1.5.2"},
{asn1,"The Erlang ASN1 compiler version 5.0.4","5.0.4"},
{amqp_client,"RabbitMQ AMQP Client","3.6.10"},
{rabbit_common,
"Modules shared by rabbitmq-server and rabbitmq-erlang-client",
"3.6.10"},
{compiler,"ERTS CXC 138 10","7.1.4"},
{inets,"INETS CXC 138 49","6.4.5"},
{xmerl,"XML parser","1.3.16"},
{cowlib,"Support library for manipulating Web protocols.","1.0.2"},
{crypto,"CRYPTO","4.2"},
{os_mon,"CPO CXC 138 46","2.4.4"},
{mnesia,"MNESIA CXC 138 12","4.15.3"},
{syntax_tools,"Syntax tools","2.1.4"},
{sasl,"SASL CXC 138 11","3.1.1"},
{stdlib,"ERTS CXC 138 10","3.4.3"},
{kernel,"ERTS CXC 138 10","5.4.1"}]},
{os,{unix,linux}},
{erlang_version,
"Erlang/OTP 20 [erts-9.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:64] [kernel-poll:true]\n"},
{memory,
[{total,58743592},
{connection_readers,0},
{connection_writers,0},
{connection_channels,0},
{connection_other,2744},
{queue_procs,2744},
{queue_slave_procs,0},
{plugins,987968},
{other_proc,19330000},
{mnesia,61496},
{metrics,193824},
{mgmt_db,161176},
{msg_index,41648},
{other_ets,2127616},
{binary,93040},
{code,24708619},
{atom,1041593},
{other_system,10182140}]},
{alarms,[]},
{listeners,[{clustering,25672,"::"},{amqp,5672,"::"},{http,15672,"::"}]},
{vm_memory_high_watermark,0.4},
{vm_memory_limit,1425045913},
{disk_free_limit,50000000},
{disk_free,42574852096},
{file_descriptors,
[{total_limit,924},{total_used,2},{sockets_limit,829},{sockets_used,0}]},
{processes,[{limit,1048576},{used,330}]},
{run_queue,0},
{uptime,842},
{kernel,{net_ticktime,60}}]
查看集群信息:
$ rabbitmqctl cluster_status
Cluster status of node rabbit@ubuntu
[{nodes,[{disc,[rabbit@ubuntu]}]},
{running_nodes,[rabbit@ubuntu]},
{cluster_name,<<"rabbit@ubuntu">>},
{partitions,[]},
{alarms,[{rabbit@ubuntu,[]}]}]
// 方式一
@Test
public void connectionTest1() throws IOException, TimeoutException {
// 设置好host/port/username/password
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.1.166");
factory.setPort(5672);
factory.setUsername("xf");
factory.setPassword("123456");
// 创建连接
Connection newConnection = factory.newConnection();
System.out.println(newConnection);
newConnection.close();
}
// 方式二
@Test
public void connectionTest2() throws KeyManagementException, NoSuchAlgorithmException, URISyntaxException, IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
// 通过uri连接 amqp://用户名:密码@IP:端口
factory.setUri("amqp://xf:[email protected]:5672");
// 获取连接
Connection connection = factory.newConnection();
System.out.println(connection);
connection.close();
}
通过连接直接获取信道:
@Test
public void connectionTest1() throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.1.166");
factory.setPort(5672);
factory.setUsername("xf");
factory.setPassword("123456");
Connection newConnection = factory.newConnection();
// 从连接中获取channel
Channel channel = newConnection.createChannel();
newConnection.close();
}
交换机和队列要通过channel来声明:
// 通过channel创建 交换机
// 01.指定交换机的名字,交换机的类型
channel.exchangeDeclare("exchange_demo", BuiltinExchangeType.DIRECT);
// 02.指定交换机的名字,交换机的类型,是否是持久化的
channel.exchangeDeclare("exchange_demo01", "direct", true);
// 03.指定交换机的名字,交换机类型,是否是持久化的,是否是自动删除的
channel.exchangeDeclare("exchange_demo02", "direct", false, true, null);
// 04.指定交换机的名字,交换机类型,是否是持久化的,是否自动删除的,是否是内置的
channel.exchangeDeclare("exchange_demo03", BuiltinExchangeType.DIRECT, false, false, false, null);
以上声明交换机的方法声明如下:
Exchange.DeclareOk exchangeDeclare(String exchange,
BuiltinExchangeType type,
boolean durable,
boolean autoDelete,
boolean internal,
Map<String, Object> arguments) throws IOException;
exchange:交换机名称。
type:交换机类型,fanout、direct、topic等。
durable:是否是持久化的,及持久化的交换机会存盘,重启数据不丢失。
autoDelete:是否是自动删除的,自动删除的前提是至少有一个队列或者交换机与这个交换机绑定,之后与这个交换的队列或交换机都与此解绑。
internal:是否是内置的,程序无法直接发送消息到内置交换机,只能通过交换器路由到内置交换器。
arguments:其他的一些结构化参数。
exchangeDeclareNoWait方法创建交换机,上面一种创建方式,向服务器发送创建命令后,服务器创建好交换机会返回Exchange.Declare-OK这个命令,而这个创建方式,不需要接收服务器的返回命令,所以,对于交换机是否创建完成,是否可用也是未知的!
void exchangeDeclareNoWait(String exchange,
String type,
boolean durable,
boolean autoDelete,
boolean internal,
Map<String, Object> arguments) throws IOException;
@Test
public void testExchangeDeclare() throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.1.166");
factory.setPort(5672);
factory.setUsername("xf");
factory.setPassword("123456");
Connection connection = factory.newConnection();
Channel channel = connection.createChannel();
// 通过channel创建 交换机
//channel.exchangeDeclare("exchange_Passive", BuiltinExchangeType.DIRECT);
// 检测交换机是否存在
DeclareOk declareOk = channel.exchangeDeclarePassive("exchange_Passive1");
System.out.println(declareOk);
channel.close();
connection.close();
}
通过交换机名字检测交换机是否存在,存在正常返回,不存在抛出异常,并关闭channel:
Exchange.DeclareOk exchangeDeclarePassive(String name) throws IOException;
// 删除交换机exchange_Passive
channel.exchangeDelete("exchange_Passive");
// 删除交换机exchange_demo,这个交换机必须是没有被使用的
channel.exchangeDelete("exchange_demo", true);
// 删除交换机exchange_demo,不管有没有使用
channel.exchangeDeleteNoWait("exchange_demo", false);
// 声明一个队列,匿名队列,名字由RabbitMQ自动生成
channel.queueDeclare();
// 生成一个队列,队列名queue_demo,是否持久的,是否排他的的,是否自动删除的
channel.queueDeclare("queue_demo", true, false, true, null);
方法声明:
Queue.DeclareOk queueDeclare() throws IOException;
Queue.DeclareOk queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
Map<String, Object> arguments) throws IOException;
参数:
queue:队列名称
durable:是否持久化,持久化队列会存盘,重启不丢失信息
exclusive:是否排他的,排他队列只对首次声明他的连接(connection非cnannel)是可见的,连接端口,队列删除(即使是持久的)。
autoDelete:是否自动删除,自动删除的前提是至少有一个消费者连接到这个队列,之后所有与这个队列的连接都断开时,才会自动删除。
arguments:其他参数
声明队列的第二种方法:
Queue.DeclareOk queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete,
Map<String, Object> arguments) throws IOException;
检测队列是否存在的方法:
Queue.DeclareOk queueDeclarePassive(String queue) throws IOException;
删除队列的方法:
Queue.DeleteOk queueDelete(String queue) throws IOException;
Queue.DeleteOk queueDelete(String queue, boolean ifUnused, boolean ifEmpty) throws IOException;
void queueDeleteNoWait(String queue, boolean ifUnused, boolean ifEmpty) throws IOException;
清空队列中的内容的方法:
Queue.PurgeOk queuePurge(String queue) throws IOException;
队列和交换机绑定的方法:
Queue.BindOk queueBind(String queue, String exchange, String routingKey) throws IOException;
Queue.BindOk queueBind(String queue, String exchange, String routingKey, Map<String, Object> arguments) throws IOException;
void queueBindNoWait(String queue, String exchange, String routingKey, Map<String, Object> arguments) throws IOException;
参数:
queue:队列名
exchange:交换机名
routingKey:路由键
arguments:其他绑定参数
交换机与交换机绑定的方法:
Exchange.BindOk exchangeBind(String destination, String source, String routingKey) throws IOException;
Exchange.BindOk exchangeBind(String destination, String source, String routingKey, Map<String, Object> arguments) throws IOException;
void exchangeBindNoWait(String destination, String source, String routingKey, Map<String, Object> arguments) throws IOException;
参数:
destination:接收消息的交换机
source:发送消息的交换机
routingKey:路由键
消息是从source发送到destination
void basicPublish(String exchange, String routingKey, BasicProperties props, byte[] body) throws IOException;
void basicPublish(String exchange, String routingKey, boolean mandatory, BasicProperties props, byte[] body)
throws IOException;
void basicPublish(String exchange, String routingKey, boolean mandatory, boolean immediate, BasicProperties props, byte[] body)
throws IOException;
参数:
exchange: 交换机名称,消息要发送到哪个交换机上
routingKey:路由键,根据路由键将消息存储到对应的队列上
mandatory:
immediate:
props:基本属性
body:消息体
push和pull两种模式。
push通过订阅方式消费消息:继承DefaultConsumer或者实现Consumer接口实现。
pull模式通过channel.basicGet方法获取单条消息。
autoAck设置为false,消息发送给消费者,消费者要返回确认消息,服务器接收到确认消息后,才会删除被发送的消息。如果设置为true,消息发送后,消息自动设置为确认然后删除。
void basicAck(long deliveryTag, boolean multiple) throws IOException;
void basicReject(long deliveryTag, boolean requeue) throws IOException;