public static void main(String[] args) throws IOException,TimeoutException,InterruptedException{
Address[] addresses = new Address[]{
new Address(IP_ADDRESS,PORT)
};
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("root");
factory.setPassword("root123");
// 这里的连接方式与生产者的demo略有不同,注意区别
Connection connection = factory.newConnection(addresses); //创建连接
final Channel channel = connection.createChannel(); // 创建信道
channel.basicQos(64);// 设置客户端最多接收未被ack的消息的个数
/**
* 这里采用了继承DefaultConsumer的方式来实现消费,有过RabbitMQ使用经验的开发者
* 可能喜欢使用QueueingConsumer的方式来实现消费
* 因为使用QueueingConsumer会有一些隐患。
* 同时在RabbitMQ Java客户端4.0.0版本开始将QueueingConsumer标记为@Deprecated了
*/
Consumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag,Envelope envelope,AMQP.BasicProperties properties,byte[] body)
throws IOException{
System.out.println("recv message : " + new String(body));
try{
TimeUnit.SECONDS.sleep(1);
}catch(InterruptedException e){
e.printStackTrace();
}
channel.basicAck(envelope.getDeliveryTag(),false);
}
};
channel.basicConsume(QUEUE_NAME,true, consumer);
//等待回调函数执行完毕之后,关闭资源
TimeUnit.SECONDS.sleep(5);
channel.close();
connection.close();
}
}
查看集群中诸如用户数、交换器数量、队列数量等
[root@node171 rabbitmq]# rabbitmqctl list_users
Listing users ...
admin [monitoring]
guest [administrator]
root []
[root@node171 rabbitmq]# rabbitmqctl list_exchanges
Listing exchanges for vhost / ...
amq.topic topic
amq.headers headers
exchange_test_3 direct
amq.direct direct
exchange_test_2 direct
amq.rabbitmq.trace topic
amq.match headers
direct
exchange_test_1 direct
amq.fanout fanout
[root@node171 rabbitmq]# rabbitmqctl list_queues
Timeout: 60.0 seconds ...
Listing queues for vhost / ...
queue_test_3 100000
queue_test_2 200
queue_test_1 10000
迁移方案
迁移步骤
1. 停止所有生产者和消费者的应用程序
2. 将集群B中的机器依次一台一台加入集群A中,并确认所有队列镜像完成
3. 剔除集群A中的一台一台机器
4. 将应用指向集群B
方案1【不可行】
将集群B中的一台机器加入集群A中,然后再集群B中的另一他机器已加入集群,然后剔除集群A中的一台机器,然后再剔除集群A中的另一台机器
此方案对于RabbitMQ的普通集群也即是Cluster模式是无效的
1. 停止A集群中的所有连接
2. 将集群B中的一台节点加入到A集群中
将集群A中的.erlang.cookie的值拷贝到集群B中的node173上
[root@node171 rabbitmq]# cat .erlang.cookie
ORMTFBMHOXOGFKRLQSPU[root@node171 rabbitmq]#
[root@node173 plugins]# cp /var/lib/rabbitmq/.erlang.cookie /var/lib/rabbitmq/erlang.cookie.bak
[root@node173 plugins]# chmod 700 /var/lib/rabbitmq/.erlang.cookie
[root@node173 plugins]# vi /var/lib/rabbitmq/.erlang.cookie
ORMTFBMHOXOGFKRLQSPU
[root@node173 plugins]# chmod 400 /var/lib/rabbitmq/.erlang.cookie
[root@node173 plugins]# ls -lrth /var/lib/rabbitmq/.erlang.cookie
-r-------- 1 rabbitmq rabbitmq 21 Oct 24 18:51 /var/lib/rabbitmq/.erlang.cookie
3.将集群B中的node173加入到集群A中
[root@node173 rabbitmq]# service rabbitmq-server start
Redirecting to /bin/systemctl start rabbitmq-server.service
[root@node173 rabbitmq]# rabbitmqctl stop_app
Stopping rabbit application on node mq_173@node173 ...
[root@node173 rabbitmq]# rabbitmqctl reset
Resetting node mq_173@node173 ...
[root@node173 rabbitmq]# rabbitmqctl join_cluster mq171@node171
Clustering node mq_173@node173 with mq171@node171
[root@node173 rabbitmq]# rabbitmqctl start_app
Starting node mq_173@node173 ...
completed with 3 plugins.
4. 同样的方法将集群B中的node174加入到集群A中
[root@node174 rabbitmq]# rabbitmqctl cluster_status
Cluster status of node mq_174@node174 ...
[{nodes,[{disc,[mq_174@node174]}]},
{running_nodes,[mq_174@node174]},
{cluster_name,<<"mq_174@node174">>},
{partitions,[]},
{alarms,[{mq_174@node174,[]}]}]
[root@node174 rabbitmq]# rabbitmqctl stop_app
Stopping rabbit application on node mq_174@node174 ...
[root@node174 rabbitmq]# rabbitmqctl reset
Resetting node mq_174@node174 ...
[root@node174 rabbitmq]# rabbitmqctl join_cluster mq171@node171
Clustering node mq_174@node174 with mq171@node171
[root@node174 rabbitmq]# rabbitmqctl start_app
Starting node mq_174@node174 ...
completed with 0 plugins.
5.将集群A中的node171剔除集群
[root@node171 rabbitmq]# rabbitmqctl stop
Stopping and halting node mq171@node171 ...
这时访问node172 的Web集群管理
同样在node173上的Web管理界面查看
至此对于普通的集群模式,这种方案是不行的。
方案2【可行】
若RabbitMQ采用镜像队列,将集群A中的消息数据迁移到集群B中,
集群A中的node171、node172采用镜像队列
构建集群A的镜像队列环境
1.首先集群A中的node172加入集群中
【在node172上操作】
[root@node172 ~]# rabbitmqctl stop_app
Stopping rabbit application on node mq172@node172 ...
[root@node172 ~]# rabbitmqctl reset
Resetting node mq172@node172 ...
[root@node172 ~]# rabbitmqctl join_cluster mq171@node171
Clustering node mq172@node172 with mq171@node171
ra[root@node172 ~]# rabbitmqctl start_app
Starting node mq172@node172 ...
2.设置镜像策略
【在node171上操作】
[root@node171 ~]# rabbitmqctl set_policy ha-all -p kcvhost "^" '{"ha-mode":"all","ha-sync-mode":"automatic"}'
Setting policy "ha-all" for pattern "^" to "{"ha-mode":"all","ha-sync-mode":"automatic"}" with priority "0" for vhost "kcvhost" ...
[root@node171 ~]# rabbitmqctl set_policy rabbit_mirror "^" '{"ha-mode":"all"}'
Setting policy "rabbit_mirror" for pattern "^" to "{"ha-mode":"all"}" with priority "0" for vhost "/" ...
开始集群A中的镜像队列迁移
1.停止所有消息的生产者和消费者相关应用服务
2.停止集群A中的所有机器,并备份原始数据
【node171、node172】都要操作
Node172执行如下:
[root@node172 ~]# service rabbitmq-server stop
Redirecting to /bin/systemctl stop rabbitmq-server.service
[root@node172 ~]# cd /var/lib/rabbitmq/
[root@node172 rabbitmq]# ls
mnesia
[root@node172 rabbitmq]# cp -rf mnesia mnesia.20181025.bak
[root@node172 rabbitmq]# service rabbitmq-server start
Redirecting to /bin/systemctl start rabbitmq-server.service
node171执行如下:
[root@node171 ~]# service rabbitmq-server stop
Redirecting to /bin/systemctl stop rabbitmq-server.service
[root@node171 ~]# cd /var/lib/rabbitmq/
[root@node171 rabbitmq]# cp -rf mnesia mnesia.20181025.bak
[root@node171 rabbitmq]# service rabbitmq-server start
Redirecting to /bin/systemctl start rabbitmq-server.service
2.首先将集群B的node173机器加入到集群A中
[root@node173 network-scripts]# service rabbitmq-server stop
Redirecting to /bin/systemctl stop rabbitmq-server.service
[root@mq04 rabbitmq]# cp -rf /var/lib/rabbitmq /var/lib/rabbitmq.bak
[root@mq04 rabbitmq]# cd /var/lib/rabbitmq
[root@mq04 rabbitmq]# rm -rf .erlang.cookie mnesia/
[root@mq01 rabbitmq]# scp .erlang.cookie root@mq04:/var/lib/rabbitmq
The authenticity of host 'mq04 (192.168.0.232)' can't be established.
ECDSA key fingerprint is SHA256:zgAicKOpvRLLCyhdUbpNvyanKYrPt/Pp9g+Sdq9mAoo.
ECDSA key fingerprint is MD5:15:7d:1e:c2:86:d5:4a:40:63:df:f5:4e:65:c4:24:62.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'mq04' (ECDSA) to the list of known hosts.
root@mq04's password:
Permission denied, please try again.
root@mq04's password: