RabbitMQ群集故障转移问题

RabbitMQ群集故障转移问题,rabbitmq,spring-amqp,Rabbitmq,Spring Amqp,已创建具有两个rabbitMQ节点的群集。rabbit1和rabbit2节点的配置如下所示 1> CachingConnectionFactory connectionFactory = new CachingConnectionFactory(); connectionFactory.setAddresses("rabbit1:5672,rabbit2:5672"); 2> 节点类型 rabbit1-磁盘节点 rabbit2-ram节点 3> 生产者和消费者程序位于rabbit2节点(

已创建具有两个rabbitMQ节点的群集。rabbit1和rabbit2节点的配置如下所示

1> CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setAddresses("rabbit1:5672,rabbit2:5672");
2> 节点类型 rabbit1-磁盘节点 rabbit2-ram节点 3> 生产者和消费者程序位于rabbit2节点(即>ram节点)

4>生产者示例代码-
字符串QueueName=“队列。”;
对于(int m=0;m消费者代码-
字符串QueueName=“队列。”;
公开募捐{
System.out.println(“运行主机的使用者:+this.connectionFactory.getHost());
SimpleMessageListenerContainer容器=新SimpleMessageListenerContainer();
container.setConnectionFactory(this.connectionFactory);
container.setQueueNames(this.queueName);
container.setMessageListener(新的MessageListenerAdapter(新的TestMessageHandler(this.connectionFactory.getHost()),新的JsonMessageConverter());
container.start();
}
TestMessageHandler类示例代码-
公共TestMessageHandler(字符串主机名){
System.out.println(“主机:“+hostName”);
this.hostName=主机名;
}
//处理消息
公共无效handleMessage(int消息){
System.out.println(“handleMessage主机:“+this.hostName”);
System.out.println(“Int:+消息);
}
6> 每个节点在策略下执行
cmd>rabbitmqctl set_policy ha all“^Queue”。{“ha mode”:“all”}
7> 同时启动生产者和消费者。可以将主机名视为“rabbit1”,然后使用“rabbitmqctl stop_app”命令停止“rabbit1”节点以测试故障转移方案。然后出现以下错误
WARN[.listener.SimpleMessageListenerContainer]:使用者引发的异常,如果连接工厂支持,处理可以重新启动
com.rabbitmq.client.ShutdownSignalException:连接错误;原因:{#方法(回复代码=541,回复文本=INTERNAL_error,类id=0,方法id=0),null,“}
位于com.rabbitmq.client.impl.AMQConnection.startShutton(AMQConnection.java:678)
位于com.rabbitmq.client.impl.AMQConnection.shutdown(AMQConnection.java:668)
位于com.rabbitmq.client.impl.AMQConnection.handleConnectionClose(AMQConnection.java:624)
位于com.rabbitmq.client.impl.AMQConnection.processControlCommand(AMQConnection.java:598)
位于com.rabbitmq.client.impl.AMQConnection$1.processAsync(AMQConnection.java:96)
位于com.rabbitmq.client.impl.AMQChannel.handleCompleteInboundCommand(AMQChannel.java:144)
位于com.rabbitmq.client.impl.AMQChannel.handleFrame(AMQChannel.java:91)
位于com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:523)
信息[.listener.SimpleMessageListenerContainer]:重新启动使用者:标记=[amq.ctag-5CJ3YJYfMZDnJOnXsds6_Q],通道=缓存兔子通道:AMQChannel(amqp://guest@192.168.97.70:5672/,1),acknowledgeMode=自动本地队列大小=0
在这个警告之后,我再一次得到的主机名是“rabbit1”。实际上,根据我的理解,它应该是“rabbit2”,但它并没有发生。
因此,以下是我的疑问-
1> 为什么我在停止后仍将主机名设为“rabbit1”?
2> 为了测试故障转移,我们需要任何负载平衡器吗?
3> 如果我测试故障转移案例的步骤错误,请提供相同的步骤?
4> 如何将队列/消息分发到特定节点,如下所示:1-500条消息/队列分发到节点1,501-1000条消息/队列分发到节点2等。
5> 请告诉我是否有其他方法来测试故障转移场景?

感谢您在这方面的帮助。

将主机名仅设为“rabbit1”是什么意思??在制作人方面,捕获异常并重试是您的责任(如果您愿意,可以使用
spring retry
RetryTemplate
).使用者将自动重新连接,并自动故障转移到Rabbit2。您不会将消息分发到特定节点;兔子会计算出哪个节点是特定队列的当前主节点。停止rabbit1(主机名,其中一个rabbitmq节点在群集中运行)node,我正在消费者和生产者端打印主机名。因为我们下一次已经放弃rabbit1,它必须将“rabbit2”打印为主机名,对吗?什么是“打印”请描述您正在使用的确切API。请查找消费者代码-SimpleMessageListenerContainer=new SimpleMessageListenerContainer();container.setConnectionFactory(this.connectionFactory);container.setQueueNames(this.queueName);container.setMessageListener(新建MessageListenerAdapter(新建TestMessageHandler(this.connectionFactory.getHost()),新建JsonMessageConverter());container.start();公共类TestMessageHandler{private String hostName;public TestMessageHandler(String hostName){--}public void handleMessage(int message){System.out.println(“主机:“+this.hostName+”--“+message”);}}}新的TestMessageHandler(this.connectionFactory.getHost());这只在处理程序实例化期间执行一次-它与您连接到的兔子无关。打开调试日志记录,您将看到开关。
4> Producer sample code -
String QueueName = "Queue.";
for(int m=0; m<50000; m++){
    // send message
    System.out.println(this.rabbitTemplate.getConnectionFactory().getHost());
    this.rabbitTemplate.convertAndSend(m);
    /*Thread.sleep(100);*/
}

5> consumer code -
String QueueName = "Queue.";
public void run() {
    System.out.println("Consumer running host : " + this.connectionFactory.getHost());
    SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
    container.setConnectionFactory(this.connectionFactory);
    container.setQueueNames(this.queueName);
    container.setMessageListener(new MessageListenerAdapter(new TestMessageHandler(this.connectionFactory.getHost()), new JsonMessageConverter())); 
    container.start();
}

TestMessageHandler class sample code-

    public TestMessageHandler(String hostName){
        System.out.println("Host: " + hostName);
        this.hostName = hostName;
    }

    // Handle message
    public void handleMessage(int message) {
        System.out.println("handleMessage Host: " + this.hostName);
        System.out.println("Int : " + message);
    }

6> Each node executed below policy
cmd> rabbitmqctl set_policy ha-all "^Queue\." "{""ha-mode"":""all""}"

7> Started producer and consumer simultaneously. Could see host name as "rabbit1" then stopped "rabbit1" node with "rabbitmqctl stop_app" command to test fail-over scenario. Then got the below error

    WARN  [.listener.SimpleMessageListenerContainer]: Consumer raised exception, processing can restart if the connection factory supports it
    com.rabbitmq.client.ShutdownSignalException: connection error; reason: {#method<connection.close>(reply-code=541, reply-text=INTERNAL_ERROR, class-id=0, method-id=0), null, ""}
        at com.rabbitmq.client.impl.AMQConnection.startShutdown(AMQConnection.java:678)
        at com.rabbitmq.client.impl.AMQConnection.shutdown(AMQConnection.java:668)
        at com.rabbitmq.client.impl.AMQConnection.handleConnectionClose(AMQConnection.java:624)
        at com.rabbitmq.client.impl.AMQConnection.processControlCommand(AMQConnection.java:598)
        at com.rabbitmq.client.impl.AMQConnection$1.processAsync(AMQConnection.java:96)
        at com.rabbitmq.client.impl.AMQChannel.handleCompleteInboundCommand(AMQChannel.java:144)
        at com.rabbitmq.client.impl.AMQChannel.handleFrame(AMQChannel.java:91)
        at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:523)
    INFO  [.listener.SimpleMessageListenerContainer]: Restarting Consumer: tag=[amq.ctag-5CJ3YJYfMZDnJOnXsds6_Q], channel=Cached Rabbit Channel: AMQChannel(amqp://guest@192.168.97.70:5672/,1), acknowledgeMode=AUTO local queue size=0

after this warning, again am getting host name as "rabbit1" only. actually it should be "rabbit2" as per my understanding but its not happening.

    So, here are my Queries -

        1> Why am getting host name as "rabbit1" even after stopping?
        2> To test the fail-over do we require any load balancer?
        3> If my steps are wrong for testing fail-over case, please provide steps for the same?
        4> How to distribute queues/messages to particular node, as below, 1-500 messages/queues to node1, 501-1000 messages/queues to node2, etc.
        5> Please let me know is there any other approach to test fail-over scenario?