RABBITMQ cluster Construction (centos6.5)

Source: Internet
Author: User
Tags rabbitmq

One: Installation of RABBITMQ:

Reference: http://www.blogjava.net/hellxoul/archive/2014/06/25/415135.html

http://blog.haohtml.com/archives/15249

Description: Modify the machine name before installing (prepare for the cluster behind)

Vi/etc/sysconfig/network Modify Name

Vi/etc/hosts Modify the address mapping table, such as 192.168.1.112 rabbitmq-node1.com rabbitmq-node1 #做集群时设置

Restart

1. Install Erlang:

#rpm-UVH http://mirrors.hustunique.com/epel/6/x86_64/epel-release-6-8.noarch.rpm

#yum Install Erlang

Test: Erl:

Io:format ("Hello").

2. Install RABBITMQ:

Upload rabbitmq-server-3.3.4-1.noarch.rpm

#yum Install rabbitmq-server-3.3.4-1.noarch.rpm

3. Installation Complete

4. Start:

/etc/init.d/rabbitmq-server start

5. Turn on the plugin:

Rabbitmq-plugins Enable Rabbitmq_management

Allow remote access:

Vi/etc/rabbitmq.config

[

{rabbit, [{tcp_listeners, [5672]}, {loopback_users, []}]}

].

Close Iptables:service iptables Stop

Chkconfig iptables off Disable boot start

Access http://ip:15672 account password for guest

Second: Cluster construction :

Reference: http://www.cnblogs.com/flat_peach/archive/2013/04/07/3004008.html

Http://www.centoscn.com/CentosServer/cluster/2014/1216/4324.html

Description: 3 machines, named Rabbitmq-node1,rabbitmq-node2,rabbitmq-node3

1. Modify the address mapping:

Vi/etc/hosts Modify the address mapping table, such as 192.168.1.112 rabbitmq-node1.com rabbitmq-node1

2. Set the node cookie:

First Close Rabbitmq-server:/etc/init.d/rabbitmq-server stop

Cook is stored in the/var/lib/rabbitmq/.erlang.cookie file, which has a permission of 400, modified to 777 before modification, and changed to 400

Copy the contents of one of the files to all other machines so that all machine cookies in the cluster are consistent or cannot be communicated

chmod 777/var/lib/rabbitmq/.erlang.cookie

After copying, start Rabbitmq:/etc/init.d/rabbitmq-server.

3.rabbitmq-node1,rabbitmq-node2 as a memory node, rabbitmq-node3 as a disk node, connected together to do the cluster:

Rabbitmq-node1:

Rabbitmqctl Stop_app: Stop app

Rabbitmqctl join_cluster--ram [email protected]: adding nodes

Rabbitmqctl Start_app: Open app

Rabbitmqctl cluster_status: View status

Rabbitmq-node2:

Rabbitmqctl Stop_app: Stop app

Rabbitmqctl join_cluster--ram [email protected]: adding nodes

Rabbitmqctl Start_app: Open app

Rabbitmqctl cluster_status: View status

RABBITMQ-NODE3:

Rabbitmqctl cluster_status: View status

If you want to make rabbitmq-node1 or Rabbitmq-node2 in the cluster is also a disk node, Join_cluster command to remove the--ram parameters: Rabbitmqctl join_cluster [email protected]

The cluster mode is divided into normal mode (above mode) and mirror mode:

Normal mode: Only one of the nodes exists in the message entity, and when the consumer of the other node gets the message, the other node gets the message from that node and then passes it to the consumer, which is prone to the bottleneck of the node.

Mirroring mode: The message entity is actively synchronizing between nodes, rather than synchronizing when the consumer acquires it. Reduce system performance consumption bandwidth

Execute at any node: rabbitmqctl set_policy ha-all "^" ' {"Ha-mode": "All"} ', it becomes mirror mode.

Three: Python operation Rabbitmq:

Reference: http://www.01happy.com/python-rabbitmq-work-queues/

http://www.01happy.com/python-rabbitmq-exchanges/

http://yidao620c.iteye.com/blog/1947338

1. Send a message:

#!/usr/bin/env python

#-*-Coding:utf-8-*-

Import Sys

Import Pika

Connection = Pika. Blockingconnection (Pika. Connectionparameters (' localhost ')) #连接服务器

