Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Rabbitmq summary

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Concept 1. Exchange: switch, where messages are sent 2. Queue: queues, places where messages are consumed, exchange distributes messages according to its own type and routing-key. Routing-key: routing address. Queue is bound by key and exchange. When the message specifies routing-key and sends it to exchange, exchange will push the exchange type into queue according to the list of key bindings:

Where the producer sends the message, the producer only needs to know the name of exchange and routingkey to send the message, and the rest can be forwarded by exchange

Topic: only queues that match the pattern of routes will receive messages. One route can match multiple queues, one-to-many relationship. You only need to specify routing-key to send messages. Only precise queues will receive messages. One-to-one relationship. You need to specify the queue name fanout: broadcast when sending messages. At this time, queues bound with this exchange will receive messages, and routing-key will be ignored. You only need to specify exchangequeue to send messages:

Where the message is received, where the consumer reads the message, and the consumer connects to this queue to read the message.

Vhost:

Virtual host, a virtual host can have multiple exchange and queue, they are isolated and independent

Backup and recovery:

The rabbitmq directory contains two types of information

1: meta-information metadata,schema,topology, mainly to save basic information such as exchange,queue,user,vhost

2: message data information: messages that remain in the queue and are not consumed

Configure import and export to import Exchange and Queue from server A to server B.

1: enable the rabbitmq-management plug-in

2: get rabbitmqadmin command tool: through wget http://localhost:15672/cli/rabbitmqadmin

3: install python

4:python rabbitmqadmin export myrabbit.config-H localhost-P 15672-u myuser-p mypass

5: configuration Import: python rabbitmqadmin import myrabbit.config-H locahost-P 15672-u myuser-p mypass

6: use the management port instead of port 5672

Cluster

The nodes in each cluster are connected through the transport layer. All node pairs exchange tick messages periodically to maintain a connection to detect disconnection.

Cluster building:

1: install erlang: this package is sufficient to support rabbitmq-server operation after installation.

Edit / etc/yum.repos.d/rabbitmq-erlang.repo

[rabbitmq-erlang]

Name=rabbitmq-erlang

Baseurl= https://dl.bintray.com/rabbitmq/rpm/erlang/21/el/7

Gpgcheck=1

Gpgkey= https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc

Repo_gpgcheck=0

Enabled=1

Yum install-y erlang

2: install rabbitmq-server:

Rpm-- import https://dl.bintray.com/rabbitmq/Keys/rabbitmq-release-signing-key.asc

Wget https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.7/rabbitmq-server-3.7.7-1.el7.noarch.rpm

Yum install-y rabbitmq-server-3.7.7-1.el7.noarch.rpm

3: start the server:

Chkconfig rabbitmq-server on

Or systemctl enable rabbitmq-server.

Systemctl start rabbitmq-server

View status: rabbitmqctl status

4: after startup, a .erlang.cookie is generated under / var/lib/rabbitmq, which needs to be checked by ls-al.

If you copy this file to another node, you need to modify the owner of the file, otherwise there will be an error in reading permissions at startup.

Chown rabbitmq:rabbitmq .erlang.cookie

5: each node modifies / etc/hosts and / etc/hostname respectively

6: shut down each node outside the primary node after restart: rabbitmqctl stop_app

7: other nodes join the cluster: rabbitmqctl join_cluster rabbit@rabbitmq1

8: start each node: rabbitmqctl start_app

9: view cluster information: rabbitmqctl cluster_status

After the node stops, it can be restarted directly, and the cluster will load automatically.

Queue highly available

The principle is to create one more mirror queue. By default, the contents of the queue in rabbitmq are located on a single node where the queue declaration is located. Exchange and binding are considered to be on all nodes. When the queue is mirrored, it means that two nodes have the information of a queue. By default, the master node takes effect. When master crashes, slave will be automatically promoted to master, and then the messages of the queue will be processed.

Queue mirroring:

1: use rabbitmqctl set_policy to set up, or use the UI management interface to set

2: open the UI management interface: rabbitmq-plugins enable rabbitmq_management

3: restart the service, then access ip:15672. The user name and password are all guest.

4: under amind, policy on the right, add a policy, or use the command:

Rabbitmqctl set_policy mypolicy "myReg"

Use java to operate rabbtimq

Take direct as an example

1: introduce dependency

Com.rabbitmq amqp-client 5.6.0

2: producer:

ConnectionFactory factory = new ConnectionFactory (); / / create connection factory factory.setHost ("localhost"); try (Connection connection = factory.newConnection (); / / Open connection Channel channel = connection.createChannel ()) {channel.queueDeclare (myQueueName, false, null); / / bind a queue String message = "Hello World!"; channel.basicPublish (", myQueueName, null, message.getBytes ()); send messages to queue}

3: consumers:

ConnectionFactory factory = new ConnectionFactory (); factory.setHost ("localhost"); Connection connection = factory.newConnection (); Channel channel = connection.createChannel (); channel.queueDeclare (myQueueName, false, null); DeliverCallback deliverCallback = (consumerTag, delivery)-> {String message = new String (delivery.getBody (), "UTF-8"); System.out.println ("[x] Received'" + message + "");} Channel.basicConsume (QUEUE_NAME, true, deliverCallback, consumerTag-> {})

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report