[rabbitmq-discuss] Help,question about rabbitmq+Haproxy,client java
Aaron
584911644 at qq.com
Tue Jul 22 08:53:45 BST 2014
I have let only one node rabbitmq online in my company in order to decoupling messages. This node has not been down for almost one month fortunately?but some basic problem,such as the HA and Load balancing need to be considered,so that can keep all messages complete no matter when the related server or app is down.Such like,another node will work still and keep the produce、customer healthy when a node is down.
So,i have considered a solution to protect from losting messages,it‘s rabbitmq(mirrored queue)+Haproxy.
I have tried two different code,php plays a role,both HA and load balancing.
But,java code is not enough fortunately,LOL... Testing for several times,and I get a result,that is java code doesn‘t work at all in HA,but only load balancing....,during testing,when I down a random node,producer will throw out a related session error.
Having troubled me for almost one week this problem,and looking sincerely forward to ur reply.
The environment will be shown below:
Haproxy:192.168.1.61:5670
rabbit at ubuntu :192.168.1.61:5672(3.1.5)
rabbit at ubuntu2:192.168.1.35:5672(3.1.5)
PHP code:
<?php
$n = 100000;
for($i=0;$i<$n;$i++){
echo '<br/>'.$i;
//连接RabbitMQ
$conn_args = array( 'host'=>'192.168.1.61' , 'port'=> '5670', 'login'=>'aaron' , 'password'=> '5555','vhost' =>'/');
$conn = new AMQPConnection($conn_args);
$conn->connect();
//创建exchange名称和类型
$channel = new AMQPChannel($conn);
$ex = new AMQPExchange($channel);
$ex->setName('direct_exchange_name');
$ex->setType(AMQP_EX_TYPE_DIRECT);
$ex->setFlags(AMQP_DURABLE | AMQP_AUTODELETE);
$ex->declare();
//创建queue名称,使用exchange,绑定routingkey
$q = new AMQPQueue($channel);
$q->setName('queue_name');
$q->setFlags(AMQP_DURABLE | AMQP_AUTODELETE);
$q->declare();
$q->bind('direct_exchange_name', 'routingkey_name');
//消息发布
$channel->startTransaction();
$message = json_encode(array('Hello World!','1','2','3','4','DIRECT'));
$ex->publish($message, 'routingkey_name');
$channel->commitTransaction();
$conn->disconnect();
}
?>
JAVA code:
ConnectionFactory factory = new ConnectionFactory();// 创建链接工厂
factory.setHost("192.168.1.61");
factory.setUsername("aaron");
factory.setPassword("5555");
factory.setPort(“5670”);
factory.setAutomaticRecoveryEnabled(true);
Connection connection = factory.newConnection();// 创建链接
factory.setNetworkRecoveryInterval(10000);
for(int i = 0; i < 10000; i++) {
Channel channel = connection.createChannel();// 创建信息通道
channel.exchangeDeclare(EXCHANGE_NAME, "fanout", durable);// 创建交换机并生命持久化
String message = "Hello Wrold " + Math.random();
// 消息的持久化
channel.basicPublish(EXCHANGE_NAME, "",
MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
System.out.println("[x] Sent '" + message + "'");
channel.close();
}
connection.close();
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20140722/27fb9aae/attachment.html>
More information about the rabbitmq-discuss
mailing list