Channel = Connection.channel () #创建channel

Channel.queue_declare (queue = ' Hello ') #声明消息队列, this information will be purged automatically if the queue does not exist. Hello in Benli

If Len (SYS.ARGV) < 2:

print ' message is empty! '

Sys.exit (0)

message = Sys.argv[1]

Channel.basic_publish (Exchange = ", routing_key= ' hello ', BODY = message) #发送消息到上面声明的队列中. Exchange is a switch that can specify exactly which queue to send to; Route_key is the name of the queue, body is the body of the message

print "[x] Sent: '" + message + "\ n"

Connection.close () #关闭连接

2. Receive the message:

#!/usr/bin/env python

#-*-Coding:utf-8-*-

Import Pika

Connection = Pika. Blockingconnection (Pika. Connectionparameters (' localhost '))

Channel = Connection.channel ()

Channel.queue_declare (queue = ' Hello ')

print ' [*] waiting for messages. To exit Press CTRL + C '

DEF callback (ch, method, properties, body): #回调函数, Handling accepted messages

Print body

Channel.basic_consume (callback, queue = ' Hello ', no_ack = True) #告诉rabbitmq使用callback接收消息, no_ack=true indicates that the message is not active after consumption is finished Notify RABBITMQ of the completion status.

Channel.start_consuming () #开始接收信息, into the blocking state, there is information in the queue to call callback for processing. Press CTRL + C to exit

You can open multiple receive instances, each of which can receive a subset of messages, and the sum of messages received by multiple instances is the number of messages sent.

##########################################################################################

The above message receives an instance, and after a processing message has an error, the message is discarded and will not be processed by another instance. In order to ensure that every message will not be discarded, you can add message confirmation during processing,

That is, modify the callback function as follows:

DEF callback (ch, method, properties, body):

print "[x] Received%r"% (body,)

Time.sleep (5)

print "[x] Done"

Ch.basic_ack (Delivery_tag = Method.delivery_tag) #增加确认

Channel.basic_consume (callback, queue= ' Hello ', no_ack=false) #需要ack

##########################################################################################

Message persistence:

The task will be lost when the rabbit is hung, and the message will be persisted.

Channel.queue_declare (queue= ' Hello ', durable=true) #增加durable =true, the existing queue does not, will be an error. Need to re-create queue

Channel.basic_publish (Exchange = ', routing_key= ' hello ', BODY = Message,properties=pika. Basicproperties (Delivery_mode = 2,))

Fair Dispatch:

Some tasks take a long time, some time is short, using Basic_qos to set prefetch_count=1, so that RABBITMQ does not assign multiple tasks to workers at the same time, that is, only after the worker completes the task, the task is received again.

Channel.basic_qos (Prefetch_count=1)

Send the task source code:

#!/usr/bin/env python

Import Pika

Import Sys

Connection = Pika. Blockingconnection (Pika. Connectionparameters (

host= ' localhost '))

Channel = Connection.channel ()

Channel.queue_declare (queue= ' task_queue ', durable=true)

Message = '. Join (sys.argv[1:]) or "Hello world!"

Channel.basic_publish (exchange= ",

Routing_key= ' Task_queue ',

Body=message,

Properties=pika. Basicproperties (

Delivery_mode = 2, # make message persistent

))

print "[x] Sent%r"% (message,)

Connection.close ()

Receive the task source code:

#!/usr/bin/env python

Import Pika

Import time

Connection = Pika. Blockingconnection (Pika. Connectionparameters (

host= ' localhost '))

Channel = Connection.channel ()

Channel.queue_declare (queue= ' task_queue ', durable=true)

print ' [*] waiting for messages. To exit Press CTRL + C '

DEF callback (ch, method, properties, body):

print "[x] Received%r"% (body,)

Time.sleep (Body.count ('. '))

print "[x] Done"

Ch.basic_ack (Delivery_tag = Method.delivery_tag)

Channel.basic_qos (Prefetch_count=1)

Channel.basic_consume (Callback,

Queue= ' Task_queue ')

Channel.start_consuming ()


This article is from the "Black Time" blog, so be sure to keep this source http://blacktime.blog.51cto.com/11722918/1795298

RABBITMQ cluster Construction (centos6.5)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.